From 3beb75264c3daab38f71c83f57355aeff917ed99 Mon Sep 17 00:00:00 2001
From: chenrui <1187576398@qq.com>
Date: 星期一, 12 五月 2025 10:53:45 +0800
Subject: [PATCH] Merge branch 'master' of http://114.132.189.42:9002/r/product-inventory-management-after

---
 src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDto.java                  |    4 
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java |  120 ++++++++++++++++++++++-
 /dev/null                                                                     |    4 
 src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java           |   25 +---
 src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java                    |    2 
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java        |  110 ++++++++++++++++-----
 src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java                |    6 
 7 files changed, 208 insertions(+), 63 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java b/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
index 3f325b1..1c934a1 100644
--- a/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
+++ b/src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
@@ -11,7 +11,6 @@
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.DeleteMapping;
-import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -38,10 +37,10 @@
      * 鏌ヨ閿�鍞彴璐﹀垪琛�
      */
     @GetMapping("/list")
-    public TableDataInfo list(SalesLedger salesLedger)
+    public TableDataInfo list(SalesLedgerDto salesLedgerDto)
     {
         startPage();
-        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedger);
+        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
         return getDataTable(list);
     }
 
@@ -49,11 +48,8 @@
      * 鏌ヨ閿�鍞彴璐﹀拰浜у搧鐖跺瓙鍒楄〃
      */
     @GetMapping("/getSalesLedgerWithProducts")
-    public TableDataInfo getSalesLedgerWithProducts()
-    {
-        startPage();
-        List<SalesLedgerDto> list = salesLedgerService.getSalesLedgerWithProducts();
-        return getDataTable(list);
+    public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto){
+        return  salesLedgerService.getSalesLedgerWithProducts(salesLedgerDto);
     }
 
     /**
@@ -61,20 +57,11 @@
      */
     @Log(title = "閿�鍞彴璐�", businessType = BusinessType.EXPORT)
     @PostMapping("/export")
-    public void export(HttpServletResponse response, SalesLedger salesLedger)
+    public void export(HttpServletResponse response, SalesLedgerDto salesLedgerDto)
     {
-        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedger);
+        List<SalesLedger> list = salesLedgerService.selectSalesLedgerList(salesLedgerDto);
         ExcelUtil<SalesLedger> util = new ExcelUtil<SalesLedger>(SalesLedger.class);
         util.exportExcel(response, list, "閿�鍞彴璐︽暟鎹�");
-    }
-
-    /**
-     * 鑾峰彇閿�鍞彴璐﹁缁嗕俊鎭�
-     */
-    @GetMapping(value = "/{id}")
-    public AjaxResult getInfo(@PathVariable("id") Long id)
-    {
-        return success(salesLedgerService.selectSalesLedgerById(id));
     }
 
     /**
diff --git a/src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDTO.java b/src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDTO.java
deleted file mode 100644
index bf920e9..0000000
--- a/src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDTO.java
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.ruoyi.sales.dto;
-
-public class SalesLedgerProductDTO {
-}
diff --git a/src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDto.java b/src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDto.java
new file mode 100644
index 0000000..c51e642
--- /dev/null
+++ b/src/main/java/com/ruoyi/sales/dto/SalesLedgerProductDto.java
@@ -0,0 +1,4 @@
+package com.ruoyi.sales.dto;
+
+public class SalesLedgerProductDto {
+}
diff --git a/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java b/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
index 79c397e..3d33354 100644
--- a/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
+++ b/src/main/java/com/ruoyi/sales/pojo/SalesLedgerProduct.java
@@ -59,7 +59,7 @@
      * 绋庣巼
      */
     @Excel(name = "绋庣巼")
-    private BigDecimal taxRate;
+    private String taxRate;
 
     /**
      * 鍚◣鍗曚环
diff --git a/src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java b/src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java
index a716de3..10eedf4 100644
--- a/src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java
+++ b/src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java
@@ -14,13 +14,11 @@
  */
 public interface ISalesLedgerService extends IService<SalesLedger> {
 
-    List<SalesLedger> selectSalesLedgerList(SalesLedger salesLedger);
-
-    SalesLedger selectSalesLedgerById(Long id);
+    List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto);
 
     int deleteSalesLedgerByIds(Long[] ids);
 
     int addOrUpdateSalesLedger(SalesLedgerDto salesLedgerDto);
 
