From 901bab9b8b1c3773a644bdc74f5eafd666591aeb Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 17 九月 2026 13:37:24 +0800
Subject: [PATCH] feat: 1委托单 2下单详情 3检验任务 4检验任务复核 5检验任务详情 6标准维护 7检验单详情 8报告编制

---
 basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardTreeServiceImpl.java |  749 +++++++++++++++++++++++++++------------------------------
 1 files changed, 358 insertions(+), 391 deletions(-)

diff --git a/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardTreeServiceImpl.java b/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardTreeServiceImpl.java
index ad84b75..280d3c5 100644
--- a/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardTreeServiceImpl.java
+++ b/basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardTreeServiceImpl.java
@@ -1,38 +1,50 @@
 package com.ruoyi.basic.service.impl;
 
-import cn.hutool.core.collection.CollUtil;
-import cn.hutool.poi.excel.ExcelUtil;
 import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ruoyi.basic.dto.SampleDto;
-import com.ruoyi.common.exception.base.BaseException;
-import com.ruoyi.basic.dto.FactoryDto;
-import com.ruoyi.basic.dto.LaboratoryDto;
+import com.ruoyi.basic.dto.AddStandardTreeNodeDto;
 import com.ruoyi.basic.dto.SampleTypeDto;
+import com.ruoyi.basic.dto.StandardLibraryTreeNodeDto;
+import com.ruoyi.basic.dto.StandardLibraryTreeRowDto;
+import com.ruoyi.basic.dto.UpdateStandardTreeNodeDto;
+import com.ruoyi.basic.dto.StandardTreeTemplateBindingDto;
+import com.ruoyi.basic.dto.StandardTreeTemplateBindingVo;
+import com.ruoyi.basic.dto.TemplateBindingItemDto;
+import com.ruoyi.basic.dto.TemplateInspectionItemDto;
+import com.ruoyi.basic.mapper.LaboratoryMapper;
 import com.ruoyi.basic.mapper.StandardProductListMapper;
 import com.ruoyi.basic.mapper.StandardTreeMapper;
+import com.ruoyi.basic.mapper.StandardTreeTemplateBindingMapper;
+import com.ruoyi.basic.mapper.StructureItemParameterMapper;
+import com.ruoyi.basic.mapper.StructureTestObjectMapper;
+import com.ruoyi.basic.pojo.Laboratory;
 import com.ruoyi.basic.pojo.StandardProductList;
-import com.ruoyi.basic.pojo.StandardTemplate;
 import com.ruoyi.basic.pojo.StandardTree;
+import com.ruoyi.basic.pojo.StandardTemplate;
+import com.ruoyi.basic.pojo.StandardTreeTemplateBinding;
+import com.ruoyi.basic.pojo.StructureItemParameter;
 import com.ruoyi.basic.pojo.StructureTestObject;
-import com.ruoyi.basic.service.*;
+import com.ruoyi.basic.service.StandardTreeService;
+import com.ruoyi.basic.service.StandardTemplateService;
+import com.ruoyi.basic.utils.StandardTreePath;
+import com.ruoyi.basic.utils.StandardTreePathParser;
+import com.ruoyi.basic.utils.StandardTemplateInspectionItemParser;
+import com.ruoyi.common.exception.base.BaseException;
 import lombok.AllArgsConstructor;
