| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.basic.pojo.StandardMethodList; |
| | | import com.ruoyi.common.utils.QueryWrappers; |
| | | 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; |
| | | |
| | |
| | | * @createDate 2024-03-03 19:21:41 |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class StandardMethodServiceImpl extends ServiceImpl<StandardMethodMapper, StandardMethod> |
| | | implements StandardMethodService { |
| | | |
| | | |
| | | private StandardMethodMapper standardMethodMapper; |
| | | |
| | | StandardProductListMapper standardProductListMapper; |
| | | StandardProductListService standardProductListService; |
| | | private StandardProductListMapper standardProductListMapper; |
| | | private StandardProductListService standardProductListService; |
| | | |
| | | StructureItemParameterMapper structureItemParameterMapper; |
| | | StructureItemParameterService structureItemParameterService; |
| | | 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 |
| | |
| | | } |
| | | |
| | | @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后全部替换 |
| | |
| | | return "替换完毕!"; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | @Override |
| | | public void inputExcel(MultipartFile file) throws IOException { |
| | | // 存储检测对象List |
| | | List<Object> structureTestObjectIdList = new ArrayList<>(); |
| | | List<StandardMethod> result = new ArrayList<>(); |
| | | ExcelUtil.readBySax(file.getInputStream(), 0, (i, l, list) -> { |
| | | // 去除第一行表头 |
| | | if (l == 0) { |
| | | return; |
| | | } |
| | | // 存储唯一检测对象 |
| | | if (!structureTestObjectIdList.contains(list.get(2))) { |
| | | structureTestObjectIdList.add(list.get(2)); |
| | | } |
| | | StandardMethod standardMethod = formatData(list); |
| | | result.add(standardMethod); |
| | | }); |
| | | addStructureTest(structureTestObjectIdList, result); |
| | | } |
| | | |
| | | // 格式化数据 |
| | | public StandardMethod formatData(List<Object> list) { |
| | |
| | | return standardMethod; |
| | | } |
| | | |
| | | // 新增数据 |
| | | public void addStructureTest(List<Object> structureTestObjectIdList, List<StandardMethod> standardMethodList) { |
| | | List<StandardMethod> updateList = new ArrayList<>(); |
| | | List<Integer> deleteListId = new ArrayList<>(); |
| | | List<StandardMethod> addList = new ArrayList<>(); |
| | | if (!structureTestObjectIdList.isEmpty()) { |
| | | // 循环excel里面的分组 |
| | | structureTestObjectIdList.forEach(j -> { |
| | | // 以excel中的组名查询数据库中的分组 |
| | | List<StandardMethod> standardMethods = baseMapper.selectList(Wrappers.<StandardMethod>lambdaQuery() |
| | | .like(StandardMethod::getStructureTestObjectId, "\"" + j + "\"")); |
| | | // 将结果循环匹配 |
| | | for (int i = 0; i < standardMethods.size(); i++) { |
| | | boolean isExistence = false; |
| | | for (int i1 = 0; i1 < standardMethodList.size(); i1++) { |
| | | // 更新 |
| | | if (standardMethods.get(i).getStructureTestObjectId().equals(standardMethodList.get(i1).getStructureTestObjectId()) |
| | | && standardMethods.get(i).getCode().equals(standardMethodList.get(i1).getCode()) |
| | | && standardMethods.get(i).getField().equals(standardMethodList.get(i1).getField())) { |
| | | // 给excel数据赋值id做更新 |
| | | standardMethodList.get(i1).setId(standardMethods.get(i).getId()); |
| | | // 更新 |
| | | updateList.add(standardMethodList.get(i1)); |
| | | isExistence = true; |
| | | break; |
| | | } |
| | | } |
| | | // 删除 |
| | | if (!isExistence) { |
| | | deleteListId.add(standardMethods.get(i).getId()); |
| | | } |
| | | } |
| | | for (int i = 0; i < standardMethodList.size(); i++) { |
| | | if (standardMethodList.get(i).getStructureTestObjectId().contains("\"" + j + "\"")) { |
| | | boolean isExistence = false; |
| | | for (int i1 = 0; i1 < standardMethods.size(); i1++) { |
| | | if (standardMethods.get(i1).getStructureTestObjectId().equals(standardMethodList.get(i).getStructureTestObjectId()) |
| | | && standardMethods.get(i1).getCode().equals(standardMethodList.get(i).getCode()) |
| | | && standardMethods.get(i1).getField().equals(standardMethodList.get(i).getField())) { |
| | | isExistence = true; |
| | | break; |
| | | } |
| | | } |
| | | // 新增 |
| | | if (!isExistence) { |
| | | addList.add(standardMethodList.get(i)); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | @Override |
| | | public Map<String, List<?>> selectsStandardMethodByFLSSM(String tree) { |
| | | if (tree == null || tree.trim().isEmpty()) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | if (!addList.isEmpty()) { |
| | | // 新增 |
| | | baseMapper.insertBatchSomeColumn(addList); |
| | | 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); |
| | | } |
| | | |
| | | if (!deleteListId.isEmpty()) { |
| | | // 删除 |
| | | baseMapper.deleteBatchIds(deleteListId); |
| | | } |
| | | |
| | | if (!updateList.isEmpty()) { |
| | | // 更新 |
| | | updateList.forEach(i -> { |
| | | baseMapper.updateById(i); |
| | | }); |
| | | } |
| | | Map<String, List<?>> map = new HashMap<>(); |
| | | map.put("standardMethodList", new ArrayList<>(byId.values())); |
| | | return map; |
| | | } |
| | | |
| | | @Override |
| | | public List<StandardMethodList> selectStandardMethodEnum() { |
| | | return standardMethodMapper.selectListEnum(); |
| | | } |
| | | |
| | | } |
| | | |
| | | |