-    List<SalesLedgerDto> getSalesLedgerWithProducts();
+    SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto);
 }
diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
index 793722e..c397b9f 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -1,15 +1,25 @@
 package com.ruoyi.sales.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.sales.mapper.SalesLedgerMapper;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
+import com.ruoyi.sales.pojo.SalesLedger;
 import com.ruoyi.sales.pojo.SalesLedgerProduct;
 import com.ruoyi.sales.service.ISalesLedgerProductService;
-import org.springframework.beans.factory.annotation.Autowired;
+import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
+import java.lang.reflect.Field;
+import java.math.BigDecimal;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
 
 /**
  * 浜у搧淇℃伅Service涓氬姟灞傚鐞�
@@ -18,9 +28,12 @@
  * @date 2025-05-08
  */
 @Service
+@AllArgsConstructor
 public class SalesLedgerProductServiceImpl extends ServiceImpl<SalesLedgerProductMapper, SalesLedgerProduct> implements ISalesLedgerProductService {
-    @Autowired
+
     private SalesLedgerProductMapper salesLedgerProductMapper;
+
+    private SalesLedgerMapper salesLedgerMapper;
 
     @Override
     public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
@@ -35,16 +48,109 @@
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int deleteSalesLedgerProductByIds(Long[] ids) {
-        return salesLedgerProductMapper.deleteBatchIds(Arrays.asList(ids));
+        if (ids == null || ids.length == 0) {
+            return 0;
+        }
+
+        // 1. 鍏堟煡璇㈣鍒犻櫎鐨勫瓙琛ㄨ褰曪紝鑾峰彇瀵瑰簲鐨� salesLedgerId
+        List<SalesLedgerProduct> deletedProducts = salesLedgerProductMapper.selectBatchIds(Arrays.asList(ids));
+        if (deletedProducts.isEmpty()) {
+            return 0; // 娌℃湁鍙垹闄ょ殑鏁版嵁
+        }
+
+        // 鍙兘灞炰簬澶氫釜涓昏〃锛堜絾閫氬父涓�涓帴鍙e彧澶勭悊涓�涓富琛級
+        Set<Long> mainIds = deletedProducts.stream()
+                .map(SalesLedgerProduct::getSalesLedgerId)
+                .filter(Objects::nonNull)
+                .collect(Collectors.toSet());
+
+        // 2. 鎵ц鍒犻櫎鎿嶄綔
+        int result = salesLedgerProductMapper.deleteBatchIds(Arrays.asList(ids));
+
+        // 3. 瀵规瘡涓富琛↖D杩涜閲戦鏇存柊
+        for (Long salesLedgerId : mainIds) {
+            LambdaQueryWrapper<SalesLedgerProduct> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId);
+            List<SalesLedgerProduct> remainingProducts = salesLedgerProductMapper.selectList(wrapper);
+
+            // 璋冪敤閫氱敤鏂规硶鏇存柊涓昏〃閲戦
+            updateMainContractAmount(
+                    salesLedgerId,
+                    remainingProducts,
+                    SalesLedgerProduct::getTaxInclusiveTotalPrice,
+                    salesLedgerMapper,
+                    SalesLedger.class
+            );
+        }
+        return result;
     }
 
     @Override
+    @Transactional(rollbackFor = Exception.class)
     public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
