Forráskód Böngészése

结算模块增加电子钱包接口调用逻辑

xucaiqin 2 éve
szülő
commit
ebec4ddcd6

+ 9 - 3
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/model/KwpLedgerTradeOrder.java

@@ -10,9 +10,10 @@ import java.time.LocalDateTime;
 
 /**
  * 对账-交易订单-对账关联交易订单
-* @date 2023-07-27 12:45:43
-* @author xucaiqin
-*/
+ *
+ * @author xucaiqin
+ * @date 2023-07-27 12:45:43
+ */
 @Data
 @TableName(value = "kwp_ledger_trade_order")
 public class KwpLedgerTradeOrder {
@@ -27,6 +28,11 @@ public class KwpLedgerTradeOrder {
      */
     @TableField(value = "t_ledger_id")
     private Long tLedgerId;
+    /**
+     * 中台订单号-冻结订单号
+     */
+    @TableField(value = "order_no")
+    private String orderNo;
 
     /**
      * 交易订单id(kwo_trade_order)

+ 1 - 1
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/model/KwpSettlementWallet.java

@@ -37,7 +37,7 @@ public class KwpSettlementWallet {
     @TableField(value = "ent_id")
     private Long entId;
     /**
-     * 中台流水号
+     * 中台流水号 清分单号
      */
     @TableField(value = "order_no")
     private String orderNo;

+ 10 - 2
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/service/KwpLedgerTradeService.java

@@ -59,6 +59,7 @@ public class KwpLedgerTradeService extends AbsLedger {
     private final KwpLedgerTradeTrackService tradeTrackService;
     private final KwpLedgerTradeOrderService tradeOrderService;
     private final KwpLedgerTradeUnitService tradeUnitService;
+    private final KwpTradeFreezeService kwpTradeFreezeService;
     private final MessageSender messageSender;
     @Resource
     private KwpSettlementTradeService kwpSettlementTradeService;
@@ -146,7 +147,7 @@ public class KwpLedgerTradeService extends AbsLedger {
         tradeReq.setUnitType(TradeUnitType.PURCHASE);
         tradeReq.setUnitTypeTwo(TradeUnitType.SELL);
         List<Integer> status = new ArrayList<>();
-        return tradeMapper.countSum(tradeReq,status);
+        return tradeMapper.countSum(tradeReq, status);
     }
 
     /**
@@ -246,11 +247,18 @@ public class KwpLedgerTradeService extends AbsLedger {
         //校验当前订单是否已存在绑定的对账单
         tradeOrderService.check(ids);
         List<OrderDetailRes> resList = new ArrayList<>();
+        Map<Long, String> map = new HashMap<>();
+
         for (Long id : ids) {
             OrderDetailRes orderDetailById = tradeOrderInfoService.getOrderDetailById(id);
             if (Objects.isNull(orderDetailById)) {
                 throw new BusinessException("订单未找到");
             }
+            KwpTradeFreeze byTOrderId = kwpTradeFreezeService.getByTOrderId(id);
+            if (Objects.isNull(byTOrderId)) {
+                throw new BusinessException("未找到关联的冻结单");
+            }
+            map.put(id, byTOrderId.getOrderNo());
             resList.add(orderDetailById);
         }
         for (Long id : ids) {
@@ -262,10 +270,10 @@ public class KwpLedgerTradeService extends AbsLedger {
             updateAssociateStatementParam.setUpdateByName(LoginUserHolder.getUserName());
             tradeOrderInfoService.updateAssociateStatement(updateAssociateStatementParam);
         }
-
         List<KwpLedgerTradeOrder> collect = resList.stream().map(a -> {
             KwpLedgerTradeOrder kwpLedgerTradeOrder = new KwpLedgerTradeOrder();
             kwpLedgerTradeOrder.setId(new IdWorker(NumberConstant.ONE).nextId());
+            kwpLedgerTradeOrder.setOrderNo(map.get(a.getId()));
             kwpLedgerTradeOrder.setTLedgerId(kwpLedgerTrade.getId());
             kwpLedgerTradeOrder.setTOrderId(a.getId());
             kwpLedgerTradeOrder.setTOrderNo(a.getTOrderNo());

+ 62 - 50
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/service/KwpSettlementWalletService.java

@@ -30,6 +30,7 @@ import com.sckw.payment.model.dto.LedgerUnitDto;
 import com.sckw.payment.model.dto.SettlementLogisticsDto;
 import com.sckw.payment.model.dto.SettlementTradeDto;
 import com.sckw.payment.model.dto.SettlementWalletDto;
+import com.sckw.payment.model.dto.common.BusinessNo;
 import com.sckw.payment.model.vo.SettlementVo;
 import com.sckw.payment.model.vo.req.OfflinePaymentReq;
 import com.sckw.payment.model.vo.req.SettlementWalletReq;
@@ -215,7 +216,7 @@ public class KwpSettlementWalletService {
         String trading = settlementLogisticsDto.getTrading();
         ChannelEnum channelEnum = ChannelEnum.getByTrading(trading);
         if (Objects.isNull(channelEnum)) {
-            throw new BusinessException("交易方式异常");
+            throw new BusinessException("交易方式不存在");
         }
         //加锁
         String key = String.format(RedisConstant.SETTLEMENT_KEY, offlinePaymentReq.getId());
@@ -355,50 +356,33 @@ public class KwpSettlementWalletService {
     public String confirmTradePayment(WalletPayReq walletPayReq) {
         log.info("采购-付款确认-货到付款:{}", JSONObject.toJSONString(walletPayReq));
         String key = String.format(RedisConstant.SETTLEMENT_KEY, walletPayReq.getId());
+        SettlementTradeDto byId = kwpSettlementTradeService.getById(walletPayReq.getIdLong(), TradeUnitType.SELL);
+        if (Objects.isNull(byId)) {
+            throw new BusinessException("结算单不存在");
+        }
+        if (StringUtils.isNotBlank(byId.getTrading()) && !byId.getTrading().startsWith(TradingEnum.RECEIVE_PAY.getValue())) {
+            throw new BusinessException("只支持货货到付款方式订单进行当前操作");
+        }
+        KwpLedgerTradeUnit kwpLedgerTradeUnit = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.PURCHASE);
+        KwpLedgerTradeUnit kwpLedgerTradeUnit2 = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.SELL);
+        String uid = walletRelationService.getRelation(kwpLedgerTradeUnit.getTopEntId());
+        String filter = walletRelationService.getRelation(kwpLedgerTradeUnit2.getTopEntId());
+        if (StringUtils.isBlank(uid)) {
+            throw new BusinessException("我方企业暂未开通电子钱包");
+        }
+        if (StringUtils.isBlank(filter)) {
+            throw new BusinessException("客户企业暂未开通电子钱包");
+        }
+        ChannelEnum channelEnum = ChannelEnum.getByTrading(byId.getTrading());
+        if (Objects.isNull(channelEnum)) {
+            throw new BusinessException("交易方式不存在");
+        }
+        //付款金额
+        BigDecimal price = walletPayReq.getPrice();
         if (redisLockUtil.tryLock(key)) {
             try {
-                SettlementTradeDto byId = kwpSettlementTradeService.getById(walletPayReq.getIdLong(), TradeUnitType.SELL);
-                if (Objects.isNull(byId)) {
-                    throw new BusinessException("结算单不存在");
-                }
-                if (StringUtils.isNotBlank(byId.getTrading()) && !byId.getTrading().startsWith(TradingEnum.RECEIVE_PAY.getValue())) {
-                    throw new BusinessException("只支持货货到付款方式订单进行当前操作");
-                }
-                List<LedgerUnitDto> listById = kwpSettlementTradeService.getListById(walletPayReq.getIdLong());
-                if (CollectionUtils.isEmpty(listById) || listById.size() != NumberConstant.TWO) {
-                    throw new BusinessException("对账单交易企业双方不存在或缺少");
-                }
-                Long uid = null;
-                Long filter = null;
-                for (LedgerUnitDto ledgerUnitDto : listById) {
-                    Integer unitType = ledgerUnitDto.getUnitType();
-                    if (TradeUnitType.PURCHASE.equals(unitType)) {
-                        uid = ledgerUnitDto.getTopEntId();
-                        continue;
-                    }
-                    if (TradeUnitType.SELL.equals(unitType)) {
-                        filter = ledgerUnitDto.getTopEntId();
-                    }
-                }
-                String relation = walletRelationService.getRelation(uid);
-                String filterUser = walletRelationService.getRelation(filter);
-                if (StringUtils.isBlank(relation) || StringUtils.isBlank(filterUser)) {
-                    throw new BusinessException("顶级企业未开通电子钱包");
-                }
-                //付款金额
-                BigDecimal price = walletPayReq.getPrice();
-                //todo-xcq-完善
-
-//        R<List<WalletDto>> wallet = payCenterService.wallet(relation, ChannelEnum.getByCode(byId.getTrading()), filterUser);
-//        if (CollectionUtils.isEmpty(wallet.getData())) {
-//            throw new BusinessException("暂未开通电子钱包");
-//        }
-//        List<WalletDto> data = wallet.getData();
-//        BigDecimal bigDecimal = BigDecimal.valueOf(data.get(0).getMoney()).divide(new BigDecimal("100.0"), RoundingMode.UNNECESSARY);
-//
-//        if (bigDecimal.compareTo(price) < 0) {
-//            throw new BusinessException("钱包可用余额不足");
-//        }
+                //电子钱包余额校验
+                walletBusinessService.checkMoney(uid, filter, channelEnum, price);
                 //待付款金额-本次付款金额= 剩余待付款金额
                 BigDecimal subtract = byId.getWaitPrice().subtract(walletPayReq.getPrice());
                 if (subtract.compareTo(new BigDecimal("0.0")) < NumberConstant.ZERO) {
@@ -406,9 +390,11 @@ public class KwpSettlementWalletService {
                 }
                 /*新增记录*/
                 //新增电子钱包结算记录
+                String orderNo = OrderUtils.generateOrderNo("ST");
                 KwpSettlementWallet kwpSettlementWallet = new KwpSettlementWallet();
                 kwpSettlementWallet.setId(new IdWorker(NumberConstant.ONE).nextId());
                 kwpSettlementWallet.setEntId(LoginUserHolder.getEntId());
+                kwpSettlementWallet.setOrderNo(orderNo);
                 kwpSettlementWallet.setSettlementId(walletPayReq.getIdLong());
                 kwpSettlementWallet.setOrderType(SettlementOrderTypeEnum.TRADE.getStatus());
                 kwpSettlementWallet.setChannel(WalletChannelEnum.getValue(byId.getTrading()));
@@ -454,9 +440,9 @@ public class KwpSettlementWalletService {
                         tradeOrderInfoService.updateOrderStatus(updateOrderStatusParam);
                     }
                 }
+                //电子钱包清分接口
+                walletBusinessService.splitMoney(orderNo, uid, filter, channelEnum, price, "2");
 
-                KwpLedgerTradeUnit kwpLedgerTradeUnit = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.PURCHASE);
-                KwpLedgerTradeUnit kwpLedgerTradeUnit2 = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.SELL);
                 //推送双方系统管理员
                 messageSender.sendManager(LoginUserHolder.getUserId(), new HashMap<>() {{
                     put("company", kwpLedgerTradeUnit.getFirmName());
@@ -466,9 +452,6 @@ public class KwpSettlementWalletService {
                     put("company", kwpLedgerTradeUnit2.getFirmName());
                     put("number", kwpSettlementTrade.getStOrderNo());
                 }}, kwpLedgerTradeUnit.getEntId(), MessageEnum.SETTLEMENT_START_PURCHASE);
-
-                //todo-xcq 调用中台接口
-
                 return "付款确认成功";
             } finally {
                 redisLockUtil.unlock(key);
@@ -585,10 +568,30 @@ public class KwpSettlementWalletService {
         if (StringUtils.isNotBlank(byId.getTrading()) && !byId.getTrading().startsWith(TradingEnum.PRE_PAY.getValue())) {
             throw new BusinessException("只支持预付款交易方式进行预付款确认操作");
         }
+        //冻结编号非空校验
         List<KwpLedgerTradeOrder> kwpLedgerTradeOrders = tradeOrderService.queryList(byId.getLedgerId());
         if (CollectionUtils.isEmpty(kwpLedgerTradeOrders)) {
             throw new BusinessException("未找到关联的贸易订单");
         }
+        for (KwpLedgerTradeOrder kwpLedgerTradeOrder : kwpLedgerTradeOrders) {
+            if (StringUtils.isBlank(kwpLedgerTradeOrder.getOrderNo())) {
+                throw new BusinessException(String.format("为找到贸易订单id[%s]的冻结单号", kwpLedgerTradeOrder.getTOrderId()));
+            }
+        }
+        KwpLedgerTradeUnit kwpLedgerTradeUnit = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.PURCHASE);
+        KwpLedgerTradeUnit kwpLedgerTradeUnit2 = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.SELL);
+        String uid = walletRelationService.getRelation(kwpLedgerTradeUnit.getTopEntId());
+        if (StringUtils.isBlank(uid)) {
+            throw new BusinessException("客户单位暂未开通电子钱包");
+        }
+        String filter = walletRelationService.getRelation(kwpLedgerTradeUnit2.getTopEntId());
+        if (StringUtils.isBlank(filter)) {
+            throw new BusinessException("我方单位暂未开通电子钱包");
+        }
+        ChannelEnum channelEnum = ChannelEnum.getByTrading(byId.getTrading());
+        if (Objects.isNull(channelEnum)) {
+            throw new BusinessException("交易方式不存在");
+        }
         if (redisLockUtil.tryLock(key)) {
             try {
                 //更新交易结算单状态和金额
@@ -634,8 +637,17 @@ public class KwpSettlementWalletService {
                     tradeOrderInfoService.updateOrderStatus(updateOrderStatusParam);
                 }
 
-                KwpLedgerTradeUnit kwpLedgerTradeUnit = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.PURCHASE);
-                KwpLedgerTradeUnit kwpLedgerTradeUnit2 = tradeUnitService.queryCustomerEnt(byId.getLedgerId(), TradeUnitType.SELL);
+
+                /*电子钱包相关业务*/
+                //批量获取对账单关联的贸易订单 再获取冻结编号,批量解冻
+                for (KwpLedgerTradeOrder kwpLedgerTradeOrder : kwpLedgerTradeOrders) {
+                    R<BusinessNo> businessNoR = payCenterService.walletUnFreeze(kwpLedgerTradeOrder.getOrderNo());
+                    if (!businessNoR.getStatus()) {
+                        throw new BusinessException(businessNoR.getMsg());
+                    }
+                }
+                //对结算单中的总计金额进行清分
+                walletBusinessService.splitMoney(orderNo, uid, filter, channelEnum, byId.getTotalPrice(), "1");
                 //推送双方系统管理员
                 messageSender.sendManager(LoginUserHolder.getUserId(), new HashMap<>() {{
                     put("company", kwpLedgerTradeUnit.getFirmName());

+ 1 - 1
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/service/KwpTradeFreezeService.java

@@ -25,7 +25,7 @@ public class KwpTradeFreezeService {
      * 通过贸易订单id查询
      *
      * @param tOrderId 贸易订单id
-     * @return
+     * @return 冻结订单号
      */
     public KwpTradeFreeze getByTOrderId(Long tOrderId) {
         LambdaQueryWrapper<KwpTradeFreeze> wrapper = new LambdaQueryWrapper<>();

+ 1 - 1
sckw-modules/sckw-payment/src/main/java/com/sckw/payment/service/WalletBusinessService.java

@@ -69,7 +69,7 @@ public class WalletBusinessService {
         R<BusinessNo> businessNoR = payCenterService.payAgentPayV2(uid, filter, channelEnum, value, split, businessNo, payType);
         if (!businessNoR.getStatus()) {
             log.error("流水号:{} uid:{} filter:{} channel:{} price:{} payType:{}", businessNoR, uid, filter, channelEnum, price, payType);
-            throw new BusinessException("清分失败异常");
+            throw new BusinessException("清分失败");
         }
     }
 }

+ 14 - 3
sckw-modules/sckw-payment/src/main/resources/mapper/KwpLedgerTradeOrderMapper.xml

@@ -6,6 +6,7 @@
     <!--@Table kwp_ledger_trade_order-->
     <id column="id" jdbcType="BIGINT" property="id" />
     <result column="t_ledger_id" jdbcType="BIGINT" property="tLedgerId" />
+    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
     <result column="t_order_id" jdbcType="BIGINT" property="tOrderId" />
     <result column="t_order_no" jdbcType="VARCHAR" property="tOrderNo" />
     <result column="remark" jdbcType="VARCHAR" property="remark" />
@@ -17,8 +18,18 @@
     <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
   </resultMap>
   <sql id="Base_Column_List">
-    <!--@mbg.generated-->
-    id, t_ledger_id, t_order_id, t_order_no, remark, `status`, create_by, create_time, 
-    update_by, update_time, del_flag
+      <!--@mbg.generated-->
+      id,
+      t_ledger_id,
+      order_no,
+      t_order_id,
+      t_order_no,
+      remark,
+      `status`,
+      create_by,
+      create_time,
+      update_by,
+      update_time,
+      del_flag
   </sql>
 </mapper>