liding
2 天以前 bd46f203b0d30a3d33b7c51ae9a528e4807477e1
basic-server/src/main/java/com/ruoyi/basic/service/impl/StandardMethodServiceImpl.java
@@ -12,21 +12,36 @@
import com.ruoyi.basic.mapper.StandardMethodMapper;
import com.ruoyi.basic.mapper.StandardProductListMapper;
import com.ruoyi.basic.mapper.StructureItemParameterMapper;
import com.ruoyi.basic.mapper.StandardMethodIndustryMapper;
import com.ruoyi.basic.mapper.StandardMethodAttachmentMapper;
import com.ruoyi.basic.mapper.LaboratoryMapper;
import com.ruoyi.basic.mapper.StandardTreeMapper;
import com.ruoyi.basic.mapper.StandardTreeTemplateBindingMapper;
import com.ruoyi.basic.pojo.StandardMethod;
import com.ruoyi.basic.pojo.StandardMethodIndustry;
import com.ruoyi.basic.pojo.StandardMethodAttachment;
import com.ruoyi.basic.pojo.Laboratory;
import com.ruoyi.basic.pojo.StandardTree;
import com.ruoyi.basic.pojo.StandardTreeTemplateBinding;
import com.ruoyi.basic.pojo.StandardProductList;
import com.ruoyi.basic.pojo.StructureItemParameter;
import com.ruoyi.basic.service.StandardMethodService;
import com.ruoyi.basic.service.StandardProductListService;
import com.ruoyi.basic.service.StructureItemParameterService;
import lombok.AllArgsConstructor;
import com.ruoyi.basic.utils.StandardTreePath;
import com.ruoyi.basic.utils.StandardTreePathParser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.exception.base.BaseException;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
/**
 * @author Administrator
@@ -34,7 +49,6 @@
 * @createDate 2024-03-03 19:21:41
 */
