Ver Fonte

新增接口:根据fileIds去批量获取解析的txt文本内容;新增接口:本地传file给算法,解析txt;知识库文档添加接口调整调用算法获取txt解析内容;获取标注数据列表;标注数据增删改查;发送标注数据给算法服务,生成图谱;将标注数据给算法后,状态为200再次调用算法获取taskId;

guanz há 1 ano atrás
pai
commit
35b2eeef6b
29 ficheiros alterados com 939 adições e 75 exclusões
  1. 18 3
      src/main/java/com/pavis/backend/slim/project/system/client/AlgKgClient.java
  2. 48 4
      src/main/java/com/pavis/backend/slim/project/system/controller/AnnotationController.java
  3. 13 1
      src/main/java/com/pavis/backend/slim/project/system/domain/AnnotationList.java
  4. 79 0
      src/main/java/com/pavis/backend/slim/project/system/domain/GenerateGraph.java
  5. 45 0
      src/main/java/com/pavis/backend/slim/project/system/domain/SysKg.java
  6. 7 2
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationData.java
  7. 11 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationEntity.java
  8. 11 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationEntityRelation.java
  9. 31 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationFront.java
  10. 3 1
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationToAlg.java
  11. 5 1
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ConvertAlgRes.java
  12. 4 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ConvertDocToTxt.java
  13. 3 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ConvertDocToTxtAlg.java
  14. 41 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/DelAnnotation.java
  15. 31 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/GenerateGraphResult.java
  16. 1 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ImportAnnotationDataset.java
  17. 3 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ToAnnotation.java
  18. 5 0
      src/main/java/com/pavis/backend/slim/project/system/domain/annotation/UniversalGgraphToAlg.java
  19. 20 0
      src/main/java/com/pavis/backend/slim/project/system/mapper/GenerateGraphMapper.java
  20. 8 0
      src/main/java/com/pavis/backend/slim/project/system/service/AnnotationListService.java
  21. 7 4
      src/main/java/com/pavis/backend/slim/project/system/service/EntityLablesRelationService.java
  22. 8 1
      src/main/java/com/pavis/backend/slim/project/system/service/EntityLablesSpanService.java
  23. 28 0
      src/main/java/com/pavis/backend/slim/project/system/service/GenerateGraphService.java
  24. 9 1
      src/main/java/com/pavis/backend/slim/project/system/service/SysKgService.java
  25. 59 29
      src/main/java/com/pavis/backend/slim/project/system/service/impl/AnnotationListServiceImpl.java
  26. 90 24
      src/main/java/com/pavis/backend/slim/project/system/service/impl/EntityLabelsRelationServiceImpl.java
  27. 70 2
      src/main/java/com/pavis/backend/slim/project/system/service/impl/EntityLabelsSpanServiceImpl.java
  28. 258 0
      src/main/java/com/pavis/backend/slim/project/system/service/impl/GenerateGraphServiceImpl.java
  29. 23 2
      src/main/java/com/pavis/backend/slim/project/system/service/impl/SysKgServiceImpl.java

+ 18 - 3
src/main/java/com/pavis/backend/slim/project/system/client/AlgKgClient.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.client;
 
+import com.alibaba.fastjson2.JSONArray;
 import com.pavis.backend.slim.framework.config.AlgKgConfig;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.http.MediaType;
