From 2656ae9bce8544d81da66c07aaede5386d6fbebb Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 25 七月 2025 15:41:01 +0800
Subject: [PATCH] 1.序列化问题 2.设备领用逻辑优化

---
 main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java |   93 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 67 insertions(+), 26 deletions(-)

diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
index 1a9c849..da90280 100644
--- a/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
+++ b/main-business/src/main/java/com/ruoyi/business/service/impl/SalesRecordServiceImpl.java
@@ -9,7 +9,6 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.basic.entity.CoalInfo;
 import com.ruoyi.basic.entity.Customer;
-import com.ruoyi.basic.entity.Supply;
 import com.ruoyi.basic.mapper.CoalInfoMapper;
 import com.ruoyi.basic.mapper.CustomerMapper;
 import com.ruoyi.business.dto.SalesRecordDto;
@@ -40,13 +39,9 @@
 import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeParseException;
 import java.time.temporal.TemporalAdjusters;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.function.Function;
 import java.util.stream.Collectors;
-import java.util.*;
 
 /**
  * <p>
@@ -153,23 +148,20 @@
         // 鍙傛暟鏍¢獙
         validateSalesRecordDto(salesRecordDto);
 
-        // 鏇存柊姝e紡搴撳緟琛ュ簱鏁伴噺
+        // 鑾峰彇鐓ょ搴撳瓨淇℃伅
         OfficialInventory officialInventory = officialInventoryMapper.selectById(salesRecordDto.getCoalId());
         if (officialInventory == null) {
             throw new BaseException("姝e紡搴撶叅绉嶄俊鎭笉瀛樺湪");
         }
-        if (salesRecordDto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0) {
-            throw new BaseException("閿�鍞暟閲忎笉鑳藉ぇ浜庡簱瀛樻暟閲�");
-        }
-        officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(salesRecordDto.getSaleQuantity()));
 
-        if (salesRecordDto.isAdd()){
-            officialInventory.setPendingReplenishment(salesRecordDto.getSaleQuantity());
-        }
-        officialInventoryMapper.updateById(officialInventory);
+        // 澶勭悊閿�鍞暟閲忓彉鏇撮�昏緫
+        SalesRecord existingRecord = salesRecordDto.getId() == null ? null : salesRecordMapper.selectById(salesRecordDto.getId());
+        handleQuantityChanges(salesRecordDto, officialInventory, existingRecord);
 
         // 鏋勫缓閿�鍞褰曞疄浣�
         SalesRecord salesRecord = buildSalesRecord(salesRecordDto, officialInventory.getCoalId());
+        // 璁剧疆閿�鍞褰曚腑鐨勫簱瀛樻暟閲�
+        salesRecord.setInventoryQuantity(officialInventory.getInventoryQuantity());
 
         // 澶勭悊鏂板/鏇存柊閫昏緫
         if (salesRecordDto.getId() == null) {
@@ -179,18 +171,52 @@
         }
     }
 
-    private void validateSalesRecordDto(SalesRecordDto dto) {
-        if (dto == null) {
-            throw new BaseException("閿�鍞褰曟暟鎹笉鑳戒负绌�");
+    private void handleQuantityChanges(SalesRecordDto dto, OfficialInventory officialInventory, SalesRecord existingRecord) {
+        if (existingRecord == null) {
+            // 鏂板璁板綍
+            if (dto.getSaleQuantity().compareTo(officialInventory.getInventoryQuantity()) > 0) {
+                throw new BaseException("閿�鍞暟閲忎笉鑳藉ぇ浜庡簱瀛樻暟閲�");
+            }
+            // 鏇存柊搴撳瓨鏁伴噺
+            officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(dto.getSaleQuantity()));
+            // 璁剧疆寰呰ˉ搴撴暟閲�
+            if (dto.isAdd()) {
+                officialInventory.setPendingReplenishment(
+                        officialInventory.getPendingReplenishment() == null ?
+                                dto.getSaleQuantity() :
+                                officialInventory.getPendingReplenishment().add(dto.getSaleQuantity())
+                );
+            }
+        } else {
+            // 鏇存柊璁板綍
+            // 姣旇緝閿�鍞暟閲忔槸鍚︽湁鍙樺寲
+            int quantityComparison = dto.getSaleQuantity().compareTo(existingRecord.getSaleQuantity());
+            if (quantityComparison != 0) {
+                // 璁$畻鏁伴噺宸��
+                BigDecimal quantityDiff = dto.getSaleQuantity().subtract(existingRecord.getSaleQuantity());
+
+                // 妫�鏌ユ柊鏁伴噺鏄惁浼氬鑷村簱瀛樹笉瓒�
+                if (quantityComparison > 0 && quantityDiff.compareTo(officialInventory.getInventoryQuantity()) > 0) {
+                    throw new BaseException("閿�鍞暟閲忓鍔犲悗涓嶈兘澶т簬搴撳瓨鏁伴噺");
+                }
+
+                // 鏇存柊搴撳瓨鏁伴噺
+                officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(quantityDiff));
+
+                // 鏇存柊寰呰ˉ搴撴暟閲忥紙濡傛灉鏄渶瑕佽ˉ搴撶殑璁板綍锛�
+                if (dto.isAdd()) {
+                    BigDecimal pendingDiff = officialInventory.getPendingReplenishment() == null ?
+                            quantityDiff :
+                            officialInventory.getPendingReplenishment().add(quantityDiff);
+                    officialInventory.setPendingReplenishment(pendingDiff);
+                }
+            }
         }
-        if (dto.getRegistrantId() == null) {
-            throw new BaseException("鐧昏浜篒D涓嶈兘涓虹┖");
-        }
-        if (dto.getCustomerId() == null) {
-            throw new BaseException("瀹㈡埛ID涓嶈兘涓虹┖");
-        }
-        if (dto.getCoalId() == null) {
-            throw new BaseException("璇烽�夋嫨涓�鏉$叅绉嶄俊鎭�");
+
+        // 鏇存柊搴撳瓨璁板綍
+        int updateResult = officialInventoryMapper.updateById(officialInventory);
+        if (updateResult <= 0) {
+            throw new BaseException("搴撳瓨鏇存柊澶辫触");
         }
     }
 
@@ -234,6 +260,21 @@
         return record;
     }
 
+    private void validateSalesRecordDto(SalesRecordDto dto) {
+        if (dto == null) {
+            throw new BaseException("閿�鍞褰曟暟鎹笉鑳戒负绌�");
+        }
+        if (dto.getRegistrantId() == null) {
+            throw new BaseException("鐧昏浜篒D涓嶈兘涓虹┖");
+        }
+        if (dto.getCustomerId() == null) {
+            throw new BaseException("瀹㈡埛ID涓嶈兘涓虹┖");
+        }
+        if (dto.getCoalId() == null) {
+            throw new BaseException("璇烽�夋嫨涓�鏉$叅绉嶄俊鎭�");
+        }
+    }
+
     private int insertSalesRecord(SalesRecord record) {
         int result = salesRecordMapper.insert(record);
         if (result <= 0) {

--
Gitblit v1.9.3