예제 #1
0
        public HttpResponseMessage couponOwn(int id)
        {
            if (_workContext.CurrentUser == null)
            {
                return(PageHelper.toJson(PageHelper.ReturnValue(false, "请先登录优客惠")));
            }

            var couponCategory = _couponCategoryService.GetCouponCategoryById(id);

            //检测是否已经抢过
            var sech = new CouponOwnerSearchCondition
            {
                userId = _workContext.CurrentUser.Id
            };
            var list = _couponOwnerService.GetCouponOwnByCondition(sech).Select(p => p.CouponId).ToArray();

            if (list.Any())
            {
                var sech2 = new CouponSearchCondition
                {
                    CouponCategoryId = id,
                    IdArray          = list
                };
                var count = _couponService.GetCouponCount(sech2);
                if (count != 0)
                {
                    return(PageHelper.toJson(PageHelper.ReturnValue(false, "您已经抢过这家的优惠券了")));
                }
            }

            var condition = new CouponSearchCondition
            {
                CouponCategoryId = id,
                Status           = 0
            };
            var coupon = _couponService.GetCouponByCondition(condition).FirstOrDefault();

            _couponOwnerService.CreateRecord(_workContext.CurrentUser.Id, coupon.Id);
            coupon.Status = EnumCouponStatus.Owned;
            _couponService.Update(coupon);
            couponCategory.Count = couponCategory.Count - 1;
            _couponCategoryService.UpdateCouponCategory(couponCategory);
            var brand     = _productBrandService.GetProductBrandById(couponCategory.BrandId);
            var CouponOwn = new CouponCategoryModel
            {
                Name      = couponCategory.Name,
                Number    = coupon.Number,
                BrandName = brand.Bname
            };
            var model = _brokerService.GetBrokerByUserId(_workContext.CurrentUser.Id);

            //短信发送
            SMSHelper.Sending(model.Phone, "优惠券为:" + brand.Bname + ",券号为:" + coupon.Number + " 【优客惠】");

            return(PageHelper.toJson(CouponOwn));
        }
예제 #2
0
        public IQueryable <CouponOwner> GetCouponOwnByCondition(CouponOwnerSearchCondition condition)
        {
            var query = _repository.Table;

            try
            {
                if (condition.userId.HasValue)
                {
                    query = query.Where(q => q.UserId == condition.userId);
                }
                if (condition.couponId.HasValue)
                {
                    query = query.Where(q => q.CouponId == condition.couponId);
                }
                return(query);
            }
            catch (Exception e)
            {
                _log.Error(e, "数据库操作出错");
                return(null);
            }
        }