src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -2,6 +2,8 @@
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;
@@ -11,6 +13,10 @@
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
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;
@@ -28,6 +34,7 @@
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
@Service
@AllArgsConstructor
@@ -36,6 +43,8 @@
    private StockInRecordMapper stockInRecordMapper;
    private StockInventoryMapper stockInventoryMapper;
    private StockUninventoryMapper stockUninventoryMapper;
    private SalesLedgerProductMapper salesLedgerProductMapper;
    private SalesLedgerMapper salesLedgerMapper;
    @Override
    public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -72,6 +81,7 @@
    public int batchDelete(List<Long> ids) {
        for (Long id : ids) {
            StockInRecord stockInRecord = stockInRecordMapper.selectById(id);
            validateCanDeleteBySalesLedger(stockInRecord);
            if (stockInRecord.getType().equals("0")) {
                StockInventory stockInventory = stockInventoryMapper.selectOne(new LambdaQueryWrapper<StockInventory>().eq(StockInventory::getProductModelId, stockInRecord.getProductModelId()));
                if (stockInventory == null) {
@@ -80,7 +90,12 @@
                    StockInventoryDto stockInRecordDto = new StockInventoryDto();
                    stockInRecordDto.setProductModelId(stockInventory.getProductModelId());
                    stockInRecordDto.setQualitity(stockInRecord.getStockInNum());
                    stockInventoryMapper.updateSubtractStockInventory(stockInRecordDto);
                    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()));
@@ -90,13 +105,54 @@
                    StockUninventoryDto stockUninventoryDto = new StockUninventoryDto();
                    stockUninventoryDto.setProductModelId(stockUninventory.getProductModelId());
                    stockUninventoryDto.setQualitity(stockInRecord.getStockInNum());
                    stockUninventoryMapper.updateSubtractStockUnInventory(stockUninventoryDto);
                    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);