-import org.apache.commons.lang3.ObjectUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.multipart.MultipartFile;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.*;
-import java.util.concurrent.atomic.AtomicReference;
-import java.util.stream.Collectors;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.HashSet;
+import java.util.Comparator;
+import java.util.Set;
 
 /**
  * @author Administrator
@@ -47,68 +59,157 @@
 
     private StandardTreeMapper standardTreeMapper;
 
-    private StandardMethodListService standardMethodListService;
-
     private StandardProductListMapper standardProductListMapper;
 
-    private StandardProductListService standardProductListService;
+    private StructureItemParameterMapper structureItemParameterMapper;
+
+    private StructureTestObjectMapper structureTestObjectMapper;
+
+    private LaboratoryMapper laboratoryMapper;
+
+    private StandardTreeTemplateBindingMapper standardTreeTemplateBindingMapper;
 
     private StandardTemplateService standardTemplateService;
 
-    private StructureTestObjectService structureTestObjectService;
-
 
     @Override
-    public List<FactoryDto> selectStandardTreeList() {
-        List<FactoryDto> factoryDtos = standardTreeMapper.selectStandardTreeList();
-        for (FactoryDto factoryDto : factoryDtos) {
-            for (LaboratoryDto laboratoryDto : factoryDto.getChildren()) {
-                laboratoryDto.getChildren().sort((o1, o2) -> (o1.getSort() == null ? 0 : o1.getSort())
-                        - (o2.getSort() == null ? 0 : o2.getSort()));
-                for (SampleTypeDto sampleTypeDto : laboratoryDto.getChildren()) {
-//                    if (sampleTypeDto.getChildren().size() == 0) {
-                    sampleTypeDto.getChildren().addAll(standardTreeMapper.getStandardTree3(sampleTypeDto.getValue()));
-//                    }
-                }
+    public List<StandardLibraryTreeNodeDto> selectStandardTreeList() {
+        List<StandardLibraryTreeRowDto> rows = standardTreeMapper.selectStandardLibraryTree();
+        List<StandardLibraryTreeNodeDto> industries = new ArrayList<>();
+        Map<Integer, StandardLibraryTreeNodeDto> industryMap = new LinkedHashMap<>();
+        for (StandardLibraryTreeRowDto row : rows) {
+            StandardLibraryTreeNodeDto industry = industryMap.get(row.getIndustryId());
+            if (industry == null) {
+                industry = new StandardLibraryTreeNodeDto();
+                industry.setId(row.getIndustryId());
+                industry.setLabel(row.getIndustryName());
+                industry.setValue(row.getIndustryName());
+                industry.setCode("[1]");
+                industry.setChildren(new ArrayList<>());
+                industryMap.put(row.getIndustryId(), industry);
+                industries.add(industry);
+            }
+            if (row.getStandardId() != null) {
+                StandardLibraryTreeNodeDto standard = new StandardLibraryTreeNodeDto();
+                standard.setId(row.getStandardId());
+                standard.setLabel(row.getStandardCode());
+                standard.setValue(row.getStandardLabel());
+                standard.setCode("[2]");
+                standard.setSampleEn(row.getStandardNameEn());
+                standard.setStandardMethodId(row.getStandardMethodId());
+                standard.setStandardCode(row.getStandardCode());
+                standard.setStandardName(row.getStandardName());
+                standard.setChildren(null);
+                industry.getChildren().add(standard);
             }
         }
-        return factoryDtos;
+        return industries;
     }
 
 
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public int addStandardTree(StandardTree standardTree) {
-        LambdaQueryWrapper<StandardTree> wrapper = Wrappers.<StandardTree>lambdaQuery()
-                .eq(StandardTree::getFactory, standardTree.getFactory())
-                .eq(StandardTree::getLaboratory, standardTree.getLaboratory())
-                .eq(StandardTree::getSampleType, standardTree.getSampleType())
-                .eq(StandardTree::getSample, standardTree.getSample())
-                .eq(StandardTree::getModel, standardTree.getModel());
-        if (StringUtils.isNotBlank(standardTree.getSample())) {
-            wrapper.eq(StandardTree::getSample, standardTree.getSample());
+    public int addStandardTree(AddStandardTreeNodeDto dto) {
+        if (dto == null) {
+            throw new BaseException("鏍囧噯鏍戣矾寰勬牸寮忛敊璇�");
         }
-
-        StandardTree tree = standardTreeMapper.selectOne(wrapper);
-        if (tree != null) {
-            throw new BaseException("璇ュ瀷鍙峰凡瀛樺湪");
+        String name = StandardTreePathParser.normalizeNodeName(dto.getName());
+        if (StringUtils.isBlank(dto.getParentTree())) {
+            // 鏂板琛屼笟鑺傜偣
+            Long count = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
+                    .eq(Laboratory::getLaboratoryName, name));
+            if (count > 0) {
+                throw new BaseException("琛屼笟鍚嶇О宸插瓨鍦�");
+            }
+            Laboratory laboratory = new Laboratory();
+            laboratory.setLaboratoryName(name);
+            laboratory.setSort(0);
+            return laboratoryMapper.insert(laboratory);
         }
-        return standardTreeMapper.insert(standardTree);
+        throw new BaseException("浜岀骇鏍囧噯璇峰湪鈥滄爣鍑嗙淮鎶も�濅腑閫氳繃琛屼笟缁戝畾杩涜缁存姢");
+        /*
+        // 鍦ㄨ涓氫笅鏂板鏍囧噯鑺傜偣
+        StandardTreePath path = StandardTreePathParser.parse(dto.getParentTree());
+        if (path.getStandard() != null) {
+            throw new BaseException("鏍囧噯鑺傜偣涓嬩笉鑳界户缁柊澧�");
+        }
+        String industry = path.getIndustry();
+        Long labCount = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
+                .eq(Laboratory::getLaboratoryName, industry));
+        if (labCount == 0) {
+            throw new BaseException("琛屼笟涓嶅瓨鍦�");
+        }
+        Long stdCount = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
+                .eq(StandardTree::getLaboratory, industry)
+                .eq(StandardTree::getSample, name)
+                .isNull(StandardTree::getSampleType)
+                .isNull(StandardTree::getModel));
+        if (stdCount > 0) {
+            throw new BaseException("璇ヨ涓氫笅鏍囧噯鍚嶇О宸插瓨鍦�");
+        }
+        StandardTree tree = new StandardTree();
+        tree.setLaboratory(industry);
+        tree.setSample(name);
+        tree.setFactory(null);
+        tree.setSampleType(null);
+        tree.setModel(null);
+        tree.setSort(0);
+        return standardTreeMapper.insert(tree);
+        */
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public int delStandardTree(String tree) {
-        String[] trees = tree.split(" - ");
-        switch (trees.length) {
-            case 5:
-                if (trees[3].equals("null")) {
-                    standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).isNull(StandardTree::getSample).eq(StandardTree::getModel, trees[4]));
-                } else {
-                    standardTreeMapper.delete(Wrappers.<StandardTree>lambdaUpdate().eq(StandardTree::getFactory, trees[0]).eq(StandardTree::getLaboratory, trees[1]).eq(StandardTree::getSampleType, trees[2]).eq(StandardTree::getSample, trees[3]).eq(StandardTree::getModel, trees[4]));
-                }
-                break;
+        StandardTreePath path = StandardTreePathParser.parse(tree);
+        String industry = path.getIndustry();
+        String standard = path.getStandard();
+        if (standard != null) {
+            throw new BaseException("浜岀骇鏍囧噯璇峰湪鈥滄爣鍑嗙淮鎶も�濅腑閫氳繃琛屼笟缁戝畾杩涜缁存姢");
         }
