MissingCaseReviewHandler.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using wispro.sp.api.Controllers;
  7. using wispro.sp.entity;
  8. using wispro.sp.share;
  9. namespace wispro.sp.api.AppealHandler
  10. {
  11. public class MissingCaseReviewHandler : IDoAppealObject
  12. {
  13. public void DoAppeal(AppealObject appeal, int appealRecordId, DbContext spContext)
  14. {
  15. bool isAggree = false;
  16. foreach (var iv in appeal.inputFieldValues)
  17. {
  18. if (iv.InputField == null)
  19. {
  20. iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
  21. }
  22. if (iv.InputField.FieldName == "审核意见" && iv.Value =="同意")
  23. {
  24. isAggree = true;
  25. break;
  26. }
  27. else
  28. {
  29. if (iv.InputField.FieldName == "审核意见" && string.IsNullOrEmpty(iv.Value))
  30. {
  31. throw (new ApplicationException("请填写审核意见!"));
  32. }
  33. }
  34. }
  35. if (isAggree)
  36. {
  37. var result = ((spDbContext)spContext).InputFieldValues.Where<InputFieldValue>(f => f.AppealRecordId == appealRecordId && f.InputField.AppealState == 0).ToList();
  38. var appealRecord = ((spDbContext)spContext).AppealRecords.Where<AppealRecord>(f => f.Id == appealRecordId).Include(f=>f.Creater).FirstOrDefault();
  39. string strCaseNo ="";
  40. string strDoItem="";
  41. string strCaseStage = "";
  42. foreach (var iv in result)
  43. {
  44. if (iv.InputField == null)
  45. {
  46. iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
  47. }
  48. if (iv.InputField.FieldName == "我方文号")
  49. {
  50. strCaseNo = iv.Value;
  51. continue;
  52. }
  53. if (iv.InputField.FieldName == "处理事项")
  54. {
  55. strDoItem=iv.Value;
  56. continue;
  57. }
  58. if (iv.InputField.FieldName == "案件阶段")
  59. {
  60. strCaseStage = iv.Value;
  61. continue;
  62. }
  63. }
  64. var itemControler = new PerformanceItemController((spDbContext)spContext,new Services.FileTaskCacheService());
  65. PerformanceItem Item = null;
  66. if (strDoItem == "发明一次OA授权")
  67. {
  68. try
  69. {
  70. Item = itemControler.GetItemInfoByCaseStage(strCaseNo, "处理审查意见", "");
  71. }
  72. catch { }
  73. if(Item == null || Item.ItemStaffs.Where(s => s.DoPerson.Name == appealRecord.Creater.Name).Count() ==0 )
  74. {
  75. Item = null;
  76. try
  77. {
  78. Item = itemControler.GetItemInfoByCaseStage(strCaseNo, "电询", "");
  79. }
  80. catch { }
  81. }
  82. if (Item == null || Item.ItemStaffs.Where(s => s.DoPerson.Name == appealRecord.Creater.Name).Count() == 0)
  83. {
  84. Item = null;
  85. try
  86. {
  87. Item = itemControler.GetItemInfoByCaseStage(strCaseNo, "补正", "");
  88. }
  89. catch { }
  90. }
  91. if (Item == null || Item.ItemStaffs.Where(s => s.DoPerson.Name == appealRecord.Creater.Name).Count() == 0)
  92. {
  93. Item = null;
  94. try
  95. {
  96. Item = itemControler.GetItemInfoByCaseStage(strCaseNo, "新申请", "");
  97. }
  98. catch { }
  99. }
  100. }
  101. else
  102. {
  103. Item = itemControler.GetItemInfoByCaseStage(strCaseNo, strDoItem, strCaseStage);
  104. }
  105. CalMonth calMonth = ((spDbContext)spContext).CalMonths.FirstOrDefault(c=>c.Status == 0);
  106. if (calMonth != null )
  107. {
  108. if (Item.Id == 0 || Item.CalMonthId != calMonth.Id)
  109. {
  110. Item.Id = 0;
  111. if (strDoItem == "发明一次OA授权")
  112. {
  113. if (Item.ItemStaffs.Where(s => s.DoPerson.Name == appealRecord.Creater.Name).Count() == 0)
  114. {
  115. throw new Exception($"最后一次{Item.DoItem}处理人与缺漏申请人不一致!");
  116. }
  117. else
  118. {
  119. Item.DoItem = strDoItem;
  120. Item.AgentFeedbackMemo = strDoItem;
  121. }
  122. }
  123. SaveToDb(Item, (spDbContext)spContext, calMonth);
  124. itemControler.RefreshPoint(Item);
  125. }
  126. }
  127. }
  128. }
  129. private void SaveToDb(PerformanceItem item,spDbContext Context,CalMonth calMonth)
  130. {
  131. try
  132. {
  133. item.CalMonthId = calMonth.Id;
  134. var results = Context.PerformanceItems.Where<PerformanceItem>(x =>
  135. x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告" && x.CaseStage == item.CaseStage);
  136. var items = results.Include(pi => pi.CalMonth).FirstOrDefault<PerformanceItem>();
  137. if (items != null)
  138. {
  139. item.AgentFeedbackMemo = "已算绩效";
  140. item.DoItemMemo = $"{items.DoItemMemo}\r\n{items.CalMonth.Year}-{items.CalMonth.Month}已计算!";
  141. item.BasePoint = 0;
  142. }
  143. if (item.Customer != null && !string.IsNullOrEmpty(item.Customer.Name))
  144. {
  145. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == item.Customer.Name).FirstOrDefault();
  146. if (temCustomer == null)
  147. {
  148. temCustomer = new Customer() { Name = item.Customer.Name };
  149. //item.Customer.Id = 0;
  150. Context.Customers.Add(temCustomer);
  151. Context.SaveChanges();
  152. item.Customer = temCustomer;
  153. //item.CustomerId = item.Customer.Id;
  154. }
  155. else
  156. {
  157. item.Customer = temCustomer;
  158. }
  159. item.CustomerId = item.Customer.Id;
  160. item.Customer = null;
  161. }
  162. else
  163. {
  164. item.Customer = null;
  165. }
  166. var ItemStaffs = item.ItemStaffs;
  167. item.ItemStaffs = null;
  168. if (item.ReviewerId != null)
  169. {
  170. var Reviewer = item.Reviewer;
  171. item.Reviewer = null;
  172. }
  173. Context.PerformanceItems.Add(item);
  174. Context.SaveChanges();
  175. foreach (ItemStaff itemStaff in ItemStaffs)
  176. {
  177. itemStaff.ItemId = item.Id;
  178. itemStaff.Item = null;
  179. if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null)
  180. {
  181. var temStaff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == itemStaff.DoPerson.Name);
  182. if (temStaff != null)
  183. {
  184. itemStaff.DoPersonId = temStaff.Id;
  185. itemStaff.DoPerson = null;
  186. }
  187. else
  188. {
  189. Context.Staffs.Add(itemStaff.DoPerson);
  190. Context.SaveChanges();
  191. itemStaff.DoPersonId = itemStaff.DoPerson.Id;
  192. itemStaff.DoPerson = null;
  193. }
  194. }
  195. else
  196. {
  197. itemStaff.DoPerson = null;
  198. }
  199. }
  200. Context.ItemStaffs.AddRange(ItemStaffs);
  201. Context.SaveChanges();
  202. }
  203. catch (Exception ex)
  204. {
  205. throw ex;
  206. }
  207. }
  208. }
  209. }