Browse Source

取消和退款加入事务,余额回滚在最后处理

lc 6 ngày trước cách đây
mục cha
commit
eef5949913

+ 13 - 37
mall-service/src/main/java/com/txz/mall/service/impl/StoreOrderServiceImpl.java

@@ -1924,27 +1924,9 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
 
     // 这是由pc端发起端针对个人的主动退款 不是定时任务扫描活动超时导致的退款,所以这个退款是不影响其他拼团成员的。拼团汇总表也不会上锁,拼团状态仍然是进行中
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void refund(String orderNo) {
-
-
         StoreOrder storeOrder = findBy("orderId", orderNo);
-        ////        if (storeOrder.getPaid().equals(Constants.ORDER_STATUS_H5_UNPAID)) {
-        ////            throw new ServiceException("当前状态不支持退款");
-        ////        }
-        //
-        //        PinkOrderStatusEnum pinkOrderStatusEnum = OrderStateMachine.handleEvent(storeOrder, OrderEventsEnum.GROUP_PURCHASE_APPLY_REFUND, null);
-        //
-        //        if (ObjectUtils.isEmpty(pinkOrderStatusEnum)) {
-        //            throw new ServiceException("当前状态不支持退款");
-        //        }
-        //        updateOrderStatus(storeOrder.getId(), PinkOrderStatusEnum.GROUP_ORDER_REFUND.getKey());
-        //
-        //        // 归还用户余额
-        //  accountDubboServiceClient.refund(id.toString(), storeOrder.getOrderId(), storeOrder.getPayPrice());
-        //        // 库存增加
-        //        storeCombinationService.operationStock(storeOrder.getCombinationId(), storeOrder.getTotalNum(), "add");
-        //
-        //
         if (ObjectUtils.isEmpty(storeOrder)) {
             throw new ServiceException(I18nUtil.get("order.does.not.exist.when.initiating.active.refund.id") + orderNo);
         }
@@ -1960,13 +1942,10 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
         if (ObjectUtils.isEmpty(storePink)) {
             throw new ServiceException(I18nUtil.get("group.buying.information.does.not.exist.when.closing.due.to.group.buying.timeout.id") + orderNo);
         }
-
         // 团长在未成团时不能退款
         if (storePink.getKId().equals(status) && storePink.getStatus().equals(StorePinkStatusEnum.NOT_DRAWN.getKey())) {
             throw new ServiceException(I18nUtil.get("current.member.is.the.group.leader.and.the.group.has.not.drawn.a.prize.refund.is.temporarily.not.supported") + orderNo);
         }
-
-
         Date date = new Date();
         // 主动退款
         CancelParam param = new CancelParam();
@@ -1976,15 +1955,9 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
         param.setUserIds(userIds);
         param.setAmount(storeOrder.getPayPrice());
         param.setTransTime(date);
-        Result result = accountDubboServiceClient.cancelGroup(param);
-        boolean equals = result.getCode().equals("200");
-        if (!equals) {
-            throw new ServiceException(I18nUtil.get("refund.failed.please.initiate.again"));
-        }
         updateOrderStatus(storeOrder.getOrderId(), pinkOrderStatusEnum.getKey());
         // 订单状态回退
         // 拼团状态回退
-
         StorePink storePinkForUpdate = new StorePink();
         // storePinkForUpdate.setId(storeOrder.getPinkId());
         // storePinkForUpdate.setStatus(StorePinkStatusEnum.LOTTERY_FAILED.getKey());   //定时任务触发的拼团失败导致的订单退款需要设置这个状态为拼团失败状态
@@ -2006,12 +1979,17 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
         storeProductService.operationStock(storePink.getPid(), storePink.getTotalNum(), "add");
 
         noticeService.addOrderNotice(NoticeEnum.ORDER_GROUP_BUY_FAIL, storeOrder.getOrderId(), storeOrder.getUid());
-
+        Result result = accountDubboServiceClient.cancelGroup(param);
+        boolean equals = result.getCode().equals("200");
+        if (!equals) {
+          throw new ServiceException(I18nUtil.get("refund.failed.please.initiate.again"));
+        }
     }
 
 
     //    关闭指系统层面的自动结束  如活动时间超时或者拼团超时由定时任务触发  不暴露给客户端
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public void close(String orderNo) {
 
 
@@ -2089,14 +2067,7 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
                 param.setUserIds(userIds);
                 param.setAmount(storeOrder.getPayPrice());
                 param.setTransTime(date);
-                Result result = accountDubboServiceClient.cancelGroup(param);
-                boolean equals = result.getCode().equals("200");
-                if (!equals) {
-                    throw new ServiceException(I18nUtil.get("refund.failed.please.initiate.again.orderid") + storeOrder.getOrderId());
-                }
-
                 updateOrderStatus(storeOrder.getOrderId(), pinkOrderStatusEnum.getKey());
-
                 StorePink storePinkForUpdate = new StorePink();
                 // storePinkForUpdate.setId(storeOrder.getPinkId());
                 storePinkForUpdate.setStatus(StorePinkStatusEnum.LOTTERY_FAILED.getKey());   // 定时任务触发的拼团失败导致的订单退款需要设置这个状态为拼团失败状态
@@ -2117,12 +2088,17 @@ public class StoreOrderServiceImpl extends AbstractService<StoreOrder> implement
                 // 普通商品口库存
                 storeProductService.operationStock(storePink.getPid(), storePink.getTotalNum(), "add");
                 noticeService.addOrderNotice(NoticeEnum.ORDER_GROUP_BUY_FAIL, storeOrder.getOrderId(), storeOrder.getUid());
-
+                Result result = accountDubboServiceClient.cancelGroup(param);
+                boolean equals = result.getCode().equals("200");
+                if (!equals) {
+                   throw new ServiceException(I18nUtil.get("refund.failed.please.initiate.again.orderid") + storeOrder.getOrderId());
+                 }
             } else {
                 throw new ServiceException(I18nUtil.get("current.status.does.not.support.refund.cancellationid") + orderNo);
             }
         } catch (Exception e) {
             log.error(I18nUtil.get("scheduled.task.failed.to.close.the.order.order.id") + orderNo, e);
+            throw new ServiceException(I18nUtil.get("scheduled.task.failed.to.close.the.order.order.id") + orderNo);
         }
 
         // 如果是已支付状态状态推到拼团失败退款状态