+        if (standard == null) {
+            // 鍒犻櫎琛屼笟锛氳涓氫笅浠嶆湁鏍囧噯鏃舵嫆缁濆垹闄�
+            Long count = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
+                    .eq(StandardTree::getLaboratory, industry)
+                    .isNull(StandardTree::getSampleType)
+                    .isNull(StandardTree::getModel));
+            if (count > 0) {
+                throw new BaseException("璇ヨ涓氫笅浠嶆湁鏍囧噯锛岃鍏堝垹闄ゆ爣鍑�");
+            }
+            Laboratory laboratory = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery()
+                    .eq(Laboratory::getLaboratoryName, industry));
+            if (laboratory == null) {
+                throw new BaseException("琛屼笟涓嶅瓨鍦�");
+            }
+            Long objectCount = structureTestObjectMapper.selectCount(Wrappers.<StructureTestObject>lambdaQuery()
+                    .eq(StructureTestObject::getLaboratoryId, laboratory.getId()));
+            if (objectCount > 0) {
+                throw new BaseException("璇ヨ涓氬凡缁戝畾妫�娴嬪璞★紝绂佹鍒犻櫎");
+            }
+            int deleted = laboratoryMapper.delete(Wrappers.<Laboratory>lambdaQuery()
+                    .eq(Laboratory::getLaboratoryName, industry));
+            if (deleted == 0) {
+                throw new BaseException("琛屼笟涓嶅瓨鍦�");
+            }
+            return deleted;
+        }
+        // 鍒犻櫎鏍囧噯鍙婂叾鏍囧噯搴撻厤缃紙涓嶅彲杈撅紝淇濈暀鍘嗗彶瀹炵幇璇存槑锛�
+        StandardTree standardTree = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
+                .eq(StandardTree::getLaboratory, industry)
+                .eq(StandardTree::getSample, standard)
+                .isNull(StandardTree::getSampleType)
+                .isNull(StandardTree::getModel));
+        if (standardTree == null) {
+            throw new BaseException("鏍囧噯涓嶅瓨鍦�");
+        }
+        standardTreeTemplateBindingMapper.delete(Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
+                .eq(StandardTreeTemplateBinding::getStandardTreeId, standardTree.getId()));
+        standardTreeMapper.deleteById(standardTree.getId());
+        standardProductListMapper.delete(Wrappers.<StandardProductList>lambdaQuery()
+                .eq(StandardProductList::getLaboratory, industry)
+                .eq(StandardProductList::getSample, standard)
+                .isNull(StandardProductList::getSampleType)
+                .isNull(StandardProductList::getModel));
         return 1;
     }
 
@@ -135,359 +236,225 @@
     }
 
     @Override
-    public List<StandardTree> getStandTreeBySampleType(String laboratory, String sampleType) {
-        return standardTreeMapper.selectList(Wrappers.<StandardTree>lambdaQuery()
-                .eq(StandardTree::getLaboratory, laboratory)
-                .eq(StandardTree::getSampleType, sampleType)
-                .select(StandardTree::getModel, StandardTree::getSample));
-    }
-
-    /**
-     * 娌冲彛妫�娴嬩腑蹇冨鍏�
-     * @param file
-     */
-    @Override
     @Transactional(rollbackFor = Exception.class)
