gongchunyi
16 小时以前 c30bfeb3840c69f0ad7eeb0ff713d63da7494b23
src/main/java/com/ruoyi/technology/service/impl/TechnologyRoutingServiceImpl.java
@@ -20,10 +20,14 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
@@ -123,32 +127,59 @@
        if (technologyRouting.getBomId() == null) {
            return;
        }
        List<TechnologyBomStructure> bomStructures = technologyBomStructureMapper.selectList(
                Wrappers.<TechnologyBomStructure>lambdaQuery()
                        .eq(TechnologyBomStructure::getBomId, technologyRouting.getBomId())
                        .isNotNull(TechnologyBomStructure::getOperationId)
                        .orderByDesc(TechnologyBomStructure::getId)
                        .orderByAsc(TechnologyBomStructure::getId)
        );
        if (bomStructures.isEmpty()) {
            throw new ServiceException("bom产品结构为空!");
        }
        // 同一个 BOM 中可能重复引用相同工序,按照上一层的父节点的产品是否相同和工序是否相同
        Map<Long, TechnologyBomStructure> structureById = new HashMap<>();
        Map<Long, List<TechnologyBomStructure>> childrenByParentId = new LinkedHashMap<>();
        List<TechnologyBomStructure> rootList = new ArrayList<>();
        for (TechnologyBomStructure bomStructure : bomStructures) {
            if (bomStructure != null && bomStructure.getId() != null) {
            if (bomStructure == null) {
                continue;
            }
            if (bomStructure.getId() != null) {
                structureById.put(bomStructure.getId(), bomStructure);
            }
            if (bomStructure.getParentId() == null || bomStructure.getParentId() == 0L) {
                rootList.add(bomStructure);
            } else {
                childrenByParentId.computeIfAbsent(bomStructure.getParentId(), key -> new ArrayList<>()).add(bomStructure);
            }
        }
        Map<String, TechnologyBomStructure> uniqueOperationMap = new LinkedHashMap<>();
        for (TechnologyBomStructure bomStructure : bomStructures) {
            Long outputProductModelId = resolveOutputProductModelId(bomStructure, structureById, technologyRouting.getProductModelId());
            uniqueOperationMap.putIfAbsent(buildBomOperationDedupKey(bomStructure, outputProductModelId), bomStructure);
        // 后序遍历BOM
        List<TechnologyBomStructure> orderedOperationNodes = new ArrayList<>();
        Set<Long> visitingIds = new HashSet<>();
        for (TechnologyBomStructure rootNode : rootList) {
            collectOperationNodePostOrder(rootNode, childrenByParentId, orderedOperationNodes, visitingIds);
        }
        if (orderedOperationNodes.isEmpty()) {
            throw new ServiceException("bom产品结构未配置消耗工序!");
        }
        // 同一层级(同一父节点)内相同工序合并为一道工序,例如同层的多个压接件合并成一道压接;
        // 合并后的位置取该工序最后一次出现的位置,确保其全部输入物料的工序都在它之前
        Map<String, Integer> lastPositionByKey = new HashMap<>();
        Map<String, TechnologyBomStructure> operationNodeByKey = new LinkedHashMap<>();
        for (int i = 0; i < orderedOperationNodes.size(); i++) {
            TechnologyBomStructure bomStructure = orderedOperationNodes.get(i);
            String dedupKey = buildBomOperationDedupKey(bomStructure);
            lastPositionByKey.put(dedupKey, i);
            operationNodeByKey.putIfAbsent(dedupKey, bomStructure);
        }
        List<String> sortedDedupKeys = new ArrayList<>(operationNodeByKey.keySet());
        sortedDedupKeys.sort(Comparator.comparingInt(lastPositionByKey::get));
        int dragSort = 1;
        for (TechnologyBomStructure bomStructure : uniqueOperationMap.values()) {
        for (String dedupKey : sortedDedupKeys) {
            TechnologyBomStructure bomStructure = operationNodeByKey.get(dedupKey);
            TechnologyRoutingOperation routingOperation = new TechnologyRoutingOperation();
            routingOperation.setTechnologyRoutingId(technologyRouting.getId());
            routingOperation.setProductModelId(resolveOutputProductModelId(bomStructure, structureById, technologyRouting.getProductModelId()));
@@ -163,42 +194,61 @@
        }
    }
    private String buildBomOperationDedupKey(TechnologyBomStructure bomStructure, Long outputProductModelId) {
        Long operationId = bomStructure == null ? null : bomStructure.getOperationId();
        Long parentId = bomStructure == null ? null : bomStructure.getParentId();
        return operationId + "#"
                + outputProductModelId + "#"
                + parentId;
    /**
     * 后序遍历 BOM
     */
    private void collectOperationNodePostOrder(TechnologyBomStructure node,
                                               Map<Long, List<TechnologyBomStructure>> childrenByParentId,
                                               List<TechnologyBomStructure> orderedOperationNodes,
                                               Set<Long> visitingIds) {
        if (node == null || node.getId() == null) {
            return;
        }
        if (!visitingIds.add(node.getId())) {
            return;
        }
        List<TechnologyBomStructure> children = childrenByParentId.get(node.getId());
        if (children != null && !children.isEmpty()) {
            for (TechnologyBomStructure child : children) {
                collectOperationNodePostOrder(child, childrenByParentId, orderedOperationNodes, visitingIds);
            }
        }
        visitingIds.remove(node.getId());
        if (node.getOperationId() != null) {
            orderedOperationNodes.add(node);
        }
    }
    /**
     * 去重键:同一父节点 + 相同工序(只在同一层级内合并,不同层级、不同分支的相同工序不会合并)。
     */
    private String buildBomOperationDedupKey(TechnologyBomStructure bomStructure) {
        Long operationId = bomStructure == null ? null : bomStructure.getOperationId();
        Long parentId = bomStructure == null ? null : bomStructure.getParentId();
        return operationId + "#" + (parentId == null ? "root" : String.valueOf(parentId));
    }
    /**
     * 工序产出产品 = 父节点的产品(本行物料由父节点层级的消耗工序消耗,产出的是上一层产品,
     * 而不是本行物料自身);仅当父节点缺失或父节点没有产品时,才回退到本节点自身产品、再回退到工艺路线产品。
     */
    private Long resolveOutputProductModelId(TechnologyBomStructure bomStructure,
                                             Map<Long, TechnologyBomStructure> structureById,
                                             Long routingProductModelId) {
        if (bomStructure == null) {
            return routingProductModelId;
        }
        if (hasChildren(bomStructure, structureById)) {
            return bomStructure.getProductModelId() != null
                    ? bomStructure.getProductModelId() : routingProductModelId;
        }
        Long parentId = bomStructure.getParentId();
        if (parentId == null) {
            return routingProductModelId != null ? routingProductModelId : bomStructure.getProductModelId();
        if (parentId != null && parentId != 0L && structureById != null) {
            TechnologyBomStructure parent = structureById.get(parentId);
            if (parent != null && parent.getProductModelId() != null) {
                return parent.getProductModelId();
            }
        }
        TechnologyBomStructure parent = structureById.get(parentId);
        if (parent != null && parent.getProductModelId() != null) {
            return parent.getProductModelId();
        if (bomStructure.getProductModelId() != null) {
            return bomStructure.getProductModelId();
        }
        return routingProductModelId != null ? routingProductModelId : bomStructure.getProductModelId();
    }
    private boolean hasChildren(TechnologyBomStructure bomStructure,
                                Map<Long, TechnologyBomStructure> structureById) {
        if (bomStructure == null || bomStructure.getId() == null || structureById == null) {
            return false;
        }
        return structureById.values().stream()
                .anyMatch(child -> child != null && java.util.Objects.equals(child.getParentId(), bomStructure.getId()));
        return routingProductModelId;
    }
    private void syncRoutingOperationParams(Long technologyRoutingOperationId, Long technologyOperationId) {