CreateEntity godoc @Summary 创建实体 @Description 手动创建一个新的实体 @Tags 实体管理 @Accept json @Produce json @Security BearerAuth @Param request body CreateEntityRequest true "实体信息" @Success 200 {object} model.CommonResponse{data=model.Entity} @Failure 409 {object} model.CommonResponse @Router /api/entities [post
(c *gin.Context)
| 419 | // @Failure 409 {object} model.CommonResponse |
| 420 | // @Router /api/entities [post] |
| 421 | func CreateEntity(c *gin.Context) { |
| 422 | eid := config.GetEID(c) |
| 423 | if eid <= 0 { |
| 424 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse(model.InvalidEnterpriseID)) |
| 425 | return |
| 426 | } |
| 427 | |
| 428 | var req CreateEntityRequest |
| 429 | if err := c.ShouldBindJSON(&req); err != nil { |
| 430 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 431 | return |
| 432 | } |
| 433 | |
| 434 | req.Type = strings.TrimSpace(req.Type) |
| 435 | req.Name = strings.TrimSpace(req.Name) |
| 436 | req.Status = strings.TrimSpace(req.Status) |
| 437 | if req.Type == "" || req.Name == "" { |
| 438 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("type 或 name 不能为空"))) |
| 439 | return |
| 440 | } |
| 441 | |
| 442 | status := req.Status |
| 443 | if status == "" { |
| 444 | status = "active" |
| 445 | } |
| 446 | |
| 447 | entity := &model.Entity{ |
| 448 | Eid: eid, |
| 449 | Type: req.Type, |
| 450 | Name: req.Name, |
| 451 | Status: status, |
| 452 | } |
| 453 | |
| 454 | if err := model.DB.Create(entity).Error; err != nil { |
| 455 | if strings.Contains(strings.ToLower(err.Error()), "duplicate") || strings.Contains(strings.ToLower(err.Error()), "unique") { |
| 456 | c.JSON(http.StatusConflict, model.RecordAlreadyExists.ToNewErrorResponse("实体已存在")) |
| 457 | return |
| 458 | } |
| 459 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | c.JSON(http.StatusOK, model.Success.ToResponse(entity)) |
| 464 | |
| 465 | // 异步更新向量库 |
| 466 | go func(e model.Entity, enterpriseID int64) { |
| 467 | svc := rag.NewEntityVectorService(model.DB) |
| 468 | _ = svc.IndexEntity(enterpriseID, &e) |
| 469 | }(*entity, eid) |
| 470 | } |
| 471 | |
| 472 | // UpdateEntity godoc |
| 473 | // @Summary 更新实体 |
nothing calls this directly
no test coverage detected