示例#1
0
 public JsonResult Delete(PromotionModel.PromotionSaveModel model)
 {
     _promotionRepository.Delete(o => o.id_ == model.id_);
     _promotionRepository.ClearBannerCache();
     _unitOfWork.Commit();
     return(Json(new {}, JsonRequestBehavior.AllowGet));
 }
示例#2
0
        public JsonResult Save(PromotionModel.PromotionSaveModel model)
        {
            Promotion promotion;
            IList <PromotionDetail> promotionDetails = JsonConvert.DeserializeObject <IList <PromotionDetail> >(model.Details);

            DateTime start = DateTimeHelper.ConvertToLongDay(model.StartDay);
            DateTime end   = DateTimeHelper.ConvertToLongDay(model.EndDay);

            if (model.id_ == 0)
            {
                promotion = new Promotion()
                {
                    Active             = model.Active,
                    Discount           = model.Discount,
                    EndDay             = end,
                    StartDay           = start,
                    IdUserOk           = 0,
                    ProductCategoryIds = model.ProductCategoryIds,
                    ProductIds         = model.ProductIds,
                    Region             = model.Region,
                    Title  = model.Title,
                    IdUser = 15
                };

                _promotionRepository.Add(promotion);
                _unitOfWork.Commit();
            }
            else
            {
                promotion                    = _promotionRepository.GetById(model.id_);
                promotion.Active             = model.Active;
                promotion.Discount           = model.Discount;
                promotion.EndDay             = end;
                promotion.StartDay           = start;
                promotion.ProductCategoryIds = model.ProductCategoryIds;
                promotion.ProductIds         = model.ProductIds;
                promotion.Region             = model.Region;
                promotion.Title              = model.Title;

                _promotionRepository.Update(promotion);
            }

            #region

            IList <PromotionDetail> oldDetails = model.id_ != 0
                                                    ? _promotionDetailRepository.GetMany(o => o.PromotionId == model.id_).ToList()
                                                    : new List <PromotionDetail>();

            foreach (var dt in promotionDetails)
            {
                if (dt.id_ == 0)
                {
                    PromotionDetail promotionDetail = new PromotionDetail()
                    {
                        Percent       = dt.Percent,
                        ProductId     = dt.ProductId,
                        PriceDiscount = dt.PriceDiscount,
                        PromotionId   = promotion.id_,
                        Price         = dt.Price
                    };
                    _promotionDetailRepository.Add(promotionDetail);
                }
                else
                {
                    PromotionDetail promotionDetail = oldDetails.FirstOrDefault(o => o.id_ == dt.id_);
                    if (promotionDetail != null)
                    {
                        promotionDetail.Percent       = dt.Percent;
                        promotionDetail.PriceDiscount = dt.PriceDiscount;
                        promotionDetail.Price         = dt.Price;
                        _promotionDetailRepository.Update(promotionDetail);
                    }
                }
            }
            #endregion

            _promotionRepository.ClearBannerCache();
            _unitOfWork.Commit();

            return(Json(new
            {
                PromotionId = promotion.id_
            }, JsonRequestBehavior.AllowGet));
        }