CaseFileCompareController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 wispro.sp.api.Services;
  12. using wispro.sp.entity;
  13. using wispro.sp.entity.CompareCase;
  14. using wispro.sp.share;
  15. namespace wispro.sp.api.Controllers
  16. {
  17. [Route("api/[controller]/[action]")]
  18. [ApiController]
  19. public class CaseFileCompareController : ControllerBase
  20. {
  21. spDbContext Context;
  22. IFileTaskService fileTaskService;
  23. public CaseFileCompareController(spDbContext context, IFileTaskService _fileTaskService)
  24. {
  25. Context = context;
  26. fileTaskService = _fileTaskService;
  27. }
  28. [Authorize]
  29. public ApiSaveResponse Save(CaseInfo caseInfo)
  30. {
  31. ApiSaveResponse ret = new ApiSaveResponse();
  32. ret.Success = true;
  33. using (Context.Database.BeginTransaction())
  34. {
  35. try
  36. {
  37. #region 客户处理
  38. if (caseInfo.Customer != null && !string.IsNullOrEmpty(caseInfo.Customer.Name))
  39. {
  40. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == caseInfo.Customer.Name).FirstOrDefault();
  41. if (temCustomer == null)
  42. {
  43. temCustomer = new Customer() { Name = caseInfo.Customer.Name };
  44. //item.Customer.Id = 0;
  45. Context.Customers.Add(temCustomer);
  46. Context.SaveChanges();
  47. caseInfo.Customer = temCustomer;
  48. //item.CustomerId = item.Customer.Id;
  49. }
  50. else
  51. {
  52. caseInfo.Customer = temCustomer;
  53. }
  54. caseInfo.CustomerId = caseInfo.Customer.Id;
  55. caseInfo.Customer = null;
  56. }
  57. else
  58. {
  59. caseInfo.Customer = null;
  60. }
  61. #endregion
  62. #region 审核人
  63. if (caseInfo.Reviewer != null && !string.IsNullOrEmpty(caseInfo.Reviewer.Name))
  64. {
  65. var temReviewer = Context.Staffs.Where<Staff>(c => c.Name == caseInfo.Reviewer.Name).FirstOrDefault();
  66. if (temReviewer == null)
  67. {
  68. temReviewer = new Staff() { Name = caseInfo.Reviewer.Name };
  69. //item.Customer.Id = 0;
  70. Context.Staffs.Add(temReviewer);
  71. Context.SaveChanges();
  72. caseInfo.Reviewer = temReviewer;
  73. //item.CustomerId = item.Customer.Id;
  74. }
  75. else
  76. {
  77. caseInfo.Reviewer = temReviewer;
  78. }
  79. caseInfo.ReviewerId = caseInfo.Reviewer.Id;
  80. caseInfo.Reviewer = null;
  81. }
  82. else
  83. {
  84. caseInfo.Reviewer = null;
  85. }
  86. #endregion
  87. if (caseInfo.DRRAbstract != null) {
  88. Context.CaseCompareResults.Add(caseInfo.DRRAbstract);
  89. Context.SaveChanges();
  90. caseInfo.DRRAbstractId = caseInfo.DRRAbstract.Id;
  91. caseInfo.DRRAbstract = null;
  92. }
  93. if (caseInfo.DRRCalim != null)
  94. {
  95. Context.CaseCompareResults.Add(caseInfo.DRRCalim);
  96. Context.SaveChanges();
  97. caseInfo.DRRCalimId = caseInfo.DRRCalim.Id;
  98. caseInfo.DRRCalim = null;
  99. }
  100. if (caseInfo.DRRFulltext != null)
  101. {
  102. Context.CaseCompareResults.Add(caseInfo.DRRFulltext);
  103. Context.SaveChanges();
  104. caseInfo.DRRFulltextId = caseInfo.DRRFulltext.Id;
  105. caseInfo.DRRFulltext = null;
  106. }
  107. if (caseInfo.DRRAll != null)
  108. {
  109. Context.CaseCompareResults.Add(caseInfo.DRRAll);
  110. Context.SaveChanges();
  111. caseInfo.DRRAllId = caseInfo.DRRAll.Id;
  112. caseInfo.DRRAll = null;
  113. }
  114. if (caseInfo.RFRAbstract != null)
  115. {
  116. Context.CaseCompareResults.Add(caseInfo.RFRAbstract);
  117. Context.SaveChanges();
  118. caseInfo.RFRAbstractId = caseInfo.RFRAbstract.Id;
  119. caseInfo.RFRAbstract = null;
  120. }
  121. if (caseInfo.RFRCalim != null)
  122. {
  123. Context.CaseCompareResults.Add(caseInfo.RFRCalim);
  124. Context.SaveChanges();
  125. caseInfo.RFRCalimId = caseInfo.RFRCalim.Id;
  126. caseInfo.RFRCalim = null;
  127. }
  128. if (caseInfo.RFRFulltext != null)
  129. {
  130. Context.CaseCompareResults.Add(caseInfo.RFRFulltext);
  131. Context.SaveChanges();
  132. caseInfo.RFRFulltextId = caseInfo.RFRFulltext.Id;
  133. caseInfo.RFRFulltext = null;
  134. }
  135. if (caseInfo.RFRAll != null)
  136. {
  137. Context.CaseCompareResults.Add(caseInfo.RFRAll);
  138. Context.SaveChanges();
  139. caseInfo.RFRAllId = caseInfo.RFRAll.Id;
  140. caseInfo.RFRAll = null;
  141. }
  142. Context.CaseInfos.Add(caseInfo);
  143. Context.SaveChanges();
  144. Context.Database.CommitTransaction();
  145. }
  146. catch (Exception ex)
  147. {
  148. ret.Success = false;
  149. ret.ErrorMessage = ex.Message;
  150. Context.Database.RollbackTransaction();
  151. }
  152. }
  153. return ret;
  154. }
  155. public ListApiResponse<CaseInfo> QueryFilter(QueryFilter queryFilter)
  156. {
  157. ListApiResponse<CaseInfo> ret = new ListApiResponse<CaseInfo>();
  158. IQueryable<CaseInfo> response = NewMethod(queryFilter);
  159. int totals = response.Count();
  160. if (totals > 0 && totals < (queryFilter.PageIndex) * queryFilter.PageSize)
  161. {
  162. response = response
  163. .Include(pi => pi.DRRCalim)
  164. .Include(pi => pi.DRRAbstract)
  165. .Include(pi => pi.DRRFulltext)
  166. .Include(pi => pi.DRRAll)
  167. .Include(pi => pi.RFRCalim)
  168. .Include(pi => pi.RFRAbstract)
  169. .Include(pi => pi.RFRFulltext)
  170. .Include(pi => pi.RFRAll)
  171. .Include(pi => pi.Customer)
  172. .Include(pi => pi.Reviewer)
  173. .OrderConditions<CaseInfo>(queryFilter.Sorts)
  174. .Skip((queryFilter.PageIndex - 1) * queryFilter.PageSize)
  175. .Take(totals - (queryFilter.PageIndex - 1) * queryFilter.PageSize);
  176. //.Pager<PerformanceItem>(1, queryFilter.PageSize, out totals);
  177. }
  178. else
  179. {
  180. response = response
  181. .Include(pi => pi.DRRCalim)
  182. .Include(pi => pi.DRRAbstract)
  183. .Include(pi => pi.DRRFulltext)
  184. .Include(pi=>pi.DRRAll)
  185. .Include(pi => pi.RFRCalim)
  186. .Include(pi => pi.RFRAbstract)
  187. .Include(pi => pi.RFRFulltext)
  188. .Include(pi => pi.RFRAll)
  189. .Include(pi => pi.Customer)
  190. .Include(pi => pi.Reviewer)
  191. .OrderConditions<CaseInfo>(queryFilter.Sorts)
  192. .Skip((queryFilter.PageIndex - 1) * queryFilter.PageSize)
  193. .Take(queryFilter.PageSize);
  194. }
  195. ret.TotalCount = totals;
  196. var retList = response.ToList<CaseInfo>();
  197. #region 将某些属性设为null,避免循环取值造成返回json过大
  198. foreach (CaseInfo item in retList)
  199. {
  200. item.Customer.PerformanceItems = null;
  201. item.Customer.ResponseMan = null;
  202. item.Reviewer.Customers = null;
  203. item.Reviewer.ExternalHandlerItems = null;
  204. item.Reviewer.ItemStaffs = null;
  205. item.Reviewer.AllocationRatios = null;
  206. item.Reviewer.ReviewerItems = null;
  207. }
  208. #endregion
  209. ret.Results = retList.ToList();
  210. return ret;
  211. }
  212. private string GetExpress(IList<FieldCondition> conditions)
  213. {
  214. string str = "";
  215. foreach (var c in conditions)
  216. {
  217. if (string.IsNullOrEmpty(str))
  218. {
  219. str = c.ToExpressString("s");
  220. }
  221. else
  222. {
  223. if (c.LogicOperate == LogicEnum.And)
  224. {
  225. str = $"({str}) && {c.ToExpressString("s")}";
  226. }
  227. else
  228. {
  229. str = $"({str}) || {c.ToExpressString("s")}";
  230. }
  231. }
  232. }
  233. return str;
  234. }
  235. private IQueryable<CaseInfo> NewMethod(QueryFilter queryFilter)
  236. {
  237. string strExpress = "";
  238. if (queryFilter.ConditionTree != null)
  239. {
  240. strExpress = GetExpress(queryFilter.ConditionTree);
  241. }
  242. var interpreter = new Interpreter();
  243. if (string.IsNullOrEmpty(strExpress))
  244. {
  245. return new spDbContext().CaseInfos.Where<CaseInfo>(p=>(p.Id>0));
  246. }
  247. else
  248. {
  249. Expression<Func<CaseInfo, bool>> dynamicWhere = interpreter.ParseAsExpression<Func<CaseInfo, bool>>(strExpress, "s");
  250. IQueryable<CaseInfo> response = new spDbContext().CaseInfos.Where<CaseInfo>(dynamicWhere);
  251. return response;
  252. }
  253. }
  254. }
  255. }