提交代码

dev-discount
胡锐 2 years ago
parent 93abb5496b
commit c751f35d9e
  1. 1
      hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java
  2. 29
      hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java

@ -175,6 +175,7 @@ public class HighDiscountController {
try { try {
highDiscountAgentCodeService.useDiscount(discountAgentCodeId); highDiscountAgentCodeService.useDiscount(discountAgentCodeId);
return ResponseMsgUtil.success("操作成功"); return ResponseMsgUtil.success("操作成功");
} catch (DeadlockLoserDataAccessException deadlockLoserDataAccessException) { } catch (DeadlockLoserDataAccessException deadlockLoserDataAccessException) {

@ -16,6 +16,7 @@ import com.hai.model.HighUserModel;
import com.hai.service.*; import com.hai.service.*;
import org.apache.commons.collections4.MapUtils; import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
@ -68,6 +69,9 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe
@Resource @Resource
private UserCenter userCenter; private UserCenter userCenter;
@Resource
private RedisTemplate redisTemplate;
@Override @Override
public void insertCodeList(List<HighDiscountAgentCode> discountAgentCodeList) { public void insertCodeList(List<HighDiscountAgentCode> discountAgentCodeList) {
highDiscountAgentCodeMapper.insertListCode(discountAgentCodeList); highDiscountAgentCodeMapper.insertListCode(discountAgentCodeList);
@ -181,7 +185,7 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe
} }
@Override @Override
@Transactional(rollbackFor=Exception.class,isolation = Isolation.SERIALIZABLE,propagation= Propagation.REQUIRES_NEW, timeout = 30) @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW, timeout = 30)
public void useDiscount(Long discountAgentCodeId) throws Exception { public void useDiscount(Long discountAgentCodeId) throws Exception {
HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class); HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class);
if (userModel == null) { if (userModel == null) {
@ -198,6 +202,17 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe
if (!discountAgentCode.getStatus().equals(2)) { if (!discountAgentCode.getStatus().equals(2)) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态错误");
} }
HighDiscountUserRel discountUserRel = highDiscountUserRelService.getRelByAgentCodeId(discountAgentCodeId);
if (discountUserRel != null) {
if (!userModel.getHighUser().getId().equals(discountUserRel.getUserId())) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法使用,领取人和使用人不一致。");
}
}
String key = "DiscountAgentId_" + discountAgentCode.getDiscountAgentId();
try {
// 分布式锁占坑
Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, discountAgentCode.getId());
if(lock) {
if (StringUtils.isBlank(discountAgentCode.getExt2())) { if (StringUtils.isBlank(discountAgentCode.getExt2())) {
HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId());
if (rel == null) { if (rel == null) {
@ -238,10 +253,20 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe
couponCodeOther.setCreateTime(new Date()); couponCodeOther.setCreateTime(new Date());
highCouponCodeOtherMapper.insert(couponCodeOther); highCouponCodeOtherMapper.insert(couponCodeOther);
} }
discountAgentCode.setExt2(tradeId); discountAgentCode.setExt2(tradeId);
updateCode(discountAgentCode); updateCode(discountAgentCode);
} }
} else {
// 加锁失败,重试
Thread.sleep(100);
useDiscount(discountAgentCodeId);
}
} catch (Exception e) {
} finally {
// 删除key,释放锁
redisTemplate.delete(key);
}
} }
} }

Loading…
Cancel
Save