ChangeDoPersonReviewer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 ChangeDoPersonReviewer : 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 strOldPerson = "";
  39. string strNewPerson = "";
  40. foreach (var iv in result)
  41. {
  42. if (iv.InputField == null)
  43. {
  44. iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId);
  45. }
  46. if (iv.InputField.FieldName == "变更前处理人")
  47. {
  48. strOldPerson = iv.Value;
  49. continue;
  50. }
  51. if (iv.InputField.FieldName == "变更后处理人")
  52. {
  53. strNewPerson = iv.Value;
  54. continue;
  55. }
  56. }
  57. var appRecord = ((spDbContext)spContext).AppealRecords.FirstOrDefault(f => f.Id == appealRecordId);
  58. if(appRecord != null)
  59. {
  60. var itemStaffs = ((spDbContext)spContext).ItemStaffs.Include(p=>p.DoPerson).Where(s => s.ItemId == appRecord.ItemId.Value);
  61. var oldPersons = strOldPerson.Split(new char[] { ' ', ',', ',' },StringSplitOptions.RemoveEmptyEntries);
  62. var newPersons = strNewPerson.Split(new char[] { ' ', ',', ',' }, StringSplitOptions.RemoveEmptyEntries);
  63. bool oldPersonExist = true;
  64. List<ItemStaff> waitingRemoveObj = new List<ItemStaff>();
  65. foreach(var temName in oldPersons)
  66. {
  67. var temItemStaff = itemStaffs.Where(s => s.DoPerson.Name == temName).FirstOrDefault();
  68. if(temItemStaff == null)
  69. {
  70. oldPersonExist = false;
  71. break;
  72. }
  73. else
  74. {
  75. waitingRemoveObj.Add(temItemStaff);
  76. }
  77. }
  78. //foreach (ItemStaff staff in itemStaffs)
  79. //{
  80. // if (oldPersons.Contains(staff.DoPerson.Name))
  81. // {
  82. // waitingRemoveObj.Add(staff);
  83. // oldPersonExist = true;
  84. // }
  85. // else
  86. // {
  87. // oldPersonExist = false;
  88. // break;
  89. // }
  90. //}
  91. if (oldPersonExist)
  92. {
  93. ((spDbContext)spContext).ItemStaffs.RemoveRange(waitingRemoveObj);
  94. ((spDbContext)spContext).SaveChanges();
  95. foreach (var temName in newPersons)
  96. {
  97. var newStaff = ((spDbContext)spContext).Staffs.FirstOrDefault(s => s.Name == temName.Trim());
  98. if (newStaff != null )
  99. {
  100. var temItemStaff = ((spDbContext)spContext).ItemStaffs.Where(s => s.ItemId == appRecord.ItemId.Value && s.DoPersonId == newStaff.Id).FirstOrDefault();
  101. if (temItemStaff == null)
  102. {
  103. ((spDbContext)spContext).ItemStaffs.Add(new ItemStaff()
  104. {
  105. ItemId = appRecord.ItemId.Value,
  106. DoPersonId = newStaff.Id
  107. });
  108. }
  109. }
  110. else
  111. {
  112. throw new ApplicationException("变更后处理人不存在!");
  113. }
  114. }
  115. }
  116. else
  117. {
  118. throw new ApplicationException("变更前处理人不存在!");
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }