17 小时以前 7bf754a7835d06f26240c4ca15bc5f83650de377
src/main/java/com/ruoyi/stock/service/impl/StockInRecordServiceImpl.java
@@ -13,12 +13,23 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import java.time.LocalDateTime;
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.production.mapper.ProductionOrderPickMapper;
import com.ruoyi.production.pojo.ProductionOrderPick;
import com.ruoyi.procurementrecord.mapper.ReturnManagementMapper;
import com.ruoyi.procurementrecord.mapper.ReturnSaleProductMapper;
import com.ruoyi.procurementrecord.pojo.ReturnManagement;
import com.ruoyi.procurementrecord.pojo.ReturnSaleProduct;
import com.ruoyi.sales.dto.SalesLedgerDto;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.ShippingInfoMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.ShippingInfo;
import com.ruoyi.stock.mapper.StockInRecordMapper;
import com.ruoyi.stock.mapper.StockInventoryMapper;
import com.ruoyi.stock.mapper.StockUninventoryMapper;
@@ -43,6 +54,20 @@
    private StockInventoryMapper stockInventoryMapper;
    private StockUninventoryMapper stockUninventoryMapper;
    private ProductionOrderPickMapper productionOrderPickMapper;
    private ReturnSaleProductMapper returnSaleProductMapper;
    private ReturnManagementMapper returnManagementMapper;
    private ShippingInfoMapper shippingInfoMapper;
    private SalesLedgerMapper salesLedgerMapper;
    /**
     * 入库类别:代储入库。只有这个类别要求绑定销售订单
     */
    private static final String INBOUND_CATEGORY_DEPOSIT = "代储入库";
    /**
     * 销售台账的销售类型:代储。代储入库只能绑这个类型的台账
     */
    private static final String LEDGER_TYPE_DEPOSIT = "代储";
    @Override
    public IPage<StockInRecordDto> listPage(Page page, StockInRecordDto stockInRecordDto) {
@@ -53,11 +78,49 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(StockInRecordDto stockInRecordDto) {
        String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches");
        resolveDepositSalesLedger(stockInRecordDto);
        LocalDateTime createTime = stockInRecordDto.getCreateTime();
        if (createTime == null) {
            createTime = LocalDateTime.now();
        }
        String no = OrderUtils.countTodayByCreateTime(stockInRecordMapper, "RK","inbound_batches", createTime);
        stockInRecordDto.setInboundBatches(no);
        stockInRecordDto.setCreateTime(createTime);
        StockInRecord stockInRecord = new StockInRecord();
        BeanUtils.copyProperties(stockInRecordDto, stockInRecord);
        return stockInRecordMapper.insert(stockInRecord);
    }
    /**
     * 代储入库(inboundCategory=代储入库)必须绑定一张「代储」类型的销售台账(销售订单)。
     * 只做校验与落库,**不用台账覆盖**入库单上的客户/油库/储罐 —— 那几项仍以现场填写为准。
     * 非代储入库不绑台账,即使前端传了也置空,避免落下无意义的关联。
     */
    private void resolveDepositSalesLedger(StockInRecordDto stockInRecordDto) {
        if (!INBOUND_CATEGORY_DEPOSIT.equals(stockInRecordDto.getInboundCategory())) {
            stockInRecordDto.setSalesLedgerId(null);
            return;
        }
        Long salesLedgerId = stockInRecordDto.getSalesLedgerId();
        if (salesLedgerId == null) {
            throw new BaseException("代储入库必须绑定销售订单");
        }
        SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerId);
        if (salesLedger == null) {
            throw new BaseException("所选销售订单不存在,销售订单id:" + salesLedgerId);
        }
        if (!LEDGER_TYPE_DEPOSIT.equals(salesLedger.getLedgerType())) {
            throw new BaseException("代储入库只能绑定代储类型的销售订单,销售合同号:" + salesLedger.getSalesContractNo());
        }
    }
    @Override
    public IPage<SalesLedger> listBindableSalesLedger(Page page, SalesLedgerDto salesLedgerDto) {
        // 该查询用 /*data_scope*/ 跳过数据权限,而 MP 默认会把 count 语句优化成「SELECT COUNT(*) FROM sales_ledger
        // WHERE ...」,优化后标记丢失,count 反而被追加权限条件,total 会比实际记录少。关掉 count 优化后
        // count 退化成「SELECT COUNT(*) FROM (原句) TOTAL」,标记原样保留
        page.setOptimizeCountSql(false);
        return stockInRecordMapper.bindableSalesLedgerPage(page, salesLedgerDto);
    }
    @Override
