using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using wispro.sp.api.Controllers; using wispro.sp.entity; using wispro.sp.share; namespace wispro.sp.api.AppealHandler { public class ChangeDoPersonReviewer : IDoAppealObject { public void DoAppeal(AppealObject appeal, int appealRecordId, DbContext spContext) { bool isAggree = false; foreach (var iv in appeal.inputFieldValues) { if (iv.InputField == null) { iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId); } if (iv.InputField.FieldName == "审核意见" && iv.Value == "确认变更") { isAggree = true; break; } } if (isAggree) { var result = ((spDbContext)spContext).InputFieldValues.Where(f => f.AppealRecordId == appealRecordId && f.InputField.AppealState == 0).ToList(); string strOldPerson = ""; string strNewPerson = ""; foreach (var iv in result) { if (iv.InputField == null) { iv.InputField = ((spDbContext)spContext).InputFields.FirstOrDefault(s => s.Id == iv.InputFieldId); } if (iv.InputField.FieldName == "变更前处理人") { strOldPerson = iv.Value; continue; } if (iv.InputField.FieldName == "变更后处理人") { strNewPerson = iv.Value; continue; } } var appRecord = ((spDbContext)spContext).AppealRecords.FirstOrDefault(f => f.Id == appealRecordId); if(appRecord != null) { var itemStaffs = ((spDbContext)spContext).ItemStaffs.Include(p=>p.DoPerson).Where(s => s.ItemId == appRecord.ItemId.Value); var oldPersons = strOldPerson.Split(new char[] { ' ', ',' },StringSplitOptions.RemoveEmptyEntries); var newPersons = strNewPerson.Split(new char[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries); bool oldPersonExist = false; List waitingRemoveObj = new List(); foreach (ItemStaff staff in itemStaffs) { if (oldPersons.Contains(staff.DoPerson.Name)) { waitingRemoveObj.Add(staff); oldPersonExist = true; } else { oldPersonExist = false; break; } } if (oldPersonExist) { ((spDbContext)spContext).ItemStaffs.RemoveRange(waitingRemoveObj); foreach (var temName in newPersons) { var newStaff = ((spDbContext)spContext).Staffs.FirstOrDefault(s => s.Name == temName.Trim()); if (newStaff != null) { ((spDbContext)spContext).ItemStaffs.Add(new ItemStaff() { ItemId = appRecord.ItemId.Value, DoPersonId = newStaff.Id }); } else { throw new ApplicationException("变更后处理人不存在!"); } } } else { throw new ApplicationException("变更前处理人不存在!"); } } } } } }