From 849648e82e5dec96f9f30fcc9c9bd799268b1f4c Mon Sep 17 00:00:00 2001
From: wujianwei <wjw@11.com>
Date: 星期四, 09 四月 2026 10:40:44 +0800
Subject: [PATCH] 新增资金流水导入

---
 service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java |  155 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 115 insertions(+), 40 deletions(-)

diff --git a/service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java b/service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java
index 030053d..67e9d76 100644
--- a/service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java
+++ b/service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java
@@ -1,20 +1,29 @@
 package com.ruoyi.cwgl.service.impl;
 
+import java.io.File;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.util.*;
 
+import com.ruoyi.common.annotation.Excel;
 import com.ruoyi.common.enums.SystemDataNoEnum;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
-import com.ruoyi.cwgl.domain.ReceivableFeeDetail;
-import com.ruoyi.cwgl.domain.ReceivableFeeManagementLog;
+import com.ruoyi.common.utils.file.DownloadExportUtil;
+import com.ruoyi.common.utils.file.DownloadExportUtil.ExprotStatus;
+import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.cwgl.domain.*;
 import com.ruoyi.cwgl.domain.vo.ReceivableFeeStatisticsVo;
 import com.ruoyi.cwgl.service.*;
 import com.ruoyi.system.domain.SysConfig;
 import com.ruoyi.system.mapper.SysConfigMapper;
 import com.ruoyi.system.service.ISystemDataNoService;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.stereotype.Service;
@@ -26,15 +35,17 @@
 import com.ruoyi.common.annotation.DataSource;
 import com.ruoyi.common.enums.DataSourceType;
 import com.ruoyi.common.core.service.BaseService;
+import com.ruoyi.common.config.RuoYiConfig;
+import com.ruoyi.common.exception.UtilException;
+import com.ruoyi.common.core.redis.RedisCache;
 
 import com.ruoyi.cwgl.mapper.ReceivableFeeManagementMapper;
-import com.ruoyi.cwgl.domain.ReceivableFeeManagement;
-import com.ruoyi.cwgl.domain.ReceivableBillManagement;
-import com.ruoyi.cwgl.domain.ReceivableBillSettlementDetail;
 import com.ruoyi.cwgl.domain.vo.ReceivableBillCreateVo;
 import com.ruoyi.common.core.text.Convert;
 
 import javax.annotation.Resource;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 
 /**
  * 搴旀敹璐圭敤绠$悊Service涓氬姟灞傚鐞�
@@ -66,6 +77,12 @@
 
     @Autowired
     private IReceivableFeeManagementLogService logService;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    @Autowired
+    private ITmsCustomerInfoService tmsCustomerInfoService;
 
     /**
      * 鏌ヨ搴旀敹璐圭敤绠$悊
@@ -564,56 +581,80 @@
     }
 
     /**
-     * 瀵煎叆搴旀敹璐圭敤绠$悊鏁版嵁
+     * 瀵煎叆搴旀敹璐圭敤绠$悊鏁版嵁锛堝寘鍚槑缁嗘暟鎹級
      * 
-     * @param receivableFeeList 搴旀敹璐圭敤绠$悊鏁版嵁鍒楄〃
-     * @param updateSupport 鏄惁鏇存柊鏀寔锛屽鏋滃凡瀛樺湪锛屽垯杩涜鏇存柊鏁版嵁
+     * @param receivableFeeList 搴旀敹璐圭敤绠$悊鏁版嵁鍒楄〃锛堝寘鍚槑缁嗘暟鎹級
      * @param operName 鎿嶄綔鐢ㄦ埛
      * @return 缁撴灉
      */
     @Override
