huminmin
3 天以前 4d853e25a3e28c1a8c92d105729946dd9fd5a191
src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
@@ -7,10 +7,14 @@
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.pojo.Product;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.config.AliDingConfig;
import com.ruoyi.production.pojo.ProductOrder;
import com.ruoyi.production.service.ProductOrderService;
import com.ruoyi.productionPlan.dto.ProductionPlanDto;
import com.ruoyi.productionPlan.dto.ProductionPlanSummaryDto;
import com.ruoyi.productionPlan.mapper.ProductionPlanMapper;
import com.ruoyi.productionPlan.pojo.ProductionPlan;
import com.ruoyi.productionPlan.service.ProductionPlanService;
@@ -19,6 +23,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
@@ -29,6 +34,8 @@
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import static com.ruoyi.productionPlan.enums.DataSourceTypeEnum.PRODUCTION_FORECAST;
/**
 * <br>
@@ -49,6 +56,9 @@
    @Autowired
    private ProductionPlanMapper productionPlanMapper;
    @Autowired
    private ProductOrderService productOrderService;
    /**
     * 同步锁,确保手动和定时任务不同时执行
@@ -74,6 +84,83 @@
    @Override
    public void syncProdDataJob() {
        syncProdData(2);
    }
    /**
     * 合并生产计划
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean combine(ProductionPlanDto productionPlanDto) {
        if (productionPlanDto.getIds() == null || productionPlanDto.getIds().isEmpty()) {
            return false;
        }
        //  查询主生产计划
        List<ProductionPlan> plans = productionPlanMapper.selectBatchIds(productionPlanDto.getIds());
        //  校验是否存在不同的产品名称
        String firstProductName = plans.get(0).getProductName();
        if (plans.stream().anyMatch(p -> !p.getProductName().equals(firstProductName))) {
            log.warn("合并失败,存在不同的产品名称");
            return false;
        }
        // 校验是否存在不同的产品规格
        String firstProductSpec = plans.get(0).getProductSpec();
        if (plans.stream().anyMatch(p -> !p.getProductSpec().equals(firstProductSpec))) {
            log.warn("合并失败,存在不同的产品规格");
            return false;
        }
        // 叠加方数
        BigDecimal totalVolume = plans.stream()
                .map(ProductionPlan::getVolume)
                .filter(v -> v != null)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 判断下发数量是否大于等于方数
        if (productionPlanDto.getTotalAssignedQuantity().compareTo(totalVolume) > 0) {
            log.warn("合并失败,下发数量不能大于方数");
            return false;
        }
        // 根据下发数量,从第一个生产计划开始分配方数
        BigDecimal assignedVolume = BigDecimal.ZERO;
        for (ProductionPlan plan : plans) {
            BigDecimal volume = plan.getVolume();
            if (volume == null) {
                continue;
            }
            if (assignedVolume.add(volume).compareTo(productionPlanDto.getTotalAssignedQuantity()) >= 0) {
                // 最后一个计划,分配剩余方数
                plan.setAssignedQuantity(productionPlanDto.getTotalAssignedQuantity().subtract(assignedVolume));
                break;
            }
            // 分配当前计划方数
            plan.setAssignedQuantity(volume);
            productionPlanMapper.updateById(plan);
            assignedVolume = assignedVolume.add(volume);
        }
        // 创建生产订单
        ProductOrder productOrder = new ProductOrder();
        String combineIds = StringUtils.join(productionPlanDto.getIds(), ",");
        productOrder.setCombineProductionPlanIds(combineIds);
        productOrder.setQuantity(productionPlanDto.getTotalAssignedQuantity());
        productOrder.setPlanCompleteTime(productionPlanDto.getPlanCompleteTime());
        productOrderService.addProductOrder(productOrder);
        return true;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(ProductionPlanDto productionPlanDto) {
        productionPlanDto.setDataSourceType(PRODUCTION_FORECAST.getCode());
        productionPlanMapper.insert(productionPlanDto);
        return true;
    }
    /**
@@ -333,4 +420,9 @@
            return null;
        }
    }
    @Override
    public List<ProductionPlanSummaryDto> summaryByProductType(ProductionPlanSummaryDto query) {
        return baseMapper.selectSummaryByProductType(query);
    }
}