// 使用优惠券
        public bool deleteBuyerCoupon(string couponid)
        {
            BuyerCoupon buyerCoupon = _context.BuyerCoupons.Where(x => x.CouponId == couponid).FirstOrDefault();

            if (buyerCoupon.Amount > 0)
            {
                buyerCoupon.Amount--;
                _context.BuyerCoupons.Update(buyerCoupon);
            }
            else
            {
                _context.BuyerCoupons.Remove(buyerCoupon);
            }

            if (_context.SaveChanges() > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Example #2
0
        /************************* 活动服务 *******************/
        // 发布活动
        public bool releaseActivity(string name, string category, DateTime startTime, DateTime endTime, string description, decimal?constrict, decimal?minus)
        {
            CreateIdCount createIdCount = new CreateIdCount(_context);
            string        activityID    = createIdCount.GetActivityCount();
            Activity      activity      = new Activity();

            activity.ActivityId = activityID;
            activity.Name       = name;

            switch (category)
            {
            case "全场活动": activity.Category = 1; break;

            case "特类商品": activity.Category = 2; break;

            case "店铺活动": activity.Category = 3; break;

            case "单个商品": activity.Category = 4; break;
            }

            activity.StartTime   = startTime;
            activity.EndTime     = endTime;
            activity.Description = description;
            _context.Activities.Add(activity);   // 创建活动

            // 发布优惠券
            string couponID = createIdCount.GetCouponCount();
            Coupon coupon   = new Coupon();

            coupon.CouponId  = couponID;
            coupon.StartTime = startTime;
            coupon.EndTime   = endTime;
            coupon.Threshold = constrict;
            coupon.Discount1 = minus;
            coupon.Discount2 = 0;
            switch (category)
            {
            case "全场活动": coupon.Category = 1; break;

            case "特类商品": coupon.Category = 2; break;

            case "店铺活动": coupon.Category = 3; break;

            case "单个商品": coupon.Category = 4; break;
            }
            coupon.ShopId     = "1";
            coupon.ActivityId = activityID;
            _context.Coupons.Add(coupon);     // 添加优惠券
            if (_context.SaveChanges() < 0)   // 先注入优惠券,保证约束
            {
                return(false);
            }

            // 给买家发布优惠券
            List <Buyer> buyers = _context.Buyers.Where(x => x.BuyerId != null).ToList();

            foreach (Buyer buyer in buyers)
            {
                BuyerCoupon buyerCoupon = new BuyerCoupon
                {
                    BuyerId  = buyer.BuyerId,
                    CouponId = couponID,
                    Amount   = 10
                };
                _context.BuyerCoupons.Add(buyerCoupon);     // 每个买家10张优惠券
            }

            // 给店铺发布优惠券
            CouponShop couponShop = new CouponShop
            {
                ShopId   = "1",
                CouponId = couponID,
                Amount   = 1000
            };

            _context.CouponShops.Add(couponShop);      // 每个店铺1000张优惠券

            if (_context.SaveChanges() > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }