|
|
@ -12,7 +12,9 @@ import com.project.information.domain.enums.InformationParseStatusEnum; |
|
|
import com.project.information.domain.service.GetStatisticsKnowledgePointDomainService; |
|
|
import com.project.information.domain.service.GetStatisticsKnowledgePointDomainService; |
|
|
import com.project.information.domain.service.InformationBaseService; |
|
|
import com.project.information.domain.service.InformationBaseService; |
|
|
import com.project.information.domain.service.KnowledgePointBaseService; |
|
|
import com.project.information.domain.service.KnowledgePointBaseService; |
|
|
|
|
|
import com.project.information.utils.MinIoUtils; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
|
import org.apache.commons.compress.utils.IOUtils; |
|
|
import org.apache.commons.lang3.ObjectUtils; |
|
|
import org.apache.commons.lang3.ObjectUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
@ -30,6 +32,7 @@ import org.springframework.util.MultiValueMap; |
|
|
import org.springframework.web.client.RestTemplate; |
|
|
import org.springframework.web.client.RestTemplate; |
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
@ -45,6 +48,9 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli |
|
|
@Autowired |
|
|
@Autowired |
|
|
private GetStatisticsKnowledgePointDomainService getStatisticsKnowledgePointDomainService; |
|
|
private GetStatisticsKnowledgePointDomainService getStatisticsKnowledgePointDomainService; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
|
|
private MinIoUtils minIoUtils; |
|
|
|
|
|
|
|
|
private final RestTemplate restTemplate = new RestTemplate(); |
|
|
private final RestTemplate restTemplate = new RestTemplate(); |
|
|
|
|
|
|
|
|
@Value("${analysis.host:http://172.16.204.50}") |
|
|
@Value("${analysis.host:http://172.16.204.50}") |
|
|
@ -61,32 +67,45 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@Async("asycExecutor") |
|
|
@Async("asycExecutor") |
|
|
public void parse(Map<Long, MultipartFile> fileMap) { |
|
|
public void parse(Map<Long, String> fileIdPathMap) { |
|
|
for (Long id : fileMap.keySet()) { |
|
|
|
|
|
|
|
|
for (Map.Entry<Long, String> entry : fileIdPathMap.entrySet()) { |
|
|
|
|
|
Long id = entry.getKey(); |
|
|
|
|
|
String path = entry.getValue(); |
|
|
try { |
|
|
try { |
|
|
uploadToPython(fileMap.get(id),id); |
|
|
// 从 MinIO 获取流再发给 Python
|
|
|
|
|
|
try (InputStream inputStream = minIoUtils.getObject(path)) { |
|
|
|
|
|
uploadToPython(inputStream, path , id); |
|
|
|
|
|
} |
|
|
|
|
|
// 成功后更新状态
|
|
|
|
|
|
updateStatusInformation(null, InformationParseStatusEnum.Success.getValue(), id); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
log.error("文件解析失败:{}", fileMap.get(id).getOriginalFilename(), e); |
|
|
log.error("解析异常, ID:{}", id, e); |
|
|
//更新资料状态
|
|
|
// 失败更新状态
|
|
|
updateStatuInformation(null,InformationParseStatusEnum.Success.getValue(),id); |
|
|
updateStatusInformation(null, InformationParseStatusEnum.Fail.getValue(), id); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 发送请求,解析文档知识点 |
|
|
* 发送请求,解析文档知识点 |
|
|
*/ |
|
|
*/ |
|
|
private void uploadToPython(MultipartFile file,Long id) throws Exception { |
|
|
private void uploadToPython(InputStream inputStream, String fileName, Long id) throws Exception { |
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
HttpHeaders headers = new HttpHeaders(); |
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
|
|
headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
|
|
headers.setAccept(MediaType.parseMediaTypes("*/*")); |
|
|
headers.setAccept(MediaType.parseMediaTypes("*/*")); |
|
|
|
|
|
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); |
|
|
|
|
|
|
|
|
ByteArrayResource fileResource = new ByteArrayResource(file.getBytes()) { |
|
|
// 1. 将 InputStream 转换为字节数组 (注意:此处会消耗内存,大小由文件决定)
|
|
|
|
|
|
byte[] bytes = IOUtils.toByteArray(inputStream); |
|
|
|
|
|
|
|
|
|
|
|
// 2. 封装为 Resource
|
|
|
|
|
|
ByteArrayResource fileResource = new ByteArrayResource(bytes) { |
|
|
@Override |
|
|
@Override |
|
|
public String getFilename() { |
|
|
public String getFilename() { |
|
|
return file.getOriginalFilename(); |
|
|
return fileName; // 使用传入的文件名
|
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
@ -95,23 +114,26 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli |
|
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = |
|
|
HttpEntity<MultiValueMap<String, Object>> requestEntity = |
|
|
new HttpEntity<>(body, headers); |
|
|
new HttpEntity<>(body, headers); |
|
|
|
|
|
|
|
|
|
|
|
// 3. 发送请求
|
|
|
|
|
|
log.info("正在发起文件 [{}] 的解析请求,ID: {}", fileName, id); |
|
|
ResponseEntity<String> response = |
|
|
ResponseEntity<String> response = |
|
|
restTemplate.postForEntity(analysisHost+":"+analysisPort+analysisUrl, requestEntity, String.class); |
|
|
restTemplate.postForEntity(analysisHost + ":" + analysisPort + analysisUrl, requestEntity, String.class); |
|
|
|
|
|
|
|
|
|
|
|
log.info("文件 [{}] 解析请求成功,返回结果:{}", fileName, response.getBody()); |
|
|
|
|
|
|
|
|
log.info("文件 [{}] 解析请求成功,返回结果:{}", file.getOriginalFilename(), response.getBody()); |
|
|
// 4. 解析 Python 返回的结果并入库
|
|
|
|
|
|
List<KnowledgePointEntity> knowledgePointEntities = parseAnalysisResponse(response, id); |
|
|
|
|
|
|
|
|
List<KnowledgePointEntity> knowledgePointEntities = parseAnalysisResponse(response,id); |
|
|
if (!CollectionUtils.isEmpty(knowledgePointEntities)) { |
|
|
if(!CollectionUtils.isEmpty(knowledgePointEntities)){ |
|
|
|
|
|
knowledgePointBaseService.saveBatch(knowledgePointEntities); |
|
|
knowledgePointBaseService.saveBatch(knowledgePointEntities); |
|
|
//更新状态
|
|
|
// 更新资料状态为:成功
|
|
|
updateStatuInformation(knowledgePointEntities.get(0).getParseName(),InformationParseStatusEnum.Success.getValue(),id); |
|
|
updateStatusInformation(knowledgePointEntities.get(0).getParseName(), InformationParseStatusEnum.Success.getValue(), id); |
|
|
}else{ |
|
|
} else { |
|
|
log.error("获取知识点失败:{}", "不存在知识点"); |
|
|
log.warn("文件 [{}] 解析成功但未提取到知识点", fileName); |
|
|
//更新状态
|
|
|
// 更新资料状态为:成功 (或根据业务定义为无知识点状态)
|
|
|
updateStatuInformation(null,InformationParseStatusEnum.Success.getValue(),id); |
|
|
updateStatusInformation(null, InformationParseStatusEnum.Success.getValue(), id); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 解析返回数据 |
|
|
* 解析返回数据 |
|
|
*/ |
|
|
*/ |
|
|
@ -178,7 +200,7 @@ public class KnowledgePointApplicationServiceImpl implements KnowledgePointAppli |
|
|
/** |
|
|
/** |
|
|
* 更新文件解析状态 |
|
|
* 更新文件解析状态 |
|
|
*/ |
|
|
*/ |
|
|
private void updateStatuInformation(String pareName,Integer status,Long id){ |
|
|
private void updateStatusInformation(String pareName,Integer status,Long id){ |
|
|
InformationEntity informationEntity = new InformationEntity(); |
|
|
InformationEntity informationEntity = new InformationEntity(); |
|
|
informationEntity.setId(id); |
|
|
informationEntity.setId(id); |
|
|
if (StringUtils.isNotBlank(pareName)){ |
|
|
if (StringUtils.isNotBlank(pareName)){ |
|
|
|