Browse Source

保存关系(含id的BUG)

fuyou 7 months ago
parent
commit
16652466d6

+ 12 - 0
autoremark-business/pom.xml

@@ -23,5 +23,17 @@
             <artifactId>autoremark-common</artifactId>
         </dependency>
     </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>9</source>
+                    <target>9</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 
 </project>

+ 12 - 2
autoremark-business/src/main/java/com/autoremark/business/controller/TblEntityController.java

@@ -1,6 +1,7 @@
 package com.autoremark.business.controller;
 
 import java.util.List;
+import java.util.Map;
 import javax.servlet.http.HttpServletResponse;
 
 import com.autoremark.common.utils.uuid.UUID;
@@ -82,8 +83,17 @@ public class TblEntityController extends BaseController
         String uuid = UUID.randomUUID().toString().replace("-", "");  // 去掉 UUID 中的短横线
         // 截取前 20 个字符
         String shortUUID = uuid.substring(0, 25);
-        tblEntity.setEntityId(shortUUID);
-        return toAjax(tblEntityService.insertTblEntity(tblEntity));
+        tblEntity.setEntityId(shortUUID); //设置实体id
+        //return toAjax(tblEntityService.insertTblEntity(tblEntity));
+
+        int result = tblEntityService.insertTblEntity(tblEntity);
+
+        //返回生成的实体id
+        if (result>0) {
+            return AjaxResult.success("操作成功", Map.of("entityId", shortUUID));
+        } else {
+            return AjaxResult.error("操作失败");
+        }
     }
 
     /**

+ 25 - 2
autoremark-business/src/main/java/com/autoremark/business/controller/TblRelationController.java

@@ -3,6 +3,8 @@ package com.autoremark.business.controller;
 import java.util.List;
 import javax.servlet.http.HttpServletResponse;
 
+import com.autoremark.business.domain.TblEntity;
+import com.autoremark.business.service.ITblEntityService;
 import com.autoremark.common.utils.uuid.UUID;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +29,7 @@ import com.autoremark.common.core.page.TableDataInfo;
  * 关系Controller
  * 
  * @author ruoyi
- * @date 2025-01-14
+ * @date 2025-01-17
  */
 @RestController
 @RequestMapping("/business/relation")
@@ -36,6 +38,9 @@ public class TblRelationController extends BaseController
     @Autowired
     private ITblRelationService tblRelationService;
 
+    @Autowired
+    private ITblEntityService tblEntityService;
+
     /**
      * 查询关系列表
      */
@@ -83,7 +88,25 @@ public class TblRelationController extends BaseController
         // 截取前 20 个字符
         String shortUUID = uuid.substring(0, 25);
         tblRelation.setRelationId(shortUUID);
-        return toAjax(tblRelationService.insertTblRelation(tblRelation));
+
+//        // 检查头实体和尾实体是否存在于实体表中
+//        TblEntity headEntity = tblEntityService.selectTblEntityByEntityId(tblRelation.getStartId());
+//        TblEntity tailEntity = tblEntityService.selectTblEntityByEntityId(tblRelation.getEndId());
+
+        System.out.println(tblRelation.getStartId());
+
+        if (tblRelation.getStartId() == null || tblRelation.getEndId() == null) {
+            return AjaxResult.error("头实体或尾实体不存在");
+        }
+
+        //保存关系到数据库
+        int result = tblRelationService.insertTblRelation(tblRelation);
+
+        if (result > 0) {
+            return AjaxResult.success("关系保存成功");
+        } else {
+            return AjaxResult.error("关系保存失败");
+        }
     }
 
     /**

+ 7 - 7
autoremark-business/src/main/java/com/autoremark/business/domain/TblRelation.java

@@ -11,7 +11,7 @@ import com.autoremark.common.core.domain.BaseEntity;
  * 关系对象 tbl_relation
  * 
  * @author ruoyi
- * @date 2025-01-14
+ * @date 2025-01-17
  */
 public class TblRelation extends BaseEntity
 {
@@ -30,11 +30,11 @@ public class TblRelation extends BaseEntity
 
     /** 该关系中头实体的id */
     @Excel(name = "该关系中头实体的id")
-    private Long startId;
+    private String startId;
 
     /** 该关系中尾实体的id */
     @Excel(name = "该关系中尾实体的id")
-    private Long endId;
+    private String endId;
 
     /** 标记类型(区分人工还是机器标注) */
     @Excel(name = "标记类型", readConverterExp = "区=分人工还是机器标注")
@@ -140,21 +140,21 @@ public class TblRelation extends BaseEntity
     {
         return typeId;
     }
-    public void setStartId(Long startId) 
+    public void setStartId(String startId) 
     {
         this.startId = startId;
     }
 
-    public Long getStartId() 
+    public String getStartId() 
     {
         return startId;
     }
-    public void setEndId(Long endId) 
+    public void setEndId(String endId) 
     {
         this.endId = endId;
     }
 
-    public Long getEndId() 
+    public String getEndId() 
     {
         return endId;
     }

+ 1 - 1
autoremark-business/src/main/java/com/autoremark/business/mapper/TblRelationMapper.java

@@ -7,7 +7,7 @@ import com.autoremark.business.domain.TblRelation;
  * 关系Mapper接口
  * 
  * @author ruoyi
- * @date 2025-01-14
+ * @date 2025-01-17
  */
 public interface TblRelationMapper 
 {

+ 1 - 1
autoremark-business/src/main/java/com/autoremark/business/service/ITblRelationService.java

@@ -7,7 +7,7 @@ import com.autoremark.business.domain.TblRelation;
  * 关系Service接口
  * 
  * @author ruoyi
- * @date 2025-01-14
+ * @date 2025-01-17
  */
 public interface ITblRelationService 
 {

+ 1 - 1
autoremark-business/src/main/java/com/autoremark/business/service/impl/TblRelationServiceImpl.java

@@ -11,7 +11,7 @@ import com.autoremark.business.service.ITblRelationService;
  * 关系Service业务层处理
  * 
  * @author ruoyi
- * @date 2025-01-14
+ * @date 2025-01-17
  */
 @Service
 public class TblRelationServiceImpl implements ITblRelationService 

+ 2 - 2
autoremark-business/src/main/resources/mapper/business/TblRelationMapper.xml

@@ -39,8 +39,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <where>  
             <if test="taskId != null  and taskId != ''"> and task_id = #{taskId}</if>
             <if test="typeId != null  and typeId != ''"> and type_id = #{typeId}</if>
-            <if test="startId != null "> and start_id = #{startId}</if>
-            <if test="endId != null "> and end_id = #{endId}</if>
+            <if test="startId != null  and startId != ''"> and start_id = #{startId}</if>
+            <if test="endId != null  and endId != ''"> and end_id = #{endId}</if>
             <if test="type != null  and type != ''"> and type = #{type}</if>
             <if test="createdBy != null "> and created_by = #{createdBy}</if>
             <if test="createdTime != null "> and created_time = #{createdTime}</if>

+ 1 - 2
autoremark-ui/src/api/business/relation.js

@@ -50,5 +50,4 @@ export function exportRelation(query) {
     method: 'get',
     params: query
   })
-}
-
+}

