Przeglądaj źródła

退款操作加锁

xucaiqin 2 lat temu
rodzic
commit
7d8d29d73d

+ 32 - 24
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/service/WalletService.java

@@ -1043,31 +1043,39 @@ public class WalletService {
         if (Objects.isNull(kwpWalletRefund)) {
             throw new BusinessException("退款订单不存在");
         }
-        if (kwpWalletRefund.getStatus() != RefundEnum.APPLY.getStatus()) {
-            throw new BusinessException("退款订单状态异常");
-        }
-        ChannelEnum channelEnum = ChannelEnum.getByChannel(kwpWalletRefund.getChannel());
-        if (Objects.isNull(channelEnum)) {
-            throw new BusinessException("支付通道不存在");
-        }
-        // 1.记账
-        List<PatchPay> list = new ArrayList<>();
-        PatchPay patchPay = new PatchPay();
-        patchPay.setUid(kwpWalletRefund.getFilter());
-        patchPay.setMoney(refundReq.bigMoney(refundReq.getMoney()));
-        patchPay.setRemark(StringUtils.isBlank(refundReq.getRemark()) ? "" : refundReq.getRemark());
-        list.add(patchPay);
-        R<BusinessNo> businessNoR = payCenterService.payAgentPayV2(kwpWalletRefund.getUid(), kwpWalletRefund.getFilter(), channelEnum, refundReq.bigMoney(refundReq.getMoney()), list, OrderUtils.generateOrderNo("SEQ"), "1");
-        if (!businessNoR.getStatus()) {
-            throw new BusinessException(StringUtils.isNotBlank(businessNoR.getMsg()) ? businessNoR.getMsg() : "退款失败");
-        }
-        //保存操作记录
-        kwpWalletRefundSubService.saveRecord(kwpWalletRefund.getId(), refundReq.getRemark(), RefundEnum.REFUNDING.getStatus());
+        String key = RedisLockKey.REFUND + refundReq.getIdLong();
+        if (redisLockUtil.tryLock(key)) {
+            try {
+                if (kwpWalletRefund.getStatus() != RefundEnum.APPLY.getStatus()) {
+                    throw new BusinessException("当前退款单已提交退款申请");
+                }
+                ChannelEnum channelEnum = ChannelEnum.getByChannel(kwpWalletRefund.getChannel());
+                if (Objects.isNull(channelEnum)) {
+                    throw new BusinessException("支付通道不存在");
+                }
+                // 1.记账
+                List<PatchPay> list = new ArrayList<>();
+                PatchPay patchPay = new PatchPay();
+                patchPay.setUid(kwpWalletRefund.getFilter());
+                patchPay.setMoney(refundReq.bigMoney(refundReq.getMoney()));
+                patchPay.setRemark(StringUtils.isBlank(refundReq.getRemark()) ? "" : refundReq.getRemark());
+                list.add(patchPay);
+                R<BusinessNo> businessNoR = payCenterService.payAgentPayV2(kwpWalletRefund.getUid(), kwpWalletRefund.getFilter(), channelEnum, refundReq.bigMoney(refundReq.getMoney()), list, OrderUtils.generateOrderNo("SEQ"), "1");
+                if (!businessNoR.getStatus()) {
+                    throw new BusinessException(StringUtils.isNotBlank(businessNoR.getMsg()) ? businessNoR.getMsg() : "退款失败");
+                }
+                //保存操作记录
+                kwpWalletRefundSubService.saveRecord(kwpWalletRefund.getId(), refundReq.getRemark(), RefundEnum.REFUNDING.getStatus());
 
-        Long userId = LoginUserHolder.getUserId();
-        //异步提交清分退款任务
-        AsyncPool.addTask(new Refund(refundReq, kwpWalletRefund, payCenterService, kwpWalletRefundMapper, userId));
-        return businessNoR;
+                Long userId = LoginUserHolder.getUserId();
+                //异步提交清分退款任务
+                AsyncPool.addTask(new Refund(refundReq, kwpWalletRefund, payCenterService, kwpWalletRefundMapper, userId));
+                return businessNoR;
+            } finally {
+                redisLockUtil.unlock(key);
+            }
+        }
+        throw new BusinessException("请勿重复提交退款操作");
     }
 
     /**