Authored by tanling

解决并发不可重复领取的问题

@@ -12,6 +12,8 @@ public interface UserCouponMapper { @@ -12,6 +12,8 @@ public interface UserCouponMapper {
12 12
13 int insert(UserCoupon record); 13 int insert(UserCoupon record);
14 14
  15 + int insertWhere(UserCoupon record);
  16 +
15 int insertSelective(UserCoupon record); 17 int insertSelective(UserCoupon record);
16 18
17 UserCoupon selectByUidAndToken(@Param("uid") Integer id, @Param("couponToken") String couponToken); 19 UserCoupon selectByUidAndToken(@Param("uid") Integer id, @Param("couponToken") String couponToken);
@@ -88,6 +88,20 @@ @@ -88,6 +88,20 @@
88 #{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER}, 88 #{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
89 #{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR}) 89 #{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR})
90 </insert> 90 </insert>
  91 +
  92 +
  93 + <insert id="insertWhere" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
  94 + insert into user_coupon (id, uid, coupon_id,
  95 + coupon_type, coupon_code, status,
  96 + order_code, use_time, start_time,
  97 + end_time, create_time,coupon_token)
  98 + select #{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{couponId,jdbcType=INTEGER},
  99 + #{couponType,jdbcType=INTEGER}, #{couponCode,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
  100 + #{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
  101 + #{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR} from user_coupon
  102 + where not EXISTS (select 1 from user_coupon where uid=#{uid,jdbcType=INTEGER} and coupon_token=#{couponToken,jdbcType=VARCHAR})
  103 + </insert>
  104 +
91 <insert id="insertSelective" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" > 105 <insert id="insertSelective" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
92 insert into user_coupon 106 insert into user_coupon
93 <trim prefix="(" suffix=")" suffixOverrides="," > 107 <trim prefix="(" suffix=")" suffixOverrides="," >
@@ -134,7 +134,7 @@ public class CouponServiceImpl implements ICouponService { @@ -134,7 +134,7 @@ public class CouponServiceImpl implements ICouponService {
134 134
135 if(!repeatable){ 135 if(!repeatable){
136 // 校验是否可以领取(重复领取,时间是否合法,状态) 136 // 校验是否可以领取(重复领取,时间是否合法,状态)
137 - checkCanAcquire(uid, couponToken, couponAndType); 137 +// checkCanAcquire(uid, couponToken, couponAndType);
138 } 138 }
139 // 校验 优惠券发放总数 139 // 校验 优惠券发放总数
140 checkAndAddCouponSendNum(uid, couponToken); 140 checkAndAddCouponSendNum(uid, couponToken);
@@ -150,7 +150,20 @@ public class CouponServiceImpl implements ICouponService { @@ -150,7 +150,20 @@ public class CouponServiceImpl implements ICouponService {
150 userCoupon.setStartTime(validityRange.lowerEndpoint()); 150 userCoupon.setStartTime(validityRange.lowerEndpoint());
151 userCoupon.setEndTime(validityRange.upperEndpoint()); 151 userCoupon.setEndTime(validityRange.upperEndpoint());
152 } 152 }
153 - int count = userCouponMapper.insert(userCoupon); 153 +
  154 + int count = 0;
  155 + // 不可重复
  156 + if (!repeatable){
  157 + count = userCouponMapper.insertWhere(userCoupon);
  158 + }else{
  159 + count = userCouponMapper.insert(userCoupon);
  160 + }
  161 +
  162 + if (count == 0){
  163 + logger.info("can not repeatable acquire coupon. uid is {}, coupon is {}", uid, couponToken);
  164 + throw new ServiceException(ServiceError.PROMOTION_COUPON_HAS_RECEIVED);
  165 + }
  166 +
154 logger.info("senCoupon success,{},{},{},{}", uid, couponToken, couponCode, count); 167 logger.info("senCoupon success,{},{},{},{}", uid, couponToken, couponCode, count);
155 return couponCode; 168 return couponCode;
156 } 169 }