gongchunyi
2 天以前 1511296b46ddc0ed8a285d2d19e5491ef3506488
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -4,6 +4,7 @@
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.AmountUtils;
import com.ruoyi.framework.web.domain.R;
import com.ruoyi.procurementrecord.utils.StockUtils;
@@ -38,6 +39,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
@@ -177,12 +179,9 @@
            result = salesLedgerProductMapper.insert(salesLedgerProduct);
            addProductionData(salesLedgerProduct);
        } else {
            //查询原本的产品型号id
            result = salesLedgerProductMapper.updateById(salesLedgerProduct);
            /*删除对应的生产数据并重新新增*/
            deleteProductionData(Arrays.asList(salesLedgerProduct.getId()));
            addProductionData(salesLedgerProduct);
            /*同步对应的生产数据:计划未下发时原地更新需求数量,保留生产计划号*/
            syncProductionData(salesLedgerProduct);
        }
        // 如果插入或更新成功,并且有 salesLedgerId,才继续更新主表金额
@@ -238,6 +237,38 @@
    }
    /**
     * 同步生产数据
     * 需要生产:生产计划已下发时禁止修改;未下发时原地更新需求数量,保留生产计划号;
     * 改为走库存:删除未下发的生产计划。
     */
    public void syncProductionData(SalesLedgerProduct salesLedgerProduct) {
        if (salesLedgerProduct.getId() == null) {
            return;
        }
        List<ProductionPlan> productionPlans = listProductionData(salesLedgerProduct.getId());
        if (CollectionUtils.isEmpty(productionPlans)) {
            //原来不需要生产,现在改为需要生产,补建生产计划
            if (Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
                addProductionData(salesLedgerProduct);
            }
            return;
        }
        //改为走库存,删除未下发的生产计划
        if (!Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
            deleteProductionData(Collections.singletonList(salesLedgerProduct.getId()));
            return;
        }
        if (productionPlans.stream().anyMatch(plan -> plan.getStatus() != null && plan.getStatus() != 0)) {
            throw new ServiceException("生产计划已下发,不能修改该销售产品");
        }
        for (ProductionPlan productionPlan : productionPlans) {
            productionPlan.setQtyRequired(salesLedgerProduct.getQuantity());
            productionPlan.setProductModelId(salesLedgerProduct.getProductModelId());
            productionPlanMapper.updateById(productionPlan);
        }
    }
    /**
     * 删除生产计划
     */
    public void deleteProductionData(List<Long> productIds) {
@@ -246,12 +277,12 @@
        }
        List<ProductionPlan> productionPlans = productionPlanMapper.selectList(
                new LambdaQueryWrapper<ProductionPlan>()
                        .in(ProductionPlan::getSalesLedgerProductId, productIds.stream().map(Long::intValue).collect(Collectors.toList())));
                        .in(ProductionPlan::getSalesLedgerProductId, productIds));
        if (CollectionUtils.isEmpty(productionPlans)) {
            return;
        }
        //如果生产计划已下发则不能删除
        if (productionPlans.stream().anyMatch(productionPlan -> productionPlan.getStatus() != 0)) {
        if (productionPlans.stream().anyMatch(productionPlan -> productionPlan.getStatus() != null && productionPlan.getStatus() != 0)) {
            throw new RuntimeException("生产计划已下发,不能删除该销售产品");
        }
        List<Long> ids = productionPlans.stream().map(ProductionPlan::getId).collect(Collectors.toList());
@@ -259,6 +290,14 @@
    }
    /**
     * 查询销售产品对应的生产计划
     */
    private List<ProductionPlan> listProductionData(Long productId) {
        return productionPlanMapper.selectList(new LambdaQueryWrapper<ProductionPlan>()
                .eq(ProductionPlan::getSalesLedgerProductId, productId));
    }
    /**
     * 通用方法:根据主表ID和子表列表,更新主表的合同金额
     */
    public <T, S> void updateMainContractAmount(