@Service
@AllArgsConstructor
public class StandardMethodServiceImpl extends ServiceImpl<StandardMethodMapper, StandardMethod>
        implements StandardMethodService {
@@ -47,9 +61,51 @@
    private StructureItemParameterMapper structureItemParameterMapper;
    private StructureItemParameterService structureItemParameterService;
    private StandardMethodIndustryMapper standardMethodIndustryMapper;
    private StandardMethodAttachmentMapper standardMethodAttachmentMapper;
    private LaboratoryMapper laboratoryMapper;
    private StandardTreeMapper standardTreeMapper;
    private StandardTreeTemplateBindingMapper standardTreeTemplateBindingMapper;
    @Value("${file.path}")
    private String filePath;
    @Value("${wordUrl}")
    private String wordUrl;
    /**
     * 业务依赖由构造器注入;文件路径由 @Value 从配置读取,不能参与 Lombok 的全参构造器,
     * 否则 Spring 会把 String 误认为需要注入的 Bean。
     */
    public StandardMethodServiceImpl(StandardMethodMapper standardMethodMapper,
                                     StandardProductListMapper standardProductListMapper,
                                     StandardProductListService standardProductListService,
                                     StructureItemParameterMapper structureItemParameterMapper,
                                     StructureItemParameterService structureItemParameterService,
                                     StandardMethodIndustryMapper standardMethodIndustryMapper,
                                     StandardMethodAttachmentMapper standardMethodAttachmentMapper,
                                     LaboratoryMapper laboratoryMapper,
                                     StandardTreeMapper standardTreeMapper,
                                     StandardTreeTemplateBindingMapper standardTreeTemplateBindingMapper) {
        this.standardMethodMapper = standardMethodMapper;
        this.standardProductListMapper = standardProductListMapper;
        this.standardProductListService = standardProductListService;
        this.structureItemParameterMapper = structureItemParameterMapper;
        this.structureItemParameterService = structureItemParameterService;
        this.standardMethodIndustryMapper = standardMethodIndustryMapper;
        this.standardMethodAttachmentMapper = standardMethodAttachmentMapper;
        this.laboratoryMapper = laboratoryMapper;
        this.standardTreeMapper = standardTreeMapper;
        this.standardTreeTemplateBindingMapper = standardTreeTemplateBindingMapper;
    }
    @Override
    public IPage<StandardMethod> selectStandardMethodList(Page page, StandardMethod standardMethod) {
        return standardMethodMapper.selectStandardMethodList(page, QueryWrappers.queryWrappers(standardMethod));
        IPage<StandardMethod> result = standardMethodMapper.selectStandardMethodList(page, QueryWrappers.queryWrappers(standardMethod));
        for (StandardMethod method : result.getRecords()) {
            method.setIndustryIds(selectIndustryIds(method.getId()));
        }
        return result;
    }
    @Override
@@ -58,25 +114,169 @@
    }
    @Override
    public int addStandardMethod(StandardMethod standardMethod) {
        int insert = standardMethodMapper.insert(standardMethod);
        return insert;
    @Transactional(rollbackFor = Exception.class)
    public Integer addStandardMethod(StandardMethod standardMethod) {
        standardMethod.setStructureTestObjectId(null);
        standardMethodMapper.insert(standardMethod);
        syncIndustries(standardMethod, Collections.<Integer>emptySet());
        return standardMethod.getId();
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delStandardMethod(Integer id) {
        int i = standardMethodMapper.deleteById(id);
        return i;
        StandardMethod method = standardMethodMapper.selectById(id);
        if (method == null) {
            throw new BaseException("标准不存在");
        }
        for (Integer industryId : selectIndustryIds(id)) {
            removeIndustryTree(method, industryId);
        }
        standardMethodIndustryMapper.delete(Wrappers.<StandardMethodIndustry>lambdaQuery()
                .eq(StandardMethodIndustry::getStandardMethodId, id));
        standardMethodAttachmentMapper.delete(Wrappers.<StandardMethodAttachment>lambdaQuery()
                .eq(StandardMethodAttachment::getStandardMethodId, id));
        return standardMethodMapper.deleteById(id);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upStandardMethod(StandardMethod standardMethod) {
        StandardMethod oldStandardMethod = standardMethodMapper.selectById(standardMethod.getId());
        if (oldStandardMethod == null) {
            throw new BaseException("标准不存在");
        }
        if (!oldStandardMethod.getCode().equals(standardMethod.getCode())) {
            CompletableFuture.supplyAsync(() -> replaceMethod(oldStandardMethod.getCode(), standardMethod.getCode()));
        }
        int i = standardMethodMapper.updateById(standardMethod);
        syncIndustries(standardMethod, new LinkedHashSet<>(selectIndustryIds(oldStandardMethod.getId())));
        return i;
    }
    private List<Integer> selectIndustryIds(Integer methodId) {
        List<StandardMethodIndustry> list = standardMethodIndustryMapper.selectList(
                Wrappers.<StandardMethodIndustry>lambdaQuery()
                        .eq(StandardMethodIndustry::getStandardMethodId, methodId));
        List<Integer> ids = new ArrayList<>();
        for (StandardMethodIndustry item : list) {
            ids.add(item.getIndustryId());
        }
        return ids;
    }
    private void syncIndustries(StandardMethod method, Set<Integer> oldIds) {
        Set<Integer> newIds = method.getIndustryIds() == null ? new LinkedHashSet<Integer>()
                : new LinkedHashSet<>(method.getIndustryIds());
        for (Integer industryId : newIds) {
            if (industryId == null || laboratoryMapper.selectById(industryId) == null) {
                throw new BaseException("行业不存在");
            }
        }
        for (Integer oldId : oldIds) {
            if (!newIds.contains(oldId)) {
                removeIndustryTree(method, oldId);
                standardMethodIndustryMapper.delete(Wrappers.<StandardMethodIndustry>lambdaQuery()
                        .eq(StandardMethodIndustry::getStandardMethodId, method.getId())
                        .eq(StandardMethodIndustry::getIndustryId, oldId));
            }
        }
        for (Integer industryId : newIds) {
            Laboratory lab = laboratoryMapper.selectById(industryId);
            Long count = standardMethodIndustryMapper.selectCount(Wrappers.<StandardMethodIndustry>lambdaQuery()
                    .eq(StandardMethodIndustry::getStandardMethodId, method.getId())
                    .eq(StandardMethodIndustry::getIndustryId, industryId));
            if (count == 0) {
                StandardMethodIndustry relation = new StandardMethodIndustry();
                relation.setStandardMethodId(method.getId());
                relation.setIndustryId(industryId);
                standardMethodIndustryMapper.insert(relation);
            }
            StandardTree tree = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
                    .eq(StandardTree::getLaboratory, lab.getLaboratoryName())
                    .eq(StandardTree::getStandardMethodId, method.getId())
                    .isNull(StandardTree::getSampleType).isNull(StandardTree::getModel));
            if (tree == null) {
                tree = new StandardTree();
                tree.setLaboratory(lab.getLaboratoryName());
                tree.setSample(buildLabel(method));
                tree.setStandardMethodId(method.getId());
                tree.setSort(0);
                standardTreeMapper.insert(tree);
            } else if (!buildLabel(method).equals(tree.getSample())) {
                StandardTree update = new StandardTree();
                update.setId(tree.getId());
                update.setSample(buildLabel(method));
                standardTreeMapper.updateById(update);
            }
        }
    }
    private void removeIndustryTree(StandardMethod method, Integer industryId) {
        Laboratory lab = laboratoryMapper.selectById(industryId);
        if (lab == null) return;
        StandardTree tree = standardTreeMapper.selectOne(Wrappers.<StandardTree>lambdaQuery()
                .eq(StandardTree::getLaboratory, lab.getLaboratoryName())
                .eq(StandardTree::getStandardMethodId, method.getId())
                .isNull(StandardTree::getSampleType).isNull(StandardTree::getModel));
        if (tree != null) {
            standardTreeTemplateBindingMapper.delete(Wrappers.<StandardTreeTemplateBinding>lambdaQuery()
                    .eq(StandardTreeTemplateBinding::getStandardTreeId, tree.getId()));
            standardProductListMapper.delete(Wrappers.<StandardProductList>lambdaQuery()
                    .eq(StandardProductList::getLaboratory, lab.getLaboratoryName())
                    .eq(StandardProductList::getSample, tree.getSample())
                    .isNull(StandardProductList::getSampleType).isNull(StandardProductList::getModel));
            standardTreeMapper.deleteById(tree.getId());
        }
    }
    private String buildLabel(StandardMethod method) {
        return method.getCode().trim();
    }
    @Override
    public List<StandardMethodAttachment> selectAttachments(Integer standardMethodId) {
        return standardMethodAttachmentMapper.selectList(Wrappers.<StandardMethodAttachment>lambdaQuery()
                .eq(StandardMethodAttachment::getStandardMethodId, standardMethodId)
                .orderByAsc(StandardMethodAttachment::getSort, StandardMethodAttachment::getId));
    }
    @Override
    public StandardMethodAttachment uploadAttachment(Integer standardMethodId, MultipartFile file) {
        if (standardMethodMapper.selectById(standardMethodId) == null) throw new BaseException("标准不存在");
        if (file == null || file.isEmpty() || file.getSize() > 10 * 1024 * 1024)
            throw new BaseException("附件不能为空且不能超过10MB");
        String originalName = file.getOriginalFilename();
        if (originalName == null || !originalName.matches("(?i).+\\.(jpg|jpeg|png|gif|doc|docx|xls|xlsx|ppt|pptx|pdf|zip|rar)$"))
            throw new BaseException("不支持的附件格式");
        try {
            boolean image = originalName.matches("(?i).+\\.(jpg|jpeg|png|gif)$");
            File directory = new File(image ? filePath : wordUrl);
            if (!directory.exists()) directory.mkdirs();
            String fileUrl = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS")) + "-" + originalName;
            file.transferTo(new File(directory, fileUrl));
            StandardMethodAttachment attachment = new StandardMethodAttachment();
            attachment.setStandardMethodId(standardMethodId);
            attachment.setFileName(originalName);
            attachment.setFileUrl(fileUrl);
            attachment.setSort(selectAttachments(standardMethodId).size());
            standardMethodAttachmentMapper.insert(attachment);
            return attachment;
        } catch (IOException e) {
            throw new BaseException("附件上传失败");
        }
    }
    @Override
    public boolean deleteAttachment(Long id) {
        return standardMethodAttachmentMapper.deleteById(id) > 0;
    }
    @Override
    public List<Laboratory> selectIndustryOptions() {
        return laboratoryMapper.selectList(Wrappers.<Laboratory>lambdaQuery()
                .select(Laboratory::getId, Laboratory::getLaboratoryName, Laboratory::getSort)
                .orderByAsc(Laboratory::getSort, Laboratory::getId));
    }
    //编辑method后全部替换
@@ -172,33 +372,16 @@
        if (tree == null || tree.trim().isEmpty()) {
            return Collections.emptyMap();
        }
        String[] trees = tree.split(" - ");
        Map<String, List<?>> map = new HashMap<>();
        String str = "";
        List<StandardMethodList> standardMethodLists = new ArrayList<>();
        switch (trees.length){
            case 5:
                str += "\"" + trees[2] + "\",\"" + trees[3] + "\",\"" + trees[4] + "\"";
                standardMethodLists.addAll(standardMethodMapper.selectStandardMethodLists(str));
                standardMethodLists.addAll(standardMethodMapper.selectStandardMethodLists("\"" + trees[2] + "\",\"" + trees[3] + "\""));
                standardMethodLists.addAll(standardMethodMapper.selectStandardMethodLists("\"" + trees[2] + "\""));
                break;
            case 4:
                str += "\"" + trees[2] + "\",\"" + trees[3] + "\"";
                standardMethodLists.addAll(standardMethodMapper.selectStandardMethodLists(str));
                standardMethodLists.addAll(standardMethodMapper.selectStandardMethodLists("\"" + trees[2] + "\""));
                break;
            case 3:
                str += "\"" + trees[2] + "\"";
                standardMethodLists.addAll(standardMethodMapper.selectStandardMethodLists3(str));
                break;
            default:
                map.put("standardMethodList", null);
                return map;
        StandardTreePath path = StandardTreePathParser.parse(tree);
        String industry = path.getIndustry();
        List<StandardMethodList> standardMethodLists = new ArrayList<>(standardMethodMapper.selectStandardMethodListsByIndustry(industry));
        // 按 ID 去重,保持稳定排序
        Map<Integer, StandardMethodList> byId = new LinkedHashMap<>();
        for (StandardMethodList m : standardMethodLists) {
            byId.putIfAbsent(m.getId(), m);
        }
        standardMethodLists.addAll(standardMethodMapper.selectStandardMethodListsByNull(str));
        List<StandardMethodList> collect = standardMethodLists.stream().distinct().collect(Collectors.toList());
        map.put("standardMethodList", collect);
        Map<String, List<?>> map = new HashMap<>();
        map.put("standardMethodList", new ArrayList<>(byId.values()));
        return map;
    }