MCPcopy Create free account
hub / github.com/53AI/53AIHub / BatchLinkEntities

Function BatchLinkEntities

api/controller/entity.go:63–244  ·  view source on GitHub ↗

BatchLinkEntities godoc @Summary 批量关联实体 @Description 批量为分块/文件/知识库创建并关联实体。如果实体不存在则创建,如果关联已存在则跳过。 @Tags 实体管理 @Accept json @Produce json @Security BearerAuth @Param request body BatchLinkEntityRequest true "批量关联信息" @Success 200 {object} model.CommonResponse{data=BatchLinkEntityResponse} @Router /api/en

(c *gin.Context)

Source from the content-addressed store, hash-verified

61// @Success 200 {object} model.CommonResponse{data=BatchLinkEntityResponse}
62// @Router /api/entities/batch-link [post]
63func BatchLinkEntities(c *gin.Context) {
64 eid := config.GetEID(c)
65 if eid <= 0 {
66 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse(model.InvalidEnterpriseID))
67 return
68 }
69
70 var req BatchLinkEntityRequest
71 if err := c.ShouldBindJSON(&req); err != nil {
72 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
73 return
74 }
75
76 if len(req.Items) == 0 {
77 c.JSON(http.StatusOK, model.Success.ToResponse(BatchLinkEntityResponse{0, 0}))
78 return
79 }
80
81 chunkCache := make(map[int64]*model.DocumentChunk)
82 fileCache := make(map[int64]*model.File)
83 libraryCache := make(map[int64]*model.Library)
84 successCount := 0
85 failureCount := 0
86 createdEntities := make(map[int64]*model.Entity)
87
88 err := model.DB.Transaction(func(tx *gorm.DB) error {
89 for _, item := range req.Items {
90 item.Type = strings.TrimSpace(item.Type)
91 item.Name = strings.TrimSpace(item.Name)
92 if item.Type == "" || item.Name == "" {
93 failureCount++
94 continue
95 }
96 if item.ChunkID <= 0 && item.FileID <= 0 && item.LibraryID <= 0 {
97 failureCount++
98 continue
99 }
100
101 // 1. 获取/创建实体
102 entity, created, err := model.GetOrCreateEntityWithDBAndCreated(tx, eid, item.Type, item.Name)
103 if err != nil {
104 failureCount++
105 continue
106 }
107 if created {
108 createdEntities[entity.ID] = entity
109 }
110
111 var spaceID int64
112 var chunkID int64
113 var fileID int64
114 var libraryID int64
115 chunkType := "knowledge"
116
117 switch {
118 case item.ChunkID > 0:
119 chunk, ok := chunkCache[item.ChunkID]
120 if !ok {

Callers

nothing calls this directly

Calls 5

IndexEntityMethod · 0.95
GetEIDMethod · 0.80
ToNewErrorResponseMethod · 0.80
ToResponseMethod · 0.80
CreateMethod · 0.45

Tested by

no test coverage detected