public async Task <IActionResult> Put(int id, [FromBody] RetailPromotionSeriesBonusViewModel retailPromotionSeriesBonusVm)
        {
            if (retailPromotionSeriesBonusVm == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var dbItem = await _retailPromotionSeriesBonusRepository.GetSingleAsync(id);

            if (dbItem == null)
            {
                return(NotFound());
            }
            Mapper.Map(retailPromotionSeriesBonusVm, dbItem);
            dbItem.SetModification(UserName);
            _retailPromotionSeriesBonusRepository.Update(dbItem);
            if (!await UnitOfWork.SaveAsync())
            {
                return(StatusCode(500, "保存时出错"));
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> Put(int id, [FromBody] RetailPromotionSeriesEditViewModel retailPromotionSeriesVm)
        {
            if (retailPromotionSeriesVm == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var dbItem = await _retailPromotionSeriesRepository.GetSingleAsync(x => x.Id == id, x => x.RetailPromotionSeriesBonuses);

            if (dbItem == null)
            {
                return(NotFound());
            }
            await ValidateRetailDay(retailPromotionSeriesVm.StartDate);

            var bonusVms = retailPromotionSeriesVm.RetailPromotionSeriesBonuses;

            retailPromotionSeriesVm.RetailPromotionSeriesBonuses = null;
            var bonuses = dbItem.RetailPromotionSeriesBonuses;

            dbItem.RetailPromotionSeriesBonuses = null;
            Mapper.Map(retailPromotionSeriesVm, dbItem);
            dbItem.SetModification(UserName);

            var toAddVms = bonusVms.Where(x => x.Id == 0).ToList();
            var toAdd    = Mapper.Map <List <RetailPromotionSeriesBonus> >(toAddVms);

            foreach (var bonus in toAdd)
            {
                bonus.SetCreation(UserName);
            }
            _retailPromotionSeriesBonusRepository.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();

            _retailPromotionSeriesBonusRepository.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);
                    _retailPromotionSeriesBonusRepository.Update(bonus);
                }
            }

            dbItem.RetailPromotionSeriesBonuses = toUpdate.Concat(toAdd).ToList();

            var toDeleteEvents = await _retailPromotionEventRepository
                                 .AllIncluding(x => x.RetailPromotionEventBonuses)
                                 .Where(x => x.RetailPromotionSeriesId == id).ToListAsync();

            var toDeleteEventBonuses = toDeleteEvents.SelectMany(x => x.RetailPromotionEventBonuses).ToList();

            _retailPromotionEventBonusRepository.DeleteRange(toDeleteEventBonuses);
            _retailPromotionEventRepository.DeleteRange(toDeleteEvents);

            var events = _retailPromotionEventRepository.GenerateEvents(dbItem).ToList();

            TryValidateModel(events);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            _retailPromotionEventRepository.AddRange(events);

            _retailPromotionSeriesRepository.Update(dbItem);
            if (!await UnitOfWork.SaveAsync())
            {
                return(StatusCode(500, "保存时出错"));
            }

            return(NoContent());
        }