CaseFileCompareController.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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 Microsoft.EntityFrameworkCore.Query;
  8. using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
  9. using System;
  10. using System.Collections;
  11. using System.Collections.Generic;
  12. using System.Data;
  13. using System.Diagnostics;
  14. using System.Dynamic;
  15. using System.Linq;
  16. using System.Linq.Expressions;
  17. using System.Text.RegularExpressions;
  18. using wispro.sp.api.AppealHandler;
  19. using wispro.sp.api.Services;
  20. using wispro.sp.entity;
  21. using wispro.sp.entity.CompareCase;
  22. using wispro.sp.share;
  23. using static OneOf.Types.TrueFalseOrNull;
  24. namespace wispro.sp.api.Controllers
  25. {
  26. [Route("api/[controller]/[action]")]
  27. [ApiController]
  28. public class CaseFileCompareController : ControllerBase
  29. {
  30. spDbContext Context;
  31. IFileTaskService fileTaskService;
  32. public CaseFileCompareController(spDbContext context, IFileTaskService _fileTaskService)
  33. {
  34. Context = context;
  35. fileTaskService = _fileTaskService;
  36. }
  37. [Authorize]
  38. public ApiSaveResponse Save(CaseInfo caseInfo)
  39. {
  40. ApiSaveResponse ret = new ApiSaveResponse();
  41. ret.Success = true;
  42. using (Context.Database.BeginTransaction())
  43. {
  44. try
  45. {
  46. #region 客户处理
  47. if (caseInfo.Customer != null && !string.IsNullOrEmpty(caseInfo.Customer.Name))
  48. {
  49. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == caseInfo.Customer.Name).FirstOrDefault();
  50. if (temCustomer == null)
  51. {
  52. temCustomer = new Customer() { Name = caseInfo.Customer.Name };
  53. //item.Customer.Id = 0;
  54. Context.Customers.Add(temCustomer);
  55. Context.SaveChanges();
  56. caseInfo.Customer = temCustomer;
  57. //item.CustomerId = item.Customer.Id;
  58. }
  59. else
  60. {
  61. caseInfo.Customer = temCustomer;
  62. }
  63. caseInfo.CustomerId = caseInfo.Customer.Id;
  64. caseInfo.Customer = null;
  65. }
  66. else
  67. {
  68. caseInfo.Customer = null;
  69. }
  70. #endregion
  71. #region 审核人
  72. if (caseInfo.Reviewer != null && !string.IsNullOrEmpty(caseInfo.Reviewer.Name))
  73. {
  74. var temReviewer = Context.Staffs.Where<Staff>(c => c.Name == caseInfo.Reviewer.Name).FirstOrDefault();
  75. if (temReviewer == null)
  76. {
  77. temReviewer = new Staff() { Name = caseInfo.Reviewer.Name };
  78. //item.Customer.Id = 0;
  79. Context.Staffs.Add(temReviewer);
  80. Context.SaveChanges();
  81. caseInfo.Reviewer = temReviewer;
  82. //item.CustomerId = item.Customer.Id;
  83. }
  84. else
  85. {
  86. caseInfo.Reviewer = temReviewer;
  87. }
  88. caseInfo.ReviewerId = caseInfo.Reviewer.Id;
  89. caseInfo.Reviewer = null;
  90. }
  91. else
  92. {
  93. caseInfo.Reviewer = null;
  94. }
  95. #endregion
  96. if (caseInfo.DRRAbstract != null) {
  97. Context.CaseCompareResults.Add(caseInfo.DRRAbstract);
  98. Context.SaveChanges();
  99. caseInfo.DRRAbstractId = caseInfo.DRRAbstract.Id;
  100. caseInfo.DRRAbstract = null;
  101. }
  102. if (caseInfo.DRRCalim != null)
  103. {
  104. Context.CaseCompareResults.Add(caseInfo.DRRCalim);
  105. Context.SaveChanges();
  106. caseInfo.DRRCalimId = caseInfo.DRRCalim.Id;
  107. caseInfo.DRRCalim = null;
  108. }
  109. if (caseInfo.DRRFulltext != null)
  110. {
  111. Context.CaseCompareResults.Add(caseInfo.DRRFulltext);
  112. Context.SaveChanges();
  113. caseInfo.DRRFulltextId = caseInfo.DRRFulltext.Id;
  114. caseInfo.DRRFulltext = null;
  115. }
  116. if (caseInfo.DRRAll != null)
  117. {
  118. Context.CaseCompareResults.Add(caseInfo.DRRAll);
  119. Context.SaveChanges();
  120. caseInfo.DRRAllId = caseInfo.DRRAll.Id;
  121. caseInfo.DRRAll = null;
  122. }
  123. if (caseInfo.RFRAbstract != null)
  124. {
  125. Context.CaseCompareResults.Add(caseInfo.RFRAbstract);
  126. Context.SaveChanges();
  127. caseInfo.RFRAbstractId = caseInfo.RFRAbstract.Id;
  128. caseInfo.RFRAbstract = null;
  129. }
  130. if (caseInfo.RFRCalim != null)
  131. {
  132. Context.CaseCompareResults.Add(caseInfo.RFRCalim);
  133. Context.SaveChanges();
  134. caseInfo.RFRCalimId = caseInfo.RFRCalim.Id;
  135. caseInfo.RFRCalim = null;
  136. }
  137. if (caseInfo.RFRFulltext != null)
  138. {
  139. Context.CaseCompareResults.Add(caseInfo.RFRFulltext);
  140. Context.SaveChanges();
  141. caseInfo.RFRFulltextId = caseInfo.RFRFulltext.Id;
  142. caseInfo.RFRFulltext = null;
  143. }
  144. if (caseInfo.RFRAll != null)
  145. {
  146. Context.CaseCompareResults.Add(caseInfo.RFRAll);
  147. Context.SaveChanges();
  148. caseInfo.RFRAllId = caseInfo.RFRAll.Id;
  149. caseInfo.RFRAll = null;
  150. }
  151. Context.CaseInfos.Add(caseInfo);
  152. Context.SaveChanges();
  153. Context.Database.CommitTransaction();
  154. }
  155. catch (Exception ex)
  156. {
  157. ret.Success = false;
  158. ret.ErrorMessage = ex.Message;
  159. Context.Database.RollbackTransaction();
  160. }
  161. }
  162. return ret;
  163. }
  164. public ListApiResponse<CaseInfo> QueryFilter(QueryFilter queryFilter)
  165. {
  166. ListApiResponse<CaseInfo> ret = new ListApiResponse<CaseInfo>();
  167. IQueryable<CaseInfo> response = NewMethod(queryFilter);
  168. int totals = response.Count();
  169. if (totals > 0 && totals < (queryFilter.PageIndex) * queryFilter.PageSize)
  170. {
  171. response = response
  172. .Include(pi => pi.DRRCalim)
  173. .Include(pi => pi.DRRAbstract)
  174. .Include(pi => pi.DRRFulltext)
  175. .Include(pi => pi.DRRAll)
  176. .Include(pi => pi.RFRCalim)
  177. .Include(pi => pi.RFRAbstract)
  178. .Include(pi => pi.RFRFulltext)
  179. .Include(pi => pi.RFRAll)
  180. .Include(pi => pi.Customer)
  181. .Include(pi => pi.Reviewer)
  182. .OrderConditions<CaseInfo>(queryFilter.Sorts)
  183. .Skip((queryFilter.PageIndex - 1) * queryFilter.PageSize)
  184. .Take(totals - (queryFilter.PageIndex - 1) * queryFilter.PageSize);
  185. //.Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  186. }
  187. else
  188. {
  189. response = response
  190. .Include(pi => pi.DRRCalim)
  191. .Include(pi => pi.DRRAbstract)
  192. .Include(pi => pi.DRRFulltext)
  193. .Include(pi=>pi.DRRAll)
  194. .Include(pi => pi.RFRCalim)
  195. .Include(pi => pi.RFRAbstract)
  196. .Include(pi => pi.RFRFulltext)
  197. .Include(pi => pi.RFRAll)
  198. .Include(pi => pi.Customer)
  199. .Include(pi => pi.Reviewer)
  200. .OrderConditions<CaseInfo>(queryFilter.Sorts)
  201. .Skip((queryFilter.PageIndex - 1) * queryFilter.PageSize)
  202. .Take(queryFilter.PageSize);
  203. }
  204. ret.TotalCount = totals;
  205. var retList = response.ToList<CaseInfo>();
  206. #region 将某些属性设为null,避免循环取值造成返回json过大
  207. foreach (CaseInfo item in retList)
  208. {
  209. if (item.Customer != null)
  210. {
  211. item.Customer.PerformanceItems = null;
  212. item.Customer.ResponseMan = null;
  213. }
  214. if (item.Reviewer != null)
  215. {
  216. item.Reviewer.Customers = null;
  217. item.Reviewer.ExternalHandlerItems = null;
  218. item.Reviewer.ItemStaffs = null;
  219. item.Reviewer.AllocationRatios = null;
  220. item.Reviewer.ReviewerItems = null;
  221. }
  222. }
  223. #endregion
  224. ret.Results = retList.ToList();
  225. return ret;
  226. }
  227. public bool CaseExist(string caseNo)
  228. {
  229. return Context.CaseInfos.Where(p => p.CaseNo == caseNo.Trim()).Count() > 0;
  230. }
  231. public static (double, double) UpdateStatistics(int n, double mean, double variance, double newValue)
  232. {
  233. double newMean = (n * mean + newValue) / (n + 1);
  234. double newVariance = Math.Sqrt((n * (mean - newMean) * (mean - newMean) + (newValue - newMean) * (newValue - newMean) + n * variance * variance) / (n + 1));
  235. return (newMean, newVariance);
  236. }
  237. /// <summary>
  238. /// 计算指定日期之前一年的指定客户的平均权利要求差异度和标准方差
  239. /// </summary>
  240. /// <param name="endDate">结束日期</param>
  241. /// /// <param name="customerName">客户名称,默认不指定,获取所有客户的平均权利要求差异度和标准方差</param>
  242. /// <returns></returns>
  243. private List<CustomerCompareStatistics> getLaseYearMean_Variance(DateTime endDate,string customerName=null)
  244. {
  245. IIncludableQueryable<CaseInfo, CompareResult> caseList;
  246. if (customerName != null) {
  247. caseList = Context.CaseInfos.Where<CaseInfo>(
  248. p => p.CreateTime >= endDate.AddYears(-1) && p.CreateTime <= endDate && p.Customer.Name == customerName)
  249. .Include(p => p.Customer)
  250. .Include(p => p.Reviewer)
  251. .Include(p => p.DRRAbstract)
  252. .Include(p => p.DRRAbstract)
  253. .Include(p => p.DRRCalim)
  254. .Include(p => p.DRRFulltext)
  255. .Include(p => p.DRRAll)
  256. .Include(p => p.RFRAbstract)
  257. .Include(p => p.RFRCalim)
  258. .Include(p => p.RFRFulltext)
  259. .Include(p => p.RFRAll);
  260. }
  261. else
  262. {
  263. caseList = Context.CaseInfos.Where<CaseInfo>(
  264. p => p.CreateTime >= endDate.AddYears(-1) && p.CreateTime <= endDate)
  265. .Include(p => p.Customer)
  266. .Include(p => p.Reviewer)
  267. .Include(p => p.DRRAbstract)
  268. .Include(p => p.DRRAbstract)
  269. .Include(p => p.DRRCalim)
  270. .Include(p => p.DRRFulltext)
  271. .Include(p => p.DRRAll)
  272. .Include(p => p.RFRAbstract)
  273. .Include(p => p.RFRCalim)
  274. .Include(p => p.RFRFulltext)
  275. .Include(p => p.RFRAll);
  276. }
  277. List < CustomerCompareStatistics > retList = new List < CustomerCompareStatistics >();
  278. foreach ( var item in caseList)
  279. {
  280. var temObj = retList.Where(p=>p.CustomerName == item.Customer.Name).FirstOrDefault<CustomerCompareStatistics>();
  281. if(temObj == null) {
  282. temObj = new CustomerCompareStatistics();
  283. temObj.CustomerName = item.Customer.Name;
  284. retList.Add(temObj);
  285. }
  286. temObj.CaseCount += 1;
  287. if (item.DRRCalim != null)
  288. {
  289. if (temObj.Diff_FinalDrafted_Claims_Count > 0) {
  290. var retStatistics = UpdateStatistics(temObj.Diff_Drafted_Claims_Count, temObj.Diff_Drafted_Claims_Avg, temObj.Diff_Drafted_Claims_Std, item.DRRCalim.diffRate);
  291. temObj.Diff_Drafted_Claims_Std = retStatistics.Item2;
  292. temObj.Diff_Drafted_Claims_Avg = retStatistics.Item1;
  293. temObj.Diff_Drafted_Claims_Count += 1;
  294. }
  295. else
  296. {
  297. temObj.Diff_Drafted_Claims_Std = 0.0;
  298. temObj.Diff_Drafted_Claims_Avg = item.DRRCalim.diffRate;
  299. temObj.Diff_Drafted_Claims_Count = 1;
  300. }
  301. }
  302. if (item.RFRCalim != null)
  303. {
  304. if (temObj.Diff_FinalDrafted_Claims_Count > 0)
  305. {
  306. var retStatistics = UpdateStatistics(temObj.Diff_FinalDrafted_Claims_Count, temObj.Diff_FinalDrafted_Claims_Avg, temObj.Diff_FinalDrafted_Claims_Std, item.RFRCalim.diffRate);
  307. temObj.Diff_FinalDrafted_Claims_Std = retStatistics.Item2;
  308. temObj.Diff_FinalDrafted_Claims_Avg = retStatistics.Item1;
  309. temObj.Diff_FinalDrafted_Claims_Count += 1;
  310. }
  311. else
  312. {
  313. temObj.Diff_FinalDrafted_Claims_Std = 0.0;
  314. temObj.Diff_FinalDrafted_Claims_Avg = item.RFRCalim.diffRate;
  315. temObj.Diff_FinalDrafted_Claims_Count = 1;
  316. }
  317. }
  318. }
  319. return retList;
  320. }
  321. /// <summary>
  322. /// 获取指定时间内客户的权要修改差异率最高的10各案件
  323. /// </summary>
  324. /// <param name="start">开始日期</param>
  325. /// <param name="end">结束日期</param>
  326. /// <param name="customerName">客户名称</param>
  327. /// <param name="type">类型:
  328. /// 0:外部核稿差异率,默认值,
  329. /// 1:内部核稿差异率
  330. /// </param>
  331. /// <returns></returns>
  332. private List<CaseInfo> getTop10DiffRateCases(DateTime start, DateTime end,string customerName,int type=0)
  333. {
  334. if (type == 0) {
  335. var top10Case = Context.CaseInfos.Where<CaseInfo>(
  336. p => p.CreateTime >= start &&
  337. p.CreateTime <= end &&
  338. p.Customer.Name == customerName
  339. )
  340. .Include(p => p.Customer)
  341. .Include(p => p.Reviewer)
  342. .Include(p => p.DRRCalim)
  343. .Include(p => p.RFRCalim)
  344. .AsEnumerable()
  345. .OrderByDescending(p => p.RFRCalim?.diffRate);
  346. return top10Case.Take(10).ToList();
  347. }
  348. else
  349. {
  350. var top10Case = Context.CaseInfos.Where<CaseInfo>(
  351. p => p.CreateTime >= start &&
  352. p.CreateTime <= end &&
  353. p.Customer.Name == customerName
  354. )
  355. .Include(p => p.Customer)
  356. .Include(p => p.Reviewer)
  357. .Include(p => p.DRRCalim)
  358. .Include(p => p.RFRCalim)
  359. .AsEnumerable()
  360. .OrderByDescending(p => p.DRRCalim?.diffRate);
  361. return top10Case.Take(10).ToList();
  362. }
  363. }
  364. /// <summary>
  365. /// 获取指定时间内客户权要修改差异度计算出的异常案件
  366. /// </summary>
  367. /// <param name="start">开始日期</param>
  368. /// <param name="end">结束日期</param>
  369. /// <param name="customerName">客户名称</param>
  370. /// <returns></returns>
  371. private List<CaseInfo> GetAbnormalCases(DateTime start, DateTime end, string customerName)
  372. {
  373. var customerStatics = getLaseYearMean_Variance(end, customerName);
  374. var cStatics = customerStatics.Where(s => s.CustomerName == customerName).FirstOrDefault();
  375. return _GetAbnormalCases(start, end, customerName, cStatics);
  376. }
  377. private List<CaseInfo> _GetAbnormalCases(DateTime start, DateTime end, string customerName, CustomerCompareStatistics cStatics)
  378. {
  379. if (cStatics != null)
  380. {
  381. var caseList = Context.CaseInfos.Where<CaseInfo>(
  382. p => p.CreateTime >= start &&
  383. p.CreateTime <= end &&
  384. p.Customer.Name == customerName
  385. )
  386. .Include(p => p.Customer)
  387. .Include(p => p.Reviewer)
  388. .Include(p => p.DRRAbstract)
  389. .Include(p => p.DRRAbstract)
  390. .Include(p => p.DRRCalim)
  391. .Include(p => p.DRRFulltext)
  392. .Include(p => p.DRRAll)
  393. .Include(p => p.RFRAbstract)
  394. .Include(p => p.RFRCalim)
  395. .Include(p => p.RFRFulltext)
  396. .Include(p => p.RFRAll);
  397. var retList = new List<CaseInfo>();
  398. foreach (var item in caseList)
  399. {
  400. double first = item.DRRCalim == null ? 0.0 : item.DRRCalim.diffRate;
  401. double second = item.RFRCalim == null ? 0.0 : item.RFRCalim.diffRate;
  402. double score1 = (first - cStatics.Diff_Drafted_Claims_Avg) / cStatics.Diff_Drafted_Claims_Std;
  403. double score2 = (second - cStatics.Diff_FinalDrafted_Claims_Avg) / cStatics.Diff_FinalDrafted_Claims_Std;
  404. double score = Math.Sqrt(score1 * score1 + score2 * score2);
  405. if (score > 3.0)
  406. {
  407. if (score2 > 0)
  408. {
  409. if (score1 < 0)
  410. {
  411. item.AbnormalMessage = "内部核稿修改相对少,外部修改相对很多!【核稿人不给力/沟通有问题?】";
  412. }
  413. else
  414. {
  415. item.AbnormalMessage = "内部核稿相对多,外部修改也相对多!【沟通有问题?】";
  416. }
  417. }
  418. else
  419. {
  420. //if (score1 < 0)
  421. //{
  422. // item.AbnormalMessage = "内部核稿修改相对少,外部修改相对少!【撰稿人给力】";
  423. //}
  424. //else
  425. //{
  426. // item.AbnormalMessage = "内部核稿相对多,外部修改也相对少!【核稿人给力】";
  427. //}
  428. }
  429. retList.Add(item);
  430. }
  431. }
  432. return retList;
  433. }
  434. else
  435. {
  436. return new List<CaseInfo>();
  437. }
  438. }
  439. internal class mailCaseInfo
  440. {
  441. public CustomerCompareStatistics CustomerCompareStatistics { get; set; }
  442. public List<CaseInfo> Top10Cases { get; set; } = new List<CaseInfo>();
  443. public List<CaseInfo> AbnormalCases { get; set; }= new List<CaseInfo>();
  444. }
  445. public void getMailCaseInfo(DateTime start, DateTime end)
  446. {
  447. List<string> defaultStaffNames = new List<string>() { "罗才洋","李庆波","钟子敏"};
  448. var DefaultEmailAccounts = Context.Staffs.Where(p => defaultStaffNames.Contains(p.Name)).ToList();
  449. var customerStatics = getLaseYearMean_Variance(end);
  450. Hashtable staffMailInfo = new Hashtable();
  451. foreach (var cStatics in customerStatics)
  452. {
  453. if(cStatics.CaseCount < 20)
  454. {
  455. continue;
  456. }
  457. //var cStatics = customerStatics.Where(s => s.CustomerName == item.Customer.Name).FirstOrDefault();
  458. var Top10Cases =getTop10DiffRateCases(start, end, cStatics.CustomerName);
  459. foreach (var caseInfo in Top10Cases)
  460. {
  461. foreach (Staff staff in DefaultEmailAccounts)
  462. {
  463. AddtoHashTable(caseInfo, 0, staff, staffMailInfo, cStatics);
  464. }
  465. if (caseInfo.Reviewer != null)
  466. {
  467. AddtoHashTable(caseInfo,0, caseInfo.Reviewer,staffMailInfo,cStatics);
  468. }
  469. if (caseInfo.Customer.ResponseManId != null)
  470. {
  471. Staff respMan = Context.Staffs.FirstOrDefault<Staff>(p => p.Id == caseInfo.Customer.ResponseManId);
  472. if (respMan != null)
  473. {
  474. AddtoHashTable(caseInfo, 0, respMan, staffMailInfo, cStatics);
  475. }
  476. }
  477. }
  478. var AbnormalCases = _GetAbnormalCases(start,end, cStatics.CustomerName, cStatics);
  479. foreach (var caseInfo in AbnormalCases)
  480. {
  481. foreach(Staff staff in DefaultEmailAccounts)
  482. {
  483. AddtoHashTable(caseInfo, 1, staff, staffMailInfo, cStatics);
  484. }
  485. if (caseInfo.Reviewer != null)
  486. {
  487. AddtoHashTable(caseInfo, 1, caseInfo.Reviewer, staffMailInfo, cStatics);
  488. }
  489. if (caseInfo.Customer.ResponseManId != null)
  490. {
  491. Staff respMan = Context.Staffs.FirstOrDefault<Staff>(p => p.Id == caseInfo.Customer.ResponseManId);
  492. if (respMan != null)
  493. {
  494. AddtoHashTable(caseInfo, 1, respMan, staffMailInfo, cStatics);
  495. }
  496. }
  497. }
  498. }
  499. }
  500. private void AddtoHashTable(CaseInfo caseInfo, int v, Staff staff, Hashtable staffMailInfo,CustomerCompareStatistics cStatics)
  501. {
  502. var staffCaseMailInfo = new mailCaseInfo()
  503. {
  504. CustomerCompareStatistics = cStatics,
  505. };
  506. List<mailCaseInfo> staffCaseMailInfos = new List<mailCaseInfo>();
  507. if (staffMailInfo.ContainsKey(staff))
  508. {
  509. staffCaseMailInfos = (List<mailCaseInfo>)staffMailInfo[staff];
  510. }
  511. else
  512. {
  513. staffMailInfo.Add(staff,staffCaseMailInfos);
  514. }
  515. var temObj = staffCaseMailInfos.Where(p => p.CustomerCompareStatistics.CustomerName == caseInfo.Customer.Name).FirstOrDefault();
  516. if (temObj != null)
  517. {
  518. staffCaseMailInfo = temObj;
  519. }
  520. else
  521. {
  522. staffCaseMailInfos.Add(staffCaseMailInfo);
  523. }
  524. if (v == 0)
  525. {
  526. staffCaseMailInfo.Top10Cases.Add(caseInfo);
  527. }
  528. else
  529. {
  530. staffCaseMailInfo.AbnormalCases.Add(caseInfo);
  531. }
  532. }
  533. /// <summary>
  534. /// 计算案件的zScore
  535. /// </summary>
  536. /// <param name="start">定稿日开始时间</param>
  537. /// <param name="end">定稿日结束时间</param>
  538. /// <param name="type">
  539. /// 0:基于文本相似度计算,
  540. /// 1:基于文本修改差异度计算
  541. /// 2:基于权要权重70说明书30计算
  542. /// 3:基于权要文本相似度计算
  543. /// 4: 基于权要文字修改差异度计算
  544. /// </param>
  545. /// <returns></returns>
  546. public IList<Object> CalCustomer_mean(DateTime start,DateTime end,int type=0)
  547. {
  548. var caseList = Context.CaseInfos.Where<CaseInfo>(
  549. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  550. .Include(p=>p.Customer)
  551. .Include(p=>p.Reviewer)
  552. .Include(p=>p.DRRAbstract)
  553. .Include(p => p.DRRAbstract)
  554. .Include(p => p.DRRCalim)
  555. .Include(p => p.DRRFulltext)
  556. .Include(p => p.DRRAll)
  557. .Include(p => p.RFRAbstract)
  558. .Include(p => p.RFRCalim)
  559. .Include(p => p.RFRFulltext)
  560. .Include(p => p.RFRAll);
  561. int iTotals = caseList.Count();
  562. int iIndex = 0;
  563. List<Object> retList = new List<Object>();
  564. #region 计算客户相似度平均值和标准方差
  565. List<CustomerAvg_Std> avg_std1 = new List<CustomerAvg_Std>();
  566. List<CustomerAvg_Std> avg_std2 = new List<CustomerAvg_Std>();
  567. foreach (var caseInfo in caseList)
  568. {
  569. iIndex++;
  570. Debug.WriteLine($"{iIndex}/{iTotals}\t{caseInfo.CaseNo}\t{caseInfo.DRRAbstractId},{caseInfo.DRRCalimId},{caseInfo.DRRFulltextId},{caseInfo.DRRAllId},{caseInfo.RFRAbstractId},{caseInfo.RFRCalimId},{caseInfo.RFRFulltextId},{caseInfo.RFRAllId}");
  571. string tmpCustomerName = "未知";
  572. if(caseInfo.Customer!= null)
  573. {
  574. tmpCustomerName = caseInfo.Customer.Name;
  575. }
  576. var one = avg_std1.Where<CustomerAvg_Std>(p => p.Name == tmpCustomerName).FirstOrDefault();
  577. if (one == null)
  578. {
  579. one = new CustomerAvg_Std() { Name = tmpCustomerName };
  580. avg_std1.Add(one);
  581. }
  582. var two = avg_std2.Where<CustomerAvg_Std>(p => p.Name == tmpCustomerName).FirstOrDefault();
  583. if (two == null)
  584. {
  585. two = new CustomerAvg_Std() { Name = tmpCustomerName };
  586. avg_std2.Add(two);
  587. }
  588. double? oneTmp = getCalValue(caseInfo, type, 0);
  589. if(oneTmp != null)
  590. {
  591. one.Sum += oneTmp.Value;
  592. one.SquareSum += oneTmp.Value * oneTmp.Value;
  593. one.count += 1;
  594. }
  595. double? twoTmp = getCalValue(caseInfo, type, 1);
  596. if (twoTmp != null)
  597. {
  598. two.Sum += twoTmp.Value;
  599. two.SquareSum += twoTmp.Value * twoTmp.Value;
  600. two.count += 1;
  601. }
  602. }
  603. #endregion
  604. System.Data.DataTable dt = new System.Data.DataTable();
  605. dt.Columns.Add("我方文号");
  606. dt.Columns.Add("案件名称");
  607. dt.Columns.Add("客户");
  608. dt.Columns.Add("处理人");
  609. dt.Columns.Add("核稿人");
  610. dt.Columns.Add("内部核稿相似度或差异度");
  611. dt.Columns.Add("内部客户平均值");
  612. dt.Columns.Add("内部客户标准偏差");
  613. dt.Columns.Add("内部核稿分数");
  614. dt.Columns.Add("外部核稿相似度或差异度");
  615. dt.Columns.Add("外部客户平均值");
  616. dt.Columns.Add("外部客户标准偏差");
  617. dt.Columns.Add("外部核稿分数");
  618. dt.Columns.Add("备注");
  619. foreach (var item in caseList)
  620. {
  621. string tmpCustomerName = "未知";
  622. if (item.Customer != null)
  623. {
  624. tmpCustomerName = item.Customer.Name;
  625. }
  626. var one = avg_std1.Where<CustomerAvg_Std>(p => p.Name == tmpCustomerName).FirstOrDefault();
  627. var two = avg_std2.Where<CustomerAvg_Std>(p => p.Name == tmpCustomerName).FirstOrDefault();
  628. double? oneSim = getCalValue(item,type,0);
  629. double? twoSim = getCalValue(item, type, 1); ;
  630. DataRow row = dt.NewRow();
  631. row["我方文号"] = item.CaseNo;
  632. row["案件名称"] = item.CaseName;
  633. row["客户"] = item.Customer?.Name;
  634. row["核稿人"] = item.Reviewer?.Name;
  635. row["处理人"] = item.Handlers;
  636. double? zScoreA = null;
  637. if (oneSim != null)
  638. {
  639. zScoreA = (oneSim - one.Average) / one.Std_Deviation;
  640. }
  641. row["内部核稿分数"] = zScoreA;
  642. row["内部核稿相似度或差异度"] = oneSim;
  643. row["内部客户平均值"] = one.Average;
  644. row["内部客户标准偏差"] = one.Std_Deviation;
  645. row["外部核稿相似度或差异度"] = twoSim;
  646. row["外部客户平均值"] = two.Average;
  647. row["外部客户标准偏差"] = two.Std_Deviation;
  648. double? zScoreB = null;
  649. if (twoSim != null)
  650. {
  651. zScoreB = (twoSim - two.Average) / two.Std_Deviation;
  652. }
  653. row["外部核稿分数"] = zScoreB;
  654. if (zScoreA != null && zScoreB != null)
  655. {
  656. var distince = Math.Sqrt(zScoreB.Value * zScoreB.Value + zScoreA.Value * zScoreA.Value);
  657. if (type == 0 || type == 2 || type==3)
  658. {
  659. if (distince > 3)
  660. {
  661. if (zScoreA.Value > 0 && zScoreB.Value > 0)
  662. {
  663. row["备注"] = "内部核稿、外部核稿修改都较少![撰稿人给力或客户友好]";
  664. }
  665. if (zScoreA.Value > 0 && zScoreB.Value < 0)
  666. {
  667. row["备注"] = "内部核稿较少、外部核稿修改较多![核稿人没有尽责!]";
  668. }
  669. if (zScoreA.Value < 0 && zScoreB.Value > 0)
  670. {
  671. row["备注"] = "内部核稿较多、外部核稿修改都较少![核稿人给力]";
  672. }
  673. if (zScoreA.Value < 0 && zScoreB.Value < 0)
  674. {
  675. row["备注"] = "内部核稿和外部核稿修改都较多![案件沟通问题?]";
  676. }
  677. }
  678. }
  679. else
  680. {
  681. if (distince > 3)
  682. {
  683. if (zScoreA.Value > 0 && zScoreB.Value > 0)
  684. {
  685. row["备注"] = "内部核稿和外部核稿修改都较多![案件沟通问题?]";
  686. }
  687. if (zScoreA.Value > 0 && zScoreB.Value < 0)
  688. {
  689. row["备注"] = "内部核稿较多、外部核稿修改都较少![核稿人给力]";
  690. }
  691. if (zScoreA.Value < 0 && zScoreB.Value > 0)
  692. {
  693. row["备注"] = "内部核稿较少、外部核稿修改较多![核稿人没有尽责!]";
  694. }
  695. if (zScoreA.Value < 0 && zScoreB.Value < 0)
  696. {
  697. row["备注"] = "内部核稿、外部核稿修改都较少![撰稿人给力或客户友好]";
  698. }
  699. }
  700. }
  701. }
  702. dt.Rows.Add(row);
  703. retList.Add(
  704. new
  705. {
  706. Id = item.Id,
  707. CaseNo = item.CaseNo,
  708. CaseName = item.CaseName,
  709. Customer = item.Customer,
  710. Reviewer = item.Reviewer,
  711. Handlers = item.Handlers,
  712. zScoreA = (oneSim - one.Average) / one.Std_Deviation,
  713. zScoreB = (twoSim - two.Average) / two.Std_Deviation
  714. }
  715. );
  716. }
  717. wispro.sp.utility.NPOIExcel.DataTableToExcel(dt,$"c:\\temp\\{DateTime.Now.ToString("yyyyMMdd")}-内部核稿外部核稿情况案件清单_{typeName(type)}.xlsx");
  718. return retList;
  719. }
  720. private double? getCalValue(CaseInfo caseInfo,int type,int stage)
  721. {
  722. double? calValue = null;
  723. switch (type)
  724. {
  725. case 0:
  726. if(stage == 0)
  727. {
  728. if(caseInfo.DRRFulltext != null && caseInfo.DRRAbstract != null)
  729. {
  730. calValue = caseInfo.DRRAll.TextSimilarity;
  731. }
  732. else
  733. {
  734. if(caseInfo.DRRCalim != null)
  735. {
  736. calValue = caseInfo.DRRCalim.TextSimilarity;
  737. }
  738. }
  739. }
  740. else
  741. {
  742. if (caseInfo.RFRFulltext != null && caseInfo.RFRAbstract != null)
  743. {
  744. calValue = caseInfo.RFRAll.TextSimilarity;
  745. }
  746. else
  747. {
  748. if (caseInfo.RFRCalim != null)
  749. {
  750. calValue = caseInfo.RFRCalim.TextSimilarity;
  751. }
  752. }
  753. }
  754. break;
  755. case 1:
  756. if (stage == 0)
  757. {
  758. if (caseInfo.DRRFulltext != null && caseInfo.DRRAbstract != null)
  759. {
  760. calValue = caseInfo.DRRAll.diffRate;
  761. }
  762. else
  763. {
  764. if (caseInfo.DRRCalim != null)
  765. {
  766. calValue = caseInfo.DRRCalim.diffRate;
  767. }
  768. }
  769. }
  770. else
  771. {
  772. if (caseInfo.RFRFulltext != null && caseInfo.RFRAbstract != null)
  773. {
  774. calValue = caseInfo.RFRAll.diffRate;
  775. }
  776. else
  777. {
  778. if (caseInfo.RFRCalim != null)
  779. {
  780. calValue = caseInfo.RFRCalim.diffRate;
  781. }
  782. }
  783. }
  784. break;
  785. case 2:
  786. if (stage == 0)
  787. {
  788. if(caseInfo.DRRFulltext != null && caseInfo.DRRCalim!= null)
  789. {
  790. calValue = caseInfo.DRRCalim.TextSimilarity * 0.7 + caseInfo.DRRFulltext.TextSimilarity * 0.3;
  791. }
  792. else
  793. {
  794. if( caseInfo.DRRCalim != null)
  795. {
  796. calValue = caseInfo.DRRCalim.TextSimilarity;
  797. }
  798. else
  799. {
  800. if(caseInfo.DRRAll != null)
  801. {
  802. calValue = caseInfo.DRRAll.TextSimilarity;
  803. }
  804. }
  805. }
  806. }
  807. else
  808. {
  809. if (caseInfo.RFRFulltext != null && caseInfo.RFRCalim != null)
  810. {
  811. calValue = caseInfo.RFRCalim.TextSimilarity * 0.7 + caseInfo.RFRFulltext.TextSimilarity * 0.3;
  812. }
  813. else
  814. {
  815. if (caseInfo.RFRCalim != null)
  816. {
  817. calValue = caseInfo.RFRCalim.TextSimilarity;
  818. }
  819. else
  820. {
  821. if (caseInfo.RFRAll != null)
  822. {
  823. calValue = caseInfo.RFRAll.TextSimilarity;
  824. }
  825. }
  826. }
  827. }
  828. break;
  829. case 3:
  830. if (stage == 0)
  831. {
  832. if (caseInfo.DRRCalim != null)
  833. {
  834. calValue = caseInfo.DRRCalim.TextSimilarity;
  835. }
  836. }
  837. else
  838. {
  839. if (caseInfo.RFRCalim != null)
  840. {
  841. calValue = caseInfo.RFRCalim.TextSimilarity;
  842. }
  843. }
  844. break;
  845. case 4:
  846. if (stage == 0)
  847. {
  848. if (caseInfo.DRRCalim != null)
  849. {
  850. calValue = caseInfo.DRRCalim.diffRate;
  851. }
  852. }
  853. else
  854. {
  855. if (caseInfo.RFRCalim != null)
  856. {
  857. calValue = caseInfo.RFRCalim.diffRate;
  858. }
  859. }
  860. break;
  861. }
  862. return calValue;
  863. }
  864. private string typeName(int type)
  865. {
  866. switch (type)
  867. {
  868. case 0:
  869. return "文本相似度计算";
  870. case 1:
  871. return "字符修改计算";
  872. case 2:
  873. return "权要权重70说明书30";
  874. case 3:
  875. return "权要相似度计算";
  876. }
  877. return "";
  878. }
  879. public IList<Object> CalMean_Std(DateTime start,DateTime end)
  880. {
  881. double AverageA = Context.CaseInfos.Where<CaseInfo>(
  882. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end && p.DRRCalim!= null)
  883. .Average(x=>x.DRRCalim.TextSimilarity);
  884. double AverageB = Context.CaseInfos.Where<CaseInfo>(
  885. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end && p.RFRAll != null)
  886. .Average(x => x.RFRAll.TextSimilarity);
  887. double stdA = Math.Sqrt( Context.CaseInfos.Where<CaseInfo>(
  888. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  889. .Average((x => (x.DRRCalim.TextSimilarity - AverageA) * (x.DRRCalim.TextSimilarity - AverageA))));
  890. double stdB = Math.Sqrt(Context.CaseInfos.Where<CaseInfo>(
  891. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  892. .Average((x => (x.RFRAll.TextSimilarity - AverageB) * (x.RFRAll.TextSimilarity - AverageB))));
  893. var response2 = Context.CaseInfos.Where<CaseInfo>(
  894. p => p.FinalVersionDate >= start && p.FinalVersionDate <= end)
  895. .Include(p=>p.DRRCalim)
  896. .Include(p=>p.RFRAll);
  897. IList<Object> results = new List<Object>();
  898. foreach(var item in response2.ToList())
  899. {
  900. results.Add(
  901. new {
  902. Id = item.Id,
  903. CaseNo = item.CaseNo,
  904. CaseName = item.CaseName,
  905. Customer = item.Customer,
  906. Reviewer = item.Reviewer,
  907. Handlers = item.Handlers,
  908. zScoreA = (item.DRRCalim?.TextSimilarity -AverageA)/stdA,
  909. zScoreB = (item.RFRAll?.TextSimilarity - AverageB) /stdB
  910. }
  911. );
  912. }
  913. return results;
  914. }
  915. private string GetExpress(IList<FieldCondition> conditions)
  916. {
  917. string str = "";
  918. foreach (var c in conditions)
  919. {
  920. if (string.IsNullOrEmpty(str))
  921. {
  922. str = c.ToExpressString("s");
  923. }
  924. else
  925. {
  926. if (c.LogicOperate == LogicEnum.And)
  927. {
  928. str = $"({str}) && {c.ToExpressString("s")}";
  929. }
  930. else
  931. {
  932. str = $"({str}) || {c.ToExpressString("s")}";
  933. }
  934. }
  935. }
  936. return str;
  937. }
  938. private IQueryable<CaseInfo> NewMethod(QueryFilter queryFilter)
  939. {
  940. string strExpress = "";
  941. if (queryFilter.ConditionTree != null)
  942. {
  943. strExpress = GetExpress(queryFilter.ConditionTree);
  944. }
  945. var interpreter = new Interpreter();
  946. if (string.IsNullOrEmpty(strExpress))
  947. {
  948. return new spDbContext().CaseInfos.Where<CaseInfo>(p=>(p.Id>0));
  949. }
  950. else
  951. {
  952. Expression<Func<CaseInfo, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<CaseInfo, bool>>(strExpress, "s");
  953. IQueryable<CaseInfo> response = new spDbContext().CaseInfos.Where<CaseInfo>(dynamicWhere);
  954. return response;
  955. }
  956. }
  957. }
  958. }