-    public String importReceivableFee(List<ReceivableFeeManagement> receivableFeeList, Boolean updateSupport, String operName) {
+    @Transactional(rollbackFor = Exception.class)
+    public String importReceivableFee(List<ReceivableFeeManagement> receivableFeeList, String operName) {
         if (receivableFeeList == null || receivableFeeList.isEmpty()) {
-            return "瀵煎叆鐢ㄦ埛鏁版嵁涓嶈兘涓虹┖锛�";
+            throw new ServiceException("瀵煎叆搴旀敹璐圭敤鏁版嵁涓嶈兘涓虹┖锛�");
         }
         int successNum = 0;
         int failureNum = 0;
         StringBuilder successMsg = new StringBuilder();
         StringBuilder failureMsg = new StringBuilder();
         
+        // 鑾峰彇褰撳墠鏃ユ湡
+        Date now = DateUtils.getNowDate();
+        
         for (ReceivableFeeManagement receivableFee : receivableFeeList) {
             try {
-                // 楠岃瘉绯荤粺缂栧彿鏄惁鍞竴
-                ReceivableFeeManagement checkFee = new ReceivableFeeManagement();
-                checkFee.setSystemNo(receivableFee.getSystemNo());
-                List<ReceivableFeeManagement> existingFees = selectReceivableFeeManagementList(checkFee);
+                // 鑷姩鐢熸垚绯荤粺缂栧彿锛堝拰鏂板鍔熻兘涓�鏍凤級
+                String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YS);
+                receivableFee.setSystemNo(noByKey);
                 
-                if (!existingFees.isEmpty()) {
-                    // 瀛樺湪閲嶅璁板綍
-                    if (updateSupport) {
-                        // 鏇存柊鏀寔锛岃繘琛屾洿鏂版搷浣�
-                        ReceivableFeeManagement existingFee = existingFees.get(0);
-                        receivableFee.setId(existingFee.getId());
-                        receivableFee.setUpdateTime(DateUtils.getNowDate());
-                        receivableFee.setUpdateBy(operName);
-                        updateReceivableFeeManagement(receivableFee);
-                        successNum++;
-                        successMsg.append("<br/>" + successNum + ". 绯荤粺缂栧彿 " + receivableFee.getSystemNo() + " 鏇存柊鎴愬姛");
+                // 璁剧疆鍒涘缓鏃堕棿鍜屽垱寤轰汉
+                receivableFee.setCreateTime(now);
+                receivableFee.setCreateBy(operName);
+                
+                // 鏍规嵁瀹㈡埛鍚嶇О鏌ヨ瀹㈡埛ID
+                if (receivableFee.getCustomerName() != null && !receivableFee.getCustomerName().isEmpty()) {
+                    TmsCustomerInfo customerQuery = new TmsCustomerInfo();
+                    customerQuery.setCustomerFullName(receivableFee.getCustomerName());
+                    List<TmsCustomerInfo> customerList = tmsCustomerInfoService.selectTmsCustomerInfoList(customerQuery);
+                    if (customerList != null && !customerList.isEmpty()) {
+                        // 鍙栫涓�涓尮閰嶇殑瀹㈡埛
+                        TmsCustomerInfo customer = customerList.get(0);
+                        receivableFee.setCustomerId(customer.getId());
                     } else {
-                        failureNum++;
-                        failureMsg.append("<br/>" + failureNum + ". 绯荤粺缂栧彿 " + receivableFee.getSystemNo() + " 宸插瓨鍦�");
+                        // 濡傛灉娌℃湁鎵惧埌鍖归厤鐨勫鎴凤紝鍙互璁板綍鏃ュ織鎴栨姏鍑哄紓甯�
+                        logger.warn("鏈壘鍒板鎴峰悕绉颁负 '{}' 鐨勫鎴蜂俊鎭紝customerId灏嗚缃负null", receivableFee.getCustomerName());
                     }
-                } else {
-                    // 鏂板璁板綍
-                    receivableFee.setCreateTime(DateUtils.getNowDate());
-                    receivableFee.setCreateBy(operName);
-                    insertReceivableFeeManagement(receivableFee);
-                    successNum++;
-                    successMsg.append("<br/>" + successNum + ". 绯荤粺缂栧彿 " + receivableFee.getSystemNo() + " 瀵煎叆鎴愬姛");
                 }
+                
+                // 璁$畻搴旀敹閲戦瀛楃涓叉弿杩�
+                if (receivableFee.getReceivableFeeDetailList() != null && !receivableFee.getReceivableFeeDetailList().isEmpty()) {
+                    String receivableAmountStr = calculateReceivableAmountStr(receivableFee.getReceivableFeeDetailList());
+                    receivableFee.setReceivableAmountStr(receivableAmountStr);
+                }
+                
+                // 璁剧疆榛樿鐘舵�佷负寰呯敓鎴愯处鍗曪紙0锛�
+                if (receivableFee.getStatus() == null || receivableFee.getStatus().isEmpty()) {
+                    receivableFee.setStatus("0");
+                }
+                
+                // 鎻掑叆涓昏〃鏁版嵁
+                receivableFeeManagementMapper.insertReceivableFeeManagement(receivableFee);
+                
+                // 澶勭悊鏄庣粏鏁版嵁锛堝鏋滄湁锛�
+                if (receivableFee.getReceivableFeeDetailList() != null && !receivableFee.getReceivableFeeDetailList().isEmpty()) {
+                    for (ReceivableFeeDetail detail : receivableFee.getReceivableFeeDetailList()) {
+                        detail.setReceivableFeeId(receivableFee.getId());
+                        detail.setCreateBy(operName);
+                        detail.setCreateTime(now);
+                        receivableFeeDetailService.insertReceivableFeeDetail(detail);
+                    }
+                }
+                
+                successNum++;
+                successMsg.append("<br/>" + successNum + ".搴旀敹璐圭敤 " + receivableFee.getSystemNo() + " 瀵煎叆鎴愬姛");
             } catch (Exception e) {
                 failureNum++;
-                String msg = "<br/>" + failureNum + ". 绯荤粺缂栧彿 " + receivableFee.getSystemNo() + " 瀵煎叆澶辫触锛�";
+                String msg = "<br/>" + failureNum + ".搴旀敹璐圭敤 " + receivableFee.getSystemNo() + " 瀵煎叆澶辫触锛�";
                 failureMsg.append(msg + e.getMessage());
                 logger.error(msg, e);
             }
@@ -629,7 +670,7 @@
     }
 
     /**
-     * 涓嬭浇瀵煎叆妯℃澘
+     * 涓嬭浇瀵煎叆妯℃澘锛堝寘鍚玈heet1涓昏〃鍜孲heet2鏄庣粏琛級
      * 
      * @param exportKey 瀵煎嚭鍔熻兘鐨勫敮涓�鏍囪瘑
      */
@@ -637,9 +678,43 @@
     @Async
     @Override
     public void importTemplate(String exportKey) {
-        super.export(ReceivableFeeManagement.class, exportKey, "receivableFeeManagementTemplate", (pageNum) -> {
-            // 杩斿洖绌哄垪琛紝鍙鍑烘ā鏉跨粨鏋�
-            return new ArrayList<ReceivableFeeManagement>();
-        });
+        String fileName = ExcelUtil.encodeFileName("搴旀敹璐圭敤瀵煎叆妯℃澘");
+
+        // 璁剧疆褰撳墠浠诲姟涓�"涓嬭浇涓�"鐘舵��
+        DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, ExprotStatus.XZZ.getStatus());
+        
+        try {
+            // 鍒涘缓绌哄垪琛ㄧ敤浜庣敓鎴愭ā鏉匡紙鍙渶瑕佽〃澶达級
+            List<ReceivableFeeManagement> mainList = new ArrayList<>();
+            List<ReceivableFeeDetail> detailList = new ArrayList<>();
+            
+            // 浣跨敤鎴戜滑鏂板疄鐜扮殑exportMultiSheetData鏂规硶鍒涘缓鍙宻heet妯℃澘
+            exportMultiSheetData(
+                ReceivableFeeManagement.class, 
+                ReceivableFeeDetail.class, 
+                fileName, 
+                "搴旀敹璐圭敤涓昏〃", 
+                "搴旀敹璐圭敤鏄庣粏",
+                mainList, 
+                detailList
+            );
+            
+            // 璁剧疆涓嬭浇瀹屾垚鐘舵��
+            DownloadExportUtil.setDownloadFile(redisCache, exportKey, fileName);
+            logger.info("瀵煎叆妯℃澘瀵煎嚭瀹屾垚: {}, file: {}", exportKey, fileName);
+        } catch (Exception e) {
+            logger.error("瀵煎叆妯℃澘瀵煎嚭澶辫触: {}, error: {}", exportKey, e.getMessage(), e);
+            DownloadExportUtil.deleteDownloadFile(redisCache, exportKey, ExprotStatus.XZYC.getStatus()); // 璁剧疆澶辫触鐘舵��
+            throw e;
+        }
+    }
+
+    // 鍒犻櫎涓嶅啀闇�瑕佺殑鏂规硶
+    /**
+     * 鑾峰彇鏂囦欢鐨勭粷瀵硅矾寰�
+     */
+    private File getAbsoluteFile(String fileName) {
+        String downloadPath = RuoYiConfig.getDownloadPath();
+        return new File(downloadPath + fileName);
     }
 }
\ No newline at end of file

--
Gitblit v1.8.0