From 0e0a373af02e073f6bd079873743c58fcd4251f3 Mon Sep 17 00:00:00 2001
From: wujianwei <wjw@11.com>
Date: 星期四, 02 四月 2026 13:38:07 +0800
Subject: [PATCH] 1.TMS 增加一个推送状态字段,记录是否推送成功 2.TMS 增加一个手动推送按钮,如果推送失败或者被驳回的账单可以二次手动推送

---
 tms/src/main/java/com/ruoyi/tms/service/impl/TmsApBillServiceImpl.java |  203 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 203 insertions(+), 0 deletions(-)

diff --git a/tms/src/main/java/com/ruoyi/tms/service/impl/TmsApBillServiceImpl.java b/tms/src/main/java/com/ruoyi/tms/service/impl/TmsApBillServiceImpl.java
index 2e08ea5..5c97285 100644
--- a/tms/src/main/java/com/ruoyi/tms/service/impl/TmsApBillServiceImpl.java
+++ b/tms/src/main/java/com/ruoyi/tms/service/impl/TmsApBillServiceImpl.java
@@ -23,6 +23,21 @@
 import com.ruoyi.tms.domain.TmsApBill;
 import com.ruoyi.tms.service.ITmsApBillService;
 import com.ruoyi.common.core.text.Convert;
