From c751f35d9e60af9757489264b0353768526948a5 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Wed, 19 Oct 2022 17:11:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HighDiscountController.java | 1 + .../HighDiscountAgentCodeServiceImpl.java | 107 +++++++++++------- 2 files changed, 67 insertions(+), 41 deletions(-) diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java b/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java index 9236a4a3..94076ef6 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighDiscountController.java @@ -175,6 +175,7 @@ public class HighDiscountController { try { highDiscountAgentCodeService.useDiscount(discountAgentCodeId); + return ResponseMsgUtil.success("操作成功"); } catch (DeadlockLoserDataAccessException deadlockLoserDataAccessException) { diff --git a/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java index c4493210..86dc8316 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighDiscountAgentCodeServiceImpl.java @@ -16,6 +16,7 @@ import com.hai.model.HighUserModel; import com.hai.service.*; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; @@ -68,6 +69,9 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe @Resource private UserCenter userCenter; + @Resource + private RedisTemplate redisTemplate; + @Override public void insertCodeList(List discountAgentCodeList) { highDiscountAgentCodeMapper.insertListCode(discountAgentCodeList); @@ -181,7 +185,7 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe } @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 { HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class); if (userModel == null) { @@ -198,49 +202,70 @@ public class HighDiscountAgentCodeServiceImpl implements HighDiscountAgentCodeSe if (!discountAgentCode.getStatus().equals(2)) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态错误"); } - if (StringUtils.isBlank(discountAgentCode.getExt2())) { - HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); - if (rel == null) { - 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 couponRelList = highDiscountCouponRelService.getRelByDiscount(discount.getId()); - if (couponRelList.size() == 0) { - 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, "无法使用,领取人和使用人不一致。"); } - List couponList = couponRelList.stream().filter(o -> o.getHighCoupon().getCouponSource().equals(5)).collect(Collectors.toList()); - if (couponList.size() == 0) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置重庆中石油卡券"); - } - String tradeId = discountAgentCode.getId()+""+System.currentTimeMillis()+""; - // 给用户发码 - 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); + } + String key = "DiscountAgentId_" + discountAgentCode.getDiscountAgentId(); + try { + // 分布式锁占坑 + Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, discountAgentCode.getId()); + if(lock) { + if (StringUtils.isBlank(discountAgentCode.getExt2())) { + HighDiscountAgentRel rel = highDiscountAgentRelService.getRelById(discountAgentCode.getDiscountAgentId()); + if (rel == null) { + 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 couponRelList = highDiscountCouponRelService.getRelByDiscount(discount.getId()); + if (couponRelList.size() == 0) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置卡券"); + } + List couponList = couponRelList.stream().filter(o -> o.getHighCoupon().getCouponSource().equals(5)).collect(Collectors.toList()); + if (couponList.size() == 0) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券未配置重庆中石油卡券"); + } + String tradeId = discountAgentCode.getId()+""+System.currentTimeMillis()+""; + // 给用户发码 + 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); - updateCode(discountAgentCode); + } finally { + // 删除key,释放锁 + redisTemplate.delete(key); } }