From 30f8c39fe175a48eee61e7559692abb1d1c07729 Mon Sep 17 00:00:00 2001 From: luogw <3132758203@qq.com> Date: Mon, 13 Apr 2026 09:30:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BAid=E7=B2=BE=E5=BA=A6=E7=BC=BA?= =?UTF-8?q?=E5=A4=B1=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../KnowledgePointApplicationServiceImpl.java | 6 +- .../UploadInformationDomainServiceImpl.java | 110 +++++++++++------- 2 files changed, 69 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java b/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java index d3c94bd..29c9037 100644 --- a/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java +++ b/src/main/java/com/project/information/application/impl/KnowledgePointApplicationServiceImpl.java @@ -84,8 +84,6 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli try (InputStream inputStream = minIoUtils.getObject(path)) { uploadToPython(inputStream, filename , id); } - // 成功后更新状态 - updateStatusInformation(null, InformationParseStatusEnum.InProgress.getValue(), id); } catch (Exception e) { log.error("解析异常, ID:{}", id, e); @@ -143,11 +141,11 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli if (!CollectionUtils.isEmpty(knowledgePointEntities)) { knowledgePointBaseService.saveBatch(knowledgePointEntities); // 更新资料状态为:成功 - updateStatusInformation(knowledgePointEntities.get(0).getParseName(), InformationParseStatusEnum.InProgress.getValue(), id); + updateStatusInformation(knowledgePointEntities.get(0).getParseName(), InformationParseStatusEnum.Success.getValue(), id); } else { log.warn("文件 [{}] 解析成功但未提取到知识点", fileName); // 更新资料状态为:成功 (或根据业务定义为无知识点状态) - updateStatusInformation(null, InformationParseStatusEnum.InProgress.getValue(), id); + updateStatusInformation(null, InformationParseStatusEnum.Success.getValue(), id); } } /** diff --git a/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java b/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java index 5452bc9..93c7ed8 100644 --- a/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java +++ b/src/main/java/com/project/information/domain/service/impl/UploadInformationDomainServiceImpl.java @@ -5,16 +5,13 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.util.IdUtil; -import cn.hutool.db.handler.StringHandler; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.project.base.config.CustomIdGenerator; import com.project.base.domain.exception.BusinessErrorException; import com.project.base.domain.result.Result; import com.project.information.application.KnowledgePointApplicationService; import com.project.information.domain.dto.InformationDTO; -import com.project.information.domain.dto.KnowledgePointDTO; import com.project.information.domain.entity.InformationEntity; -import com.project.information.domain.entity.KnowledgePointEntity; import com.project.information.domain.entity.ProductLineEntity; import com.project.information.domain.enums.InformationParseStatusEnum; import com.project.information.domain.param.FileCheckItem; @@ -29,12 +26,14 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; -import java.io.InputStream; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; @Service public class UploadInformationDomainServiceImpl implements UploadInformationDomainService { + private final ConcurrentHashMap subLineUploadLocks = new ConcurrentHashMap<>(); @Autowired private InformationBaseService informationBaseService; @@ -108,49 +107,74 @@ public class UploadInformationDomainServiceImpl implements UploadInformationDoma } - List successFiles = new ArrayList<>(); - Map fileMap = new HashMap<>(); - Map fileNameMap = new HashMap<>(); - for (MultipartFile file : files) { - String fileName = file.getOriginalFilename(); + ReentrantLock lock = subLineUploadLocks.computeIfAbsent(subLineId, key -> new ReentrantLock()); + lock.lock(); + try { + List sortedFiles = Arrays.stream(files) + .sorted(Comparator.comparing(file -> Optional.ofNullable(file.getOriginalFilename()).orElse(""))) + .toList(); + + List inputFileNames = sortedFiles.stream() + .map(MultipartFile::getOriginalFilename) + .filter(Objects::nonNull) + .toList(); + Set existNameSet = new HashSet<>(); + if (CollUtil.isNotEmpty(inputFileNames)) { + existNameSet = informationBaseService.list(new LambdaQueryWrapper() + .select(InformationEntity::getName) + .eq(InformationEntity::getSubLineId, subLineId) + .in(InformationEntity::getName, inputFileNames)) + .stream() + .map(InformationEntity::getName) + .collect(java.util.stream.Collectors.toSet()); + } - LambdaQueryWrapper wrapper = new LambdaQueryWrapper() - .eq(InformationEntity::getSubLineId, subLineId) - .eq(InformationEntity::getName, fileName); - long count = informationBaseService.count(wrapper); - //如果存在重复的,并且是跳过则不做处理 - if (count > 0 && skipExisting) { - continue; + List successFiles = new ArrayList<>(); + Map fileMap = new HashMap<>(); + Map fileNameMap = new HashMap<>(); + for (MultipartFile file : sortedFiles) { + String fileName = file.getOriginalFilename(); + if (Objects.isNull(fileName)) { + continue; + } + if (existNameSet.contains(fileName) && skipExisting) { + continue; + } + + LambdaQueryWrapper wrapper = new LambdaQueryWrapper() + .eq(InformationEntity::getSubLineId, subLineId) + .eq(InformationEntity::getName, fileName); + informationBaseService.remove(wrapper); + String filePath = String.format("%s/%s/%s.%s", + subLineId, + DateUtil.format(new Date(), "yyyyMMdd"), + IdUtil.fastSimpleUUID(), + FileNameUtil.extName(fileName)); + minIoUtils.uploadFile(file.getInputStream() , filePath); + successFiles.add(fileName); + InformationDTO informationDTO = new InformationDTO(); + informationDTO.setSubLineId(subLineId); + informationDTO.setSubLineName(productLine.getName()); + informationDTO.setName(fileName); + informationDTO.setFileSuffix(FileUtil.getSuffix(fileName)); + informationDTO.setFilePath(filePath); + informationDTO.setParseStatus(InformationParseStatusEnum.NotStart.getValue()); + InformationEntity entity = informationDTO.toEntity(InformationEntity::new); + entity.setId(customIdGenerator.nextId(entity)); + informationBaseService.save(entity); + + //收集file + fileMap.put(entity.getId(), filePath); + fileNameMap.put(entity.getId(), fileName); } - // 删掉原来的 - informationBaseService.remove(wrapper); - String filePath = String.format("%s/%s/%s.%s", - subLineId, - DateUtil.format(new Date(), "yyyyMMdd"), - IdUtil.fastSimpleUUID(), - FileNameUtil.extName(fileName)); - minIoUtils.uploadFile(file.getInputStream() , filePath); - successFiles.add(fileName); - InformationDTO informationDTO = new InformationDTO(); - informationDTO.setSubLineId(subLineId); - informationDTO.setSubLineName(productLine.getName()); - informationDTO.setName(fileName); - informationDTO.setFileSuffix(FileUtil.getSuffix(fileName)); - informationDTO.setFilePath(filePath); - informationDTO.setParseStatus(InformationParseStatusEnum.NotStart.getValue()); - InformationEntity entity = informationDTO.toEntity(InformationEntity::new); - entity.setId(customIdGenerator.nextId(entity)); - informationBaseService.save(entity); - - //收集file - fileMap.put(entity.getId(), filePath); - fileNameMap.put(entity.getId(), fileName); - } - //发起解析文档知识点 - knowledgePointApplicationService.parse(fileMap,fileNameMap); + //发起解析文档知识点 + knowledgePointApplicationService.parse(fileMap,fileNameMap); - return Result.success(String.format("上传成功:【%s】" , String.join("," , successFiles))); + return Result.success(String.format("上传成功:【%s】" , String.join("," , successFiles))); + } finally { + lock.unlock(); + } } }