-    public void inExcelOfTree(MultipartFile file) {
-        InputStream inputStream;
-        try {
-            inputStream = file.getInputStream();
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-        List<StandardProductList> lists = new ArrayList<>();
-        AtomicReference<String> laboratory = new AtomicReference<>();
-        ExcelUtil.readBySax(inputStream, 0, (i, l, list) -> {
-            StandardProductList str = new StandardProductList();
-            if (i == 0 && l == 1) {
-                laboratory.set(standardTreeMapper.getLaboratory(list.get(1) + ""));
-                if (laboratory.get() == null) {
-                    throw new BaseException("妫�楠屽璞′笉瀛樺湪锛�" + list.get(1));
-                }
-            }
-            if (i == 0 && l >= 1) {
-                Integer standardMethodId = standardMethodListService.getStandardMethodId(list.get(0) + "");
-                if (standardMethodId == null) {
-                    throw new BaseException("鏍囧噯缂栧彿涓嶅瓨鍦細" + list.get(0));
-                }
-                str.setStandardMethodListId(standardMethodId);
-                str.setSampleType(list.get(1) + "");
-                if (list.get(2) != null) {
-                    String sample = baseMapper.selSample(list.get(2) + "");
-                    if (sample == null) {
-                        throw new BaseException("鏍峰搧涓嶅瓨鍦細" + list.get(2));
-                    }
-                    str.setSample(list.get(2) + "");
-                } else {
-                    str.setSample(null);
-                }
-                if (list.get(3) != null) {
-                    str.setModel(list.get(3) + "");
-                    Long aLong = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
-                            .eq(StandardTree::getModel, str.getModel())
-                            .eq(StandardTree::getSampleType, list.get(1) + ""));
-                    if (aLong == 0) {
-                        StandardTree standardTree = new StandardTree();
-                        standardTree.setFactory("涓ぉ绉戞妧妫�娴嬩腑蹇�");
-                        standardTree.setLaboratory(laboratory.get());
-                        standardTree.setSampleType(str.getSampleType());
-                        standardTree.setSample(str.getSample());
-                        standardTree.setModel(str.getModel());
-                        standardTreeMapper.insert(standardTree);
-                    }
-                } else {
-                    str.setModel(null);
-                }
-                str.setInspectionItem(list.get(4) + "");
-                if (list.get(6) == null) {
-                    str.setInspectionItemSubclass("");
-                } else {
-                    str.setInspectionItemSubclass(list.get(6).toString());
-                }
-                StandardProductList db_str;
-                try {
-                    db_str = standardProductListMapper.getOne(str.getStandardMethodListId(), str.getInspectionItem(), str.getSample(), str.getInspectionItemSubclass(), str.getModel(), str.getInspectionItemClass());
-                } catch (Exception e) {
-                    throw new BaseException("閲嶅鏌ヨ锛�" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
-                }
-                if (ObjectUtils.isNotEmpty(db_str)) {
-                    str.setId(db_str.getId());
-                    str.setStructureItemParameterId(db_str.getStructureItemParameterId());
-                }
-                if (list.get(8) != null) {
-                    str.setMethodS(list.get(8) + "");
-                }
+    public boolean updateTreeSort(List<StandardLibraryTreeNodeDto> list) {
+        List<StandardTree> trees = new ArrayList<>();
+        int industrySort = 0;
+        for (StandardLibraryTreeNodeDto industry : list) {
+            Laboratory lab = new Laboratory();
+            lab.setId(industry.getId());
+            lab.setSort(industrySort);
+            laboratoryMapper.updateById(lab);
+            industrySort++;
 
-                if (list.get(11) == null) {
-                    str.setTell(null);
-                } else {
-                    str.setTell(list.get(11).toString());
-                }
-
-                if (list.get(12) == null) {
-                    str.setAsk(null);
-                } else {
-                    str.setAsk(list.get(12).toString());
-                }
-
-                if (list.get(13) == null) {
-                    str.setPrice(null);
-                } else {
-                    str.setPrice((list.get(13).toString()));
-                }
-
-                if (list.get(14) == null) {
-                    str.setManHour(null);
-                } else {
-                    str.setManHour(list.get(14).toString());
-                }
-
-                StandardTemplate standTempIdByName = standardTemplateService.getStandTempIdByName(String.valueOf(list.get(21)));
-                if (standTempIdByName != null) {
-                    str.setTemplateId(standTempIdByName.getId());
-                } else {
-                    throw new BaseException("妯℃澘涓嶅瓨鍦細" + list.get(21));
-                }
-                str.setFactory("涓ぉ绉戞妧妫�娴嬩腑蹇�");
-                str.setLaboratory(laboratory.get());
-                str.setState(1);
-                str.setTree(str.getFactory() + " - " + str.getLaboratory() + " - " + str.getSampleType() + " - " + str.getSample() + " - " + (str.getModel() == null ? "" : str.getModel()));
-                try {
-                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
-                } catch (Exception e) {
-                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\",\"" + str.getSample() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
-                }
-                if (str.getStructureItemParameterId() == null) {
-                    throw new BaseException("妫�楠岄」鐩笉瀛樺湪锛�" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
-                }
-                lists.add(str);
-            }
-        });
-        lists.forEach(a -> {
-            if (a.getId() != null) {
-                standardProductListMapper.updateById(a);
-            } else {
-                standardProductListMapper.insert(a);
-            }
-        });
-    }
-
-    @Override
-    public void importWorkstationExcel(MultipartFile file) {
-        InputStream inputStream;
-        try {
-            inputStream = file.getInputStream();
-        } catch (IOException e) {
-            throw new RuntimeException(e);
-        }
-        List<StandardProductList> lists = new ArrayList<>();
-        AtomicReference<String> laboratory = new AtomicReference<>();
-        ExcelUtil.readBySax(inputStream, 0, (i, l, list) -> {
-            StandardProductList str = new StandardProductList();
-            if (i == 0 && l == 1) {
-                laboratory.set(standardTreeMapper.getLaboratory(list.get(1) + ""));
-                if (laboratory.get() == null) {
-                    throw new BaseException("妫�楠屽璞′笉瀛樺湪锛�" + list.get(1));
-                }
-            }
-            if (i == 0 && l >= 1) {
-                Integer standardMethodId = standardMethodListService.getStandardMethodId(list.get(0) + "");
-                if (standardMethodId == null) {
-                    throw new BaseException("鏍囧噯缂栧彿涓嶅瓨鍦細" + list.get(0));
-                }
-                str.setStandardMethodListId(standardMethodId);
-                str.setSampleType(list.get(1) + "");
-                if (list.get(2) != null) {
-                    String sample = baseMapper.selSample(list.get(2) + "");
-                    if (sample == null) {
-                        throw new BaseException("鏍峰搧涓嶅瓨鍦細" + list.get(2));
-                    }
-                    str.setSample(list.get(2) + "");
-                } else {
-                    str.setSample(null);
-                }
-                if (list.get(3) != null) {
-                    str.setModel(list.get(3) + "");
-                    Long aLong = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
-                            .eq(StandardTree::getModel, str.getModel())
-                            .eq(StandardTree::getSampleType, list.get(1) + ""));
-                    if (aLong == 0) {
-                        StandardTree standardTree = new StandardTree();
-                        standardTree.setFactory("涓ぉ绉戞妧妫�娴嬩腑蹇�");
-                        standardTree.setLaboratory(laboratory.get());
-                        standardTree.setSampleType(str.getSampleType());
-                        standardTree.setSample(str.getSample());
-                        standardTree.setModel(str.getModel());
-                        standardTreeMapper.insert(standardTree);
-                    }
-                } else {
-                    str.setModel(null);
-                }
-                str.setInspectionItemClass(list.get(4).toString());
-                str.setInspectionItemClassEn(list.get(5).toString());
-                // 寮�濮嬪姞2
-                str.setInspectionItem(list.get(6) + "");
-                if (list.get(8) == null) {
-                    str.setInspectionItemSubclass("");
-                } else {
-                    str.setInspectionItemSubclass(list.get(8).toString());
-                }
-                StandardProductList db_str;
-                try {
-                    db_str = standardProductListMapper.getOne(str.getStandardMethodListId(), str.getInspectionItem(), str.getSample(), str.getInspectionItemSubclass(), str.getModel(), str.getInspectionItemClass());
-                } catch (Exception e) {
-                    throw new BaseException("閲嶅鏌ヨ锛�" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
-                }
-                if (ObjectUtils.isNotEmpty(db_str)) {
-                    str.setId(db_str.getId());
-                    str.setStructureItemParameterId(db_str.getStructureItemParameterId());
-                }
-                if (list.get(10) != null) {
-                    str.setMethodS(list.get(10) + "");
-                }
-                if (list.get(13) == null) {
-                    str.setTell(null);
-                } else {
-                    str.setTell(list.get(13).toString());
-                }
-
-                if (list.get(14) == null) {
-                    str.setAsk(null);
-                } else {
-                    str.setAsk(list.get(14).toString());
-                }
-
-                if (list.get(15) == null) {
-                    str.setPrice(null);
-                } else {
-                    str.setPrice((list.get(15).toString()));
-                }
-
-                if (list.get(16) == null) {
-                    str.setManHour(null);
-                } else {
-                    str.setManHour(list.get(16).toString());
-                }
-
-                StandardTemplate standTempIdByName = standardTemplateService.getStandTempIdByName(String.valueOf(list.get(23)));
-                if (standTempIdByName != null) {
-                    str.setTemplateId(standTempIdByName.getId());
-                } else {
-                    throw new BaseException("妯℃澘涓嶅瓨鍦細" + list.get(23));
-                }
-                str.setFactory("涓ぉ绉戞妧妫�娴嬩腑蹇�");
-                str.setLaboratory(laboratory.get());
-                str.setState(1);
-                str.setTree(str.getFactory() + " - " + str.getLaboratory() + " - " + str.getSampleType() + " - " + str.getSample() + " - " + (str.getModel() == null ? "" : str.getModel()));
-                try {
-                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
-                } catch (Exception e) {
-                    str.setStructureItemParameterId(standardTreeMapper.getStructureItemParameterId("\"" + str.getSampleType() + "\",\"" + str.getSample() + "\"", str.getInspectionItem(), str.getInspectionItemSubclass(), str.getInspectionItemClass()));
-                }
-                if (str.getStructureItemParameterId() == null) {
-                    throw new BaseException("妫�楠岄」鐩笉瀛樺湪锛�" + str.getInspectionItem() + " " + str.getInspectionItemSubclass());
-                }
-                lists.add(str);
-            }
-        });
-        lists.forEach(a -> {
-            if (a.getId() != null) {
-                standardProductListMapper.updateById(a);
-            } else {
-                standardProductListMapper.insert(a);
-            }
-        });
-    }
-
-    @Override
-    public boolean updateTreeSort(List<FactoryDto> list) {
-        List<StructureTestObject> testObjects = new ArrayList<>();
-        for (FactoryDto factoryDto : list) {
-            for (LaboratoryDto laboratoryDto : factoryDto.getChildren()) {
-                List<SampleTypeDto> children = laboratoryDto.getChildren();
-                int sort = 0;
-                // 寰幆绗笁灞�
-                for (SampleTypeDto child : children) {
-                    StructureTestObject structureTestObject = new StructureTestObject();
-                    structureTestObject.setSort(sort);
-                    structureTestObject.setId(child.getSampleTypeId());
-                    testObjects.add(structureTestObject);
-                    sort++;
+            List<StandardLibraryTreeNodeDto> children = industry.getChildren();
+            if (children != null) {
+                int standardSort = 0;
+                for (StandardLibraryTreeNodeDto standard : children) {
+                    StandardTree tree = new StandardTree();
+                    tree.setId(standard.getId());
+                    tree.setSort(standardSort);
+                    trees.add(tree);
+                    standardSort++;
                 }
             }
         }
-        structureTestObjectService.updateBatchById(testObjects);
-
+        if (!trees.isEmpty()) {
+            this.updateBatchById(trees);
+        }
         return true;
     }
 
     /**
-     * 淇敼鏍囧噯鏁�
-     * @param standardTree
-     * @return
+     * 淇敼鏍囧噯鏍戣妭鐐瑰悕
      */
     @Override
-    public int updateStandardTree(StandardTree standardTree) {
-        // 淇敼鍚嶇О鍖归厤鐨勬爣鍑嗘爲涓嬬殑妫�楠岄」鐩�
-        // 鏌ヨ鎵�鏈夊璞�+鍚嶇О鐨勬爲
+    @Transactional(rollbackFor = Exception.class)
+    public int updateStandardTree(UpdateStandardTreeNodeDto dto) {
+        if (dto == null) {
+            throw new BaseException("鏍囧噯鏍戣矾寰勬牸寮忛敊璇�");
+        }
+        String name = StandardTreePathParser.normalizeNodeName(dto.getName());
+        String oldLabel = StandardTreePathParser.normalizeNodeName(dto.getOldLabel());
+        if (StringUtils.isBlank(dto.getParentTree())) {
+            return updateIndustry(oldLabel, name);
+        }
+        throw new BaseException("浜岀骇鏍囧噯璇峰湪鈥滄爣鍑嗙淮鎶も�濅腑閫氳繃琛屼笟缁戝畾杩涜缁存姢");
+    }
 
-        List<StandardProductList> standardProductLists = standardProductListService.list(Wrappers.<StandardProductList>lambdaUpdate()
-                .eq(StandardProductList::getSample, standardTree.getSample())
-                .eq(StandardProductList::getSampleType, standardTree.getSampleType())
-                .eq(StandardProductList::getModel, standardTree.getOldModel()));
-        if (CollectionUtils.isNotEmpty(standardProductLists)) {
-            for (StandardProductList standardProductList : standardProductLists) {
-                // 淇敼鏍峰搧鍚嶇О
-                standardProductList.setModel(standardTree.getModel());
-                // 淇敼鏍戝悕绉�
-                // 闇�瑕佹埅鍙栫鍥涚骇, 閬垮厤涓夊洓绾у悕绉颁竴鏍蜂慨鏀归敊璇�
-                String[] trees = standardProductList.getTree().split(" - ");
-                trees[4] = standardTree.getModel();
-                List<String> list = CollUtil.newArrayList(trees);
-                String newName = CollUtil.join(list, " - ");
-                standardProductList.setTree(newName);
+    private int updateIndustry(String oldName, String newName) {
+        if (Objects.equals(oldName, newName)) {
+            return ensureIndustryExists(oldName);
+        }
+        Long count = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
+                .eq(Laboratory::getLaboratoryName, newName));
+        if (count > 0) {
+            throw new BaseException("琛屼笟鍚嶇О宸插瓨鍦�");
+        }
+        Laboratory old = laboratoryMapper.selectOne(Wrappers.<Laboratory>lambdaQuery()
+                .eq(Laboratory::getLaboratoryName, oldName));
+        if (old == null) {
+            throw new BaseException("琛屼笟涓嶅瓨鍦�");
+        }
+        Laboratory update = new Laboratory();
+        update.setId(old.getId());
+        update.setLaboratoryName(newName);
+        if (laboratoryMapper.updateById(update) == 0) {
+            throw new BaseException("琛屼笟涓嶅瓨鍦�");
+        }
+        // 绾ц仈鏍囧噯鏍戣鑼冨寲璁板綍
+        standardTreeMapper.update(null, Wrappers.<StandardTree>lambdaUpdate()
+                .eq(StandardTree::getLaboratory, oldName)
+                .isNull(StandardTree::getSampleType)
+                .isNull(StandardTree::getModel)
+                .set(StandardTree::getLaboratory, newName));
+        // 绾ц仈鏍囧噯搴撻厤缃�
+        standardTreeMapper.updateProductListIndustry(oldName, newName);
+        // 绾ц仈鍩虹妫�楠岄」
+        structureItemParameterMapper.update(null, Wrappers.<StructureItemParameter>lambdaUpdate()
+                .eq(StructureItemParameter::getLaboratory, oldName)
+                .set(StructureItemParameter::getLaboratory, newName));
+        // 绾ц仈鏈畬鎴愯鍗�
+        standardTreeMapper.updateInsOrderIndustry(oldName, newName);
+        standardTreeMapper.updateInsSampleIndustry(oldName, newName);
+        standardTreeMapper.updateInsProductIndustry(oldName, newName);
+        return 1;
+    }
+
+    private int updateStandard(String industry, String oldName, String newName) {
+        if (Objects.equals(oldName, newName)) {
+            return ensureStandardExists(industry, oldName);
+        }
+        Long count = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
+                .eq(StandardTree::getLaboratory, industry)
+                .eq(StandardTree::getSample, newName)
+                .isNull(StandardTree::getSampleType)
+                .isNull(StandardTree::getModel));
+        if (count > 0) {
+            throw new BaseException("璇ヨ涓氫笅鏍囧噯鍚嶇О宸插瓨鍦�");
+        }
+        StandardTree old = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
+                .eq(StandardTree::getLaboratory, industry)
+                .eq(StandardTree::getSample, oldName)
+                .isNull(StandardTree::getSampleType)
+                .isNull(StandardTree::getModel));
+        if (old == null) {
+            throw new BaseException("鏍囧噯涓嶅瓨鍦�");
+        }
+        StandardTree update = new StandardTree();
+        update.setId(old.getId());
+        update.setSample(newName);
+        if (standardTreeMapper.updateById(update) == 0) {
+            throw new BaseException("鏍囧噯涓嶅瓨鍦�");
+        }
+        // 绾ц仈鏍囧噯搴撻厤缃�
+        standardTreeMapper.updateProductListStandard(industry, oldName, newName);
+        // 绾ц仈鏈畬鎴愯鍗�
+        standardTreeMapper.updateInsOrderStandard(industry, oldName, newName);
+        standardTreeMapper.updateInsSampleStandard(industry, oldName, newName);
+        standardTreeMapper.updateInsProductStandard(industry, oldName, newName);
+        return 1;
+    }
+
+    private int ensureIndustryExists(String industry) {
+        Long count = laboratoryMapper.selectCount(Wrappers.<Laboratory>lambdaQuery()
+                .eq(Laboratory::getLaboratoryName, industry));
+        if (count == 0) {
+            throw new BaseException("琛屼笟涓嶅瓨鍦�");
+        }
+        return 1;
+    }
+
+    private int ensureStandardExists(String industry, String standard) {
+        Long count = standardTreeMapper.selectCount(Wrappers.<StandardTree>lambdaQuery()
+                .eq(StandardTree::getLaboratory, industry)
+                .eq(StandardTree::getSample, standard)
+                .isNull(StandardTree::getSampleType)
+                .isNull(StandardTree::getModel));
+        if (count == 0) {
+            throw new BaseException("鏍囧噯涓嶅瓨鍦�");
+        }
+        return 1;
+    }
+
+    @Override
+    public List<StandardTreeTemplateBindingVo> selectTemplateBindings(Integer standardTreeId) {
+        ensureTwoLevelStandard(standardTreeId);
+        List<StandardTreeTemplateBinding> bindings = standardTreeTemplateBindingMapper.selectList(
+                Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
+                        .eq(StandardTreeTemplateBinding::getStandardTreeId, standardTreeId)
+                        .orderByAsc(StandardTreeTemplateBinding::getSort, StandardTreeTemplateBinding::getId));
+        List<StandardTreeTemplateBindingVo> result = new ArrayList<>();
+        for (StandardTreeTemplateBinding binding : bindings) {
+            StandardTemplate template = standardTemplateService.getById(binding.getTemplateId());
+            if (template == null) {
+                continue;
             }
-            standardProductListService.updateBatchById(standardProductLists);
+            StandardTreeTemplateBindingVo vo = new StandardTreeTemplateBindingVo();
+            vo.setTemplateId(template.getId());
+            vo.setName(template.getName());
+            vo.setNumber(template.getNumber());
+            vo.setRemark(template.getRemark());
+            vo.setSort(binding.getSort());
+            result.add(vo);
         }
+        return result;
+    }
 
-        // 淇敼鏍囧噯鏁版楠岄」鐩�
-        LambdaUpdateWrapper<StandardTree> wrapper = Wrappers.<StandardTree>lambdaUpdate()
-                .eq(StandardTree::getFactory, standardTree.getFactory())
-                .eq(StandardTree::getLaboratory, standardTree.getLaboratory())
-                .eq(StandardTree::getSampleType, standardTree.getSampleType())
-                .eq(StandardTree::getModel, standardTree.getOldModel())
-                .set(StandardTree::getModel, standardTree.getModel());
-        if (StringUtils.isNotBlank(standardTree.getSample())) {
-            wrapper.eq(StandardTree::getSample, standardTree.getSample());
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean saveTemplateBindings(StandardTreeTemplateBindingDto dto) {
+        if (dto == null || dto.getStandardTreeId() == null) {
+            throw new BaseException("鏍囧噯涓嶈兘涓虹┖");
         }
-        return standardTreeMapper.update(null, wrapper);
+        ensureTwoLevelStandard(dto.getStandardTreeId());
+        List<TemplateBindingItemDto> templates = dto.getTemplates() == null ? new ArrayList<>() : dto.getTemplates();
+        Set<Integer> templateIds = new HashSet<>();
+        List<StandardTreeTemplateBinding> bindings = new ArrayList<>();
+        int index = 0;
+        for (TemplateBindingItemDto item : templates) {
+            if (item == null || item.getTemplateId() == null || !templateIds.add(item.getTemplateId())) {
+                throw new BaseException("鍘熷璁板綍妯℃澘涓嶈兘閲嶅缁戝畾");
+            }
+            StandardTemplate template = standardTemplateService.getById(item.getTemplateId());
+            if (template == null) {
+                throw new BaseException("鍘熷璁板綍妯℃澘涓嶅瓨鍦�");
+            }
+            StandardTreeTemplateBinding binding = new StandardTreeTemplateBinding();
+            binding.setStandardTreeId(dto.getStandardTreeId());
+            binding.setTemplateId(item.getTemplateId());
+            binding.setSort(item.getSort() == null ? index : item.getSort());
+            bindings.add(binding);
+            index++;
+        }
+        standardTreeTemplateBindingMapper.delete(Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
+                .eq(StandardTreeTemplateBinding::getStandardTreeId, dto.getStandardTreeId()));
+        for (StandardTreeTemplateBinding binding : bindings) {
+            standardTreeTemplateBindingMapper.insert(binding);
+        }
+        return true;
     }
 
     @Override
-    public void resetTreeOfPrice(String tree, Integer standardId) {
-        standardProductListMapper.update(null, Wrappers.<StandardProductList>lambdaUpdate().like(StandardProductList::getTree, tree).eq(StandardProductList::getStandardMethodListId, standardId).set(StandardProductList::getPrice, null));
+    public List<TemplateInspectionItemDto> selectTemplateInspectionItems(Integer standardTreeId) {
+        List<StandardTreeTemplateBindingVo> bindings = selectTemplateBindings(standardTreeId);
+        List<TemplateInspectionItemDto> result = new ArrayList<>();
+        for (StandardTreeTemplateBindingVo binding : bindings) {
+            List<TemplateInspectionItemDto> items = StandardTemplateInspectionItemParser.parse(binding.getTemplateId(),
+                    binding.getSort(), standardTemplateService.getStandTempThingById(binding.getTemplateId()));
+            result.addAll(items);
+        }
+        result.sort(Comparator.comparing(TemplateInspectionItemDto::getTemplateSort)
+                .thenComparing(TemplateInspectionItemDto::getTemplateRowIndex));
+        return result;
     }
 
-    @Override
-    public void resetTreeOfHour(String tree, Integer standardId) {
-        standardProductListMapper.update(null, Wrappers.<StandardProductList>lambdaUpdate().like(StandardProductList::getTree, tree).eq(StandardProductList::getStandardMethodListId, standardId).set(StandardProductList::getManHour, null));
+    private StandardTree ensureTwoLevelStandard(Integer standardTreeId) {
+        StandardTree standardTree = this.getById(standardTreeId);
+        if (standardTree == null || StringUtils.isBlank(standardTree.getSample())
+                || standardTree.getSampleType() != null || standardTree.getModel() != null) {
+            throw new BaseException("璇烽�夋嫨鏍囧噯搴撲簩绾ф爣鍑�");
+        }
+        return standardTree;
     }
-
-    @Override
-    public void resetTreeOfAsk(String tree, Integer standardId) {
-        standardProductListMapper.update(null, Wrappers.<StandardProductList>lambdaUpdate().like(StandardProductList::getTree, tree).eq(StandardProductList::getStandardMethodListId, standardId)
-                .set(StandardProductList::getAsk, null)
-                .set(StandardProductList::getTell, null)
-                .set(StandardProductList::getSection, null)
-                .set(StandardProductList::getCores, null)
-                .set(StandardProductList::getConductorMaterial, null)
-                .set(StandardProductList::getConductorType, null)
-                .set(StandardProductList::getPrice, null)
-                .set(StandardProductList::getManHour, null));
-    }
-
 }
-
-
-
-

--
Gitblit v1.9.3