-        if (salesLedgerProduct.getId() == null){
-            return salesLedgerProductMapper.insert(salesLedgerProduct);
-        }else {
-            return  salesLedgerProductMapper.updateById(salesLedgerProduct);
+        int result;
+        Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
+
+        if (salesLedgerProduct.getId() == null) {
+            result = salesLedgerProductMapper.insert(salesLedgerProduct);
+        } else {
+            result = salesLedgerProductMapper.updateById(salesLedgerProduct);
+        }
+
+        // 濡傛灉鎻掑叆鎴栨洿鏂版垚鍔燂紝骞朵笖鏈� salesLedgerId锛屾墠缁х画鏇存柊涓昏〃閲戦
+        if (result > 0 && salesLedgerId != null) {
+            // 鏌ヨ璇ヤ富琛ㄤ笅鐨勬墍鏈夊瓙琛ㄦ暟鎹�
+            LambdaQueryWrapper<SalesLedgerProduct> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId);
+            List<SalesLedgerProduct> productList = salesLedgerProductMapper.selectList(wrapper);
+
+            // 璋冪敤閫氱敤鏂规硶鏇存柊涓昏〃閲戦
+            updateMainContractAmount(
+                    salesLedgerId,
+                    productList,
+                    SalesLedgerProduct::getTaxInclusiveTotalPrice,
+                    salesLedgerMapper,
+                    SalesLedger.class
+            );
+        }
+
+        return result;
+    }
+
+    /**
+     * 閫氱敤鏂规硶锛氭牴鎹富琛↖D鍜屽瓙琛ㄥ垪琛紝鏇存柊涓昏〃鐨勫悎鍚岄噾棰�
+     */
+    public <T, S> void updateMainContractAmount(
+            Long mainId,
+            List<T> subList,
+            Function<T, BigDecimal> amountGetter,
+            BaseMapper<S> mainMapper,
+            Class<S> mainEntityClass) {
+
+        if (mainId == null || subList == null || subList.isEmpty()) {
+            return;
+        }
+
+        BigDecimal totalAmount = subList.stream()
+                .map(amountGetter)
+                .filter(Objects::nonNull)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+        try {
+            S entity = mainEntityClass.getDeclaredConstructor().newInstance();
+            Field idField = mainEntityClass.getDeclaredField("id");
+            idField.setAccessible(true);
+            idField.set(entity, mainId);
+
+            Field amountField = mainEntityClass.getDeclaredField("contractAmount");
+            amountField.setAccessible(true);
+            amountField.set(entity, totalAmount);
+
+            mainMapper.updateById(entity);
+        } catch (Exception e) {
+            throw new RuntimeException("鍔ㄦ�佹洿鏂颁富琛ㄩ噾棰濆け璐�", e);
         }
     }
 }
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 a1f884d..52ec03e 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -1,11 +1,13 @@
 package com.ruoyi.sales.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.mapper.CustomerMapper;
 import com.ruoyi.basic.pojo.Customer;
 import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.sales.dto.SalesLedgerDto;
 import com.ruoyi.sales.mapper.SalesLedgerMapper;
 import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
@@ -19,10 +21,13 @@
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import java.lang.reflect.Field;
+import java.math.BigDecimal;
 import java.time.LocalDate;
 import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -48,33 +53,34 @@
     private final RedisTemplate<String, String> redisTemplate;
 
     @Override
-    public List<SalesLedger> selectSalesLedgerList(SalesLedger salesLedger) {
-        return salesLedgerMapper.selectList(new LambdaQueryWrapper<>());
+    public List<SalesLedger> selectSalesLedgerList(SalesLedgerDto salesLedgerDto) {
+        LambdaQueryWrapper<SalesLedger> queryWrapper = new LambdaQueryWrapper<>();
+        if (StringUtils.isNotBlank(salesLedgerDto.getCustomerName())) {
+            queryWrapper.eq(SalesLedger::getCustomerName, salesLedgerDto.getCustomerName());
+        }
+        return salesLedgerMapper.selectList(queryWrapper);
     }
 
-    public List<SalesLedgerDto> getSalesLedgerWithProducts() {
-        List<SalesLedger> ledgers = salesLedgerMapper.selectList(new LambdaQueryWrapper<>());
-        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(new LambdaQueryWrapper<>());
+    public SalesLedgerDto getSalesLedgerWithProducts(SalesLedgerDto salesLedgerDto) {
+        // 1. 鏌ヨ涓昏〃
+        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerDto.getId());
+        if (salesLedger == null) {
+            throw new BaseException("鍙拌处涓嶅瓨鍦�");
+        }
 
-        Map<Long, List<SalesLedgerProduct>> productMap = products.stream()
-                .collect(Collectors.groupingBy(SalesLedgerProduct::getSalesLedgerId));
+        // 2. 鏌ヨ瀛愯〃
+        LambdaQueryWrapper<SalesLedgerProduct> productWrapper = new LambdaQueryWrapper<>();
+        productWrapper.eq(SalesLedgerProduct::getSalesLedgerId, salesLedger.getId());
+        List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList(productWrapper);
 
-        return ledgers.stream().map(ledger -> {
-            SalesLedgerDto dto = new SalesLedgerDto();
-            org.springframework.beans.BeanUtils.copyProperties(ledger, dto);
-
-            List<SalesLedgerProduct> ledgerProducts = productMap.getOrDefault(ledger.getId(), Collections.emptyList());
-            if (!ledgerProducts.isEmpty()) {
-                dto.setHasChildren(true);
-                dto.setProductData(ledgerProducts);
-            }
-            return dto;
-        }).collect(Collectors.toList());
-    }
-
-    @Override
-    public SalesLedger selectSalesLedgerById(Long id) {
-        return salesLedgerMapper.selectById(id);
+        // 3. 杞崲 DTO
+        SalesLedgerDto resultDto = new SalesLedgerDto();
+        BeanUtils.copyProperties(salesLedger, resultDto);
+        if (!products.isEmpty()) {
+            resultDto.setHasChildren(true);
+            resultDto.setProductData(products);
+        }
+        return resultDto;
     }
 
     @Override
@@ -120,10 +126,19 @@
         }
 
         // 4. 澶勭悊瀛愯〃鏁版嵁
