MissingCaseReviewHandler.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. string strCaseNo="";
  39. string strDoItem="";
  40. string strCaseStage = "";
  41. foreach (var iv in result)
  42. {
  43. if (iv.InputField == null)
  44. {
  45. iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
  46. }
  47. if (iv.InputField.FieldName == "我方文号")
  48. {
  49. strCaseNo = iv.Value;
  50. continue;
  51. }
  52. if (iv.InputField.FieldName == "处理事项")
  53. {
  54. strDoItem=iv.Value;
  55. continue;
  56. }
  57. if (iv.InputField.FieldName == "案件阶段")
  58. {
  59. strCaseStage = iv.Value;
  60. continue;
  61. }
  62. }
  63. var itemControler = new PerformanceItemController((spDbContext)spContext,new Services.FileTaskCacheService());
  64. PerformanceItem Item = itemControler.GetItemInfoByCaseStage(strCaseNo,strDoItem, strCaseStage);
  65. var Rules = ((spDbContext)spContext).BasePointRules.ToList();
  66. Utility.Utility.CalBasePoint(Item, Rules);
  67. CalMonth calMonth = ((spDbContext)spContext).CalMonths.FirstOrDefault(c=>c.Status == 0);
  68. if (calMonth != null )
  69. {
  70. if (Item.Id == 0 || Item.CalMonthId != calMonth.Id)
  71. {
  72. Item.Id = 0;
  73. SaveToDb(Item, (spDbContext)spContext, calMonth);
  74. }
  75. }
  76. //((spDbContext)spContext).PerformanceItems.Add(Item);
  77. //((spDbContext)spContext).SaveChanges();
  78. }
  79. }
  80. private void SaveToDb(PerformanceItem item,spDbContext Context,CalMonth calMonth)
  81. {
  82. try
  83. {
  84. item.CalMonthId = calMonth.Id;
  85. var results = Context.PerformanceItems.Where<PerformanceItem>(x =>
  86. x.CaseNo == item.CaseNo && x.DoItem == item.DoItem && x.DoItem != "提出报告" && x.CaseStage == item.CaseStage);
  87. var items = results.Include(pi => pi.CalMonth).FirstOrDefault<PerformanceItem>();
  88. if (items != null)
  89. {
  90. item.AgentFeedbackMemo = "已算绩效";
  91. item.DoItemMemo = $"{items.DoItemMemo}\r\n{items.CalMonth.Year}-{items.CalMonth.Month}已计算!";
  92. item.BasePoint = 0;
  93. }
  94. if (item.Customer != null && !string.IsNullOrEmpty(item.Customer.Name))
  95. {
  96. var temCustomer = Context.Customers.Where<Customer>(c => c.Name == item.Customer.Name).FirstOrDefault();
  97. if (temCustomer == null)
  98. {
  99. temCustomer = new Customer() { Name = item.Customer.Name };
  100. //item.Customer.Id = 0;
  101. Context.Customers.Add(temCustomer);
  102. Context.SaveChanges();
  103. item.Customer = temCustomer;
  104. //item.CustomerId = item.Customer.Id;
  105. }
  106. else
  107. {
  108. item.Customer = temCustomer;
  109. }
  110. item.CustomerId = item.Customer.Id;
  111. item.Customer = null;
  112. }
  113. else
  114. {
  115. item.Customer = null;
  116. }
  117. var ItemStaffs = item.ItemStaffs;
  118. item.ItemStaffs = null;
  119. if (item.ReviewerId != null)
  120. {
  121. var Reviewer = item.Reviewer;
  122. item.Reviewer = null;
  123. }
  124. Context.PerformanceItems.Add(item);
  125. Context.SaveChanges();
  126. foreach (ItemStaff itemStaff in ItemStaffs)
  127. {
  128. itemStaff.ItemId = item.Id;
  129. itemStaff.Item = null;
  130. if (itemStaff.DoPersonId == 0 && itemStaff.DoPerson != null)
  131. {
  132. var temStaff = Context.Staffs.FirstOrDefault<Staff>(s => s.Name == itemStaff.DoPerson.Name);
  133. if (temStaff != null)
  134. {
  135. itemStaff.DoPersonId = temStaff.Id;
  136. itemStaff.DoPerson = null;
  137. }
  138. else
  139. {
  140. Context.Staffs.Add(itemStaff.DoPerson);
  141. Context.SaveChanges();
  142. itemStaff.DoPersonId = itemStaff.DoPerson.Id;
  143. itemStaff.DoPerson = null;
  144. }
  145. }
  146. else
  147. {
  148. itemStaff.DoPerson = null;
  149. }
  150. }
  151. Context.ItemStaffs.AddRange(ItemStaffs);
  152. Context.SaveChanges();
  153. }
  154. catch (Exception ex)
  155. {
  156. throw ex;
  157. }
  158. }
  159. }
  160. }