From 530b456eded06e0692e1d8bd5891069439c8f8f8 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期五, 17 四月 2026 15:10:56 +0800
Subject: [PATCH] fix: 1.已发货的销售台账对应的出入库记录做限制不能删除;2.销售台账入库操作后,点击删除,对应的销售入库记录未一并删除;3.销售台账入库状态新增部分入库

---
 src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java |  117 ++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 104 insertions(+), 13 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
index e360c95..1bbba92 100644
--- a/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
+++ b/src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -2,31 +2,49 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.enums.StockOutQualifiedRecordTypeEnum;
+import com.ruoyi.common.enums.StockInUnQualifiedRecordTypeEnum;
 import com.ruoyi.common.exception.base.BaseException;
+import com.ruoyi.common.utils.EnumUtil;
 import com.ruoyi.common.utils.OrderUtils;
 import com.ruoyi.common.utils.bean.BeanUtils;
-import com.ruoyi.staff.pojo.StaffOnJob;
+import com.ruoyi.common.utils.poi.ExcelUtil;
+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.stock.dto.StockInRecordDto;
 import com.ruoyi.stock.dto.StockInventoryDto;
+import com.ruoyi.stock.dto.StockUninventoryDto;
+import com.ruoyi.stock.execl.StockInRecordExportData;
 import com.ruoyi.stock.mapper.StockInRecordMapper;
 import com.ruoyi.stock.mapper.StockInventoryMapper;
+import com.ruoyi.stock.mapper.StockUninventoryMapper;
 import com.ruoyi.stock.pojo.StockInRecord;
 import com.ruoyi.stock.pojo.StockInventory;
+import com.ruoyi.stock.pojo.StockUninventory;
 import com.ruoyi.stock.service.StockInRecordService;
-import org.springframework.beans.factory.annotation.Autowired;
+import lombok.AllArgsConstructor;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
+import java.util.Objects;
 
 @Service
+@AllArgsConstructor
 public class StockInRecordServiceImpl extends ServiceImpl<StockInRecordMapper, StockInRecord> implements StockInRecordService {
-    @Autowired
+
     private StockInRecordMapper stockInRecordMapper;
-    @Autowired
     private StockInventoryMapper stockInventoryMapper;
+    private StockUninventoryMapper stockUninventoryMapper;
+    private SalesLedgerProductMapper salesLedgerProductMapper;
+    private SalesLedgerMapper salesLedgerMapper;
 
     @Override
     public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -49,7 +67,7 @@
     public int update(Long id, StockInRecordDto stockInRecordDto) {
         // 鍒ゆ柇瀵硅薄鏄惁瀛樺湪
         StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
-        if (stockInRecord == null){
+        if (stockInRecord == null) {
             throw new BaseException("璇ュ叆搴撹褰曚笉瀛樺湪,鏃犳硶鏇存柊!!!");
         }
 
@@ -63,16 +81,89 @@
     public int batchDelete(List<Long> ids) {
         for (Long id : ids) {
             StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
-            StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getProductModelId, id));
-            if (stockInventory == null) {
-                throw new BaseException("璇ュ叆搴撹褰曚笅娌℃湁瀵瑰簲鐨勪骇鍝�,鏃犳硶鍒犻櫎!!!");
-            }else {
-                StockInventoryDto stockInRecordDto = new StockInventoryDto();
-                stockInRecordDto.setProductModelId(stockInventory.getProductModelId());
-                stockInRecordDto.setQualitity(stockInRecord.getStockInNum());
-                stockInventoryMapper.updateSubtractStockInventory(stockInRecordDto);
+            validateCanDeleteBySalesLedger(stockInRecord);
+            if (stockInRecord.getType().equals("0")) {
+                StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getProductModelId, stockInRecord.getProductModelId()));
+                if (stockInventory == null) {
+                    throw new BaseException("搴撳瓨璁板綍涓病鏈夊搴旂殑浜у搧,鏃犳硶鍒犻櫎!!!");
+                } else {
+                    StockInventoryDto stockInRecordDto = new StockInventoryDto();
+                    stockInRecordDto.setProductModelId(stockInventory.getProductModelId());
+                    stockInRecordDto.setQualitity(stockInRecord.getStockInNum());
+                    int affectRows = stockInventoryMapper.updateSubtractStockInventory(stockInRecordDto);
+                    if (affectRows <= 0) {
+                        throw new BaseException("搴撳瓨鍥為��澶辫触,褰撳墠搴撳瓨涓嶈冻,鏃犳硶鍒犻櫎璇ュ叆搴撹褰�");
+                    }
+                    // 閿�鍞叆搴撹褰曞垹闄ゆ椂锛屽洖閫�閿�鍞骇鍝佸拰閿�鍞鍗曞叆搴撶姸鎬�
+                    rollbackSalesStockStatus(stockInRecord);
+                }
+            } else if (stockInRecord.getType().equals("1")) {
+                StockUninventory stockUninventory = stockUninventoryMapper.selectOne(new LambdaQueryWrapper<StockUninventory>().eq(StockUninventory::getProductModelId, stockInRecord.getProductModelId()));
+                if (stockUninventory == null) {
+                    throw new BaseException("搴撳瓨璁板綍涓病鏈夊搴旂殑浜у搧,鏃犳硶鍒犻櫎!!!");
+                } else {
+                    StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
+                    stockUninventoryDto.setProductModelId(stockUninventory.getProductModelId());
+                    stockUninventoryDto.setQualitity(stockInRecord.getStockInNum());
+                    int affectRows = stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto);
+                    if (affectRows <= 0) {
+                        throw new BaseException("涓嶅悎鏍煎簱瀛樺洖閫�澶辫触,褰撳墠搴撳瓨涓嶈冻,鏃犳硶鍒犻櫎璇ュ叆搴撹褰�");
+                    }
+                }
             }
         }
         return stockInRecordMapper.deleteBatchIds(ids);
     }