+ 42 - 10
autoremark-ui/src/views/index.vue

@@ -185,6 +185,13 @@ export default {
         endOffset:'',
         text:"",
         type:"人工指定",
+      },
+      relationForm:{
+        taskId:"",
+        typeId:"",
+        startId:"",
+        endId:"",
+        type:"人工标注"
       }
     };
   },
@@ -345,7 +352,7 @@ export default {
       this.highlightedText = highlightedText;
     },
     saveRelation() {
-      if (this.relation_flag != 1) {
+      if (this.relation_flag !== 1) {
         this.$message({
           message: '请先保存实体!',
           type: 'warning'
@@ -366,15 +373,18 @@ export default {
           return;
         }
 
-        // 创建关系对象
-        const relation = {
-          startId: headEntity.id,
-          endId: tailEntity.id,
-          type: '人工标注'
-        };
+        this.relationForm.startId = this.headEntityId;
+        this.relationForm.endId = this.tailEntityId;
+
+        // // 创建关系对象
+        // const relation = {
+        //   startId: headEntity.id,
+        //   endId: tailEntity.id,
+        //   type: '人工标注'
+        // };
 
         // 调用 addRelation 接口保存关系
-        addRelation(relation).then(response => {
+        addRelation(this.relationForm).then(response => {
            if (response.code === 200) {
              // 保存成功,将关系添加到前端的关系列表中
              // this.relations.push({
@@ -435,11 +445,18 @@ export default {
         console.log("提交实体数据:", this.entityForm);
 
         addEntity(this.entityForm).then(response => {
+
           console.log("实体保存响应:",response);
-          if (response.code === 200) {
+          if (this.highlightHistory.length === 1){
+            this.headEntityId = response.data.entityId;
+          } else {
+            this.tailEntityId = response.data.entityId;
+          }
+          console.log(this.headEntityId);
+          if (response.code === 200 && response.data) {
             // // 实体保存成功后,更新 entities 数组
             // const entity = {
-            //   id: response.data.id,
+            //   id: response.data.entityId,// 使用后端返回的entity_id
             //   text: this.selectedText,
             //   startOffset: this.startOffset,
             //   endOffset: this.endOffset,
@@ -448,6 +465,21 @@ export default {
             // };
             // this.entities.push(entity);
 
+            // 获取后端返回的 entity_id
+            const entityId = response.data.entityId;
+
+            // 找到当前实体在entities数组中的索引
+            const entityIndex = this.entities.findIndex(
+              entity => entity.text === this.selectedText &&
+                        entity.startOffset === this.startOffset &&
+                        entity.endOffset === this.endOffset
+            );
+
+            if (entityIndex !== -1) {
+              //更新实体的id尾后端返回的entity_id
+              this.entities[entityIndex].id = entityId;
+            }
+
             this.$message({
               message: '成功提交',
               type: 'success'