Authored by chenchao

base coupon check receive start time

package com.yohoufo.dal.promotion.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.math.BigDecimal;
@ToString
public class Coupon {
private Integer id;
... ... @@ -27,6 +32,9 @@ public class Coupon {
private String productLimitValue;
@Getter@Setter
private Integer receiveStartTime;
private Integer startTime;
private Integer endTime;
... ...
... ... @@ -23,11 +23,13 @@
<result column="skup_forbid_type" jdbcType="VARCHAR" property="skupForbidType" />
<result column="skup_allow_type" jdbcType="VARCHAR" property="skupAllowType" />
<result column="business_client" jdbcType="VARCHAR" property="businessClient" />
<result column="receive_start_time" property="receiveStartTime" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List">
id, coupon_token, coupon_name, coupon_amount, coupon_type, coupon_num, use_num, send_num,
use_limit_type, use_limit_value, product_limit_type, product_limit_value, start_time,
end_time, status, create_time, pid, remark,skup_forbid_type,skup_allow_type,business_client
end_time, status, create_time, pid, remark,skup_forbid_type,skup_allow_type,business_client,
receive_start_time
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
... ...
... ... @@ -109,7 +109,7 @@ public class CouponServiceImpl implements ICouponService {
validityRange = Range.open(now, end);
}
return sendCoupons(uid, couponTokens.stream().collect(Collectors.joining(",")), repeatable, validityRange);
return sendCoupons(uid, couponTokens.stream().collect(Collectors.joining(",")), repeatable, validityRange, true);
}
... ... @@ -118,6 +118,8 @@ public class CouponServiceImpl implements ICouponService {
// 允许重复发放
boolean repeatable;
//是否需要校验可领取时间
boolean receiveStartTimeChkNecessary = false;
// 有效期
Range<Integer> validityRange;
... ... @@ -129,15 +131,20 @@ public class CouponServiceImpl implements ICouponService {
}else if(CouponSendType.PLATFORM_SEND_COUPON.equals(sendType)){
repeatable = true;
validityRange = null;
} else {
} else {//用户领取
repeatable = false;
validityRange = null;
receiveStartTimeChkNecessary = true;
}
return sendCoupons(uid, couponTokens, repeatable, validityRange);
return sendCoupons(uid, couponTokens, repeatable, validityRange, receiveStartTimeChkNecessary);
}
private List<CouponSendBo> sendCoupons(Integer uid, String couponTokens, boolean repeatable, Range<Integer> validityRange) {
private List<CouponSendBo> sendCoupons(Integer uid,
String couponTokens,
boolean repeatable,
Range<Integer> validityRange,
boolean receiveStartTimeChkNecessary) {
if (uid == null || uid.intValue() <=0
|| StringUtils.isEmpty(couponTokens)){
... ... @@ -154,7 +161,7 @@ public class CouponServiceImpl implements ICouponService {
CouponSendBo couponSendBo = new CouponSendBo();
couponSendBo.setCouponToken(couponToken);
try {
couponSendBo.setCouponCode(sendPerCoupon(uid, couponToken, repeatable,validityRange));
couponSendBo.setCouponCode(sendPerCoupon(uid, couponToken, repeatable,validityRange, receiveStartTimeChkNecessary));
couponSendBo.setSuccess(true);
} catch (ServiceException ex) {
logger.info("happened serviceException,uid:{},couponTokens:{}", uid, couponTokens, ex);
... ... @@ -171,23 +178,43 @@ public class CouponServiceImpl implements ICouponService {
return couponSendBoList;
}
private String sendPerCoupon(Integer uid, String couponToken, boolean repeatable, Range<Integer> validityRange) {
private void checkReceiveStartTime(int uid, Coupon coupon){
Integer receiveStartTime;
Integer currentDT = DateUtil.getCurrentTimeSecond();
if (Objects.nonNull(receiveStartTime = coupon.getReceiveStartTime())
&& currentDT < receiveStartTime){
logger.warn("sendCoupon find current time not reach receiveStartTime,uid {} couponToken {} [{} {}]", uid, coupon.getCouponToken(), currentDT, receiveStartTime);
throw new ServiceException(ServiceError.PROMOTION_COUPON_NOT_ARRIVE_GET_START_TIME);
}
}
logger.info("senCoupon enter,{},{},{}", uid, couponToken, repeatable);
private String sendPerCoupon(Integer uid,
String couponToken,
boolean repeatable,
Range<Integer> validityRange,
boolean receiveStartTimeChkNecessary) {
logger.info("sendCoupon enter,{},{},{},{},{}", uid, couponToken, repeatable, validityRange, receiveStartTimeChkNecessary);
// 根据 couponToken 获取coupon&couponType信息
CouponAndType couponAndType = couponCacheService.getCouponAndType(couponToken);
if (couponAndType == null){
logger.warn("query coupon info,coupontype is null:{}", couponToken);
logger.warn("sendCoupon query coupon info,coupontype is null:{}", couponToken);
throw new ServiceException(ServiceError.PROMOTION_COUPON_HAS_NOT_EXISTS);
}
Coupon coupon = couponAndType.getCoupon();
//check receive start time
if(receiveStartTimeChkNecessary){
//
checkReceiveStartTime(uid, coupon);
}
if(!repeatable){
// 校验是否可以领取(重复领取,时间是否合法,状态)
checkCanAcquire(uid, couponToken, couponAndType);
}
if (couponAndType.getCoupon().getStatus() != CouponsStatusEnum.VALID.getCode()){
if (coupon.getStatus() != CouponsStatusEnum.VALID.getCode()){
logger.info("couponrulebo status is not valid:{},{}",uid, couponToken);
throw new ServiceException(ServiceError.PROMOTION_COUPON_IS_NOT_VAILD);
}
... ...