@@ -17,7 +18,7 @@ import java.util.Map;
 public interface AlgKgClient {
 
     /**
-     * 发送标注数据给算法服务,生成图谱。
+     * 发送标注数据给算法服务,生成图谱。状态为200,调用下一个接口:入参kgId、fileIds等。
      * url: http://192.168.1.150:12000/labels/import
      *
      * @param data
@@ -28,13 +29,22 @@ public interface AlgKgClient {
             consumes = "application/json")
     String sendToAlgForGenerateKgGraph(String data);
 
+    /**
+     * todo 将标注数据给算法后,状态为200再次调用算法获取taskId。
+     * 入参:kgId、fielIds、type[true 通用;false 标注。]
+     * @param data
+     * @return
+     */
+    @PostMapping(value = "/graph/create_graph", produces = {MediaType.APPLICATION_JSON_VALUE},
+            consumes = "application/json")
+    String afterSendAnnotationToGetTaskId(String data);
+
     /**
      * 发送文档需转换为txt的数据给算法服务。
      *
      * @param data
      * @return
      */
-    // @PostMapping(value = "/annotation/convertToTxt",produces = {MediaType.APPLICATION_JSON_VALUE},
     @PostMapping(value = "/data_import/data_access", produces = {MediaType.APPLICATION_JSON_VALUE},
             consumes="application/json")
     String convertToTxt(String data);
@@ -49,10 +59,15 @@ public interface AlgKgClient {
             consumes="application/json")
     String getAlgTxt(String data);
 
+    /**
+     * 传本地上传的文件,调用算法实时获取文本内容。
+     * @param params
+     * @return
+     */
     @PostMapping(value = "/data_import/upload_file",
             consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
             produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
-    String getAlgTxtByFile(Map<String, ?> params);
+    JSONArray getAlgTxtByFile(Map<String, ?> params);
 
 
     /**

+ 48 - 4
src/main/java/com/pavis/backend/slim/project/system/controller/AnnotationController.java

@@ -4,13 +4,17 @@ import com.alibaba.fastjson2.JSON;
 import com.pavis.backend.slim.framework.web.domain.AjaxResult;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntity;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntityRelation;
+import com.pavis.backend.slim.project.system.domain.annotation.AnnotationFront;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationListParam;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationToAlg;
+import com.pavis.backend.slim.project.system.domain.annotation.DelAnnotation;
+import com.pavis.backend.slim.project.system.domain.annotation.GenerateGraphResult;
 import com.pavis.backend.slim.project.system.domain.annotation.ImportAnnotationDataset;
 import com.pavis.backend.slim.project.system.domain.annotation.UniversalGgraphToAlg;
 import com.pavis.backend.slim.project.system.service.AnnotationListService;
 import com.pavis.backend.slim.project.system.service.EntityLablesRelationService;
 import com.pavis.backend.slim.project.system.service.EntityLablesSpanService;
+import com.pavis.backend.slim.project.system.service.GenerateGraphService;
 import com.pavis.backend.slim.project.system.service.SysFileService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiImplicitParam;
@@ -57,6 +61,9 @@ public class AnnotationController {
     @Autowired
     private AnnotationListService annotationListService;
 
+    @Autowired
+    private GenerateGraphService generateGraphService;
+
     @ApiOperation("获取标注数据列表接口")
     @PostMapping("/getAnnotationLists")
     public AjaxResult getAnnotationLists(@RequestBody AnnotationListParam annotationListParam) {
@@ -64,7 +71,7 @@ public class AnnotationController {
         if (null != annotationListParam.getKgId() && StringUtils.isNotEmpty(annotationListParam.getKgId()) && null != annotationListParam.getPageNum() && null != annotationListParam.getPageSize()) {
             return AjaxResult.success("获取数据标注列表成功", annotationListService.getAnnotationLists(annotationListParam));
         }
-        return AjaxResult.error("参数不能为空");
+        return AjaxResult.error(1001,"参数不能为空");
     }
 
     @ApiOperation("删除标注数据列表接口")
@@ -79,7 +86,7 @@ public class AnnotationController {
                 return AjaxResult.error("删除标注数据列表失败", delRes);
             }
         }
-        return AjaxResult.error("参数不能为空");
+        return AjaxResult.error(1001,"参数不能为空");
     }
 
     @ApiOperation("数据集导入之知识库文档导入接口")
@@ -94,7 +101,7 @@ public class AnnotationController {
                 return AjaxResult.error("数据集导入失败", saveRes);
             }
         }
-        return AjaxResult.error("参数不能为空");
+        return AjaxResult.error(1001,"参数不能为空");
     }
 
     @ApiOperation("数据集导入之本地文件导入接口")
@@ -110,10 +117,12 @@ public class AnnotationController {
                 return AjaxResult.error("文件不能为空");
             }
         }else {
-            return AjaxResult.error("参数不能为空");
+            return AjaxResult.error(1001,"参数不能为空");
         }
     }
 
+
+
     // 数据集的导入和导出。
 
 
@@ -144,6 +153,29 @@ public class AnnotationController {
         return AjaxResult.success("实体间关系保存成功", entityLablesRelationService.saveAnnotationRelation(relation));
     }
 
+    /**
+     * 文档标注实体数据删除接口,传主键ID。
+     * 文档标注实体间关系数据删除接口,传主键ID。
+     * @param delAnnotation
+     * @return
+     */
+    @ApiOperation("文档标注实体&实体间关系数据删除接口")
+    @PostMapping("/del/entity")
+    public AjaxResult delAnnotationEntity(@RequestBody DelAnnotation delAnnotation) {
+        return AjaxResult.success("文档标注实体数据删除成功", entityLablesSpanService.delAnnotation(delAnnotation));
+    }
+
+
+    @ApiOperation("文档标注最终实体数据存储接口")
+    @PostMapping("/save/annotation")
+    public AjaxResult saveAnnotation(@RequestBody AnnotationFront annotationFront) {
+        if (null != annotationFront.getKgId() && null != annotationFront.getFileId() && StringUtils.isNotEmpty(annotationFront.getKgId()) && StringUtils.isNotEmpty(annotationFront.getFileId())) {
+            return AjaxResult.error(1001,"参数不能为空");
+        }
+        return AjaxResult.success("文档标注最终实体数据保存成功", annotationListService.saveAnnotation(annotationFront));
+    }
+
+
     @ApiOperation("去标注接口")
     @ApiImplicitParam(name = "fileId", value = "文档id")
     @GetMapping("/toAnnotation")
@@ -173,6 +205,18 @@ public class AnnotationController {
         return AjaxResult.error(1001, "参数不能为空");
     }
 
+    @ApiOperation("算法通知后端更新生成图谱结果接口")
+    @PostMapping("/generateKgGraphResult")
+    public AjaxResult generateKgGraphResult(@RequestBody GenerateGraphResult genGraphResult) {
+        log.info("算法通知后端更新生成图谱结果genGraphResult:{}",JSON.toJSONString(genGraphResult));
+        if (null != genGraphResult.getTaskId() && StringUtils.isNotEmpty(genGraphResult.getTaskId())) {
+            log.info("---> taskId:{}", genGraphResult.getTaskId());
+            generateGraphService.generateKgGraphResult(genGraphResult);
+            return AjaxResult.success("算法通知后端更新生成图谱结果成功");
+        }
+        return AjaxResult.error(1001, "参数不能为空");
+    }
+
 
     // ----------------------------以下接口均为模拟算法测试接口,后续可删除下列接口------------------------------------。
 

+ 13 - 1
src/main/java/com/pavis/backend/slim/project/system/domain/AnnotationList.java

@@ -33,7 +33,7 @@ public class AnnotationList extends BaseEntity {
     private Long id;
 
     /**
-     * 知识库所属用户ID。
+     * 图谱ID。
      */
     @ApiModelProperty(value = "图谱ID",required = true)
     private String kgId;
@@ -62,10 +62,22 @@ public class AnnotationList extends BaseEntity {
     @ApiModelProperty("文档转txt内容")
     private String textContent;
 
+    /**
+     * 标注数据,即前端传过来的标注数据html。
+     */
+    @ApiModelProperty("标注数据")
+    private String textFront;
+
     /**
      * 文档是否被标注,0 未标注 1 已标注。
      */
     @ApiModelProperty("文档标注状态")
     private Integer annotationStatus;
 
+    // /**
+    //  * 用户id
+    //  */
+    // @ApiModelProperty("用户id")
+    // private Long userId;
+
 }

+ 79 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/GenerateGraph.java

@@ -0,0 +1,79 @@
+package com.pavis.backend.slim.project.system.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.pavis.backend.slim.framework.web.domain.BaseEntity;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 生成图谱结果列表
+ * @date: 2023-11-08 13:47
+ */
+@Builder
+@Data
+@Getter
+@Setter
+@NoArgsConstructor
+@AllArgsConstructor
+public class GenerateGraph extends BaseEntity {
+    private static final long serialVersionUID = 1L;
+    /**
+     * 主键id。
+     */
+    @ApiModelProperty("主键id")
+    @TableId(type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 图谱ID。
+     */
+    @ApiModelProperty(value = "任务ID",required = true)
+    private String taskId;
+
+    /**
+     * 图谱ID。
+     */
+    @ApiModelProperty(value = "图谱ID",required = true)
+    private String kgId;
+
+    @ApiModelProperty(value = "任务类型,true 通用图谱;false 标注数据",required = true)
+    private String taskType;
+
+    @ApiModelProperty("解析txt任务进行状态名称,如:解析、训练、抽取。")
+    private String parseTaskName;
+
+    @ApiModelProperty("解析txt任务进行状态结果,如:true false。")
+    private Integer parseTaskStatus;
+
+    @ApiModelProperty("解析txt任务失败原因")
+    private String parseTaskReason;
+
+
+    @ApiModelProperty("训练txt任务进行状态名称,如:解析、训练、抽取。")
+    private String trainTaskName;
+
+    @ApiModelProperty("训练txt任务进行状态结果,如:true false。")
+    private Integer trainTaskStatus;
+
+    @ApiModelProperty("训练txt任务失败原因")
+    private String trainTaskReason;
+
+
+    @ApiModelProperty("抽取txt任务进行状态名称,如:解析、训练、抽取。")
+    private String extractTaskName;
+
+    @ApiModelProperty("抽取txt任务进行状态结果,如:true false。")
+    private Integer extractTaskStatus;
+
+    @ApiModelProperty("抽取txt任务失败原因")
+    private String extractTaskReason;
+
+}

+ 45 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/SysKg.java

@@ -55,6 +55,19 @@ public class SysKg extends BaseEntity {
     @TableField("files_copy")
     private String filesCopy;
 
+    @ApiModelProperty("图模型状态")
+    @TableField("graph_status")
+    private Integer graphStatus;
+    @ApiModelProperty("是否标注状态")
+    @TableField("annotation_status")
+    private Integer annotationStatus;
+    @ApiModelProperty("生成图谱的状态")
+    @TableField("generate_graph_status")
+    private Integer generateGraphStatus;
+    @ApiModelProperty("生成图谱失败的原因")
+    @TableField("generate_graph_reason")
+    private String generateGraphReason;
+
 
     public String getKgId() {
         return kgId;
@@ -104,6 +117,38 @@ public class SysKg extends BaseEntity {
         this.profile = profile;
     }
 
+    public Integer getGraphStatus() {
+        return graphStatus;
+    }
+
+    public void setGraphStatus(Integer graphStatus) {
+        this.graphStatus = graphStatus;
+    }
+
+    public Integer getAnnotationStatus() {
+        return annotationStatus;
+    }
+
+    public void setAnnotationStatus(Integer annotationStatus) {
+        this.annotationStatus = annotationStatus;
+    }
+
+    public Integer getGenerateGraphStatus() {
+        return generateGraphStatus;
+    }
+
+    public void setGenerateGraphStatus(Integer generateGraphStatus) {
+        this.generateGraphStatus = generateGraphStatus;
+    }
+
+    public String getGenerateGraphReason() {
+        return generateGraphReason;
+    }
+
+    public void setGenerateGraphReason(String generateGraphReason) {
+        this.generateGraphReason = generateGraphReason;
+    }
+
     @Override
     public String toString() {
         return "SysKg{" +

+ 7 - 2
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationData.java

@@ -1,8 +1,7 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
-import com.pavis.backend.slim.framework.web.domain.BaseEntity;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
-import lombok.Builder;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
@@ -23,9 +22,15 @@ import java.util.List;
 @NoArgsConstructor
 public class AnnotationData {
     // 文章内容。如:乒乓球、羽毛球、网球。
+    @ApiModelProperty("文档txt内容")
     private String text;
+    // 前端标注实体数据存储内容。
+    @ApiModelProperty("标注实体数据文本")
+    private String textFront;
     // 标注的实体。
+    @ApiModelProperty("标注的实体数据列表")
     private List<AnnotationEntity> entities;
     // 标注的实体关系。
+    @ApiModelProperty("标注的实体关系数据列表")
     private List<AnnotationEntityRelation> relations;
 }

+ 11 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationEntity.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -19,23 +20,33 @@ import lombok.Setter;
 @AllArgsConstructor
 public class AnnotationEntity {
     // 唯一标识主键id。
+    @ApiModelProperty("主键ID")
     private Long id;
     // 图谱ID。
+    @ApiModelProperty("图谱ID")
     private String kgId;
     // 文档关联id
+    @ApiModelProperty("文档ID")
     private String fileId;
     // 开始标注长度。
+    @ApiModelProperty("开始标注长度")
     private String startOffset;
     // 结束标注长度:需要当前长度+1。
+    @ApiModelProperty("结束标注长度")
     private String endOffset;
     // 标签实体的类型即id,如:51、52。
+    @ApiModelProperty("标签实体的类型即id,如:51、52。")
     private String entityId;
     // 标签实体的类型即id,如:运动员、国家。
+    @ApiModelProperty("标签实体的类型即id,如:运动员、国家。")
     private String name;
     // 标注实体文本内容。
+    @ApiModelProperty("标注实体文本内容")
     private String nameValue;
     // 用户id。
+    @ApiModelProperty("用户ID")
     private Long userId;
     // 唯一标识ID。
+    @ApiModelProperty("唯一标识ID")
     private String uuid;
 }

+ 11 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationEntityRelation.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -19,23 +20,33 @@ import lombok.Setter;
 @NoArgsConstructor
 public class AnnotationEntityRelation {
     // 唯一标识主键id。
+    @ApiModelProperty("唯一标识主键ID")
     private Long id;
     // 图谱ID。
+    @ApiModelProperty("图谱ID")
     private String kgId;
     // 文档关联id
+    @ApiModelProperty("文档关联ID")
     private String fileId;
     // 开始实体ID。
+    @ApiModelProperty("开始实体ID")
     private String fromId;
     // 结束实体ID。
+    @ApiModelProperty("结束实体ID")
     private String toId;
     // 标签实体关系的类型即id,如:13。
+    @ApiModelProperty("标签实体关系的类型即id,如:13。")
     private String relationId;
     // 标签实体关系的类型即id,如:获得、参加、是、国籍等。
+    @ApiModelProperty("标签实体关系的类型即id,如:获得、参加、是、国籍等。")
     private String name;
     // 标注实体关系文本内容。
+    @ApiModelProperty("标注实体关系文本内容。")
     private String nameValue;
     // 用户id。
+    @ApiModelProperty("用户ID")
     private Long userId;
     // 唯一标识ID。
+    @ApiModelProperty("唯一标识ID")
     private String uuid;
 }

+ 31 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationFront.java

@@ -0,0 +1,31 @@
+package com.pavis.backend.slim.project.system.domain.annotation;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.experimental.SuperBuilder;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 标注信息转换数据结构给算法使用。
+ * @date: 2023-10-26 9:47
+ */
+@SuperBuilder
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class AnnotationFront {
+    // 前端标注的实体数据。
+    @ApiModelProperty("前端标注的实体数据")
+    private String textFront;
+    // 图谱ID
+    @ApiModelProperty("图谱ID")
+    private String kgId;
+    // 文档ID。
+    @ApiModelProperty("文档ID")
+    private String fileId;
+}

+ 3 - 1
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/AnnotationToAlg.java

@@ -1,6 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
-import com.pavis.backend.slim.framework.web.domain.BaseEntity;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -22,7 +22,9 @@ import java.util.List;
 @AllArgsConstructor
 public class AnnotationToAlg {
     // 唯一标识主键id。
+    @ApiModelProperty("唯一标识主键ID")
     private String kgId;
     // 该知识库下所有标注的实体、实体关系、文章内容数据。如:体育库下的10个乒乓球文档标注、3个羽毛球文档标注、7个网球文档标注都要给算法。
+    @ApiModelProperty("该知识库下所有标注的实体、实体关系、文章内容数据")
     private List<AnnotationData> annotationDatas; // 即一共三个list:乒乓球、羽毛球、网球。
 }

+ 5 - 1
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ConvertAlgRes.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -18,10 +19,13 @@ import lombok.Setter;
 @NoArgsConstructor
 @AllArgsConstructor
 public class ConvertAlgRes {
-    // 错误代码,如果errorCode=0 表示正常解析,可以获取文本内容;非零即其他情况,均有各种解析异常,无法获取文本内容
+    // 错误代码,如果errorCode=0 表示正常解析,可以获取文本内容;非零即其他情况,均有各种解析异常,无法获取文本内容。
+    @ApiModelProperty("错误代码")
     private String errorCode;
     // 文件ID。
+    @ApiModelProperty("文件ID")
     private String fileId;
     // 转换后的txt文本内容。
+    @ApiModelProperty("转换后的txt文本内容")
     private String text;
 }

+ 4 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ConvertDocToTxt.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -19,9 +20,12 @@ import lombok.Setter;
 @AllArgsConstructor
 public class ConvertDocToTxt {
     // 文件ID。
+    @ApiModelProperty("文件ID")
     private String fileId;
     // 文件在oss中的key值。
+    @ApiModelProperty("文件在oss中的key值")
     private String objectKey;
     // 文件md5值。
+    @ApiModelProperty("文件md5值")
     private String identifier;
 }

+ 3 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ConvertDocToTxtAlg.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Getter;
@@ -21,7 +22,9 @@ import java.util.List;
 @AllArgsConstructor
 public class ConvertDocToTxtAlg {
     // 算法需要根据此key获取到文件列表,即:fileId、objectKey、identifier。
+    @ApiModelProperty("算法需要根据此key获取到文件列表,即:fileId、objectKey、identifier")
     private String files;
     // 待转换的文件列表。
+    @ApiModelProperty("待转换的文件列表")
     private List<ConvertDocToTxt> comments;
 }

+ 41 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/DelAnnotation.java

@@ -0,0 +1,41 @@
+package com.pavis.backend.slim.project.system.domain.annotation;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.experimental.SuperBuilder;
+
+import java.util.List;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 标注信息转换数据结构给算法使用。
+ * @date: 2023-10-26 9:47
+ */
+@SuperBuilder
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class DelAnnotation {
+    @ApiModelProperty("删除的标签类型,1 实体 0 关系")
+    private String delType;
+
+    /**
+     * 图谱ID。
+     */
+    @ApiModelProperty(value = "图谱ID",required = true)
+    private String kgId;
+
+    /**
+     * 文档ID。
+     */
+    @ApiModelProperty(value = "文档ID",required = true)
+    private String fileId;
+
+    @ApiModelProperty("图谱ID")
+    private List<String> ids;
+}

+ 31 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/GenerateGraphResult.java

@@ -0,0 +1,31 @@
+package com.pavis.backend.slim.project.system.domain.annotation;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+import lombok.experimental.SuperBuilder;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: 标注信息转换数据结构给算法使用。
+ * @date: 2023-10-26 9:47
+ */
+@SuperBuilder
+@Getter
+@Setter
+@AllArgsConstructor
+@NoArgsConstructor
+public class GenerateGraphResult {
+    @ApiModelProperty("生成图谱任务ID")
+    private String taskId;
+    @ApiModelProperty("生成图谱构建状态名称")
+    private String taskName;
+    @ApiModelProperty("生成图谱构建状态结果")
+    private Boolean taskStatus;
+    @ApiModelProperty("生成图谱构建失败原因")
+    private String msg;
+
+}

+ 1 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ImportAnnotationDataset.java

@@ -29,5 +29,6 @@ public class ImportAnnotationDataset {
     private Integer importType;
 
     // 导入数据集列表。
+    @ApiModelProperty("导入数据集列表")
     List<ImportDataset> importDatasets;
 }

+ 3 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/ToAnnotation.java

@@ -2,6 +2,7 @@ package com.pavis.backend.slim.project.system.domain.annotation;
 
 import com.pavis.backend.slim.project.system.domain.SysEntity;
 import com.pavis.backend.slim.project.system.domain.SysRelation;
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
@@ -23,7 +24,9 @@ import java.util.List;
 @NoArgsConstructor
 public class ToAnnotation extends AnnotationData{
     // 该知识库下的所有实体信息。
+    @ApiModelProperty("该知识库下的所有实体信息")
     private List<SysEntity> entityList;
     // 该知识库下的所有实体间关系信息。
+    @ApiModelProperty("该知识库下的所有实体间关系信息")
     private List<SysRelation> relationList;
 }

+ 5 - 0
src/main/java/com/pavis/backend/slim/project/system/domain/annotation/UniversalGgraphToAlg.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.domain.annotation;
 
+import io.swagger.annotations.ApiModelProperty;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
@@ -21,7 +22,11 @@ import java.util.List;
 @NoArgsConstructor
 public class UniversalGgraphToAlg {
     // 图谱ID。
+    @ApiModelProperty("图谱ID")
     private String kgId;
     // 该图谱下的所有文档ID列表。
+    @ApiModelProperty("该图谱下的所有文档ID列表")
     private List<String> fileIds;
+    @ApiModelProperty("调用接口的类型,true 通用图谱;false 标注数据")
+    private Boolean taskType;
 }

+ 20 - 0
src/main/java/com/pavis/backend/slim/project/system/mapper/GenerateGraphMapper.java

@@ -0,0 +1,20 @@
+package com.pavis.backend.slim.project.system.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.pavis.backend.slim.project.system.domain.AnnotationList;
+import com.pavis.backend.slim.project.system.domain.GenerateGraph;
+import com.pavis.backend.slim.project.system.domain.annotation.AnnotationListParam;
+import org.apache.ibatis.annotations.Mapper;
+
+import java.util.List;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: GenerateGraphMapper
+ * @date: 2023-11-06 13:58
+ */
+@Mapper
+public interface GenerateGraphMapper extends BaseMapper<GenerateGraph> {
+
+}

+ 8 - 0
src/main/java/com/pavis/backend/slim/project/system/service/AnnotationListService.java

@@ -3,6 +3,7 @@ package com.pavis.backend.slim.project.system.service;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.github.pagehelper.PageInfo;
 import com.pavis.backend.slim.project.system.domain.AnnotationList;
+import com.pavis.backend.slim.project.system.domain.annotation.AnnotationFront;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationListParam;
 import com.pavis.backend.slim.project.system.domain.annotation.ImportAnnotationDataset;
 import org.springframework.web.multipart.MultipartFile;
@@ -44,4 +45,11 @@ public interface AnnotationListService extends IService<AnnotationList> {
      * @return
      */
     Boolean importLocalAnnotationDataset(String kgId,MultipartFile file);
+
+    /**
+     * 保存前端标注的实体数据html。
+     * @param annotationFront
+     * @return
+     */
+    Boolean saveAnnotation(AnnotationFront annotationFront);
 }

+ 7 - 4
src/main/java/com/pavis/backend/slim/project/system/service/EntityLablesRelationService.java

@@ -2,13 +2,9 @@ package com.pavis.backend.slim.project.system.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.pavis.backend.slim.project.system.domain.EntityLabelsRelation;
-import com.pavis.backend.slim.project.system.domain.EntityLabelsSpan;
-import com.pavis.backend.slim.project.system.domain.annotation.AnnotationData;
-import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntity;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntityRelation;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationToAlg;
 import com.pavis.backend.slim.project.system.domain.annotation.ToAnnotation;
-import org.springframework.web.bind.annotation.RequestParam;
 
 import java.util.List;
 
@@ -46,4 +42,11 @@ public interface EntityLablesRelationService extends IService<EntityLabelsRelati
      * @return
      */
     AnnotationToAlg generateKgGraph(String kgId);
+
+    /**
+     * 根据主键ID删除该标注数据关系。
+     * @param id
+     * @return
+     */
+    Boolean delRelById(Long id);
 }

+ 8 - 1
src/main/java/com/pavis/backend/slim/project/system/service/EntityLablesSpanService.java

@@ -2,8 +2,8 @@ package com.pavis.backend.slim.project.system.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.pavis.backend.slim.project.system.domain.EntityLabelsSpan;
-import com.pavis.backend.slim.project.system.domain.SysUser;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntity;
+import com.pavis.backend.slim.project.system.domain.annotation.DelAnnotation;
 
 import java.util.List;
 
@@ -27,4 +27,11 @@ public interface EntityLablesSpanService extends IService<EntityLabelsSpan> {
      * @return
      */
     List<AnnotationEntity> getAnnotationEntityByKgIdAndFileId(String kgId,String fileId);
+
+    /**
+     * 删除标注的实体/实体关系数据。
+     * @param delAnnotation
+     * @return
+     */
+    Boolean delAnnotation(DelAnnotation delAnnotation);
 }

+ 28 - 0
src/main/java/com/pavis/backend/slim/project/system/service/GenerateGraphService.java

@@ -0,0 +1,28 @@
+package com.pavis.backend.slim.project.system.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.pavis.backend.slim.project.system.domain.GenerateGraph;
+import com.pavis.backend.slim.project.system.domain.annotation.GenerateGraphResult;
+import org.springframework.web.bind.annotation.RequestBody;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: GenerateGraphService
+ * @date: 2023-11-06 13:58
+ */
+public interface GenerateGraphService extends IService<GenerateGraph> {
+    /**
+     * 算法通知后端更新生成图谱结果接口。
+     * @param genGraphResult
+     */
+    void generateKgGraphResult(@RequestBody GenerateGraphResult genGraphResult);
+
+    /**
+     * 生成一条生成图谱的结果记录。
+     * @param taskId
+     * @param kgId
+     * @return
+     */
+    Boolean createGenerateKgGraphResult(String taskId,String kgId,Boolean isGenerate);
+}

+ 9 - 1
src/main/java/com/pavis/backend/slim/project/system/service/SysKgService.java

@@ -1,7 +1,6 @@
 package com.pavis.backend.slim.project.system.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.pavis.backend.slim.project.system.domain.SysEntity;
 import com.pavis.backend.slim.project.system.domain.SysEntityAll;
 import com.pavis.backend.slim.project.system.domain.SysKg;
 import com.pavis.backend.slim.project.system.domain.algorithm.AlgSpO;
@@ -80,4 +79,13 @@ public interface SysKgService extends IService<SysKg> {
      * @return
      */
     List<String> getFileIdsByKgId(String kgId);
+
+    /**
+     * 根据kgId更新解析、训练、抽取状态。
+     * @param kgId
+     * @param finalStatus
+     * @param finalReason
+     * @return
+     */
+    Boolean updateGrapgStatus(String kgId,Integer graphStatus,Integer annotationStatus,Integer finalStatus,String finalReason);
 }

+ 59 - 29
src/main/java/com/pavis/backend/slim/project/system/service/impl/AnnotationListServiceImpl.java

@@ -1,5 +1,6 @@
 package com.pavis.backend.slim.project.system.service.impl;
 
+import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -7,9 +8,11 @@ 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.SecurityUtils;
 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.AnnotationFront;
 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;
@@ -45,8 +48,7 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
 
     @Override
     public PageInfo getAnnotationLists(AnnotationListParam annotationListParam) {
-        List<AnnotationList> annotationLists = new ArrayList<>();
-        annotationLists = baseMapper.selectAnnotationList(annotationListParam);
+        List<AnnotationList> annotationLists = baseMapper.selectAnnotationList(annotationListParam);
         // 获取txt文本内容为空,向算法请求txt文本内容并更新。
         if (!CollectionUtils.isEmpty(annotationLists)) {
             // 过滤出textContent为空的数据。
@@ -78,23 +80,27 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
             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);
-                }
+                // 判重处理。
+                // 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);
+                // }
+                // 不判重。
+                int saveRes = baseMapper.insert(annotationList);
+                saveResLists.add(saveRes);
                 annotationLists.add(annotationList);
             }
         }
         // 获取算法txt文本内容.
         toAlgTxt(annotationLists);
         // 返回数据。
-        log.info("saveResLists:{}",JSON.toJSONString(saveResLists));
+        log.info("saveResLists:{}", JSON.toJSONString(saveResLists));
         if (saveResLists.contains(0)) {
             return false;
         } else {
@@ -103,12 +109,12 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
     }
 
     @Override
-    public Boolean importLocalAnnotationDataset(String kgId,MultipartFile file) {
+    public Boolean importLocalAnnotationDataset(String kgId, MultipartFile file) {
         // todo 文件重复上传。知识库文档重复上传。
         try {
             String s = DigestUtils.md5Hex(file.getBytes());
-            log.info("file to MD5:{}",s);
-        }catch (Exception ex){
+            log.info("file to MD5:{}", s);
+        } catch (Exception ex) {
 
         }
         SnowflakeIdWorkerUtils idWorker = new SnowflakeIdWorkerUtils(0, 0);
@@ -118,18 +124,38 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
         annotationList.setOriginalName(file.getOriginalFilename());
         annotationList.setAnnotationStatus(0);
         annotationList.setParseStatus(0);
+        // 设置基本信息,创建人、创建时间等。
+        // annotationList.setUserId(SecurityUtils.getUserId());
+        annotationList.setCreateBy(SecurityUtils.getUsername());
+        annotationList.setCreateTime(DateUtil.date());
+        annotationList.setUpdateBy(SecurityUtils.getUsername());
+        annotationList.setUpdateTime(DateUtil.date());
         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));
+        algMap.put("file", file);
+        algMap.put("fileId", annotationList.getFileId());
+        // String algTxtByFile = algKgClient.getAlgTxtByFile(algMap);
+        JSONArray algTxtByFile = algKgClient.getAlgTxtByFile(algMap);
+        log.info("算法algTxtByFile:{}", JSON.toJSONString(algTxtByFile));
         List<AnnotationList> annotationLists = new ArrayList<>();
         annotationLists.add(annotationList);
         try {
-            dealAlgToTxtRes(algTxtByFile,annotationLists,false);
+            dealAlgToTxtRes(algTxtByFile.toString(), annotationLists, false);
             return true;
-        }catch (Exception ex){
-            log.info("算法本地文件转txt异常 ex:{}",ex);
+        } catch (Exception ex) {
+            log.info("算法本地文件转txt异常 ex:{}", ex);
+        }
+        return false;
+    }
+
+    @Override
+    public Boolean saveAnnotation(AnnotationFront annotationFront) {
+        List<AnnotationList> annotationLists = baseMapper.selectList(new QueryWrapper<AnnotationList>().lambda().eq(AnnotationList::getKgId, annotationFront.getKgId()).eq(AnnotationList::getFileId, annotationFront.getFileId()));
+        if(annotationLists.size() > 0){
+            for (AnnotationList annotationList : annotationLists) {
+                annotationList.setTextFront(annotationFront.getTextFront());
+                int upRes = baseMapper.updateById(annotationList);
+                return upRes > 0 ? true : false;
+            }
         }
         return false;
     }
@@ -148,15 +174,16 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
         algMap.put("fileIds", fileIds);
         log.info("algMap:{}", JSON.toJSONString(algMap));
         String algTxtTmp = algKgClient.getAlgTxt(JSON.toJSONString(algMap));
-        dealAlgToTxtRes(algTxtTmp, hasNullTexts,true);
+        dealAlgToTxtRes(algTxtTmp, hasNullTexts, true);
         log.info("实时调用算法获取txt文本内容结束");
     }
 
-    public void dealAlgToTxtRes(String algTxtTmp, List<AnnotationList> hasNullTexts,Boolean upFlag) {
+    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);
+                log.info("解析算法返回数据convertAlgRes:{}",JSON.toJSONString(convertAlgRes));
                 if (!CollectionUtils.isEmpty(convertAlgRes)) {
                     for (ConvertAlgRes algRes : convertAlgRes) {
                         if (null != algRes.getErrorCode() && "0".equals(algRes.getErrorCode())) {
@@ -164,11 +191,13 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
                             for (AnnotationList hasNullText : hasNullTexts) {
                                 if (algRes.getFileId().equals(hasNullText.getFileId())) {
                                     hasNullText.setTextContent(algRes.getText());
-                                    if (upFlag){
+                                    if (upFlag) {
+                                        hasNullText.setUpdateBy(SecurityUtils.getUsername());
+                                        hasNullText.setUpdateTime(DateUtil.date());
                                         // true 更新textContent.
                                         int updateText = baseMapper.updateById(hasNullText);
                                         log.info("更新文本内容结果updateText:{}", updateText);
-                                    }else {
+                                    } else {
                                         // false 新增textContent
                                         int saveRes = baseMapper.insert(hasNullText);
                                         log.info("新增文本内容结果updateText:{}", saveRes);
@@ -176,9 +205,10 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
                                 }
                             }
                         } else {
-                            if (null != algRes.getErrorCode() && "-1".equals(algRes.getErrorCode()) && false == upFlag){
+                            if (null != algRes.getErrorCode() && "-1".equals(algRes.getErrorCode()) && false == upFlag) {
                                 for (AnnotationList hasNullText : hasNullTexts) {
                                     hasNullText.setParseStatus(-1);
+                                    // hasNullText.setUserId(SecurityUtils.getUserId());
                                     int saveRes = baseMapper.insert(hasNullText);
                                     log.info("解析出错 新增文本内容结果updateText:{}", saveRes);
                                 }
@@ -190,7 +220,7 @@ public class AnnotationListServiceImpl extends ServiceImpl<AnnotationListMapper,
                     log.info("获取算法解析txt文本内容返回结果 返回数据为NULL ex convertAlgRes:{}", JSON.toJSONString(convertAlgRes));
                 }
             } catch (Exception ex) {
-                log.info("获取算法解析txt文本内容返回结果 返回数据异常 NOT JSONArray ex:{}", JSON.toJSONString(algTxtTmp));
+                log.info("获取算法解析txt文本内容返回结果 返回数据异常 NOT JSONArray ex:{}", ex);
             }
         } else {
             log.info("获取算法解析txt文本内容返回结果接口异常 ex convertToTxtResTmp:{}", algTxtTmp);

+ 90 - 24
src/main/java/com/pavis/backend/slim/project/system/service/impl/EntityLabelsRelationServiceImpl.java

@@ -1,8 +1,12 @@
 package com.pavis.backend.slim.project.system.service.impl;
 
+import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.pavis.backend.slim.common.utils.CommonUtils;
+import com.pavis.backend.slim.common.utils.SecurityUtils;
 import com.pavis.backend.slim.project.system.client.AlgKgClient;
 import com.pavis.backend.slim.project.system.domain.EntityLabelsRelation;
 import com.pavis.backend.slim.project.system.domain.SysEntity;
@@ -16,6 +20,7 @@ import com.pavis.backend.slim.project.system.domain.annotation.UniversalGgraphTo
 import com.pavis.backend.slim.project.system.mapper.EntityLabelsRelationMapper;
 import com.pavis.backend.slim.project.system.service.EntityLablesRelationService;
 import com.pavis.backend.slim.project.system.service.EntityLablesSpanService;
+import com.pavis.backend.slim.project.system.service.GenerateGraphService;
 import com.pavis.backend.slim.project.system.service.SysEntityService;
 import com.pavis.backend.slim.project.system.service.SysFileService;
 import com.pavis.backend.slim.project.system.service.SysKgService;
@@ -58,12 +63,19 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
     @Autowired
     private AlgKgClient algKgClient;
 
+    @Autowired
+    private GenerateGraphService generateGraphService;
+
     @Override
     public EntityLabelsRelation saveAnnotationRelation(AnnotationEntityRelation relation) {
         EntityLabelsRelation entityLabelsRelation = new EntityLabelsRelation();
-        BeanUtils.copyProperties(relation,entityLabelsRelation);
+        BeanUtils.copyProperties(relation, entityLabelsRelation);
+        // 设置基本信息,创建人、创建时间等。
+        entityLabelsRelation.setUserId(SecurityUtils.getUserId());
+        entityLabelsRelation.setCreateBy(SecurityUtils.getUsername());
+        entityLabelsRelation.setCreateTime(DateUtil.date());
         int insert = baseMapper.insert(entityLabelsRelation);
-        log.info("标注的实体信息存储结果res:{},entityLabelsRelation:{}",insert, JSON.toJSONString(entityLabelsRelation));
+        log.info("标注的实体信息存储结果res:{},entityLabelsRelation:{}", insert, JSON.toJSONString(entityLabelsRelation));
         return entityLabelsRelation;
     }
 
@@ -74,8 +86,8 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
         // List<TreeFile> treeFiles = sysFileService.listTreeFiles(kgId);
         // log.info("获取知识库下的文档信息treeFiles:{}",JSON.toJSONString(treeFiles));
         String fileTextContent = sysFileService.getFileTextContent(fileId);
-        if (null != fileTextContent && StringUtils.isNotBlank(fileTextContent)){
-            log.info("文本内容不为空:{}",fileTextContent);
+        if (null != fileTextContent && StringUtils.isNotBlank(fileTextContent)) {
+            log.info("文本内容不为空:{}", fileTextContent);
             // 1.1 获取历史标注实体信息数据列表。
             List<AnnotationEntity> entities = entityLablesSpanService.getAnnotationEntityByKgIdAndFileId(kgId, fileId);
             // 1.2 获取历史标注实体间关系数据列表。
@@ -95,12 +107,12 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
     @Override
     public List<AnnotationEntityRelation> getAnnotationEntityRelationByKgIdAndFileId(String kgId, String fileId) {
         List<EntityLabelsRelation> entityLabelsRelations = baseMapper.selectList(new QueryWrapper<EntityLabelsRelation>().lambda()
-                .eq(EntityLabelsRelation::getKgId, kgId).eq(EntityLabelsRelation::getFileId,fileId));
+                .eq(EntityLabelsRelation::getKgId, kgId).eq(EntityLabelsRelation::getFileId, fileId));
         List<AnnotationEntityRelation> annotationEntityRelations = new ArrayList<>();
         AnnotationEntityRelation annotationEntityRelation;
         for (EntityLabelsRelation entityLabelsRelation : entityLabelsRelations) {
             annotationEntityRelation = new AnnotationEntityRelation();
-            BeanUtils.copyProperties(entityLabelsRelation,annotationEntityRelation);
+            BeanUtils.copyProperties(entityLabelsRelation, annotationEntityRelation);
             annotationEntityRelation.setId(Long.valueOf(entityLabelsRelation.getId()));
             // 处理个别字段,fileId是string类型的。
             annotationEntityRelations.add(annotationEntityRelation);
@@ -115,37 +127,43 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
         // 根据kgId获取文档id列表。
         List<String> fileIds = sysKgService.getFileIdsByKgId(kgId);
         // 2. 判断该图谱是否创建实体和关系数据。
-        if (CollectionUtils.isEmpty(entityByKgId)){
+        if (CollectionUtils.isEmpty(entityByKgId)) {
             // 2.1 如果该图谱的未创建实体和关系数据,则调用算法另一个服务,入参为:fileIds列表、kgId。
-            // todo 调用算法另一个服务
-            UniversalGgraphToAlg universalGgraphToAlg = UniversalGgraphToAlg.builder().kgId(kgId).fileIds(fileIds).build();
-            String universalGgraphToAlgRes = "";
+            // 更新图模型状态
+
+            // String universalGgraphToAlgRes = "";
             try {
-                universalGgraphToAlgRes = algKgClient.universalGgraphToAlg(JSON.toJSONString(universalGgraphToAlg));
-                log.info("该图谱无实体、关系数据时,调用通用知识图谱算法接口返回res:{}",universalGgraphToAlgRes);
-            }catch (Exception ex){
+                // todo 临时弃用,改用下面的接口,直接调用获取taskId。
+                // universalGgraphToAlgRes = algKgClient.universalGgraphToAlg(JSON.toJSONString(universalGgraphToAlg));
+                // log.info("该图谱无实体、关系数据时,调用通用知识图谱算法接口返回res:{}", universalGgraphToAlgRes);
+
+                // todo 直接调用算法获取taskId,type=true。返回json{"taskId":""}。生成一条记录。
+                UniversalGgraphToAlg universalGgraphToAlg = UniversalGgraphToAlg.builder().kgId(kgId).fileIds(fileIds).taskType(true).build();
+                String getTaskIdRes = algKgClient.afterSendAnnotationToGetTaskId(JSON.toJSONString(universalGgraphToAlg));
+                createGenerateGraph(getTaskIdRes, kgId, true,0,0);
+            } catch (Exception ex) {
                 ex.printStackTrace();
-                log.info("该图谱无实体、关系数据时,调用通用知识图谱算法接口返回异常ex:{}",ex);
+                log.info("该图谱无实体、关系数据时,调用通用知识图谱算法接口返回异常ex:{}", ex);
             }
             return new AnnotationToAlg();
-        }else {
+        } else {
             // 2.2 如果该图谱的创建了实体和关系数据,则调用算法的生成图谱服务,即入参为:text、实体标注数据列表、实体关系标注数据列表。
             // 如果图谱有实体关系数据,且标注了数据,则发送标注的数据给算法;如果该文档未被标注则不发送。如果仅有实体关系数据,所有文档均未被标注,也发送,只不过标注数据为空。
             // 图谱下的所有文档标注信息。
             List<AnnotationData> annotationDatas = new ArrayList<>();
             for (String fileId : fileIds) {
                 String fileTextContent = sysFileService.getFileTextContent(fileId);
-                if (null != fileTextContent && StringUtils.isNotBlank(fileTextContent)){
-                    log.info("文本内容不为空:{}",fileTextContent);
-                    log.info("--->fileId:{}",fileId);
+                if (null != fileTextContent && StringUtils.isNotBlank(fileTextContent)) {
+                    log.info("文本内容不为空:{}", fileTextContent);
+                    log.info("--->fileId:{}", fileId);
                     // 获取历史标注实体信息数据列表,即知识库下的实体标注信息列表。
                     List<AnnotationEntity> entities = entityLablesSpanService.getAnnotationEntityByKgIdAndFileId(kgId, fileId);
                     // 如果标注实体数据不为空,则需要发送给算法,否则不发。
-                    if(!CollectionUtils.isEmpty(entities)){
-                        log.info("--->entities:{}",entities.size());
+                    if (!CollectionUtils.isEmpty(entities)) {
+                        log.info("--->entities:{}", entities.size());
                         // 获取历史标注实体间关系数据列表,即图谱下的实体关系标注信息列表。
                         List<AnnotationEntityRelation> relations = getAnnotationEntityRelationByKgIdAndFileId(kgId, fileId);
-                        log.info("--->relations:{}",entities.size());
+                        log.info("--->relations:{}", entities.size());
                         AnnotationData annotationData = AnnotationData.builder().text(fileTextContent).entities(entities).relations(relations).build();
                         annotationDatas.add(annotationData);
                     }
@@ -153,11 +171,59 @@ public class EntityLabelsRelationServiceImpl extends ServiceImpl<EntityLabelsRel
             }
             // 3.算法所需数据结构。
             AnnotationToAlg annotationToAlg = AnnotationToAlg.builder().kgId(kgId).annotationDatas(annotationDatas).build();
-            log.info("标注数据待发送至算法数据结构annotationToAlg:{}",JSON.toJSONString(annotationToAlg));
+            log.info("标注数据待发送至算法数据结构annotationToAlg:{}", JSON.toJSONString(annotationToAlg));
             // todo 4. 发送数据给算法。
-            String algRes = algKgClient.sendToAlgForGenerateKgGraph(JSON.toJSONString(annotationToAlg));
-            log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回algRes:{}",algRes);
+            String algRes = null;
+            try {
+                algRes = algKgClient.sendToAlgForGenerateKgGraph(JSON.toJSONString(annotationToAlg));
+                log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回algRes:{}", algRes);
+                if (null != algRes && "ok".equals(algRes)) {
+                    // todo 调用算法获取taskId,type=false。返回json{"taskId":""}。生成一条记录。状态为200.才调用。
+                    UniversalGgraphToAlg universalGgraphToAlg = UniversalGgraphToAlg.builder().kgId(kgId).fileIds(fileIds).taskType(false).build();
+                    String getTaskIdRes = algKgClient.afterSendAnnotationToGetTaskId(JSON.toJSONString(universalGgraphToAlg));
+                    createGenerateGraph(getTaskIdRes, kgId,  false,1,annotationDatas.size() > 0 ? 1 : 0);
+                }
+            } catch (Exception ex) {
+                ex.printStackTrace();
+                log.info("该图谱存在实体关系数据时发送标注数据列表,调用算法生成知识图谱接口返回异常ex:{}", ex);
+            }
             return annotationToAlg;
         }
     }
+
+    @Override
+    public Boolean delRelById(Long id) {
+        EntityLabelsRelation entityLabelsRelation = baseMapper.selectById(id);
+        if (null != entityLabelsRelation) {
+            int del = baseMapper.deleteById(id);
+            return del > 0 ? true : false;
+        }
+        return false;
+    }
+
+    /**
+     * 根据算法返回的taskId生成一条图谱的记录。
+     *
+     * @param getTaskIdRes
+     * @param kgId
+     * @param isGenerate
+     * @return
+     */
+    public Boolean createGenerateGraph(String getTaskIdRes, String kgId, Boolean isGenerate,Integer graphStatus,Integer annotationStatus) {
+        // 更新图谱的状态为进行中。
+        Boolean upRes = sysKgService.updateGrapgStatus(kgId,graphStatus,annotationStatus,2, "进行中");
+        log.info("更新图谱的状态为进行中:{}",upRes);
+        log.info("调用算法获取taskId:{}", getTaskIdRes);
+        if (CommonUtils.isJson(getTaskIdRes)) {
+            JSONObject taskObj = JSON.parseObject(getTaskIdRes);
+            if (taskObj.containsKey("taskId")) {
+                String taskId = taskObj.get("taskId").toString();
+                // 新增一条生成图谱的任务id。
+                Boolean generateKgGraphResult = generateGraphService.createGenerateKgGraphResult(taskId, kgId, isGenerate);
+                log.info("生成图谱记录状态:{}", generateKgGraphResult);
+                return generateKgGraphResult;
+            }
+        }
+        return false;
+    }
 }

+ 70 - 2
src/main/java/com/pavis/backend/slim/project/system/service/impl/EntityLabelsSpanServiceImpl.java

@@ -1,17 +1,26 @@
 package com.pavis.backend.slim.project.system.service.impl;
 
+import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson2.JSON;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.pavis.backend.slim.common.utils.SecurityUtils;
+import com.pavis.backend.slim.project.system.domain.EntityLabelsRelation;
 import com.pavis.backend.slim.project.system.domain.EntityLabelsSpan;
+import com.pavis.backend.slim.project.system.domain.SysKg;
 import com.pavis.backend.slim.project.system.domain.annotation.AnnotationEntity;
+import com.pavis.backend.slim.project.system.domain.annotation.DelAnnotation;
+import com.pavis.backend.slim.project.system.mapper.EntityLabelsRelationMapper;
 import com.pavis.backend.slim.project.system.mapper.EntityLabelsSpanMapper;
+import com.pavis.backend.slim.project.system.mapper.SysKgMapper;
 import com.pavis.backend.slim.project.system.service.EntityLablesSpanService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
 
+import javax.annotation.Resource;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -25,16 +34,33 @@ import java.util.List;
 @Slf4j
 public class EntityLabelsSpanServiceImpl extends ServiceImpl<EntityLabelsSpanMapper, EntityLabelsSpan> implements EntityLablesSpanService {
 
+    // @Autowired
+    // private EntityLabelsSpanMapper entityLabelsSpanMapper;
+
     @Autowired
-    private EntityLabelsSpanMapper entityLabelsSpanMapper;
+    private EntityLabelsRelationMapper entityLabelsRelationMapper;
+
+    @Resource
+    private SysKgMapper sysKgMapper;
 
     @Override
     public EntityLabelsSpan saveAnnotationEntity(AnnotationEntity entity) {
         // 将前端传过来的实体信息进行存储,备注:不对实体信息做判重信息。
         EntityLabelsSpan entityLabelsSpan = new EntityLabelsSpan();
         BeanUtils.copyProperties(entity,entityLabelsSpan);
-        int insert = entityLabelsSpanMapper.insert(entityLabelsSpan);
+        // 设置基本信息,创建人、创建时间等。
+        entityLabelsSpan.setUserId(SecurityUtils.getUserId());
+        entityLabelsSpan.setCreateBy(SecurityUtils.getUsername());
+        entityLabelsSpan.setCreateTime(DateUtil.date());
+        int insert = baseMapper.insert(entityLabelsSpan);
         log.info("标注的实体信息存储结果res:{},entityLabelsSpan:{}",insert, JSON.toJSONString(entityLabelsSpan));
+        // 更新标注状态。
+        SysKg sysKg = sysKgMapper.selectById(entity.getKgId());
+        if (null != sysKg){
+            sysKg.setAnnotationStatus(1);
+            int upRes = sysKgMapper.updateById(sysKg);
+            log.info("更新图谱的标注状态annotationStatus:{}",upRes);
+        }
         return entityLabelsSpan;
     }
 
@@ -53,4 +79,46 @@ public class EntityLabelsSpanServiceImpl extends ServiceImpl<EntityLabelsSpanMap
         }
         return annotationEntities;
     }
+
+    @Override
+    public Boolean delAnnotation(DelAnnotation delAnnotation) {
+        // 删除实体之前,判断是否存在关系,若存在先删除关系,在删除实体。
+        if ("0".equals(delAnnotation.getDelType())){
+            // 0 删除关系,检索到可直接删除该关系。
+            List<Integer> dels = new ArrayList<>();
+            for (String id : delAnnotation.getIds()) {
+                int delRes = entityLabelsRelationMapper.deleteById(id);
+                dels.add(delRes);
+            }
+            return dels.contains(0) ? false : true;
+        }else if ("1".equals(delAnnotation.getDelType())){
+            // 1 删除实体,判断是否存在关系,若存在先删除关系,在删除实体。
+            List<Integer> delRels = new ArrayList<>();
+            List<Integer> delEntities = new ArrayList<>();
+            for (String id : delAnnotation.getIds()) {
+                // EntityLabelsSpan entityLabelsSpan = baseMapper.selectById(id);
+                List<EntityLabelsRelation> entityLabelsRelations = entityLabelsRelationMapper.selectList(new QueryWrapper<EntityLabelsRelation>().lambda().eq(EntityLabelsRelation::getKgId, delAnnotation.getKgId()).eq(EntityLabelsRelation::getFileId, delAnnotation.getFileId()).eq(EntityLabelsRelation::getFromId, id).or().eq(EntityLabelsRelation::getToId, id));
+                log.info("***entityLabelsRelations:{}",JSON.toJSONString(entityLabelsRelations));
+                if (!CollectionUtils.isEmpty(entityLabelsRelations)){
+                    // 存在关系,先删除关系,在删除实体。
+                    for (EntityLabelsRelation entityLabelsRelation : entityLabelsRelations) {
+                        int delRes = entityLabelsRelationMapper.deleteById(entityLabelsRelation.getId());
+                        delRels.add(delRes);
+                    }
+                    if (!delRels.contains(0)){
+                        // 删除实体。
+                        int delEntitieRes = baseMapper.deleteById(id);
+                        delEntities.add(delEntitieRes);
+                    }
+                }else {
+                    // 不存在关系,直接删除实体。
+                    // 删除实体。
+                    int delEntitieRes = baseMapper.deleteById(id);
+                    delEntities.add(delEntitieRes);
+                }
+            }
+            return delEntities.contains(0) ? false : true;
+        }
+        return false;
+    }
 }

+ 258 - 0
src/main/java/com/pavis/backend/slim/project/system/service/impl/GenerateGraphServiceImpl.java

@@ -0,0 +1,258 @@
+package com.pavis.backend.slim.project.system.service.impl;
+
+import cn.hutool.core.date.DateUtil;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.pavis.backend.slim.common.utils.SecurityUtils;
+import com.pavis.backend.slim.project.system.domain.GenerateGraph;
+import com.pavis.backend.slim.project.system.domain.annotation.GenerateGraphResult;
+import com.pavis.backend.slim.project.system.mapper.GenerateGraphMapper;
+import com.pavis.backend.slim.project.system.service.GenerateGraphService;
+import com.pavis.backend.slim.project.system.service.SysKgService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
+
+/**
+ * @version: java version 1.8
+ * @Author: Guan H.J.
+ * @description: GenerateGraphServiceImpl
+ * @date: 2023-11-06 13:58
+ */
+@Slf4j
+@Service
+public class GenerateGraphServiceImpl extends ServiceImpl<GenerateGraphMapper, GenerateGraph> implements GenerateGraphService {
+
+    @Autowired
+    private SysKgService sysKgService;
+
+
+    @Override
+    public void generateKgGraphResult(GenerateGraphResult genGraphResult) {
+        if (null != genGraphResult && null != genGraphResult.getTaskId()) {
+            // 先检索数据库中是否已存在该任务,未存在则新增;已存在则更新。
+            List<GenerateGraph> generateGraphs = baseMapper.selectList(new QueryWrapper<GenerateGraph>().lambda().eq(GenerateGraph::getTaskId, genGraphResult.getTaskId()));
+            if (!CollectionUtils.isEmpty(generateGraphs)) {
+                // 更新状态。
+                for (GenerateGraph graph : generateGraphs) {
+                    // 更新解析状态:0 未解析 2 解析中 -1 解析失败 1 解析成功。
+                    if ("解析".equals(genGraphResult.getTaskName())){
+                        graph = toUpParse(graph, genGraphResult);
+                    }else if ("训练".equals(genGraphResult.getTaskName())){
+                        graph = toUpTrain(graph, genGraphResult);
+                    }else if ("抽取".equals(genGraphResult.getTaskName())){
+                        graph = toUpExtract(graph, genGraphResult);
+                    }
+                    int upRes = baseMapper.updateById(graph);
+                    log.info("算法生成知识图谱-:{},更新结果:{}", genGraphResult.getTaskName(), upRes);
+                    // 更新图谱的状态:图模型状态 0 无图模型 1 有图模型;是否标注状态 0 未标注  1 已标注;生成图谱的状态 0 未生成 1 已生成。
+                    if (graph.getParseTaskStatus() == -1) {
+                        sysKgService.updateGrapgStatus(graph.getKgId(), null,null, graph.getParseTaskStatus(), "解析失败");
+                    } else if (graph.getTrainTaskStatus() == -1) {
+                        sysKgService.updateGrapgStatus(graph.getKgId(), null,null, graph.getTrainTaskStatus(), "训练失败");
+                    } else if (graph.getExtractTaskStatus() == -1) {
+                        sysKgService.updateGrapgStatus(graph.getKgId(), null,null, graph.getExtractTaskStatus(), "抽取失败");
+                    } else if (graph.getExtractTaskStatus() == 1) {
+                        sysKgService.updateGrapgStatus(graph.getKgId(), null,null, graph.getExtractTaskStatus(), "抽取成功");
+                    }
+                }
+            }
+        }
+    }
+
+    @Override
+    public Boolean createGenerateKgGraphResult(String taskId, String kgId, Boolean isGenerate) {
+        // isGenerate: 为true表示调用通用图谱,无训练,所以训练状态为完成1。
+        GenerateGraph generateGraph = GenerateGraph.builder().taskId(taskId).kgId(kgId).taskType(String.valueOf(isGenerate)).parseTaskName("解析").parseTaskStatus(2)
+                .trainTaskName("训练").trainTaskStatus(isGenerate == true ? 1 : 2).extractTaskName("抽取").extractTaskStatus(2).build();
+        generateGraph.setCreateBy(SecurityUtils.getUsername());
+        generateGraph.setCreateTime(DateUtil.date());
+        generateGraph.setUpdateBy(SecurityUtils.getUsername());
+        generateGraph.setUpdateTime(DateUtil.date());
+        int insert = baseMapper.insert(generateGraph);
+        log.info("算法新增生成图谱记录:{}", insert);
+        return insert > 0 ? true : false;
+    }
+
+    /**
+     * 处理解析状态。
+     *
+     * @param graph
+     * @param genGraphResult
+     * @return
+     */
+    public GenerateGraph toUpParse(GenerateGraph graph, GenerateGraphResult genGraphResult) {
+        if ("解析".equals(genGraphResult.getTaskName())) {
+            if (genGraphResult.getTaskStatus() == true) {
+                // 解析成功,设置状态后,继续更新train状态。
+                graph.setParseTaskName("解析");
+                graph.setParseTaskStatus(1);
+                graph.setParseTaskReason("解析成功");
+                // 更新训练状态;0 未训练 2 训练中 -1 训练失败 1 训练成功。
+                if ("训练".equals(genGraphResult.getTaskName())) {
+                    if (genGraphResult.getTaskStatus() == true) {
+                        // 训练成功,设置状态后,继续更新extract状态。
+                        graph.setParseTaskName("解析");
+                        graph.setParseTaskStatus(1);
+                        graph.setParseTaskReason("解析成功");
+                        graph.setTrainTaskName("训练");
+                        graph.setTrainTaskStatus(1);
+                        graph.setTrainTaskReason("训练成功");
+                        if ("抽取".equals(genGraphResult.getTaskName())) {
+                            // 抽取成功,设置状态并更新。
+                            graph.setExtractTaskName("抽取");
+                            graph.setExtractTaskStatus(1);
+                            graph.setExtractTaskReason("抽取成功");
+                        } else {
+                            log.info("抽取失败,更新状态和原因,同时抽取状态失败。");
+                            graph.setParseTaskName("解析");
+                            graph.setParseTaskStatus(1);
+                            graph.setParseTaskReason("解析成功");
+                            graph.setTrainTaskName("训练");
+                            graph.setTrainTaskStatus(1);
+                            graph.setTrainTaskReason("训练成功");
+                            graph.setExtractTaskName("抽取");
+                            graph.setExtractTaskStatus(-1);
+                            graph.setExtractTaskReason("抽取失败");
+                        }
+                    } else {
+                        log.info("训练失败,更新状态和原因,同时抽取状态失败,原因默认:因第二步骤抽取失败。");
+                        graph.setParseTaskName("解析");
+                        graph.setParseTaskStatus(1);
+                        graph.setParseTaskReason("解析成功");
+                        graph.setTrainTaskName("训练");
+                        graph.setTrainTaskStatus(-1);
+                        graph.setTrainTaskReason(genGraphResult.getMsg());
+                        graph.setExtractTaskName("抽取");
+                        graph.setExtractTaskStatus(-1);
+                        graph.setExtractTaskReason("因第二步骤训练失败");
+                    }
+                }
+            } else {
+                // 解析失败,更新状态和原因,同时训练和抽取状态也均失败,原因默认:因第一步骤解析失败。
+                log.info("解析失败,更新状态和原因,同时训练和抽取状态也均失败,原因默认:因第一步骤解析失败。");
+                graph.setParseTaskName("解析");
+                graph.setParseTaskStatus(-1);
+                graph.setParseTaskReason(genGraphResult.getMsg());
+                graph.setTrainTaskName("训练");
+                graph.setTrainTaskStatus(-1);
+                graph.setTrainTaskReason("因第一步骤解析失败");
+                graph.setExtractTaskName("抽取");
+                graph.setExtractTaskStatus(-1);
+                graph.setExtractTaskReason("因第一步骤解析失败");
+            }
+        }
+        if ("true".equals(graph.getTaskType())){
+            // 通用图谱训练状态永远为成功。
+            graph.setTrainTaskName("训练");
+            graph.setTrainTaskStatus(1);
+            graph.setTrainTaskReason("训练成功");
+        }
+        return graph;
+    }
+
+    /**
+     * 处理训练状态。
+     *
+     * @param graph
+     * @param genGraphResult
+     * @return
+     */
+    public GenerateGraph toUpTrain(GenerateGraph graph, GenerateGraphResult genGraphResult) {
+
+        // 更新训练状态;0 未训练 2 训练中 -1 训练失败 1 训练成功。
+        if ("训练".equals(genGraphResult.getTaskName())) {
+            if (genGraphResult.getTaskStatus() == true) {
+                // 训练成功,设置状态后,继续更新extract状态。
+                graph.setParseTaskName("解析");
+                graph.setParseTaskStatus(1);
+                graph.setParseTaskReason("解析成功");
+                graph.setTrainTaskName("训练");
+                graph.setTrainTaskStatus(1);
+                graph.setTrainTaskReason("训练成功");
+                if ("抽取".equals(genGraphResult.getTaskName())) {
+                    // 抽取成功,设置状态并更新。
+                    graph.setExtractTaskName("抽取");
+                    graph.setExtractTaskStatus(1);
+                    graph.setExtractTaskReason("抽取成功");
+                } else {
+                    log.info("抽取失败,更新状态和原因,同时抽取状态失败。");
+                    graph.setParseTaskName("解析");
+                    graph.setParseTaskStatus(1);
+                    graph.setParseTaskReason("解析成功");
+                    graph.setTrainTaskName("训练");
+                    graph.setTrainTaskStatus(1);
+                    graph.setTrainTaskReason("训练成功");
+                    graph.setExtractTaskName("抽取");
+                    graph.setExtractTaskStatus(-1);
+                    graph.setExtractTaskReason("抽取失败");
+                }
+            } else {
+                log.info("训练失败,更新状态和原因,同时抽取状态失败,原因默认:因第二步骤抽取失败。");
+                graph.setParseTaskName("解析");
+                graph.setParseTaskStatus(1);
+                graph.setParseTaskReason("解析成功");
+                graph.setTrainTaskName("训练");
+                graph.setTrainTaskStatus(-1);
+                graph.setTrainTaskReason(genGraphResult.getMsg());
+                graph.setExtractTaskName("抽取");
+                graph.setExtractTaskStatus(-1);
+                graph.setExtractTaskReason("因第二步骤训练失败");
+            }
+        }
+        if ("true".equals(graph.getTaskType())){
+            // 通用图谱训练状态永远为成功。
+            graph.setTrainTaskName("训练");
+            graph.setTrainTaskStatus(1);
+            graph.setTrainTaskReason("训练成功");
+        }
+        return graph;
+    }
+
+    /**
+     * 处理抽取状态。
+     *
+     * @param graph
+     * @param genGraphResult
+     * @return
+     */
+    public GenerateGraph toUpExtract(GenerateGraph graph, GenerateGraphResult genGraphResult) {
+        // 更新训练状态;0 未训练 2 训练中 -1 训练失败 1 训练成功。
+        if ("抽取".equals(genGraphResult.getTaskName())) {
+            if (genGraphResult.getTaskStatus() == true) {
+                // 训练成功,设置状态后,继续更新extract状态。
+                graph.setParseTaskName("解析");
+                graph.setParseTaskStatus(1);
+                graph.setParseTaskReason("解析成功");
+                graph.setTrainTaskName("训练");
+                graph.setTrainTaskStatus(1);
+                graph.setTrainTaskReason("训练成功");
+                // 抽取成功,设置状态并更新。
+                graph.setExtractTaskName("抽取");
+                graph.setExtractTaskStatus(1);
+                graph.setExtractTaskReason("抽取成功");
+            } else {
+                log.info("抽取失败,更新状态和原因,同时抽取状态失败。");
+                graph.setParseTaskName("解析");
+                graph.setParseTaskStatus(1);
+                graph.setParseTaskReason("解析成功");
+                graph.setTrainTaskName("训练");
+                graph.setTrainTaskStatus(1);
+                graph.setTrainTaskReason("训练成功");
+                graph.setExtractTaskName("抽取");
+                graph.setExtractTaskStatus(-1);
+                graph.setExtractTaskReason("抽取失败");
+            }
+        }
+        if ("true".equals(graph.getTaskType())){
+            // 通用图谱训练状态永远为成功。
+            graph.setTrainTaskName("训练");
+            graph.setTrainTaskStatus(1);
+            graph.setTrainTaskReason("训练成功");
+        }
+        return graph;
+    }
+}

+ 23 - 2
src/main/java/com/pavis/backend/slim/project/system/service/impl/SysKgServiceImpl.java

@@ -3,7 +3,6 @@ package com.pavis.backend.slim.project.system.service.impl;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.crypto.digest.MD5;
 import com.alibaba.fastjson2.JSON;
-import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.pavis.backend.slim.common.constant.Hodgepodge;
@@ -44,7 +43,6 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import springfox.documentation.spring.web.json.Json;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -104,6 +102,10 @@ public class SysKgServiceImpl extends ServiceImpl<SysKgMapper, SysKg> implements
         kg.setCreateBy(SecurityUtils.getUsername());
         kg.setUserId(SecurityUtils.getUserId());
         kg.setCreateTime(DateUtil.date());
+        // 设置图谱状态、标注状态、生成图谱状态默认未0.
+        kg.setGraphStatus(0);
+        kg.setAnnotationStatus(0);
+        kg.setGenerateGraphStatus(0);
         // 存储kg信息
         baseMapper.insert(kg);
         // 处理前端传过来的文件字段--fileIds
@@ -410,4 +412,23 @@ public class SysKgServiceImpl extends ServiceImpl<SysKgMapper, SysKg> implements
         }
         return null;
     }
+
+    @Override
+    public Boolean updateGrapgStatus(String kgId,Integer graphStatus,Integer annotationStatus, Integer finalStatus,String finalReason) {
+        SysKg sysKg = baseMapper.selectById(kgId);
+        if (null != sysKg){
+            if (null != graphStatus){
+                sysKg.setGraphStatus(graphStatus);
+            }
+            if (null != annotationStatus){
+                sysKg.setAnnotationStatus(annotationStatus);
+            }
+            sysKg.setGenerateGraphStatus(finalStatus);
+            sysKg.setGenerateGraphReason(finalReason);
+            int upRes = baseMapper.updateById(sysKg);
+            log.info("更新生成图谱的状态:{}",upRes);
+            return upRes > 0 ? true : false;
+        }
+        return false;
+    }
 }