提交代码

dev-discount
胡锐 2 years ago
parent 93abb5496b
commit c751f35d9e
  1. 1
      hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java
  2. 107
      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,49 +202,70 @@ 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, "优惠券状态错误");
} }
if (StringUtils.isBlank(discountAgentCode.getExt2())) { HighDiscountUserRel discountUserRel = highDiscountUserRelService.getRelByAgentCodeId(discountAgentCodeId);
HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); if (discountUserRel != null) {
if (rel == null) { if (!userModel.getHighUser().getId().equals(discountUserRel.getUserId())) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商关联关系"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法使用,领取人和使用人不一致。");
}
HighDiscount discount = highDiscountService.getDiscountById(rel.getDiscountId());
if (discount == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券");
}
List<HighDiscountCouponRel> couponRelList = highDiscountCouponRelService.getRelByDiscount(discount.getId());
if (couponRelList.size() == 0) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置卡券");
} }
List<HighDiscountCouponRel> couponList = couponRelList.stream().filter(o -> o.getHighCoupon().getCouponSource().equals(5)).collect(Collectors.toList()); }
if (couponList.size() == 0) { String key = "DiscountAgentId_" + discountAgentCode.getDiscountAgentId();
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置重庆中石油卡券"); try {
} // 分布式锁占坑
String tradeId = discountAgentCode.getId()+""+System.currentTimeMillis()+""; Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, discountAgentCode.getId());
// 给用户发码 if(lock) {
JSONObject response = ChongQingCNPCService.sendCNPCTicket( if (StringUtils.isBlank(discountAgentCode.getExt2())) {
couponList.get(0).getHighCoupon().getCouponKey(), HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId());
tradeId, if (rel == null) {
1, throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到代理商关联关系");
userModel.getHighUser().getPhone()); }
JSONObject couponDetail = response.getJSONObject("ticketDetail"); HighDiscount discount = highDiscountService.getDiscountById(rel.getDiscountId());
JSONArray codeList = response.getJSONArray("codeList"); if (discount == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到优惠券");
for (Object data : codeList) { }
HighCouponCodeOther couponCodeOther = new HighCouponCodeOther(); List<HighDiscountCouponRel> couponRelList = highDiscountCouponRelService.getRelByDiscount(discount.getId());
couponCodeOther.setType(3); if (couponRelList.size() == 0) {
couponCodeOther.setDiscountAgentCodeId(discountAgentCode.getId()); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置卡券");
couponCodeOther.setCouTypeCode(couponDetail.getString("requestCode")); }
couponCodeOther.setCouNo(ChongQingCNPCService.decryptCouponCode(String.valueOf(data))); List<HighDiscountCouponRel> couponList = couponRelList.stream().filter(o -> o.getHighCoupon().getCouponSource().equals(5)).collect(Collectors.toList());
couponCodeOther.setActiveTime(new Date()); if (couponList.size() == 0) {
couponCodeOther.setValidStartDate(DateUtil.format(couponDetail.getString("effectiveTime"), "yyyy-MM-dd")); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置重庆中石油卡券");
couponCodeOther.setValidEndDate(DateUtil.format(couponDetail.getString("expiredDate"), "yyyy-MM-dd")); }
couponCodeOther.setStatus(20); String tradeId = discountAgentCode.getId()+""+System.currentTimeMillis()+"";
couponCodeOther.setCreateTime(new Date()); // 给用户发码
highCouponCodeOtherMapper.insert(couponCodeOther); JSONObject response = ChongQingCNPCService.sendCNPCTicket(
couponList.get(0).getHighCoupon().getCouponKey(),
tradeId,
1,
userModel.getHighUser().getPhone());
JSONObject couponDetail = response.getJSONObject("ticketDetail");
JSONArray codeList = response.getJSONArray("codeList");
for (Object data : codeList) {
HighCouponCodeOther couponCodeOther = new HighCouponCodeOther();
couponCodeOther.setType(3);
couponCodeOther.setDiscountAgentCodeId(discountAgentCode.getId());
couponCodeOther.setCouTypeCode(couponDetail.getString("requestCode"));
couponCodeOther.setCouNo(ChongQingCNPCService.decryptCouponCode(String.valueOf(data)));
couponCodeOther.setActiveTime(new Date());
couponCodeOther.setValidStartDate(DateUtil.format(couponDetail.getString("effectiveTime"), "yyyy-MM-dd"));
couponCodeOther.setValidEndDate(DateUtil.format(couponDetail.getString("expiredDate"), "yyyy-MM-dd"));
couponCodeOther.setStatus(20);
couponCodeOther.setCreateTime(new Date());
highCouponCodeOtherMapper.insert(couponCodeOther);
}
discountAgentCode.setExt2(tradeId);
updateCode(discountAgentCode);
}
} else {
// 加锁失败,重试
Thread.sleep(100);
useDiscount(discountAgentCodeId);
} }
} catch (Exception e) {
discountAgentCode.setExt2(tradeId); } finally {
updateCode(discountAgentCode); // 删除key,释放锁
redisTemplate.delete(key);
} }
} }

Loading…
Cancel
Save