@@ -239,14 +302,18 @@
                    stockInventoryDto.setQualitity(stockInRecord.getStockInNum());
                    stockInventoryDto.setRemark(stockInRecord.getRemark());
                    if (stockInventory == null) {
                        stockInventoryMapper.insert(new StockInventory() {{
                            setProductModelId(stockInRecord.getProductModelId());
                            setQualitity(stockInRecord.getStockInNum());
                            setBatchNo(stockInRecord.getBatchNo());
                            setRemark(stockInRecord.getRemark());
                            setWarnNum(stockInRecord.getWarnNum());
                            setVersion(1);
                        }});
                        try {
                            stockInventoryMapper.insert(new StockInventory() {{
                                setProductModelId(stockInRecord.getProductModelId());
                                setQualitity(stockInRecord.getStockInNum());
                                setBatchNo(stockInRecord.getBatchNo());
                                setRemark(stockInRecord.getRemark());
                                setWarnNum(stockInRecord.getWarnNum());
                                setVersion(1);
                            }});
                        } catch (org.springframework.dao.DuplicateKeyException e) {
                            stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
                        }
                    } else {
                        stockInventoryMapper.updateAddStockInventory(stockInventoryDto);
                    }
@@ -259,17 +326,23 @@
                    stockUninventoryDto.setQualitity(stockInRecord.getStockInNum());
                    stockUninventoryDto.setRemark(stockInRecord.getRemark());
                    if (stockUninventory == null) {
                        stockUninventoryMapper.insert(new StockUninventory() {{
                            setProductModelId(stockInRecord.getProductModelId());
                            setQualitity(stockInRecord.getStockInNum());
                            setBatchNo(stockInRecord.getBatchNo());
                            setRemark(stockInRecord.getRemark());
                            setVersion(1);
                        }});
                        try {
                            stockUninventoryMapper.insert(new StockUninventory() {{
                                setProductModelId(stockInRecord.getProductModelId());
                                setQualitity(stockInRecord.getStockInNum());
                                setBatchNo(stockInRecord.getBatchNo());
                                setRemark(stockInRecord.getRemark());
                                setVersion(1);
                            }});
                        } catch (org.springframework.dao.DuplicateKeyException e) {
                            stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
                        }
                    } else {
                        stockUninventoryMapper.updateAddStockUnInventory(stockUninventoryDto);
                    }
                }
                // 销售退货入库:扣减销售台账实际合同金额(退货生效)
                handleSalesReturnRefund(stockInRecord);
            }
        }
        return ids.size();
@@ -320,4 +393,47 @@
        return ids.size();
    }
    /**
     * 销售退货入库审批通过时,扣减对应销售台账的实际合同金额。
     * 关联链:stock_in_record.record_id = return_sale_product.id
     *        → return_management.shipping_id = shipping_info.id
     *        → shipping_info.sales_ledger_id = sales_ledger.id
     * 只处理 record_type 为 14(合格)或 15(不合格)的销售退货入库。
     */
    private void handleSalesReturnRefund(StockInRecord stockInRecord) {
        String recordType = stockInRecord.getRecordType();
        if (!StockInQualifiedRecordTypeEnum.RETURN_HE_IN.getCode().equals(recordType)
                && !StockInQualifiedRecordTypeEnum.RETURN_UNSTOCK_IN.getCode().equals(recordType)) {
            return;
        }
        if (stockInRecord.getRecordId() == null) {
            return;
        }
        ReturnSaleProduct rsp = returnSaleProductMapper.selectById(stockInRecord.getRecordId());
        if (rsp == null || rsp.getReturnManagementId() == null) {
            return;
        }
        ReturnManagement rm = returnManagementMapper.selectById(rsp.getReturnManagementId());
        if (rm == null || rm.getShippingId() == null) {
            return;
        }
        ShippingInfo shippingInfo = shippingInfoMapper.selectById(rm.getShippingId());
        if (shippingInfo == null || shippingInfo.getSalesLedgerId() == null) {
            return;
        }
        SalesLedger salesLedger = salesLedgerMapper.selectById(shippingInfo.getSalesLedgerId());
        if (salesLedger == null || salesLedger.getContractAmount() == null) {
            return;
        }
        BigDecimal refund = rsp.getAmount() != null ? rsp.getAmount() : BigDecimal.ZERO;
        if (refund.compareTo(BigDecimal.ZERO) == 0) {
            return;
        }
        BigDecimal baseAmount = salesLedger.getNetContractAmount() != null
                ? salesLedger.getNetContractAmount()
                : salesLedger.getContractAmount();
        salesLedger.setNetContractAmount(baseAmount.subtract(refund));
        salesLedgerMapper.updateById(salesLedger);
    }
}