-        if (salesLedgerDto.getProductData() != null && !salesLedgerDto.getProductData().isEmpty()) {
-            handleSalesLedgerProducts(salesLedger.getId(), salesLedgerDto.getProductData());
-        }
+        List<SalesLedgerProduct> productList = salesLedgerDto.getProductData();
+        if (productList != null && !productList.isEmpty()) {
+            handleSalesLedgerProducts(salesLedger.getId(), productList);
 
+            // 鉁� 璋冪敤閫氱敤鏂规硶鏇存柊涓昏〃閲戦
+            updateMainContractAmount(
+                    salesLedger.getId(),
+                    productList,
+                    SalesLedgerProduct::getTaxInclusiveTotalPrice,
+                    salesLedgerMapper,
+                    SalesLedger.class
+            );
+        }
         return 1; // 鎿嶄綔鎴愬姛杩斿洖1
     }
 
@@ -138,11 +153,15 @@
 
         // 鎵ц鏇存柊鎿嶄綔
         if (!updateList.isEmpty()) {
-            salesLedgerProductMapper.updateBatchSomeColumn(updateList);
+            for (SalesLedgerProduct product : updateList) {
+                salesLedgerProductMapper.updateById(product);
+            }
         }
         // 鎵ц鎻掑叆鎿嶄綔
         if (!insertList.isEmpty()) {
-            salesLedgerProductMapper.insertBatchSomeColumn(insertList);
+            for (SalesLedgerProduct salesLedgerProduct : insertList) {
+                salesLedgerProductMapper.insert(salesLedgerProduct);
+            }
         }
     }
 
@@ -213,4 +232,39 @@
         }
         return next;
     }
+
+    public <T, S> void updateMainContractAmount(
+            Long mainId,
+            List<T> subList,
+            Function<T, BigDecimal> amountGetter,
+            BaseMapper<S> mainMapper,
+            Class<S> mainEntityClass) {
+
+        if (mainId == null || subList == null || subList.isEmpty()) {
+            return;
+        }
+
+        // 璁$畻瀛愯〃閲戦鎬诲拰
+        BigDecimal totalAmount = subList.stream()
+                .map(amountGetter)
+                .filter(Objects::nonNull)
+                .reduce(BigDecimal.ZERO, BigDecimal::add);
+
+        // 鏋勯�犱富琛ㄦ洿鏂板璞★紙鏀寔浠绘剰涓昏〃绫诲瀷锛�
+        try {
+            S entity = mainEntityClass.getDeclaredConstructor().newInstance();
+            Field idField = mainEntityClass.getDeclaredField("id");
+            idField.setAccessible(true);
+            idField.set(entity, mainId);
+
+            // 璁剧疆 contractAmount 瀛楁锛屾敞鎰忚繖閲屽亣璁惧瓧娈靛悕涓� "contractAmount"
+            Field amountField = mainEntityClass.getDeclaredField("contractAmount");
+            amountField.setAccessible(true);
+            amountField.set(entity, totalAmount);
+
+            mainMapper.updateById(entity);
+        } catch (Exception e) {
+            throw new RuntimeException("鍔ㄦ�佹洿鏂颁富琛ㄩ噾棰濆け璐�", e);
+        }
+    }
 }

--
Gitblit v1.9.3