From a8e4132c3e2e9c3b3fcb0360901b35571b6464ea Mon Sep 17 00:00:00 2001
From: hsy <1029150275@qq.com>
Date: 星期五, 28 八月 2026 10:13:01 +0800
Subject: [PATCH] feat: 综合业务模块功能补充与实体拓展

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |   83 ++++++++++++++++++++++++++++++-----------
 1 files changed, 61 insertions(+), 22 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 75ee5a6..73a5b7f 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -1,9 +1,9 @@
 package com.ruoyi.sales.service.impl;
 
+import cn.hutool.core.collection.CollUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -38,8 +38,6 @@
 import com.ruoyi.project.system.domain.SysUser;
 import com.ruoyi.project.system.mapper.SysDeptMapper;
 import com.ruoyi.project.system.mapper.SysUserMapper;
-import com.ruoyi.purchase.dto.SimpleReturnOrderGroupDto;
-import com.ruoyi.purchase.mapper.PurchaseReturnOrderProductsMapper;
 import com.ruoyi.quality.mapper.QualityInspectMapper;
 import com.ruoyi.sales.dto.*;
 import com.ruoyi.sales.mapper.CommonFileMapper;
@@ -77,6 +75,9 @@
 import java.util.concurrent.TimeUnit;
 import java.util.function.Function;
 import java.util.stream.Collectors;
