Преглед изворни кода

交易管理增加退款功能

FengChaoYu пре 10 месеци
родитељ
комит
7c529f0dbf

+ 1 - 0
mall-server-api/src/main/java/com/gree/mall/manager/constant/Constant.java

@@ -57,6 +57,7 @@ public class Constant {
         public final static String GOODS_MATERIAL_PURCHASE = "jsm:sxb:lock:goods:material:purchase:";
         public final static String GOODS_MATERIAL_PURCHASE_RET = "jsm:sxb:lock:goods:material:purchase:ret:";
         public final static String MATERIAL_WORKER_INCR = "jsm:sxb:lock:material:worker:incr";
+        public final static String TRADE_RECORD_LOCK = "jsm:sxb:lock:trade:record:";
         //订单号每日流水
         public final static String ORDER_NUM = "jsm:sxb:orderNo:";
 

+ 34 - 6
mall-server-api/src/main/java/com/gree/mall/manager/controller/material/manage/WebsitTradeController.java

@@ -3,23 +3,27 @@ package com.gree.mall.manager.controller.material.manage;
 import cn.hutool.core.lang.TypeReference;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.gree.mall.manager.annotation.ZfireList;
+import com.gree.mall.manager.bean.PayDetail;
 import com.gree.mall.manager.bean.material.manage.WebsitTradeVO;
+import com.gree.mall.manager.constant.Constant;
 import com.gree.mall.manager.helper.ResponseHelper;
 import com.gree.mall.manager.logic.material.manage.WebsitTradeLogic;
 import com.gree.mall.manager.zfire.bean.ZfireParamBean;
 import com.gree.mall.manager.zfire.util.FieldUtils;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.integration.redis.util.RedisLockRegistry;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import java.math.BigDecimal;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Lock;
 
 @Slf4j
 @RestController
@@ -30,10 +34,12 @@ public class WebsitTradeController {
 
     @Resource
     WebsitTradeLogic websitTradeLogic;
+    @Resource
+    RedisLockRegistry redisLockRegistry;
 
     @ZfireList
     @PostMapping("/list")
-    @ApiOperation(value = "网点销售订单-列表")
+    @ApiOperation(value = "交易记录-列表")
     public ResponseHelper<IPage<WebsitTradeVO>> page(
             @RequestBody ZfireParamBean zfireParamBean
     ) {
@@ -42,7 +48,7 @@ public class WebsitTradeController {
     }
 
     @PostMapping("/list/export")
-    @ApiOperation("网点销售订单-导出")
+    @ApiOperation("交易记录-导出")
     public void listExport(
             @RequestBody ZfireParamBean zfireParamBean,
             HttpServletRequest request,
@@ -53,4 +59,26 @@ public class WebsitTradeController {
         //3.导出
         FieldUtils.exportData(baseVOIPage.getRecords(), zfireParamBean.getExportFields(), request, response);
     }
+
+    @PostMapping("/trade/refund")
+    @ApiOperation(value = "交易记录-退款")
+    public ResponseHelper<PayDetail> tradeRefund(
+            @ApiParam(value = "销售记录id", required = true) @RequestParam String id,
+            @ApiParam(value = "退款金额", required = true) @RequestParam BigDecimal retAmount,
+            HttpServletRequest request
+    ) throws Exception {
+        Lock obtain = redisLockRegistry.obtain(Constant.RedisPrefix.TRADE_RECORD_LOCK + id);
+        try {
+            if (obtain.tryLock(5, TimeUnit.SECONDS)) {
+                websitTradeLogic.tradeRefund(id, retAmount, request);
+            }
+        } catch(Exception e) {
+            log.error("【退款处理】失败", e);
+            throw e;
+        } finally {
+            obtain.unlock();
+        }
+
+        return ResponseHelper.success();
+    }
 }

+ 55 - 0
mall-server-api/src/main/java/com/gree/mall/manager/logic/material/manage/WebsitTradeLogic.java

@@ -1,16 +1,31 @@
 package com.gree.mall.manager.logic.material.manage;
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.gree.mall.manager.bean.admin.AdminUserCom;
 import com.gree.mall.manager.bean.material.manage.WebsitTradeVO;
 import com.gree.mall.manager.commonmapper.MaterialMapper;
+import com.gree.mall.manager.enums.IsYesNoEnum;
+import com.gree.mall.manager.enums.material.PayOrderTypeEnum;
+import com.gree.mall.manager.enums.material.PayTypeEnum;
+import com.gree.mall.manager.exception.RemoteServiceException;
+import com.gree.mall.manager.logic.common.AllInPayLogic;
 import com.gree.mall.manager.logic.common.CommonLogic;
+import com.gree.mall.manager.plus.entity.WebsitSalesPayOrder;
+import com.gree.mall.manager.plus.entity.WebsitSalesPayOrderRet;
+import com.gree.mall.manager.plus.service.WebsitSalesPayOrderRetService;
+import com.gree.mall.manager.plus.service.WebsitSalesPayOrderService;
 import com.gree.mall.manager.zfire.bean.ZfireParamBean;
 import com.gree.mall.manager.zfire.util.FieldUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.servlet.http.HttpServletRequest;
+import java.math.BigDecimal;
+import java.util.Objects;
 
 @Slf4j
 @Service
@@ -19,6 +34,9 @@ public class WebsitTradeLogic {
 
     private final CommonLogic commonLogic;
     private final MaterialMapper materialMapper;
+    private final AllInPayLogic allInPayLogic;
+    private final WebsitSalesPayOrderService websitSalesPayOrderService;
+    private final WebsitSalesPayOrderRetService websitSalesPayOrderRetService;
 
     public IPage<WebsitTradeVO> page(ZfireParamBean zfireParamBean) {
         AdminUserCom adminUser = commonLogic.getAdminUser();
@@ -26,4 +44,41 @@ public class WebsitTradeLogic {
 
         return materialMapper.websitTradePage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
     }
+
+    @Transactional
+    public void tradeRefund(String id, BigDecimal retAmount, HttpServletRequest request) {
+        WebsitSalesPayOrder payOrder = websitSalesPayOrderService.getById(id);
+        if (Objects.isNull(payOrder)) {
+            throw new RemoteServiceException("订单不存在");
+        }
+        if (!payOrder.getPayType().equals(PayTypeEnum.ALLINPAY.getKey())) {
+            throw new RemoteServiceException("支付方式非”通联支付“");
+        }
+        if (payOrder.getPayFlag().equals(IsYesNoEnum.NO.getKey())) {
+            throw new RemoteServiceException("未支付,退款失败");
+        }
+        if (payOrder.getOrderType().equals(PayOrderTypeEnum.R.getKey())) {
+            throw new RemoteServiceException("订单类型非”收款“");
+        }
+        if (Objects.isNull(retAmount) || retAmount.compareTo(BigDecimal.ZERO) <= 0) {
+            throw new RemoteServiceException("请输入正确的退款金额");
+        }
+        if (payOrder.getRetValue().compareTo(payOrder.getPayValue()) >= 0) {
+            throw new RemoteServiceException("订单退款金额已等于支付金额");
+        }
+        final BigDecimal realRetAmount = payOrder.getRetValue().add(retAmount);
+        if (realRetAmount.compareTo(payOrder.getPayValue()) > 0) {
+            throw new RemoteServiceException("订单退款金额加本次退款金额已大于支付金额");
+        }
+        final String retOrderId = IdWorker.getIdStr();
+        allInPayLogic.serviceRefundOrder(payOrder, retOrderId, retAmount);
+        payOrder.setRetValue(realRetAmount)
+                .updateById();
+        new WebsitSalesPayOrderRet()
+                .setId(retOrderId)
+                .setWebsitSalesPayOrderId(payOrder.getId())
+                .setOrderId(payOrder.getOrderId())
+                .setAmount(retAmount)
+                .insert();
+    }
 }