|
@@ -0,0 +1,231 @@
|
|
|
+package com.pavis.backend.slim.project.system.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson2.JSONArray;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import com.pavis.backend.slim.common.utils.CommonUtils;
|
|
|
+import com.pavis.backend.slim.common.utils.SnowflakeIdWorkerUtils;
|
|
|
+import com.pavis.backend.slim.project.system.client.AlgKgClient;
|
|
|
+import com.pavis.backend.slim.project.system.domain.AnnotationList;
|
|
|
+import com.pavis.backend.slim.project.system.domain.annotation.AnnotationListParam;
|
|
|
+import com.pavis.backend.slim.project.system.domain.annotation.ConvertAlgRes;
|
|
|
+import com.pavis.backend.slim.project.system.domain.annotation.ImportAnnotationDataset;
|
|
|
+import com.pavis.backend.slim.project.system.domain.annotation.ImportDataset;
|
|
|
+import com.pavis.backend.slim.project.system.mapper.AnnotationListMapper;
|
|
|
+import com.pavis.backend.slim.project.system.service.AnnotationListService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @version: java version 1.8
|
|
|
+ * @Author: Guan H.J.
|
|
|
+ * @description: AnnotationListServiceImpl
|
|
|
+ * @date: 2023-11-06 13:58
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper, AnnotationList> implements AnnotationListService {
|
|
|
+ @Autowired
|
|
|
+ private AlgKgClient algKgClient;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo getAnnotationLists(AnnotationListParam annotationListParam) {
|
|
|
+ List<AnnotationList> annotationLists = new ArrayList<>();
|
|
|
+ annotationLists = baseMapper.selectAnnotationList(annotationListParam);
|
|
|
+ // 获取txt文本内容为空,向算法请求txt文本内容并更新。
|
|
|
+ if (!CollectionUtils.isEmpty(annotationLists)) {
|
|
|
+ // 过滤出textContent为空的数据。
|
|
|
+ toAlgTxt(annotationLists);
|
|
|
+ annotationLists = baseMapper.selectAnnotationList(annotationListParam);
|
|
|
+ }
|
|
|
+ PageHelper.startPage(annotationListParam.getPageNum(), annotationListParam.getPageSize());
|
|
|
+ PageInfo page = new PageInfo(annotationLists);
|
|
|
+ return page;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean delAnnotationLists(List<String> ids) {
|
|
|
+ int delRes = baseMapper.deleteBatchIds(ids);
|
|
|
+ return delRes > 0 ? true : false;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean importAnnotationDataset(ImportAnnotationDataset importAnnotationDataset) {
|
|
|
+ List<Integer> saveResLists = new ArrayList<>();
|
|
|
+ List<ImportDataset> importDatasets = importAnnotationDataset.getImportDatasets();
|
|
|
+ List<AnnotationList> annotationLists = new ArrayList<>();
|
|
|
+ AnnotationList annotationList;
|
|
|
+ for (ImportDataset importDataset : importDatasets) {
|
|
|
+ annotationList = new AnnotationList();
|
|
|
+ BeanUtils.copyProperties(importDataset, annotationList);
|
|
|
+ annotationList.setKgId(importAnnotationDataset.getKgId());
|
|
|
+ // 如果是知识库中的数据集,则默认parseStatus=1 解析完成,调用算法获取txt内容。
|
|
|
+ if (1 == importAnnotationDataset.getImportType()) {
|
|
|
+ annotationList.setParseStatus(1);
|
|
|
+ annotationList.setAnnotationStatus(0);
|
|
|
+ List<AnnotationList> annotations = baseMapper.selectList(new QueryWrapper<AnnotationList>().lambda().eq(AnnotationList::getKgId, importAnnotationDataset.getKgId())
|
|
|
+ .eq(AnnotationList::getFileId, importDataset.getFileId()));
|
|
|
+ if (!CollectionUtils.isEmpty(annotations)){
|
|
|
+ annotationList.setId(annotations.get(0).getId());
|
|
|
+ int upRes = baseMapper.updateById(annotationList);
|
|
|
+ saveResLists.add(upRes);
|
|
|
+ }else {
|
|
|
+ int saveRes = baseMapper.insert(annotationList);
|
|
|
+ saveResLists.add(saveRes);
|
|
|
+ }
|
|
|
+ annotationLists.add(annotationList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 获取算法txt文本内容.
|
|
|
+ toAlgTxt(annotationLists);
|
|
|
+ // 返回数据。
|
|
|
+ log.info("saveResLists:{}",JSON.toJSONString(saveResLists));
|
|
|
+ if (saveResLists.contains(0)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean importLocalAnnotationDataset(String kgId,MultipartFile file) {
|
|
|
+ // todo 文件重复上传。知识库文档重复上传。
|
|
|
+ try {
|
|
|
+ String s = DigestUtils.md5Hex(file.getBytes());
|
|
|
+ log.info("file to MD5:{}",s);
|
|
|
+ }catch (Exception ex){
|
|
|
+
|
|
|
+ }
|
|
|
+ SnowflakeIdWorkerUtils idWorker = new SnowflakeIdWorkerUtils(0, 0);
|
|
|
+ AnnotationList annotationList = new AnnotationList();
|
|
|
+ annotationList.setFileId(String.valueOf(idWorker.nextId()));
|
|
|
+ annotationList.setKgId(kgId);
|
|
|
+ annotationList.setOriginalName(file.getOriginalFilename());
|
|
|
+ annotationList.setAnnotationStatus(0);
|
|
|
+ annotationList.setParseStatus(0);
|
|
|
+ Map<String, Object> algMap = new HashMap<>();
|
|
|
+ algMap.put("file",file);
|
|
|
+ algMap.put("fileId",annotationList.getFileId());
|
|
|
+ String algTxtByFile = algKgClient.getAlgTxtByFile(algMap);
|
|
|
+ log.info("算法algTxtByFile:{}",JSON.toJSONString(algTxtByFile));
|
|
|
+ List<AnnotationList> annotationLists = new ArrayList<>();
|
|
|
+ annotationLists.add(annotationList);
|
|
|
+ try {
|
|
|
+ dealAlgToTxtRes(algTxtByFile,annotationLists,false);
|
|
|
+ return true;
|
|
|
+ }catch (Exception ex){
|
|
|
+ log.info("算法本地文件转txt异常 ex:{}",ex);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用算法实时获取txt文本内容。
|
|
|
+ *
|
|
|
+ * @param annotationLists
|
|
|
+ */
|
|
|
+ public void toAlgTxt(List<AnnotationList> annotationLists) {
|
|
|
+ List<AnnotationList> hasNullTexts = annotationLists.stream().filter(wmy -> null == wmy.getTextContent()).collect(Collectors.toList());
|
|
|
+ log.info("hasNullTexts:{}", hasNullTexts.size());
|
|
|
+ List<String> fileIds = hasNullTexts.stream().map(wmy -> wmy.getFileId()).collect(Collectors.toList());
|
|
|
+ // 调用算法获取txt文本内容。
|
|
|
+ Map<String, Object> algMap = new HashMap<>();
|
|
|
+ algMap.put("fileIds", fileIds);
|
|
|
+ log.info("algMap:{}", JSON.toJSONString(algMap));
|
|
|
+ String algTxtTmp = algKgClient.getAlgTxt(JSON.toJSONString(algMap));
|
|
|
+ dealAlgToTxtRes(algTxtTmp, hasNullTexts,true);
|
|
|
+ log.info("实时调用算法获取txt文本内容结束");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dealAlgToTxtRes(String algTxtTmp, List<AnnotationList> hasNullTexts,Boolean upFlag) {
|
|
|
+ if (null != algTxtTmp && StringUtils.isNotEmpty(algTxtTmp) && CommonUtils.isJsonArray(algTxtTmp)) {
|
|
|
+ // 解析算法返回数据。
|
|
|
+ try {
|
|
|
+ List<ConvertAlgRes> convertAlgRes = JSONArray.parseArray(algTxtTmp, ConvertAlgRes.class);
|
|
|
+ if (!CollectionUtils.isEmpty(convertAlgRes)) {
|
|
|
+ for (ConvertAlgRes algRes : convertAlgRes) {
|
|
|
+ if (null != algRes.getErrorCode() && "0".equals(algRes.getErrorCode())) {
|
|
|
+ // 调用算法接口成功,并且解析txt文件成功,将文本内容更新至sysFile的textContent字段中。
|
|
|
+ for (AnnotationList hasNullText : hasNullTexts) {
|
|
|
+ if (algRes.getFileId().equals(hasNullText.getFileId())) {
|
|
|
+ hasNullText.setTextContent(algRes.getText());
|
|
|
+ if (upFlag){
|
|
|
+ // true 更新textContent.
|
|
|
+ int updateText = baseMapper.updateById(hasNullText);
|
|
|
+ log.info("更新文本内容结果updateText:{}", updateText);
|
|
|
+ }else {
|
|
|
+ // false 新增textContent
|
|
|
+ int saveRes = baseMapper.insert(hasNullText);
|
|
|
+ log.info("新增文本内容结果updateText:{}", saveRes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (null != algRes.getErrorCode() && "-1".equals(algRes.getErrorCode()) && false == upFlag){
|
|
|
+ for (AnnotationList hasNullText : hasNullTexts) {
|
|
|
+ hasNullText.setParseStatus(-1);
|
|
|
+ int saveRes = baseMapper.insert(hasNullText);
|
|
|
+ log.info("解析出错 新增文本内容结果updateText:{}", saveRes);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ log.info("获取算法解析txt文本内容返回结果 TXT未解析成功:{}", JSON.toJSONString(algRes));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果 返回数据为NULL ex convertAlgRes:{}", JSON.toJSONString(convertAlgRes));
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果 返回数据异常 NOT JSONArray ex:{}", JSON.toJSONString(algTxtTmp));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果接口异常 ex convertToTxtResTmp:{}", algTxtTmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void dealAlgToTxtResByLocal(String algTxtTmp, AnnotationList annotationList) {
|
|
|
+ if (null != algTxtTmp && StringUtils.isNotEmpty(algTxtTmp) && CommonUtils.isJsonArray(algTxtTmp)) {
|
|
|
+ // 解析算法返回数据。
|
|
|
+ try {
|
|
|
+ List<ConvertAlgRes> convertAlgRes = JSONArray.parseArray(algTxtTmp, ConvertAlgRes.class);
|
|
|
+ if (!CollectionUtils.isEmpty(convertAlgRes)) {
|
|
|
+ for (ConvertAlgRes algRes : convertAlgRes) {
|
|
|
+ if (null != algRes.getErrorCode() && "0".equals(algRes.getErrorCode())) {
|
|
|
+ // 调用算法接口成功,并且解析txt文件成功,将文本内容更新至sysFile的textContent字段中。
|
|
|
+
|
|
|
+ // for (AnnotationList hasNullText : hasNullTexts) {
|
|
|
+ // if (algRes.getFileId().equals(hasNullText.getFileId())) {
|
|
|
+ // hasNullText.setTextContent(algRes.getText());
|
|
|
+ // int updateText = baseMapper.updateById(hasNullText);
|
|
|
+ // log.info("更新文本内容结果updateText:{}", updateText);
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ } else {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果 TXT未解析成功:{}", JSON.toJSONString(algRes));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果 返回数据为NULL ex convertAlgRes:{}", JSON.toJSONString(convertAlgRes));
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果 返回数据异常 NOT JSONArray ex:{}", JSON.toJSONString(algTxtTmp));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ log.info("获取算法解析txt文本内容返回结果接口异常 ex convertToTxtResTmp:{}", algTxtTmp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|