From b2ca7af3db0d1e2baf37829c33a82cd43d690751 Mon Sep 17 00:00:00 2001
From: wujianwei <wjw@11.com>
Date: 星期五, 26 十二月 2025 16:27:14 +0800
Subject: [PATCH] 新增字段

---
 service/src/main/java/com/ruoyi/cwgl/service/impl/ReceivableFeeManagementServiceImpl.java |  283 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 263 insertions(+), 20 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 5484a48..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,22 +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.ArrayList;
-import java.util.List;
-import java.util.Objects;
+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;
@@ -28,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涓氬姟灞傚鐞�
@@ -68,6 +77,12 @@
 
     @Autowired
     private IReceivableFeeManagementLogService logService;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    @Autowired
+    private ITmsCustomerInfoService tmsCustomerInfoService;
 
     /**
      * 鏌ヨ搴旀敹璐圭敤绠$悊
@@ -148,6 +163,12 @@
         String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YS);
         receivableFeeManagement.setSystemNo(noByKey);
 
+        // 璁$畻搴旀敹閲戦瀛楃涓叉弿杩�
+        if (receivableFeeManagement.getReceivableFeeDetailList() != null && !receivableFeeManagement.getReceivableFeeDetailList().isEmpty()) {
+            String receivableAmountStr = calculateReceivableAmountStr(receivableFeeManagement.getReceivableFeeDetailList());
+            receivableFeeManagement.setReceivableAmountStr(receivableAmountStr);
+        }
+        receivableFeeManagement.setCreateBy(SecurityUtils.getUsername());
         // 淇濆瓨涓昏〃淇℃伅
         int result = receivableFeeManagementMapper.insertReceivableFeeManagement(receivableFeeManagement);
         
@@ -191,6 +212,12 @@
     {
         receivableFeeManagement.setUpdateTime(DateUtils.getNowDate());
         
+        // 璁$畻搴旀敹閲戦瀛楃涓叉弿杩�
+        if (receivableFeeManagement.getReceivableFeeDetailList() != null && !receivableFeeManagement.getReceivableFeeDetailList().isEmpty()) {
+            String receivableAmountStr = calculateReceivableAmountStr(receivableFeeManagement.getReceivableFeeDetailList());
+            receivableFeeManagement.setReceivableAmountStr(receivableAmountStr);
+        }
+        
         // 1. 鏇存柊涓昏〃淇℃伅
         int result = receivableFeeManagementMapper.updateReceivableFeeManagement(receivableFeeManagement);
         
@@ -205,7 +232,7 @@
             // 璁剧疆姣忎釜鏄庣粏鐨勫簲鏀惰垂鐢ㄧ鐞咺D
             receivableFeeManagement.getReceivableFeeDetailList().forEach(detail -> {
                 detail.setReceivableFeeId(receivableFeeId);
-                detail.setUpdateTime(DateUtils.getNowDate()); // 璁剧疆鏇存柊鏃堕棿
+                detail.setCreateTime(DateUtils.getNowDate()); // 璁剧疆鏇存柊鏃堕棿
             });
             
             // 璋冪敤鎵归噺鎻掑叆鏂规硶
@@ -358,23 +385,52 @@
         SysConfig sysConfig = sysConfigMapper.selectConfig(new SysConfig() {{
             setConfigKey("sys.hk.rmb.rate");
         }});
-        // 璁$畻鎬诲簲鏀堕噾棰�
-        BigDecimal totalReceivableAmount = receivableFeeList.stream()
-                .map(ReceivableFeeManagement::getReceivableAmount)
-                .reduce(BigDecimal.ZERO, BigDecimal::add);
+        // 璁$畻鎬诲簲鏀堕噾棰濓紙鎸夊竵绉嶅垎鍒绠楋級
+        BigDecimal totalAmountRmb = BigDecimal.ZERO;
+        BigDecimal totalAmountHkd = BigDecimal.ZERO;
         
-
+        // 鑾峰彇鎵�鏈夊簲鏀惰垂鐢ㄦ槑缁嗭紝鎸夊竵绉嶆眹鎬�
+        if (!receivableFeeList.isEmpty()) {
+            // 鑾峰彇鎵�鏈夊簲鏀惰垂鐢↖D
+            Integer[] feeIds = receivableFeeList.stream()
+                    .map(ReceivableFeeManagement::getId)
+                    .toArray(Integer[]::new);
+            
+            // 鏌ヨ鎵�鏈夋槑缁嗚褰�
+            List<ReceivableFeeDetail> allDetails = receivableFeeDetailService.selectReceivableFeeDetailByReceivableFeeIds(feeIds);
+            
+            // 鎸夊竵绉嶆眹鎬婚噾棰�
+            for (ReceivableFeeDetail detail : allDetails) {
+                if (detail.getBillingAmount() != null && detail.getCurrency() != null) {
+                    if ("RMB".equals(detail.getCurrency())) {
+                        totalAmountRmb = totalAmountRmb.add(detail.getBillingAmount());
+                    } else if ("HKD".equals(detail.getCurrency())) {
+                        totalAmountHkd = totalAmountHkd.add(detail.getBillingAmount());
+                    }
+                }
+            }
+        }
+        
         BigDecimal exchangeRate = new BigDecimal(sysConfig.getConfigValue());
-        BigDecimal totalAmountHkd = totalReceivableAmount.divide(exchangeRate, 2, RoundingMode.HALF_UP);
+        
+        // 璁$畻浜烘皯甯佹�婚噾棰濓紙浜烘皯甯侀噾棰� + 娓竵鎹㈢畻鎴愪汉姘戝竵锛�
+        BigDecimal totalAmountRmbWithConversion = totalAmountRmb.add(
+                totalAmountHkd.multiply(exchangeRate).setScale(2, RoundingMode.HALF_UP)
+        );
+        
+        // 璁$畻娓竵鎬婚噾棰濓紙娓竵閲戦 + 浜烘皯甯佹崲绠楁垚娓竵锛�
+        BigDecimal totalAmountHkdWithConversion = totalAmountHkd.add(
+                totalAmountRmb.divide(exchangeRate, 2, RoundingMode.HALF_UP)
+        );
         
 
         // 缁勮杩斿洖缁撴灉
         ReceivableFeeStatisticsVo result = new ReceivableFeeStatisticsVo();
         result.setDocumentCount(documentCount);
         result.setRate(exchangeRate);
-        result.setTotalReceivableAmount(totalReceivableAmount);
-        result.setTotalAmountRmb(totalReceivableAmount);
-        result.setTotalAmountHkd(totalAmountHkd);
+        result.setTotalReceivableAmount(totalAmountRmbWithConversion); // 榛樿浣跨敤浜烘皯甯佹�婚噾棰�
+        result.setTotalAmountRmb(totalAmountRmbWithConversion);
+        result.setTotalAmountHkd(totalAmountHkdWithConversion);
         result.setIds(ids);
         
         return result;
@@ -412,12 +468,13 @@
         billManagement.setCreateTime(DateUtils.getNowDate());
         billManagement.setBillName(billCreateVo.getBillName());
         billManagement.setCustomerName(billCreateVo.getCustomerName());
+        billManagement.setCreateBy(SecurityUtils.getUsername());
         billManagement.setIsInternalSettlement(billCreateVo.getIsInternalSettlement());
         billManagement.setInternalSettlementUnit(billCreateVo.getInternalSettlementUnit());
         // 3. 鏍规嵁璐﹀崟绫诲瀷璁剧疆甯佺鍜屾�婚噾棰�
         if (billType == 0) {
             // 浜烘皯甯佽处鍗�
-            billManagement.setCurrency("CNY");
+            billManagement.setCurrency("RMB");
             billManagement.setTotalAmount(statisticsVo.getTotalAmountRmb());
         } else if (billType == 1) {
             // 娓竵璐﹀崟
@@ -426,6 +483,7 @@
         } else {
             throw new IllegalArgumentException("鏃犳晥鐨勮处鍗曠被鍨嬶細" + billType);
         }
+        billManagement.setPendingAmount(billManagement.getTotalAmount());
         String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YSZD);
         billManagement.setSystemNo(noByKey);
         // 4. 淇濆瓨涓昏处鍗曡褰�
@@ -461,7 +519,7 @@
         return i;
     }
 
-}
+
     
     /**
      * 鏍规嵁鍏宠仈璐﹀崟缂栧彿鎵归噺鏇存柊搴旀敹璐圭敤绠$悊璁板綍
@@ -474,4 +532,189 @@
     public int updateReceivableFeeManagementByRelatedBillNo(String relatedBillNo) {
         return receivableFeeManagementMapper.updateReceivableFeeManagementByRelatedBillNo(relatedBillNo);
     }
+
+    /**
+     * 璁$畻搴旀敹閲戦瀛楃涓叉弿杩�
+     * 鏍规嵁鏄庣粏鍒楄〃鎸夊竵绉嶆眹鎬婚噾棰濓紝鏍煎紡濡傦細"200娓竵100浜烘皯甯�"
+     * 
+     * @param detailList 搴旀敹璐圭敤鏄庣粏鍒楄〃
+     * @return 搴旀敹閲戦瀛楃涓叉弿杩�
+     */
+    private String calculateReceivableAmountStr(List<ReceivableFeeDetail> detailList) {
+        if (detailList == null || detailList.isEmpty()) {
+            return "";
+        }
+        
+        // 鎸夊竵绉嶆眹鎬婚噾棰�
+        Map<String, BigDecimal> currencyAmountMap = new HashMap<>();
+        for (ReceivableFeeDetail detail : detailList) {
+            String currency = detail.getCurrency();
+            BigDecimal billingAmount = detail.getActualAmount();
+            
+            if (currency != null && billingAmount != null) {
+                currencyAmountMap.merge(currency, billingAmount, BigDecimal::add);
+            }
+        }
+        
+        // 鏋勫缓瀛楃涓叉弿杩�
+        StringBuilder sb = new StringBuilder();
+        for (Map.Entry<String, BigDecimal> entry : currencyAmountMap.entrySet()) {
+            if (sb.length() > 0) {
+                sb.append(" ");
+            }
+            sb.append(entry.getValue().stripTrailingZeros().toPlainString());
+            
+            // 鏍规嵁甯佺鏄剧ず瀵瑰簲鐨勮揣甯佸悕绉�
+            String currency = entry.getKey();
+            if ("RMB".equals(currency)) {
+                sb.append("浜烘皯甯�");
+            } else if ("HKD".equals(currency)) {
+                sb.append("娓竵");
+            } else if ("USD".equals(currency)) {
+                sb.append("缇庡厓");
+            } else {
+                sb.append(currency);
+            }
+        }
+        
+        return sb.toString();
+    }
+
+    /**
+     * 瀵煎叆搴旀敹璐圭敤绠$悊鏁版嵁锛堝寘鍚槑缁嗘暟鎹級
+     * 
+     * @param receivableFeeList 搴旀敹璐圭敤绠$悊鏁版嵁鍒楄〃锛堝寘鍚槑缁嗘暟鎹級
+     * @param operName 鎿嶄綔鐢ㄦ埛
+     * @return 缁撴灉
+     */
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public String importReceivableFee(List<ReceivableFeeManagement> receivableFeeList, String operName) {
+        if (receivableFeeList == null || receivableFeeList.isEmpty()) {
+            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 {
+                // 鑷姩鐢熸垚绯荤粺缂栧彿锛堝拰鏂板鍔熻兘涓�鏍凤級
+                String noByKey = systemDataNoService.getNoByKey(SystemDataNoEnum.YS);
+                receivableFee.setSystemNo(noByKey);
+                
+                // 璁剧疆鍒涘缓鏃堕棿鍜屽垱寤轰汉
+                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 {
+                        // 濡傛灉娌℃湁鎵惧埌鍖归厤鐨勫鎴凤紝鍙互璁板綍鏃ュ織鎴栨姏鍑哄紓甯�
+                        logger.warn("鏈壘鍒板鎴峰悕绉颁负 '{}' 鐨勫鎴蜂俊鎭紝customerId灏嗚缃负null", receivableFee.getCustomerName());
+                    }
+                }
+                
+                // 璁$畻搴旀敹閲戦瀛楃涓叉弿杩�
+                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() + " 瀵煎叆澶辫触锛�";
+                failureMsg.append(msg + e.getMessage());
+                logger.error(msg, e);
+            }
+        }
+        
+        if (failureNum > 0) {
+            failureMsg.insert(0, "寰堟姳姝夛紝瀵煎叆澶辫触锛佸叡 " + failureNum + " 鏉℃暟鎹牸寮忎笉姝g‘锛岄敊璇涓嬶細");
+            throw new ServiceException(failureMsg.toString());
+        } else {
+            successMsg.insert(0, "鎭枩鎮紝鏁版嵁宸插叏閮ㄥ鍏ユ垚鍔燂紒鍏� " + successNum + " 鏉★紝鏁版嵁濡備笅锛�");
+        }
+        return successMsg.toString();
+    }
+
+    /**
+     * 涓嬭浇瀵煎叆妯℃澘锛堝寘鍚玈heet1涓昏〃鍜孲heet2鏄庣粏琛級
+     * 
+     * @param exportKey 瀵煎嚭鍔熻兘鐨勫敮涓�鏍囪瘑
+     */
+    @DataSource(DataSourceType.SLAVE)
+    @Async
+    @Override
+    public void importTemplate(String exportKey) {
+        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