public async Task <IActionResult> Put(int id, [FromBody] CountyPromotionSeriesEditViewModel countyPromotionSeriesVm) { if (countyPromotionSeriesVm == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbItem = await _countyPromotionSeriesRepository.GetSingleAsync(x => x.Id == id, x => x.CountyPromotionSeriesBonuses); if (dbItem == null) { return(NotFound()); } await ValidateCountyDay(countyPromotionSeriesVm.StartDate); var bonusVms = countyPromotionSeriesVm.CountyPromotionSeriesBonuses; countyPromotionSeriesVm.CountyPromotionSeriesBonuses = null; var bonuses = dbItem.CountyPromotionSeriesBonuses; dbItem.CountyPromotionSeriesBonuses = null; Mapper.Map(countyPromotionSeriesVm, dbItem); dbItem.SetModification(UserName); var toAddVms = bonusVms.Where(x => x.Id == 0).ToList(); var toAdd = Mapper.Map <List <CountyPromotionSeriesBonus> >(toAddVms); foreach (var bonus in toAdd) { bonus.SetCreation(UserName); } _countyPromotionSeriesBonusRepository.AddRange(toAdd); var vmIds = bonusVms.Where(x => x.Id != 0).Select(x => x.Id).ToList(); var dbIds = bonuses.Select(x => x.Id).ToList(); var toDeleteIds = dbIds.Except(vmIds).ToList(); var toDelete = bonuses.Where(x => toDeleteIds.Contains(x.Id)).ToList(); _countyPromotionSeriesBonusRepository.DeleteRange(toDelete); var toUpdateIds = vmIds.Intersect(dbIds).ToList(); var toUpdate = bonuses.Where(x => toUpdateIds.Contains(x.Id)).ToList(); foreach (var bonus in toUpdate) { var vm = bonusVms.SingleOrDefault(x => x.Id == bonus.Id); if (vm != null) { Mapper.Map(vm, bonus); bonus.SetModification(UserName); _countyPromotionSeriesBonusRepository.Update(bonus); } } dbItem.CountyPromotionSeriesBonuses = toUpdate.Concat(toAdd).ToList(); var toDeleteEvents = await _countyPromotionEventRepository .AllIncluding(x => x.CountyPromotionEventBonuses) .Where(x => x.CountyPromotionSeriesId == id).ToListAsync(); var toDeleteEventBonuses = toDeleteEvents.SelectMany(x => x.CountyPromotionEventBonuses).ToList(); _countyPromotionEventBonusRepository.DeleteRange(toDeleteEventBonuses); _countyPromotionEventRepository.DeleteRange(toDeleteEvents); var events = _countyPromotionEventRepository.GenerateEvents(dbItem).ToList(); TryValidateModel(events); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _countyPromotionEventRepository.AddRange(events); _countyPromotionSeriesRepository.Update(dbItem); if (!await UnitOfWork.SaveAsync()) { return(StatusCode(500, "保存时出错")); } return(NoContent()); }
public async Task <IActionResult> Put(int id, [FromBody] CountyPromotionEventViewModel countyPromotionEventVm) { if (countyPromotionEventVm == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await ValidateCountyDay(countyPromotionEventVm.Date); var dbItem = await _countyPromotionEventRepository.GetSingleAsync(x => x.Id == id, x => x.CountyPromotionEventBonuses); if (dbItem == null) { return(NotFound()); } var bonusVms = countyPromotionEventVm.CountyPromotionEventBonuses; countyPromotionEventVm.CountyPromotionEventBonuses = null; var bonuses = dbItem.CountyPromotionEventBonuses; dbItem.CountyPromotionEventBonuses = null; Mapper.Map(countyPromotionEventVm, dbItem); dbItem.SetModification(UserName); var toAddBonusVms = bonusVms.Where(x => x.Id == 0).ToList(); var toAddBonuses = Mapper.Map <List <CountyPromotionEventBonus> >(toAddBonusVms); foreach (var addBonus in toAddBonuses) { addBonus.SetCreation(UserName); _countyPromotionEventBonusRepository.Add(addBonus); } var bonusVmIds = bonusVms.Where(x => x.Id > 0).Select(x => x.Id).ToList(); var bonusIds = bonuses.Select(x => x.Id).ToList(); var toDeleteIds = bonusIds.Except(bonusVmIds); var toDeleteBonuses = bonuses.Where(x => toDeleteIds.Contains(x.Id)); _countyPromotionEventBonusRepository.DeleteRange(toDeleteBonuses); var toUpdateIds = bonusIds.Intersect(bonusVmIds); var toUpdateBonuses = bonuses.Where(x => toUpdateIds.Contains(x.Id)).ToList(); foreach (var bonus in toUpdateBonuses) { var bonusVm = bonusVms.SingleOrDefault(x => x.Id == bonus.Id); Mapper.Map(bonusVm, bonus); bonus.SetModification(UserName); _countyPromotionEventBonusRepository.Update(bonus); } dbItem.CountyPromotionEventBonuses = toAddBonuses.Concat(toUpdateBonuses).ToList(); _countyPromotionEventRepository.Update(dbItem); if (!await UnitOfWork.SaveAsync()) { return(StatusCode(500, "保存时出错")); } return(NoContent()); }