Authored by chenchao

base coupon check receive start time

1 package com.yohoufo.dal.promotion.model; 1 package com.yohoufo.dal.promotion.model;
2 2
  3 +import lombok.Getter;
  4 +import lombok.Setter;
  5 +import lombok.ToString;
  6 +
3 import java.math.BigDecimal; 7 import java.math.BigDecimal;
4 8
  9 +@ToString
5 public class Coupon { 10 public class Coupon {
6 private Integer id; 11 private Integer id;
7 12
@@ -27,6 +32,9 @@ public class Coupon { @@ -27,6 +32,9 @@ public class Coupon {
27 32
28 private String productLimitValue; 33 private String productLimitValue;
29 34
  35 + @Getter@Setter
  36 + private Integer receiveStartTime;
  37 +
30 private Integer startTime; 38 private Integer startTime;
31 39
32 private Integer endTime; 40 private Integer endTime;
@@ -23,11 +23,13 @@ @@ -23,11 +23,13 @@
23 <result column="skup_forbid_type" jdbcType="VARCHAR" property="skupForbidType" /> 23 <result column="skup_forbid_type" jdbcType="VARCHAR" property="skupForbidType" />
24 <result column="skup_allow_type" jdbcType="VARCHAR" property="skupAllowType" /> 24 <result column="skup_allow_type" jdbcType="VARCHAR" property="skupAllowType" />
25 <result column="business_client" jdbcType="VARCHAR" property="businessClient" /> 25 <result column="business_client" jdbcType="VARCHAR" property="businessClient" />
  26 + <result column="receive_start_time" property="receiveStartTime" jdbcType="INTEGER" />
26 </resultMap> 27 </resultMap>
27 <sql id="Base_Column_List"> 28 <sql id="Base_Column_List">
28 id, coupon_token, coupon_name, coupon_amount, coupon_type, coupon_num, use_num, send_num, 29 id, coupon_token, coupon_name, coupon_amount, coupon_type, coupon_num, use_num, send_num,
29 use_limit_type, use_limit_value, product_limit_type, product_limit_value, start_time, 30 use_limit_type, use_limit_value, product_limit_type, product_limit_value, start_time,
30 - end_time, status, create_time, pid, remark,skup_forbid_type,skup_allow_type,business_client 31 + end_time, status, create_time, pid, remark,skup_forbid_type,skup_allow_type,business_client,
  32 + receive_start_time
31 </sql> 33 </sql>
32 <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> 34 <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
33 select 35 select
@@ -109,7 +109,7 @@ public class CouponServiceImpl implements ICouponService { @@ -109,7 +109,7 @@ public class CouponServiceImpl implements ICouponService {
109 validityRange = Range.open(now, end); 109 validityRange = Range.open(now, end);
110 } 110 }
111 111
112 - return sendCoupons(uid, couponTokens.stream().collect(Collectors.joining(",")), repeatable, validityRange); 112 + return sendCoupons(uid, couponTokens.stream().collect(Collectors.joining(",")), repeatable, validityRange, true);
113 } 113 }
114 114
115 115
@@ -118,6 +118,8 @@ public class CouponServiceImpl implements ICouponService { @@ -118,6 +118,8 @@ public class CouponServiceImpl implements ICouponService {
118 118
119 // 允许重复发放 119 // 允许重复发放
120 boolean repeatable; 120 boolean repeatable;
  121 + //是否需要校验可领取时间
  122 + boolean receiveStartTimeChkNecessary = false;
121 // 有效期 123 // 有效期
122 Range<Integer> validityRange; 124 Range<Integer> validityRange;
123 125
@@ -129,15 +131,20 @@ public class CouponServiceImpl implements ICouponService { @@ -129,15 +131,20 @@ public class CouponServiceImpl implements ICouponService {
129 }else if(CouponSendType.PLATFORM_SEND_COUPON.equals(sendType)){ 131 }else if(CouponSendType.PLATFORM_SEND_COUPON.equals(sendType)){
130 repeatable = true; 132 repeatable = true;
131 validityRange = null; 133 validityRange = null;
132 - } else { 134 + } else {//用户领取
133 repeatable = false; 135 repeatable = false;
134 validityRange = null; 136 validityRange = null;
  137 + receiveStartTimeChkNecessary = true;
135 } 138 }
136 139
137 - return sendCoupons(uid, couponTokens, repeatable, validityRange); 140 + return sendCoupons(uid, couponTokens, repeatable, validityRange, receiveStartTimeChkNecessary);
138 } 141 }
139 142
140 - private List<CouponSendBo> sendCoupons(Integer uid, String couponTokens, boolean repeatable, Range<Integer> validityRange) { 143 + private List<CouponSendBo> sendCoupons(Integer uid,
  144 + String couponTokens,
  145 + boolean repeatable,
  146 + Range<Integer> validityRange,
  147 + boolean receiveStartTimeChkNecessary) {
141 148
142 if (uid == null || uid.intValue() <=0 149 if (uid == null || uid.intValue() <=0
143 || StringUtils.isEmpty(couponTokens)){ 150 || StringUtils.isEmpty(couponTokens)){
@@ -154,7 +161,7 @@ public class CouponServiceImpl implements ICouponService { @@ -154,7 +161,7 @@ public class CouponServiceImpl implements ICouponService {
154 CouponSendBo couponSendBo = new CouponSendBo(); 161 CouponSendBo couponSendBo = new CouponSendBo();
155 couponSendBo.setCouponToken(couponToken); 162 couponSendBo.setCouponToken(couponToken);
156 try { 163 try {
157 - couponSendBo.setCouponCode(sendPerCoupon(uid, couponToken, repeatable,validityRange)); 164 + couponSendBo.setCouponCode(sendPerCoupon(uid, couponToken, repeatable,validityRange, receiveStartTimeChkNecessary));
158 couponSendBo.setSuccess(true); 165 couponSendBo.setSuccess(true);
159 } catch (ServiceException ex) { 166 } catch (ServiceException ex) {
160 logger.info("happened serviceException,uid:{},couponTokens:{}", uid, couponTokens, ex); 167 logger.info("happened serviceException,uid:{},couponTokens:{}", uid, couponTokens, ex);
@@ -171,23 +178,43 @@ public class CouponServiceImpl implements ICouponService { @@ -171,23 +178,43 @@ public class CouponServiceImpl implements ICouponService {
171 return couponSendBoList; 178 return couponSendBoList;
172 } 179 }
173 180
174 - private String sendPerCoupon(Integer uid, String couponToken, boolean repeatable, Range<Integer> validityRange) { 181 + private void checkReceiveStartTime(int uid, Coupon coupon){
  182 + Integer receiveStartTime;
  183 + Integer currentDT = DateUtil.getCurrentTimeSecond();
  184 + if (Objects.nonNull(receiveStartTime = coupon.getReceiveStartTime())
  185 + && currentDT < receiveStartTime){
  186 + logger.warn("sendCoupon find current time not reach receiveStartTime,uid {} couponToken {} [{} {}]", uid, coupon.getCouponToken(), currentDT, receiveStartTime);
  187 + throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ARRIVE_GET_START_TIME);
  188 + }
  189 + }
175 190
176 - logger.info("senCoupon enter,{},{},{}", uid, couponToken, repeatable); 191 + private String sendPerCoupon(Integer uid,
  192 + String couponToken,
  193 + boolean repeatable,
  194 + Range<Integer> validityRange,
  195 + boolean receiveStartTimeChkNecessary) {
  196 +
  197 + logger.info("sendCoupon enter,{},{},{},{},{}", uid, couponToken, repeatable, validityRange, receiveStartTimeChkNecessary);
177 198
178 // 根据 couponToken 获取coupon&couponType信息 199 // 根据 couponToken 获取coupon&couponType信息
179 CouponAndType couponAndType = couponCacheService.getCouponAndType(couponToken); 200 CouponAndType couponAndType = couponCacheService.getCouponAndType(couponToken);
180 if (couponAndType == null){ 201 if (couponAndType == null){
181 - logger.warn("query coupon info,coupontype is null:{}", couponToken); 202 + logger.warn("sendCoupon query coupon info,coupontype is null:{}", couponToken);
182 throw new ServiceException(ServiceError.PROMOTION_COUPON_HAS_NOT_EXISTS); 203 throw new ServiceException(ServiceError.PROMOTION_COUPON_HAS_NOT_EXISTS);
183 } 204 }
  205 + Coupon coupon = couponAndType.getCoupon();
  206 + //check receive start time
  207 + if(receiveStartTimeChkNecessary){
  208 + //
  209 + checkReceiveStartTime(uid, coupon);
  210 + }
184 211
185 if(!repeatable){ 212 if(!repeatable){
186 // 校验是否可以领取(重复领取,时间是否合法,状态) 213 // 校验是否可以领取(重复领取,时间是否合法,状态)
187 checkCanAcquire(uid, couponToken, couponAndType); 214 checkCanAcquire(uid, couponToken, couponAndType);
188 } 215 }
189 216
190 - if (couponAndType.getCoupon().getStatus() != CouponsStatusEnum.VALID.getCode()){ 217 + if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()){
191 logger.info("couponrulebo status is not valid:{},{}",uid, couponToken); 218 logger.info("couponrulebo status is not valid:{},{}",uid, couponToken);
192 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD); 219 throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
193 } 220 }