+
+    private void validateCanDeleteBySalesLedger(StockInRecord stockInRecord) {
+        if (stockInRecord == null || stockInRecord.getSalesLedgerId() == null) {
+            return;
+        }
+        SalesLedger salesLedger = salesLedgerMapper.selectById(stockInRecord.getSalesLedgerId());
+        if (salesLedger != null && Objects.equals(salesLedger.getDeliveryStatus(), 5)) {
+            throw new BaseException("閿�鍞鍗曞凡鍙戣揣,瀵瑰簲鍏ュ簱璁板綍涓嶅厑璁稿垹闄�");
+        }
+    }
+
+    private void rollbackSalesStockStatus(StockInRecord stockInRecord) {
+        if (stockInRecord == null || stockInRecord.getSalesLedgerProductId() == null) {
+            return;
+        }
+        SalesLedgerProduct salesLedgerProduct = salesLedgerProductMapper.selectById(stockInRecord.getSalesLedgerProductId());
+        if (salesLedgerProduct == null) {
+            return;
+        }
+        salesLedgerProduct.setProductStockStatus(0);
+        salesLedgerProductMapper.updateById(salesLedgerProduct);
+
+        Long salesLedgerId = stockInRecord.getSalesLedgerId() != null ? stockInRecord.getSalesLedgerId() : salesLedgerProduct.getSalesLedgerId();
+        if (salesLedgerId == null) {
+            return;
+        }
+        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerId);
+        if (salesLedger == null) {
+            return;
+        }
+        List<SalesLedgerProduct> ledgerProducts = salesLedgerProductMapper.selectList(Wrappers.<SalesLedgerProduct>lambdaQuery().eq(SalesLedgerProduct::getSalesLedgerId, salesLedgerId));
+        boolean hasStocked = CollectionUtils.isNotEmpty(ledgerProducts)
+                && ledgerProducts.stream().anyMatch(item -> Objects.equals(item.getProductStockStatus(), 1));
+        boolean allStocked = CollectionUtils.isNotEmpty(ledgerProducts)
+                && ledgerProducts.stream().allMatch(item -> Objects.equals(item.getProductStockStatus(), 1));
+        salesLedger.setStockStatus(allStocked ? 2 : (hasStocked ? 1 : 0));
+        salesLedgerMapper.updateById(salesLedger);
+    }
+
+    @Override
+    public void exportStockInRecord(HttpServletResponse response, StockInRecordDto stockInRecordDto) {
+        List<StockInRecordExportData> list = stockInRecordMapper.listStockInRecordExportData(stockInRecordDto);
+        for (StockInRecordExportData stockInRecordExportData : list) {
+            if (stockInRecordExportData.getType().equals("0")) {
+                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockOutQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
+            } else {
+                stockInRecordExportData.setRecordType(EnumUtil.fromCode(StockInUnQualifiedRecordTypeEnum.class, Integer.parseInt(stockInRecordExportData.getRecordType())).getValue());
+            }
+        }
+        ExcelUtil<StockInRecordExportData> util = new ExcelUtil<>(StockInRecordExportData.class);
+        util.exportExcel(response, list, "鍏ュ簱璁板綍淇℃伅");
+    }
 }

--
Gitblit v1.9.3