huminmin
3 天以前 c545c8c94e08b4925335b828f5674574c3734e8c
src/main/java/com/ruoyi/productionPlan/service/impl/ProductionPlanServiceImpl.java
@@ -14,6 +14,7 @@
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;
@@ -30,9 +31,9 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
/**
 * <br>
@@ -115,17 +116,39 @@
                .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();
        //  合并生产计划ids
        String combineIds = StringUtils.join(productionPlanDto.getIds(), ",");
        productOrder.setCombineProductionPlanIds(combineIds);
        // 下发数量
        productOrder.setQuantity(productionPlanDto.getTotalAssignedQuantity());
        productOrder.setPlanCompleteTime(productionPlanDto.getPlanCompleteTime());
        productOrderService.addProductOrder(productOrder);
        return true;
    }
@@ -186,8 +209,8 @@
                List<ProductionPlan> list = parseProductionPlans(dataArr, dataSyncType, totalCount);
                if (!list.isEmpty()) {
                    //  处理更新或新增
                    processSaveOrUpdate(list);
                    totalSynced += list.size();
                    int affected = processSaveOrUpdate(list);
                    totalSynced += affected;
                }
                //  判断是否还有下一页
@@ -330,19 +353,48 @@
        return list;
    }
    private void processSaveOrUpdate(List<ProductionPlan> list) {
    private int processSaveOrUpdate(List<ProductionPlan> list) {
        if (list == null || list.isEmpty()) {
            return 0;
        }
        int affected = 0;
        //  去重 formInstanceId
        Set<String> formIds = list.stream()
                .map(ProductionPlan::getFormInstanceId)
                .collect(Collectors.toSet());
        //  查询数据库已有数据
        List<ProductionPlan> existList = this.list(new LambdaQueryWrapper<ProductionPlan>().in(ProductionPlan::getFormInstanceId, formIds));
        //  Map (formInstanceId + materialCode)
        Map<String, ProductionPlan> existMap = new HashMap<>();
        for (ProductionPlan p : existList) {
            String key = p.getFormInstanceId() + "_" + p.getMaterialCode();
            existMap.put(key, p);
        }
        //  遍历同步数据
        for (ProductionPlan plan : list) {
            LambdaQueryWrapper<ProductionPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductionPlan::getFormInstanceId, plan.getFormInstanceId())
                    .eq(ProductionPlan::getMaterialCode, plan.getMaterialCode());
            ProductionPlan existing = this.getOne(queryWrapper);
            if (existing != null) {
                plan.setId(existing.getId());
                this.updateById(plan);
            } else {
            String key = plan.getFormInstanceId() + "_" + plan.getMaterialCode();
            ProductionPlan exist = existMap.get(key);
            if (exist == null) {
                // 新增
                this.save(plan);
                affected++;
                log.info("新增数据 formInstanceId={}, materialCode={}", plan.getFormInstanceId(), plan.getMaterialCode());
            } else {
                // 判断是否需要更新
                if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(plan.getFormModifiedTime())) {
                    plan.setId(exist.getId());
                    plan.setCreateTime(exist.getCreateTime());
                    this.updateById(plan);
                    affected++;
                    log.info("更新数据 formInstanceId={}, materialCode={}", plan.getFormInstanceId(), plan.getMaterialCode());
                }
            }
        }
        return affected;
    }
    private LocalDateTime parseUtcTime(String utcString) {
@@ -357,4 +409,9 @@
            return null;
        }
    }
    @Override
    public List<ProductionPlanSummaryDto> summaryByProductType(ProductionPlanSummaryDto query) {
        return baseMapper.selectSummaryByProductType(query);
    }
}