/// <summary> /// 发放优惠券到指定会员 /// </summary> /// <param name="member"></param> /// <param name="coupon"></param> /// <returns></returns> private bool SendCouponToMember(Member member, Models.Coupon coupon) { CouponRelation couponRel = new CouponRelation { Id = KeyGenerator.GetGuidKey(), CouponId = coupon.Id, CouponCode = _couponService.CreateCouponCode(), MemberId = member.Id, CreateTime = DateTime.Now, IsUsed = false, FromType = CouponRelation.EnumFromType.Send }; //有效期 if (coupon.ExpiryType == ExpiryType.ExpiryByDay) { couponRel.StartTime = DateTime.Now; if (coupon.ExpiryDay != 0) { couponRel.EndTime = DateTime.Now.AddDays(Convert.ToInt32(coupon.ExpiryDay)); } else { couponRel.EndTime = DateTime.MaxValue; } } else { couponRel.StartTime = coupon.StartTime; couponRel.EndTime = coupon.EndTime; } return(_currencyService.Create(couponRel)); }
public ApiResult ReceiveCoupon(Guid couponId) { var coupon = _currencyService.GetSingleById <Models.Coupon>(couponId); var couponRelation = _currencyService.GetSingleByConditon <Models.CouponRelation>(c => c.CouponId == couponId && c.MemberId == AuthorizedUser.Id); if (coupon == null) { throw new WebApiInnerException("0001", "优惠券Id不合法"); } if (coupon.Quantity == 0) { throw new WebApiInnerException("0002", "优惠券已领完"); } if (couponRelation != null) { throw new WebApiInnerException("0003", "已领取过优惠券"); } Models.CouponRelation model = new Models.CouponRelation { Id = KeyGenerator.GetGuidKey(), CouponId = coupon.Id, CouponCode = _couponService.CreateCouponCode(), MemberId = AuthorizedUser.Id, CreateTime = DateTime.Now, IsUsed = false, FromType = CouponRelation.EnumFromType.Get }; //有效期 if (coupon.ExpiryType == ExpiryType.ExpiryByDay) { model.StartTime = DateTime.Now; if (coupon.ExpiryDay != 0) { model.EndTime = DateTime.Now.AddDays(Convert.ToInt32(coupon.ExpiryDay)); } else { model.EndTime = DateTime.MaxValue; } } else { model.StartTime = coupon.StartTime; model.EndTime = coupon.EndTime; } coupon.Quantity = coupon.Quantity - 1; _currencyService.Update(coupon); _currencyService.Create(model); return(new ApiResult()); }