basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardProductListServiceImpl.java
@@ -257,53 +257,57 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> selectStandardProductListByMethodId(Integer id, String tree) {
        // 将“工厂 - 实验室 - 样品类别 - 样品 - 型号”路径拆成各级节点。
        String[] trees = tree.split(" - ");
        // 判断是否拖拽
        // 四级及以上节点需要按数据库中已保存的 sort 排序返回。
        boolean isDrag = false;
        // 存放根据结构参数表重新生成的检验项目。
        List<StandardProductList> list = new ArrayList<>();
        if (trees.length == 3) {
            // 三级节点只定位到样品类别,需要继续展开该类别下的样品和型号。
            List<StandardTree> treeList = new ArrayList<>();
            StandardTree standardTree = new StandardTree();
            standardTree.setFactory(trees[0]);
            standardTree.setLaboratory(trees[1]);
            standardTree.setSampleType(trees[2]);
            // 查询该样品类别关联的产品(样品)名称。
            List<ProductDto> pList = standardTreeMapper.selectPList(trees[2]);
            if (pList.size() == 0 || pList.get(0) == null) {
            if (pList.isEmpty() || pList.get(0) == null) {
                // 没有产品时,直接使用该实验室和样品类别已配置的标准树节点。
                List<StandardTree> treeList1 = standardTreeMapper.selectList(Wrappers.<StandardTree>lambdaQuery().eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]));
                if (treeList1.size() == 0) {
                if (treeList1.isEmpty()) {
                    // 标准树也不存在时,保留三级节点作为兜底查询条件。
                    treeList.add(standardTree);
                } else {
                    treeList.addAll(treeList1);
                }
            } else {
                // 按产品逐个查找已配置的型号;未配置时创建一个只有样品信息的兜底节点。
                for (ProductDto p : pList) {
                    standardTree.setSample(p.getName());
                    List<StandardTree> treeList1 = standardTreeMapper.selectList(Wrappers.<StandardTree>lambdaQuery().eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).eq(StandardTree::getSample, p.getName()));
                    if (treeList1.size() == 0) {
                    if (treeList1.isEmpty()) {
                        treeList.add(JSON.parseObject(JSON.toJSONString(standardTree), StandardTree.class));
                    } else {
                        treeList.addAll(treeList1);
                    }
                }
            }
            // 为每个展开后的“样品 + 型号”节点查询适用的检验项目。
            for (StandardTree standardTree2 : treeList) {
                String tree2 = trees[0] + " - " + trees[1] + " - " + trees[2] + " - " + standardTree2.getSample() + " - " + standardTree2.getModel();
                // 查询适用于样品类别的通用项目及当前样品/型号的项目。
                list.addAll(standardTreeMapper.selectStandardProductListByTree("\"" + trees[2] + "\"", standardTree2.getSample(), standardTree2.getModel(), tree2, trees[1]));
                // 查询同时限定样品类别和样品名称的项目。
                list.addAll(standardTreeMapper.selectStandardProductListByTree2("\"" + trees[2] + "\",\"" + standardTree2.getSample() + "\"", standardTree2.getSample(), standardTree2.getModel(), tree2, trees[1]));
            }
        } else if (trees.length == 4) {
            // 判断第四层是否有型号
            Long count = standardProductListMapper.selectCount(Wrappers.<StandardProductList>lambdaQuery()
                    .eq(StandardProductList::getStandardMethodListId, id)
                    .isNull(StandardProductList::getModel)
                    .like(StandardProductList::getTree, tree));
            if (count == 0) {
                isDrag = true;
            }
            // 四级节点已定位到样品,后续按已保存的 sort 排序返回。
            isDrag = true;
            // 查询该样品已配置的型号节点。
            List<StandardTree> treeList = standardTreeMapper.selectList(Wrappers.<StandardTree>lambdaQuery().eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).eq(StandardTree::getSample, trees[3]));
            if (treeList.size() == 0) {
            if (treeList.isEmpty()) {
                // 没有型号配置时,创建不含型号的节点,仍可查询通用项目。
                StandardTree standardTree = new StandardTree();
                standardTree.setFactory(trees[0]);
                standardTree.setLaboratory(trees[1]);
@@ -311,37 +315,41 @@
                standardTree.setSample(trees[3]);
                treeList.add(standardTree);
            }
            // 针对样品的每个型号,合并通用项目和样品专属项目。
            for (StandardTree standardTree : treeList) {
                String str = tree + " - " + standardTree.getModel();
                list.addAll(standardTreeMapper.selectStandardProductListByTree("\"" + trees[2] + "\"", standardTree.getSample(), standardTree.getModel(), str, trees[1]));
                list.addAll(standardTreeMapper.selectStandardProductListByTree2("\"" + trees[2] + "\",\"" + trees[3] + "\"", standardTree.getSample(), standardTree.getModel(), str, trees[1]));
            }
        } else {
            // 五级节点已明确样品和型号,直接查询该节点适用的项目。
            isDrag = true;
            list.addAll(standardTreeMapper.selectStandardProductListByTree("\"" + trees[2] + "\"", trees[3].equals("null") ? null : trees[3], trees[4], tree, trees[1]));
            list.addAll(standardTreeMapper.selectStandardProductListByTree2("\"" + trees[2] + "\",\"" + trees[3] + "\"", trees[3].equals("null") ? null : trees[3], trees[4], tree, trees[1]));
        }
        // 为新生成的项目预先分配雪花 ID;匹配到旧配置后会替换为旧记录 ID。
        for (StandardProductList productList : list) {
            productList.setId(IdWorker.getId());
        }
        List<StandardProductList> standardProductLists;
        if (isDrag) {
            // 查询当前节点及其子节点的旧配置,并按 sort 读取以保留拖拽后的顺序。
            standardProductLists = standardProductListMapper.selectList(Wrappers.<StandardProductList>lambdaQuery()
                    .eq(StandardProductList::getStandardMethodListId, id)
                    .like(StandardProductList::getTree, tree)
                    .orderByAsc(StandardProductList::getSort));
            // 判断是否有没有序号的, 没有序号重置
            boolean b = standardProductLists.stream().anyMatch(standardProductList -> standardProductList.getSort() == null);
        } else {
            // 三级节点没有拖拽顺序,只读取用于覆盖默认值的旧配置。
            standardProductLists = standardProductListMapper.selectList(Wrappers.<StandardProductList>lambdaQuery()
                    .eq(StandardProductList::getStandardMethodListId, id)
                    .like(StandardProductList::getTree, tree));
        }
        // 将旧项目中人工维护的配置覆盖到本次重新生成的对应项目。
        for (StandardProductList sp : standardProductLists) {
            for (StandardProductList pl : list) {
                // 判断条件是否只有一个, 有一个的话默认第一个
                // 半径候选值只有一个时,将其作为默认半径。
                String radiusList = pl.getRadiusList();
                if (StringUtils.isNotBlank(radiusList) && !radiusList.equals("null") && !radiusList.equals("\"\"")) {
                    JSONArray jsonArray = JSON.parseArray(radiusList);
@@ -350,15 +358,17 @@
                        pl.setRadius(radius.get(0));
                    }
                }
                // 以检验项目、子项、型号、样品和结构参数 ID 判断新旧项目是否对应。
                if (Objects.equals(sp.getInspectionItem(), pl.getInspectionItem())
                        && Objects.equals((sp.getInspectionItemSubclass() == null) ? "" : sp.getInspectionItemSubclass(), pl.getInspectionItemSubclass() == null ? "" : pl.getInspectionItemSubclass())
//                        && Objects.equals(sp.getSample(), pl.getSample())
                        && Objects.equals(sp.getModel(), pl.getModel())
                        && sp.getTree().indexOf(pl.getSample() == null ? "null" : pl.getSample()) > -1
                        && Objects.equals(sp.getStructureItemParameterId(), pl.getStructureItemParameterId())) {
                    // 复用旧 ID 和排序,确保本次同步后原有配置仍关联到同一条项目记录。
                    pl.setId(sp.getId());
                    // 添加排序字段
                    pl.setSort(sp.getSort());
                    // 旧记录存在值时优先保留;状态为空时按标准 ID 设置默认状态。
                    if (sp.getState() != null && !sp.getState().equals("")) {
                        pl.setState(sp.getState());
                    } else {
@@ -405,18 +415,21 @@
            }
        }
        // 记录本次同步的创建人和更新人。
        Integer userId = SecurityUtils.getUserId().intValue();
        if (trees.length == 5) {
            // 五级节点只重建当前精确路径的数据。
            standardProductListMapper.delete(Wrappers.<StandardProductList>lambdaUpdate()
                    .eq(StandardProductList::getStandardMethodListId, id)
                    .eq(StandardProductList::getTree, tree));
        } else {
            // 三级、四级节点重建当前路径及其所有子节点的数据。
            standardProductListMapper.delete(Wrappers.<StandardProductList>lambdaUpdate()
                    .eq(StandardProductList::getStandardMethodListId, id)
                    .like(StandardProductList::getTree, tree));
        }
        // 补齐批量保存所需的树节点、用户和检验标准字段。
        List<StandardProductList> productLists = list.stream().map(a -> {
            a.setFactory(trees[0]);
            a.setLaboratory(trees[1]);
@@ -426,11 +439,12 @@
            a.setStandardMethodListId(id);
            return a;
        }).collect(Collectors.toList());
//            this.saveBatch(productLists);
        // 批量添加标准
        baseMapper.saveBatchProductLists(productLists);
        if (CollectionUtils.isNotEmpty(productLists)) {
            // 批量写入重新生成后的检验项目。
            baseMapper.saveBatchProductLists(productLists);
        }
        // 先按工时分组中的数字升序排列;空分组排到最后。
        Collections.sort(list, (o1, o2) -> {
            String field1 = o1.getManHourGroup();
            String field2 = o2.getManHourGroup();
@@ -450,12 +464,13 @@
                return Integer.compare(num1, num2);
            }
        });
          // 按照索引排序
        if (isDrag) {
            // 四级、五级节点最终按已保存的拖拽排序覆盖工时分组排序。
            list.sort((o1, o2) -> (o1.getSort() == null ? 0 : o1.getSort())
                    - (o2.getSort() == null ? 0 : o2.getSort()));
        }
        // 按前端约定返回项目列表和数量。
        Map<String, Object> map = new HashMap<>();
        map.put("productList", list);
        map.put("total", list.size());