gongchunyi
2 天以前 fe64e0bbc83ab7e89cbe0d6a9f779a4ac3919608
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerProductServiceImpl.java
@@ -4,6 +4,8 @@
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;
import com.ruoyi.production.mapper.*;
@@ -37,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;
@@ -89,7 +92,9 @@
    @Override
    public SalesLedgerProduct selectSalesLedgerProductById(Long id) {
        return salesLedgerProductMapper.selectById(id);
        SalesLedgerProduct product = salesLedgerProductMapper.selectById(id);
        AmountUtils.normalizeSalesLedgerProduct(product);
        return product;
    }
    @Override
@@ -97,6 +102,7 @@
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectSalesLedgerProductList(salesLedgerProduct);
        if(!CollectionUtils.isEmpty(salesLedgerProducts)){
            salesLedgerProducts.forEach(item -> {
                AmountUtils.normalizeSalesLedgerProduct(item);
                // 发货信息
                ShippingInfo shippingInfo = shippingInfoMapper.selectOne(new LambdaQueryWrapper<ShippingInfo>()
                        .eq(ShippingInfo::getSalesLedgerProductId, item.getId())
@@ -164,6 +170,7 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrUpdateSalesLedgerProduct(SalesLedgerProduct salesLedgerProduct) {
        AmountUtils.normalizeSalesLedgerProduct(salesLedgerProduct);
        int result;
        Long salesLedgerId = salesLedgerProduct.getSalesLedgerId();
@@ -172,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,才继续更新主表金额
@@ -215,7 +219,7 @@
     */
    public void addProductionData(SalesLedgerProduct salesLedgerProduct) {
        //先判断该产品是否需要生产
        if (salesLedgerProduct.getIsProduction()) {
        if (Boolean.TRUE.equals(salesLedgerProduct.getIsProduction())) {
            SalesLedger salesLedger = salesLedgerMapper.selectById(salesLedgerProduct.getSalesLedgerId());
            ProductionPlan productionPlan = new ProductionPlan();
            productionPlan.setSalesLedgerId(salesLedgerProduct.getSalesLedgerId());
@@ -229,6 +233,39 @@
            productionPlan.setPromisedDeliveryDate(salesLedger.getDeliveryDate());//承诺日期=交货日期
            productionPlanMapper.insert(productionPlan);
        }
    }
    /**
     * 同步生产数据
     * 需要生产:生产计划已下发时禁止修改;未下发时原地更新需求数量,保留生产计划号;
     * 改为走库存:删除未下发的生产计划。
     */
    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);
        }
    }
    /**
@@ -240,16 +277,24 @@
        }
        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());
        productionPlanMapper.deleteByIds(ids);
    }
    /**
     * 查询销售产品对应的生产计划
     */
    private List<ProductionPlan> listProductionData(Long productId) {
        return productionPlanMapper.selectList(new LambdaQueryWrapper<ProductionPlan>()
                .eq(ProductionPlan::getSalesLedgerProductId, productId));
    }
    /**
@@ -279,7 +324,7 @@
            Field amountField = mainEntityClass.getDeclaredField("contractAmount");
            amountField.setAccessible(true);
            amountField.set(entity, totalAmount);
            amountField.set(entity, AmountUtils.scaleAmount(totalAmount));
            mainMapper.updateById(entity);
        } catch (Exception e) {