Authored by tanling

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

... ... @@ -12,6 +12,8 @@ public interface UserCouponMapper {
int insert(UserCoupon record);
int insertWhere(UserCoupon record);
int insertSelective(UserCoupon record);
UserCoupon selectByUidAndToken(@Param("uid") Integer id, @Param("couponToken") String couponToken);
... ...
... ... @@ -88,6 +88,20 @@
#{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
#{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR})
</insert>
<insert id="insertWhere" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
insert into user_coupon (id, uid, coupon_id,
coupon_type, coupon_code, status,
order_code, use_time, start_time,
end_time, create_time,coupon_token)
select #{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{couponId,jdbcType=INTEGER},
#{couponType,jdbcType=INTEGER}, #{couponCode,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
#{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
#{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR} from user_coupon
where not EXISTS (select 1 from user_coupon where uid=#{uid,jdbcType=INTEGER} and coupon_token=#{couponToken,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
insert into user_coupon
<trim prefix="(" suffix=")" suffixOverrides="," >
... ...
... ... @@ -134,7 +134,7 @@ public class CouponServiceImpl implements ICouponService {
if(!repeatable){
// 校验是否可以领取(重复领取,时间是否合法,状态)
checkCanAcquire(uid, couponToken, couponAndType);
// checkCanAcquire(uid, couponToken, couponAndType);
}
// 校验 优惠券发放总数
checkAndAddCouponSendNum(uid, couponToken);
... ... @@ -150,7 +150,20 @@ public class CouponServiceImpl implements ICouponService {
userCoupon.setStartTime(validityRange.lowerEndpoint());
userCoupon.setEndTime(validityRange.upperEndpoint());
}
int count = userCouponMapper.insert(userCoupon);
int count = 0;
// 不可重复
if (!repeatable){
count = userCouponMapper.insertWhere(userCoupon);
}else{
count = userCouponMapper.insert(userCoupon);
}
if (count == 0){
logger.info("can not repeatable acquire coupon. uid is {}, coupon is {}", uid, couponToken);
throw new ServiceException(ServiceError.PROMOTION_COUPON_HAS_RECEIVED);
}
logger.info("senCoupon success,{},{},{},{}", uid, couponToken, couponCode, count);
return couponCode;
}
... ...