ChangeDoPersonReviewer.cs 5.3 KB

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