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/TmsArBillServiceImpl.java |  204 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 204 insertions(+), 0 deletions(-)

diff --git a/tms/src/main/java/com/ruoyi/tms/service/impl/TmsArBillServiceImpl.java b/tms/src/main/java/com/ruoyi/tms/service/impl/TmsArBillServiceImpl.java
index ff81c60..bdb213e 100644
--- a/tms/src/main/java/com/ruoyi/tms/service/impl/TmsArBillServiceImpl.java
+++ b/tms/src/main/java/com/ruoyi/tms/service/impl/TmsArBillServiceImpl.java
@@ -37,6 +37,16 @@
 
 import com.ruoyi.tms.service.ITmsArBillService;
 import com.ruoyi.common.core.text.Convert;
+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涓氬姟灞傚鐞�
@@ -69,6 +79,12 @@
 
     @Resource
     private TmsDispatchOrderMapper tmsDispatchOrderMapper;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Value("${custom.cwxtApi.url}")
+    private String url;
 
     /**
      * 鏌ヨ搴旀敹璐﹀崟
@@ -240,6 +256,193 @@
     public int deleteTmsArBillById(Integer id)
     {
         return tmsArBillMapper.deleteTmsArBillById(id);
+    }
+
+    /**
+     * 鎵嬪姩鎺ㄩ�佸簲鏀惰处鍗曞埌澶栭儴绯荤粺
+     *
+     * @param id 搴旀敹璐﹀崟ID
+     * @return 缁撴灉
+     */
+    @Override
+    public void manualPushToExternalSystem(Integer id) {
+        TmsArBill tmsArBill = tmsArBillMapper.selectTmsArBillById(id);
+        if (tmsArBill == null) {
+            throw new RuntimeException("搴旀敹璐﹀崟涓嶅瓨鍦�");
+        }
+
+        // 鏌ヨ鍏宠仈鐨勫簲鏀惰垂鐢ㄥ垪琛�
+        List<TmsReceivableFee> tmsReceivableFees = tmsReceivableFeeMapper.selectList(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<TmsReceivableFee>()
+                .eq(TmsReceivableFee::getBillRelationId, id)
+        );
+
+        // 涓烘瘡涓簲鏀惰垂鐢ㄥ姞杞芥槑缁�
+        for (TmsReceivableFee fee : tmsReceivableFees) {
+            List<TmsReceivableFeeItem> items = tmsReceivableFeeItemMapper.selectTmsReceivableFeeItemList(new TmsReceivableFeeItem() {
+                {
+                    setHeadId(fee.getId());
+                }
+            });
+            fee.setItems(items);
+        }
+
+        // 寮傛鎺ㄩ��
+        AsyncTaskExecutor executor = new SimpleAsyncTaskExecutor();
+        executor.execute(() -> pushToExternalSystem(tmsArBill, tmsReceivableFees));
+    }
+
+    /**
+     * 鏇存柊鎺ㄩ�佺姸鎬�
+     *
+     * @param id 搴旀敹璐﹀崟ID
+     * @param pushStatus 鎺ㄩ�佺姸鎬�
+     * @return 缁撴灉
+     */
+    @Override
+    public int updatePushStatus(Integer id, Integer pushStatus) {
+        TmsArBill tmsArBill = new TmsArBill();
+        tmsArBill.setId(id);
+        tmsArBill.setPushStatus(pushStatus);
+        tmsArBill.setPushTime(DateUtils.getNowDate());
+        return tmsArBillMapper.updateTmsArBill(tmsArBill);
+    }
+
+    /**
+     * 鍚戝閮ㄧ郴缁熸帹閫佹暟鎹�
+     * @param tmsArBill 搴旀敹璐﹀崟
+     * @param tmsReceivableFees 搴旀敹璐圭敤鍒楄〃
+     */
+    @Async
+    protected void pushToExternalSystem(TmsArBill tmsArBill, List<TmsReceivableFee> tmsReceivableFees) {
+        java.util.Map<String, Object> requestBody = new java.util.HashMap<>();
+        try {
+            // 鏇存柊鎺ㄩ�佺姸鎬佷负鎺ㄩ�佷腑
+            tmsArBill.setPushStatus(1);
+            tmsArBill.setPushTime(DateUtils.getNowDate());
+            tmsArBillMapper.updateTmsArBill(tmsArBill);
+
+            // 鏋勫缓璇锋眰浣�
+            String apiUrl = url+"/addBill";
+
+            // 鏋勫缓bill閮ㄥ垎
+            java.util.Map<String, Object> billMap = new java.util.HashMap<>();
+            billMap.put("billName", tmsArBill.getBillName());
+            billMap.put("customerName", tmsArBill.getCustomerName());
+            billMap.put("payee", "");
+            billMap.put("responsiblePerson", "");
+            billMap.put("responsibleLeader", "");
+            billMap.put("settlementMethod", "");
+            billMap.put("businessType", "");
+            billMap.put("promotionRequirement", "");
+            billMap.put("isInternalSettlement", "0");
+            billMap.put("internalSettlementUnit", "");
+            billMap.put("documentCount", tmsReceivableFees.size());
+            billMap.put("totalAmount", tmsArBill.getSettleAmount());
+            billMap.put("currency", "RMB");
+            billMap.put("discountAmount", 0.00);
+            billMap.put("receivedAmount", 0.00);
+            billMap.put("pendingAmount", tmsArBill.getSettleAmount());
+            billMap.put("exchangeRate", tmsArBill.getSettleRate());
+            billMap.put("cnyAmount", tmsArBill.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("settlementCategory", "");
+            billMap.put("settlementPeriod", "");
+            billMap.put("status", "0");
+            billMap.put("remark", "");
+
+            // 鏋勫缓fees閮ㄥ垎
+            List<java.util.Map<String, Object>> feesList = new java.util.ArrayList<>();
+            for (int i = 0; i < tmsReceivableFees.size(); i++) {
+                TmsReceivableFee fee = tmsReceivableFees.get(i);
+                java.util.Map<String, Object> feeMap = new java.util.HashMap<>();
+                feeMap.put("serialNumber", String.format("%03d", i + 1));
+                feeMap.put("relatedBillNo", "");
+                feeMap.put("sourceSystem", "TMS");
+                feeMap.put("businessSector", "0");
+                feeMap.put("documentType", "0");
+                feeMap.put("documentNo", fee.getDispatchNo() != null ? fee.getDispatchNo() : "");
+                feeMap.put("isInternalSettlement", "0");
+                feeMap.put("internalSettlementUnit", "");
+                feeMap.put("customerName", tmsArBill.getCustomerName());
+                feeMap.put("projectName", fee.getProjectName() != null ? fee.getProjectName() : "");
+                feeMap.put("businessTime", fee.getDispatchConfirmTime());
+                feeMap.put("receivableConfirmTime", fee.getDispatchConfirmTime());
+                feeMap.put("receivableAmount", fee.getReceivableRMBAmount().add(fee.getReceivableHKBAmount()));
+                // 鏋勫缓receivableAmountStr
+                BigDecimal rmbAmount = fee.getReceivableRMBAmount();
+                BigDecimal hkbAmount = fee.getReceivableHKBAmount();
+                StringBuilder amountStr = new StringBuilder();
+                if (rmbAmount.compareTo(BigDecimal.ZERO) > 0) {
+                    amountStr.append(rmbAmount).append("浜烘皯甯�");
+                }
+                if (hkbAmount.compareTo(BigDecimal.ZERO) > 0) {
+                    if (amountStr.length() > 0) {
+                        amountStr.append(" ");
+                    }
+                    amountStr.append(hkbAmount).append("娓竵");
+                }
+                feeMap.put("receivableAmountStr", amountStr.toString());
+                feeMap.put("status", "1");
+                feeMap.put("remark", "");
+
+                // 鏋勫缓feeDetails閮ㄥ垎
+                List<java.util.Map<String, Object>> feeDetailsList = new java.util.ArrayList<>();
+                List<TmsReceivableFeeItem> items = fee.getItems();
+                for (int j = 0; j < items.size(); j++) {
+                    TmsReceivableFeeItem item = items.get(j);
+                    java.util.Map<String, Object> feeDetailMap = new java.util.HashMap<>();
+                    feeDetailMap.put("serialNumber", String.format("%03d", j + 1));
+                    feeDetailMap.put("feeType", item.getFeeType());
+                    feeDetailMap.put("feeName", item.getFeeName());
+                    feeDetailMap.put("billingUnit", "娆�");
+                    feeDetailMap.put("unitPrice", item.getRegisterAmount());
+                    feeDetailMap.put("billingQuantity", item.getRegisterAmount());
+                    feeDetailMap.put("billingAmount", item.getRegisterAmount());
+                    feeDetailMap.put("actualAmount", item.getRegisterAmount());
+                    feeDetailMap.put("currency", item.getCurrency());
+                    feeDetailMap.put("feeRegTime", item.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());
+
+            // 鏇存柊鎺ㄩ�佺姸鎬佷负鎴愬姛
+            tmsArBill.setPushStatus(2);
+            tmsArBill.setPushTime(DateUtils.getNowDate());
+            tmsArBillMapper.updateTmsArBill(tmsArBill);
+        } catch (Exception e) {
+            logger.error("鎺ㄩ�佹暟鎹埌澶栭儴绯荤粺澶辫触锛岃处鍗旾D: {}, 瀹㈡埛: {}", 
+                tmsArBill.getId(), tmsArBill.getCustomerName(), e);
+            logger.debug("鎺ㄩ�佸け璐ョ殑璇锋眰鏁版嵁: {}", JSON.toJSONString(requestBody));
+
+            // 鏇存柊鎺ㄩ�佺姸鎬佷负澶辫触
+            tmsArBill.setPushStatus(3);
+            tmsArBill.setPushTime(DateUtils.getNowDate());
+            tmsArBillMapper.updateTmsArBill(tmsArBill);
+        }
     }
 
     /**
@@ -841,3 +1044,4 @@
         dateCell2.setCellStyle(styles.get("text"));
     }
 }
+

--
Gitblit v1.8.0