+import com.ruoyi.tms.mapper.TmsPayableFeeMapper;
+import com.ruoyi.tms.domain.TmsPayableFee;
+import com.ruoyi.tms.domain.TmsPayableFeeItem;
+import com.ruoyi.tms.mapper.TmsPayableFeeItemMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.task.AsyncTaskExecutor;
+import org.springframework.core.task.SimpleAsyncTaskExecutor;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+import com.alibaba.fastjson2.JSON;
 
 /**
  * 搴斾粯璐﹀崟Service涓氬姟灞傚鐞�
@@ -40,6 +55,18 @@
 
     @Resource
     private TmsApBillItemMapper tmsApBillItemMapper;
+
+    @Resource
+    private TmsPayableFeeMapper tmsPayableFeeMapper;
+
+    @Resource
+    private TmsPayableFeeItemMapper tmsPayableFeeItemMapper;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Value("${custom.cwxtApi.url}")
+    private String url;
 
     /**
      * 鏌ヨ搴斾粯璐﹀崟
@@ -187,4 +214,180 @@
     {
         return tmsApBillMapper.deleteTmsApBillById(id);
     }
+
+    /**
+     * 鎵嬪姩鎺ㄩ�佸簲浠樿处鍗曞埌澶栭儴绯荤粺
+     *
+     * @param id 搴斾粯璐﹀崟ID
+     * @return 缁撴灉
+     */
+    @Override
+    public void manualPushToExternalSystem(Integer id) {
+        TmsApBill tmsApBill = tmsApBillMapper.selectTmsApBillById(id);
+        if (tmsApBill == null) {
+            throw new RuntimeException("搴斾粯璐﹀崟涓嶅瓨鍦�");
+        }
+
+        // 鏌ヨ鍏宠仈鐨勫簲浠樿垂鐢ㄥ垪琛�
+        List<TmsPayableFee> tmsPayableFeeList = tmsPayableFeeMapper.selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<TmsPayableFee>()
+                .eq(TmsPayableFee::getBillPayableId, id)
+        );
+
+        // 涓烘瘡涓簲浠樿垂鐢ㄥ姞杞芥槑缁�
+        for (TmsPayableFee fee : tmsPayableFeeList) {
+            List<TmsPayableFeeItem> payableFeeItems = tmsPayableFeeItemMapper.selectTmsPayableFeeItemList(new TmsPayableFeeItem() {
+                {
+                    setHeadId(fee.getId());
+                }
+            });
+            fee.setPayableFeeItems(payableFeeItems);
+        }
+
+        // 寮傛鎺ㄩ��
+        AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
+        executor.execute(() -> pushPayableToExternalSystem(tmsApBill, tmsPayableFeeList));
+    }
+
+    /**
+     * 鏇存柊鎺ㄩ�佺姸鎬�
+     *
+     * @param id 搴斾粯璐﹀崟ID
+     * @param pushStatus 鎺ㄩ�佺姸鎬�
+     * @return 缁撴灉
+     */
+    @Override
+    public int updatePushStatus(Integer id, Integer pushStatus) {
+        TmsApBill tmsApBill = new TmsApBill();
+        tmsApBill.setId(id);
+        tmsApBill.setPushStatus(pushStatus);
+        tmsApBill.setPushTime(DateUtils.getNowDate());
+        return tmsApBillMapper.updateTmsApBill(tmsApBill);
+    }
+
+    /**
+     * 鍚戝閮ㄧ郴缁熸帹閫佸簲浠樻暟鎹�
+     * @param tmsApBill 搴斾粯璐﹀崟
+     * @param tmsPayableFeeList 搴斾粯璐圭敤鍒楄〃
+     */
+    @Async
+    protected void pushPayableToExternalSystem(TmsApBill tmsApBill, List<TmsPayableFee> tmsPayableFeeList) {
+        java.util.Map<String, Object> requestBody = new java.util.HashMap<>();
+        try {
+            // 鏇存柊鎺ㄩ�佺姸鎬佷负鎺ㄩ�佷腑
+            tmsApBill.setPushStatus(1);
+            tmsApBill.setPushTime(DateUtils.getNowDate());
+            tmsApBillMapper.updateTmsApBill(tmsApBill);
+
+            // 鏋勫缓璇锋眰浣�
+            String apiUrl = url+"/addPayableBill";
+
+            // 鏋勫缓bill閮ㄥ垎
+            java.util.Map<String, Object> billMap = new java.util.HashMap<>();
+            billMap.put("billName", tmsApBill.getBillName());
+            billMap.put("supplierName", tmsApBill.getServiceProviderName());
+            billMap.put("totalAmount", tmsApBill.getSettleAmount());
+            billMap.put("currency", "RMB");
+            billMap.put("status", "0");
+            billMap.put("isInternalSettlement", "0");
+            billMap.put("internalSettlementUnit", tmsApBill.getServiceProviderName());
+            billMap.put("documentCount", tmsPayableFeeList.size());
+            billMap.put("discountAmount", 0.00);
+            billMap.put("paidAmount", 0.00);
+            billMap.put("pendingAmount", tmsApBill.getSettleAmount());
+            billMap.put("exchangeRate", tmsApBill.getSettleRate());
+            billMap.put("cnyAmount", tmsApBill.getSettleAmount());
+            billMap.put("periodType", "");
+            billMap.put("businessStartDate", "");
+            billMap.put("businessEndDate", "");
+            billMap.put("billingStartDate", "");
+            billMap.put("billingEndDate", "");
+            billMap.put("billGenerateDate", "");
+            billMap.put("billSendDate", "");
+            billMap.put("billDueDate", "");
+            billMap.put("remark", "");
+
+            // 鏋勫缓fees閮ㄥ垎
+            List<java.util.Map<String, Object>> feesList = new java.util.ArrayList<>();
+            for (int i = 0; i < tmsPayableFeeList.size(); i++) {
+                TmsPayableFee fee = tmsPayableFeeList.get(i);
+                java.util.Map<String, Object> feeMap = new java.util.HashMap<>();
+                feeMap.put("sourceSystem", "TMS");
+                feeMap.put("documentNo", fee.getDispatchNo() != null ? fee.getDispatchNo() : "");
+                feeMap.put("supplierName", tmsApBill.getServiceProviderName());
+                feeMap.put("payableAmount", fee.getPayableRmbAmount().add(fee.getPayableHkbAmount()));
+                java.math.BigDecimal rmbAmount = fee.getPayableRmbAmount();
+                java.math.BigDecimal hkbAmount = fee.getPayableHkbAmount();
+                StringBuilder amountStr = new StringBuilder();
+                if (rmbAmount.compareTo(java.math.BigDecimal.ZERO) > 0) {
+                    amountStr.append(rmbAmount).append("浜烘皯甯�");
+                }
+                if (hkbAmount.compareTo(java.math.BigDecimal.ZERO) > 0) {
+                    if (amountStr.length() > 0) {
+                        amountStr.append(" ");
+                    }
+                    amountStr.append(hkbAmount).append("娓竵");
+                }
+                feeMap.put("payableAmountStr", amountStr.toString());
+                feeMap.put("serialNumber", String.format("%03d", i + 1));
+                feeMap.put("relatedBillNo", "");
+                feeMap.put("businessSector", "0");
+                feeMap.put("documentType", "0");
+                feeMap.put("isInternalSettlement", "0");
+                feeMap.put("internalSettlementUnit", "");
+                feeMap.put("projectName", fee.getProjectName() != null ? fee.getProjectName() : "");
+                feeMap.put("businessTime", fee.getDispatchConfirmTime());
+                feeMap.put("payableConfirmTime", fee.getDispatchConfirmTime());
+                feeMap.put("status", "1");
+                feeMap.put("remark", "");
+
+                // 鏋勫缓feeDetails閮ㄥ垎
+                List<java.util.Map<String, Object>> feeDetailsList = new java.util.ArrayList<>();
+                List<TmsPayableFeeItem> payableFeeItems = fee.getPayableFeeItems();
+                for (TmsPayableFeeItem payableFeeItem : payableFeeItems) {
+                    java.util.Map<String, Object> feeDetailMap = new java.util.HashMap<>();
+                    feeDetailMap.put("feeType", payableFeeItem.getFeeType());
+                    feeDetailMap.put("feeName", payableFeeItem.getFeeName());
+                    feeDetailMap.put("unitPrice", payableFeeItem.getRegisterAmount());
+                    feeDetailMap.put("billingQuantity", payableFeeItem.getRegisterAmount());
+                    feeDetailMap.put("billingAmount", payableFeeItem.getRegisterAmount());
+                    feeDetailMap.put("billingUnit", "娆�");
+                    feeDetailMap.put("actualAmount", payableFeeItem.getRegisterAmount());
+                    feeDetailMap.put("currency", payableFeeItem.getCurrency());
+                    feeDetailMap.put("feeRegTime", payableFeeItem.getRegisterTime());
+                    feeDetailMap.put("remark", "");
+                    feeDetailsList.add(feeDetailMap);
+                }
+
+                feeMap.put("feeDetails", feeDetailsList);
+                feesList.add(feeMap);
+            }
+
+            // 鏋勫缓瀹屾暣璇锋眰浣�
+            requestBody.put("bill", billMap);
+            requestBody.put("fees", feesList);
+
+            // 璁剧疆HTTP澶�
+            HttpHeaders headers = new HttpHeaders();
+            headers.setContentType(MediaType.APPLICATION_JSON);
+            HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(requestBody), headers);
+
+            // 鍙戦�丄PI璇锋眰
+            ResponseEntity<String> response = restTemplate.exchange(apiUrl, HttpMethod.POST, entity, String.class);
+            logger.info("鎺ㄩ�佸簲浠樻暟鎹埌澶栭儴绯荤粺鎴愬姛锛屽搷搴�: {}", response.getBody());
+
+            // 鏇存柊鎺ㄩ�佺姸鎬佷负鎴愬姛
+            tmsApBill.setPushStatus(2);
+            tmsApBill.setPushTime(DateUtils.getNowDate());
+            tmsApBillMapper.updateTmsApBill(tmsApBill);
+        } catch (Exception e) {
+            logger.error("鎺ㄩ�佸簲浠樻暟鎹埌澶栭儴绯荤粺澶辫触锛岃处鍗旾D: {}, 渚涘簲鍟�: {}", 
+                tmsApBill.getId(), tmsApBill.getServiceProviderName(), e);
+            logger.debug("鎺ㄩ�佸け璐ョ殑璇锋眰鏁版嵁: {}", JSON.toJSONString(requestBody));
+
+            // 鏇存柊鎺ㄩ�佺姸鎬佷负澶辫触
+            tmsApBill.setPushStatus(3);
+            tmsApBill.setPushTime(DateUtils.getNowDate());
+            tmsApBillMapper.updateTmsApBill(tmsApBill);
+        }
+    }
 }

--
Gitblit v1.8.0