public async Task <IActionResult> Put(int id, [FromBody] CountyProductSnapshotViewModel countyProductSnapshotVm) { if (countyProductSnapshotVm == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var dbItem = await _countyProductSnapshotRepository.GetSingleAsync(id); if (dbItem == null) { return(NotFound()); } Mapper.Map(countyProductSnapshotVm, dbItem); dbItem.SetModification(UserName); _countyProductSnapshotRepository.Update(dbItem); if (!await UnitOfWork.SaveAsync()) { return(StatusCode(500, "保存时出错")); } return(NoContent()); }
private void InitializeCountyProducts(string userName, CountyDay item, List <ProductForCounty> productForCountys, List <Product> products) { var dbDayProducts = item.CountyProductSnapshots; foreach (var dayProduct in dbDayProducts) { var productForCounty = productForCountys.SingleOrDefault(x => x.Id == dayProduct.ProductForCountyId); if (productForCounty == null) { throw new Exception($"未能找到集体户产品: {dayProduct.Name}"); } var product = products.SingleOrDefault(x => x.Id == productForCounty.ProductId); if (product == null) { throw new Exception($"未能找到产品: {dayProduct.Name}"); } SetCountyProductSnapshot(dayProduct, productForCounty, product); dayProduct.SetModification(userName, "重新初始化"); _collectiveProductSnapshotRepository.Update(dayProduct); } var dayProductIds = dbDayProducts.Select(x => x.ProductForCountyId).ToList(); var collectiveProductIds = productForCountys.Select(x => x.Id).ToList(); var toAddIds = collectiveProductIds.Except(dayProductIds).ToList(); var toAdd = productForCountys.Where(x => toAddIds.Contains(x.Id)).ToList(); foreach (var pr in toAdd) { var dayProduct = new CountyProductSnapshot { ProductForCountyId = pr.Id, }; dayProduct.SetCreation(userName, "初始化"); var product = products.SingleOrDefault(x => x.Id == pr.ProductId); if (product == null) { throw new Exception($"未能找到产品, 集体户产品ID: {pr.Id}"); } SetCountyProductSnapshot(dayProduct, pr, product); item.CountyProductSnapshots.Add(dayProduct); } }