+import com.ruoyi.sales.mapper.SalesLedgerContractRelMapper;
+import com.ruoyi.sales.pojo.SalesLedgerContractRel;
+import org.springframework.util.CollectionUtils;
 
 /**
  * 閿�鍞彴璐ervice涓氬姟灞傚鐞�
@@ -108,6 +109,7 @@
     private final FileUtil fileUtil;
     private final AccountInvoiceApplicationMapper accountInvoiceApplicationMapper;
     private final AccountSalesCollectionMapper accountSalesCollectionMapper;
+    private final SalesLedgerContractRelMapper salesLedgerContractRelMapper;
 
     @Autowired
     private SysDeptMapper sysDeptMapper;
@@ -120,9 +122,6 @@
     private ProductMapper productMapper;
     @Autowired
     private ProductionProductMainService productionProductMainService;
-    @Autowired
-    private PurchaseReturnOrderProductsMapper purchaseReturnOrderProductsMapper;
-    ;
     @Autowired
     private SysUserMapper sysUserMapper;
 
@@ -148,17 +147,11 @@
         productWrapper.eq(SalesLedgerProduct::getType, type.getCode());
         List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(productWrapper);
         if (type.equals(SaleEnum.PURCHASE)) {
-            // 鏌ヨ閫�璐т俊鎭�
-            List<Long> productIds = salesLedgerProducts.stream().map(SalesLedgerProduct::getProductModelId).collect(Collectors.toList());
-            List<SimpleReturnOrderGroupDto> groupListByProductIds = new ArrayList<>();
-            if(CollectionUtils.isNotEmpty(productIds)){
-                groupListByProductIds = purchaseReturnOrderProductsMapper.getReturnOrderGroupListByProductIds(productIds);
-            }
-            Map<Long, BigDecimal> returnOrderGroupDtoMap = groupListByProductIds.stream().collect(Collectors.toMap(SimpleReturnOrderGroupDto::getProductModelId, SimpleReturnOrderGroupDto::getSumReturnQuantity));
             salesLedgerProducts.forEach(item -> {
-                BigDecimal returnQuality = returnOrderGroupDtoMap.getOrDefault(item.getProductModelId(), BigDecimal.ZERO);
-                item.setReturnQuality(returnQuality);
-                item.setAvailableQuality(item.getQuantity().subtract(returnQuality));
+                BigDecimal returnQuantity = item.getReturnQuantity() == null ? BigDecimal.ZERO : item.getReturnQuantity();
+                item.setReturnQuantity(returnQuantity);
+                BigDecimal quantity = item.getQuantity() == null ? BigDecimal.ZERO : item.getQuantity();
+                item.setAvailableQuality(quantity.subtract(returnQuantity));
             });
         }
         return salesLedgerProducts;
@@ -170,6 +163,15 @@
         SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerDto.getId());
         if (salesLedger == null) {
             throw new BaseException("鍙拌处涓嶅瓨鍦�");
+        }
+
+        // 鏌ヨ鍏宠仈鐨勫悎鍚�
+        List<SalesLedgerContractRel> rels = salesLedgerContractRelMapper.selectList(
+                new LambdaQueryWrapper<SalesLedgerContractRel>().eq(SalesLedgerContractRel::getSalesLedgerId, salesLedger.getId())
+        );
+        if (!CollectionUtils.isEmpty(rels)) {
+            List<Long> contractIds = rels.stream().map(SalesLedgerContractRel::getContractId).collect(Collectors.toList());
+            salesLedger.setAssociatedContractIds(contractIds);
         }
 
         // 2. 鏌ヨ瀛愯〃
@@ -396,6 +398,7 @@
                 salesLedger.setContractAmount(salesLedgerProductImportDtos.stream()
                         .map(SalesLedgerProductImportDto::getTaxInclusiveTotalPrice)
                         .reduce(BigDecimal.ZERO, BigDecimal::add));
+                salesLedger.setNetContractAmount(salesLedger.getContractAmount());
                 salesLedgerMapper.insert(salesLedger);
 
 
@@ -540,7 +543,7 @@
         // 鍒犻櫎鍙戣揣鍙拌处璁板綍
         List<ShippingInfo> shippingInfos = shippingInfoMapper.selectList(new LambdaQueryWrapper<ShippingInfo>()
                 .in(ShippingInfo::getSalesLedgerId, idList));
-        if (CollectionUtils.isNotEmpty(shippingInfos)) {
+        if (CollUtil.isNotEmpty(shippingInfos)) {
             shippingInfoServiceImpl.delete(shippingInfos.stream().map(ShippingInfo::getId).collect(Collectors.toList()));
         }
         // 鍒犻櫎闄勪欢琛�
@@ -549,7 +552,7 @@
         // 鍒犻櫎鐢熶骇绠℃帶鏁版嵁
         //鏌ヨ鐢熶骇鎶ュ伐id
         ArrayList<Long> mainIdList = productionProductMainService.listMain(idList);
-        if (CollectionUtils.isNotEmpty(mainIdList)) {
+        if (CollUtil.isNotEmpty(mainIdList)) {
             mainIdList.stream().forEach(mainId -> {
                 productionProductMainService.removeProductMain(mainId);
             });
@@ -571,11 +574,15 @@
         SalesLedger salesLedger = convertToEntity(salesLedgerDto);
         salesLedger.setCustomerName(customer.getCustomerName());
         salesLedger.setTenantId(customer.getTenantId());
+        // 褰曞叆浜猴細濡傛灉鏈紶鍒欎娇鐢ㄥ綋鍓嶇櫥褰曠敤鎴�
+        if (StringUtils.isEmpty(salesLedger.getEntryPerson())) {
+            salesLedger.setEntryPerson(String.valueOf(SecurityUtils.getUserId()));
+        }
         // 3. 鏂板鎴栨洿鏂颁富琛�
         if (salesLedger.getId() == null) {
             String contractNo = salesLedger.getSalesContractNo();
             if (StringUtils.isEmpty(contractNo)) {
-                contractNo = generateSalesContractNo();
+                contractNo = generateSalesContractNo(salesLedgerDto.getEntryDate());
             }
             salesLedger.setSalesContractNo(contractNo);
             salesLedgerMapper.insert(salesLedger);
@@ -583,6 +590,18 @@
             salesLedgerMapper.updateById(salesLedger);
         }
 
+        // 澶勭悊鍏宠仈鍚堝悓鏁版嵁
+        salesLedgerContractRelMapper.delete(
+                new LambdaQueryWrapper<SalesLedgerContractRel>().eq(SalesLedgerContractRel::getSalesLedgerId, salesLedger.getId())
+        );
+        if (!CollectionUtils.isEmpty(salesLedgerDto.getAssociatedContractIds())) {
+            for (Long contractId : salesLedgerDto.getAssociatedContractIds()) {
+                SalesLedgerContractRel rel = new SalesLedgerContractRel();
+                rel.setSalesLedgerId(salesLedger.getId());
+                rel.setContractId(contractId);
+                salesLedgerContractRelMapper.insert(rel);
+            }
+        }
 
         // 4. 澶勭悊瀛愯〃鏁版嵁
         List<SalesLedgerProduct> productList = salesLedgerDto.getProductData();
@@ -597,7 +616,18 @@
             );
         }
 
-        // 5. 淇濆瓨閿�鍞彴璐﹂檮浠�
+        // 5. 鍏滃簳锛氱‘淇濆疄闄呭悎鍚岄噾棰濇湁鍒濆鍊肩瓑浜庡悎鍚岄噾棰�
+        if (salesLedger.getNetContractAmount() == null) {
+            SalesLedger dbLedger = salesLedgerMapper.selectById(salesLedger.getId());
+            if (dbLedger != null && dbLedger.getNetContractAmount() == null && dbLedger.getContractAmount() != null) {
+                SalesLedger updateEntity = new SalesLedger();
+                updateEntity.setId(salesLedger.getId());
+                updateEntity.setNetContractAmount(dbLedger.getContractAmount());
+                salesLedgerMapper.updateById(updateEntity);
+            }
+        }
+
+        // 6. 淇濆瓨閿�鍞彴璐﹂檮浠�
         fileUtil.saveStorageAttachment(ApplicationTypeEnum.FILE, RecordTypeEnum.SALES_LEDGER, salesLedger.getId(), salesLedgerDto.getStorageBlobDTOs());
 
         return 1;
@@ -638,8 +668,8 @@
     }
 
     @Transactional(readOnly = true)
-    public String generateSalesContractNo() {
-        LocalDate currentDate = LocalDate.now();
+    public String generateSalesContractNo(Date entryDate) {
+        LocalDate currentDate = entryDate != null ? DateUtils.toLocalDate(entryDate) : LocalDate.now();
         String datePart = currentDate.format(DateTimeFormatter.BASIC_ISO_DATE);
         String lockKey = LOCK_PREFIX + datePart;
         String lockValue = Thread.currentThread().getId() + "-" + System.nanoTime(); // 鍞竴鏍囪瘑閿佹寔鏈夎��
@@ -735,6 +765,15 @@
             amountField.setAccessible(true);
             amountField.set(entity, totalAmount);
 
+            // 瀹為檯鍚堝悓閲戦鍒濆鍊肩瓑浜庡悎鍚岄噾棰�
+            try {
+                Field netAmountField = mainEntityClass.getDeclaredField("netContractAmount");
+                netAmountField.setAccessible(true);
+                netAmountField.set(entity, totalAmount);
+            } catch (NoSuchFieldException ignored) {
+                // 瀛愮被鍙兘娌℃湁璇ュ瓧娈碉紝蹇界暐
+            }
+
             mainMapper.updateById(entity);
         } catch (Exception e) {
             throw new RuntimeException("鍔ㄦ�佹洿鏂颁富琛ㄩ噾棰濆け璐�", e);

--
Gitblit v1.9.3