CaseFileCompareController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using DocumentFormat.OpenXml.InkML;
  2. using DynamicExpresso;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.EntityFrameworkCore;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Linq.Expressions;
  11. using System.Text.RegularExpressions;
  12. using wispro.sp.api.AppealHandler;
  13. using wispro.sp.api.Services;
  14. using wispro.sp.entity;
  15. using wispro.sp.entity.CompareCase;
  16. using wispro.sp.share;
  17. using static OneOf.Types.TrueFalseOrNull;
  18. namespace wispro.sp.api.Controllers
  19. {
  20. [Route("api/[controller]/[action]")]
  21. [ApiController]
  22. public class CaseFileCompareController : ControllerBase
  23. {
  24. spDbContext Context;
  25. IFileTaskService fileTaskService;
  26. public CaseFileCompareController(spDbContext context, IFileTaskService _fileTaskService)
  27. {
  28. Context = context;
  29. fileTaskService = _fileTaskService;
  30. }
  31. [Authorize]
  32. public ApiSaveResponse Save(CaseInfo caseInfo)
  33. {
  34. ApiSaveResponse ret = new ApiSaveResponse();
  35. ret.Success = true;
  36. using (Context.Database.BeginTransaction())
  37. {
  38. try
  39. {
  40. #region 客户处理
  41. if (caseInfo.Customer != null && !string.IsNullOrEmpty(caseInfo.Customer.Name))
  42. {
  43. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == caseInfo.Customer.Name).FirstOrDefault();
  44. if (temCustomer == null)
  45. {
  46. temCustomer = new Customer() { Name = caseInfo.Customer.Name };
  47. //item.Customer.Id = 0;
  48. Context.Customers.Add(temCustomer);
  49. Context.SaveChanges();
  50. caseInfo.Customer = temCustomer;
  51. //item.CustomerId = item.Customer.Id;
  52. }
  53. else
  54. {
  55. caseInfo.Customer = temCustomer;
  56. }
  57. caseInfo.CustomerId = caseInfo.Customer.Id;
  58. caseInfo.Customer = null;
  59. }
  60. else
  61. {
  62. caseInfo.Customer = null;
  63. }
  64. #endregion
  65. #region 审核人
  66. if (caseInfo.Reviewer != null && !string.IsNullOrEmpty(caseInfo.Reviewer.Name))
  67. {
  68. var temReviewer = Context.Staffs.Where<Staff>(c => c.Name == caseInfo.Reviewer.Name).FirstOrDefault();
  69. if (temReviewer == null)
  70. {
  71. temReviewer = new Staff() { Name = caseInfo.Reviewer.Name };
  72. //item.Customer.Id = 0;
  73. Context.Staffs.Add(temReviewer);
  74. Context.SaveChanges();
  75. caseInfo.Reviewer = temReviewer;
  76. //item.CustomerId = item.Customer.Id;
  77. }
  78. else
  79. {
  80. caseInfo.Reviewer = temReviewer;
  81. }
  82. caseInfo.ReviewerId = caseInfo.Reviewer.Id;
  83. caseInfo.Reviewer = null;
  84. }
  85. else
  86. {
  87. caseInfo.Reviewer = null;
  88. }
  89. #endregion
  90. if (caseInfo.DRRAbstract != null) {
  91. Context.CaseCompareResults.Add(caseInfo.DRRAbstract);
  92. Context.SaveChanges();
  93. caseInfo.DRRAbstractId = caseInfo.DRRAbstract.Id;
  94. caseInfo.DRRAbstract = null;
  95. }
  96. if (caseInfo.DRRCalim != null)
  97. {
  98. Context.CaseCompareResults.Add(caseInfo.DRRCalim);
  99. Context.SaveChanges();
  100. caseInfo.DRRCalimId = caseInfo.DRRCalim.Id;
  101. caseInfo.DRRCalim = null;
  102. }
  103. if (caseInfo.DRRFulltext != null)
  104. {
  105. Context.CaseCompareResults.Add(caseInfo.DRRFulltext);
  106. Context.SaveChanges();
  107. caseInfo.DRRFulltextId = caseInfo.DRRFulltext.Id;
  108. caseInfo.DRRFulltext = null;
  109. }
  110. if (caseInfo.DRRAll != null)
  111. {
  112. Context.CaseCompareResults.Add(caseInfo.DRRAll);
  113. Context.SaveChanges();
  114. caseInfo.DRRAllId = caseInfo.DRRAll.Id;
  115. caseInfo.DRRAll = null;
  116. }
  117. if (caseInfo.RFRAbstract != null)
  118. {
  119. Context.CaseCompareResults.Add(caseInfo.RFRAbstract);
  120. Context.SaveChanges();
  121. caseInfo.RFRAbstractId = caseInfo.RFRAbstract.Id;
  122. caseInfo.RFRAbstract = null;
  123. }
  124. if (caseInfo.RFRCalim != null)
  125. {
  126. Context.CaseCompareResults.Add(caseInfo.RFRCalim);
  127. Context.SaveChanges();
  128. caseInfo.RFRCalimId = caseInfo.RFRCalim.Id;
  129. caseInfo.RFRCalim = null;
  130. }
  131. if (caseInfo.RFRFulltext != null)
  132. {
  133. Context.CaseCompareResults.Add(caseInfo.RFRFulltext);
  134. Context.SaveChanges();
  135. caseInfo.RFRFulltextId = caseInfo.RFRFulltext.Id;
  136. caseInfo.RFRFulltext = null;
  137. }
  138. if (caseInfo.RFRAll != null)
  139. {
  140. Context.CaseCompareResults.Add(caseInfo.RFRAll);
  141. Context.SaveChanges();
  142. caseInfo.RFRAllId = caseInfo.RFRAll.Id;
  143. caseInfo.RFRAll = null;
  144. }
  145. Context.CaseInfos.Add(caseInfo);
  146. Context.SaveChanges();
  147. Context.Database.CommitTransaction();
  148. }
  149. catch (Exception ex)
  150. {
  151. ret.Success = false;
  152. ret.ErrorMessage = ex.Message;
  153. Context.Database.RollbackTransaction();
  154. }
  155. }
  156. return ret;
  157. }
  158. public ListApiResponse<CaseInfo> QueryFilter(QueryFilter queryFilter)
  159. {
  160. ListApiResponse<CaseInfo> ret = new ListApiResponse<CaseInfo>();
  161. IQueryable<CaseInfo> response = NewMethod(queryFilter);
  162. int totals = response.Count();
  163. if (totals > 0 && totals < (queryFilter.PageIndex) * queryFilter.PageSize)
  164. {
  165. response = response
  166. .Include(pi => pi.DRRCalim)
  167. .Include(pi => pi.DRRAbstract)
  168. .Include(pi => pi.DRRFulltext)
  169. .Include(pi => pi.DRRAll)
  170. .Include(pi => pi.RFRCalim)
  171. .Include(pi => pi.RFRAbstract)
  172. .Include(pi => pi.RFRFulltext)
  173. .Include(pi => pi.RFRAll)
  174. .Include(pi => pi.Customer)
  175. .Include(pi => pi.Reviewer)
  176. .OrderConditions<CaseInfo>(queryFilter.Sorts)
  177. .Skip((queryFilter.PageIndex - 1) * queryFilter.PageSize)
  178. .Take(totals - (queryFilter.PageIndex - 1) * queryFilter.PageSize);
  179. //.Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  180. }
  181. else
  182. {
  183. response = response
  184. .Include(pi => pi.DRRCalim)
  185. .Include(pi => pi.DRRAbstract)
  186. .Include(pi => pi.DRRFulltext)
  187. .Include(pi=>pi.DRRAll)
  188. .Include(pi => pi.RFRCalim)
  189. .Include(pi => pi.RFRAbstract)
  190. .Include(pi => pi.RFRFulltext)
  191. .Include(pi => pi.RFRAll)
  192. .Include(pi => pi.Customer)
  193. .Include(pi => pi.Reviewer)
  194. .OrderConditions<CaseInfo>(queryFilter.Sorts)
  195. .Skip((queryFilter.PageIndex - 1) * queryFilter.PageSize)
  196. .Take(queryFilter.PageSize);
  197. }
  198. ret.TotalCount = totals;
  199. var retList = response.ToList<CaseInfo>();
  200. #region 将某些属性设为null,避免循环取值造成返回json过大
  201. foreach (CaseInfo item in retList)
  202. {
  203. if (item.Customer != null)
  204. {
  205. item.Customer.PerformanceItems = null;
  206. item.Customer.ResponseMan = null;
  207. }
  208. if (item.Reviewer != null)
  209. {
  210. item.Reviewer.Customers = null;
  211. item.Reviewer.ExternalHandlerItems = null;
  212. item.Reviewer.ItemStaffs = null;
  213. item.Reviewer.AllocationRatios = null;
  214. item.Reviewer.ReviewerItems = null;
  215. }
  216. }
  217. #endregion
  218. ret.Results = retList.ToList();
  219. return ret;
  220. }
  221. public bool CaseExist(string caseNo)
  222. {
  223. return Context.CaseInfos.Where(p => p.CaseNo == caseNo.Trim()).Count() > 0;
  224. }
  225. public IList<Object> CalMean_Std(DateTime start,DateTime end)
  226. {
  227. double AverageA = Context.CaseInfos.Where<CaseInfo>(
  228. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end && p.DRRCalim!= null)
  229. .Average(x=>x.DRRCalim.TextSimilarity);
  230. double AverageB = Context.CaseInfos.Where<CaseInfo>(
  231. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end && p.RFRAll != null)
  232. .Average(x => x.RFRAll.TextSimilarity);
  233. double stdA = Math.Sqrt( Context.CaseInfos.Where<CaseInfo>(
  234. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  235. .Average((x => (x.DRRCalim.TextSimilarity - AverageA) * (x.DRRCalim.TextSimilarity - AverageA))));
  236. double stdB = Math.Sqrt(Context.CaseInfos.Where<CaseInfo>(
  237. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  238. .Average((x => (x.RFRAll.TextSimilarity - AverageB) * (x.RFRAll.TextSimilarity - AverageB))));
  239. var response2 = Context.CaseInfos.Where<CaseInfo>(
  240. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  241. .Include(p=>p.DRRCalim)
  242. .Include(p=>p.RFRAll);
  243. IList<Object> results = new List<Object>();
  244. foreach(var item in response2.ToList())
  245. {
  246. results.Add(
  247. new {
  248. Id = item.Id,
  249. CaseNo = item.CaseNo,
  250. CaseName = item.CaseName,
  251. Customer = item.Customer,
  252. Reviewer = item.Reviewer,
  253. Handlers = item.Handlers,
  254. zScoreA = (item.DRRCalim?.TextSimilarity -AverageA)/stdA,
  255. zScoreB = (item.RFRAll?.TextSimilarity - AverageB) / stdB
  256. }
  257. );
  258. }
  259. return results;
  260. }
  261. private string GetExpress(IList<FieldCondition> conditions)
  262. {
  263. string str = "";
  264. foreach (var c in conditions)
  265. {
  266. if (string.IsNullOrEmpty(str))
  267. {
  268. str = c.ToExpressString("s");
  269. }
  270. else
  271. {
  272. if (c.LogicOperate == LogicEnum.And)
  273. {
  274. str = $"({str}) && {c.ToExpressString("s")}";
  275. }
  276. else
  277. {
  278. str = $"({str}) || {c.ToExpressString("s")}";
  279. }
  280. }
  281. }
  282. return str;
  283. }
  284. private IQueryable<CaseInfo> NewMethod(QueryFilter queryFilter)
  285. {
  286. string strExpress = "";
  287. if (queryFilter.ConditionTree != null)
  288. {
  289. strExpress = GetExpress(queryFilter.ConditionTree);
  290. }
  291. var interpreter = new Interpreter();
  292. if (string.IsNullOrEmpty(strExpress))
  293. {
  294. return new spDbContext().CaseInfos.Where<CaseInfo>(p=>(p.Id>0));
  295. }
  296. else
  297. {
  298. Expression<Func<CaseInfo, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<CaseInfo, bool>>(strExpress, "s");
  299. IQueryable<CaseInfo> response = new spDbContext().CaseInfos.Where<CaseInfo>(dynamicWhere);
  300. return response;
  301. }
  302. }
  303. }
  304. }