UpdateEntity godoc @Summary 更新实体 @Description 更新现有实体的详细信息 @Tags 实体管理 @Accept json @Produce json @Security BearerAuth @Param id path int true "实体 ID" @Param request body UpdateEntityRequest true "更新字段" @Success 200 {object} model.CommonResponse{data=model.Entity} @Failure 404 {object} model.CommonRes
(c *gin.Context)
| 483 | // @Failure 409 {object} model.CommonResponse |
| 484 | // @Router /api/entities/{id} [put] |
| 485 | func UpdateEntity(c *gin.Context) { |
| 486 | eid := config.GetEID(c) |
| 487 | if eid <= 0 { |
| 488 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse(model.InvalidEnterpriseID)) |
| 489 | return |
| 490 | } |
| 491 | |
| 492 | id, ok := middleware.MustParseIDParam(c, "id") |
| 493 | if !ok { |
| 494 | return |
| 495 | } |
| 496 | |
| 497 | var req UpdateEntityRequest |
| 498 | if err := c.ShouldBindJSON(&req); err != nil { |
| 499 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err)) |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | if req.Type == nil && req.Name == nil && req.Status == nil { |
| 504 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("无可更新字段"))) |
| 505 | return |
| 506 | } |
| 507 | |
| 508 | var entity model.Entity |
| 509 | if err := model.DB.Where("eid = ? AND id = ?", eid, id).First(&entity).Error; err != nil { |
| 510 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 511 | c.JSON(http.StatusNotFound, model.NotFound.ToResponse(errors.New("实体不存在"))) |
| 512 | return |
| 513 | } |
| 514 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 515 | return |
| 516 | } |
| 517 | |
| 518 | nextType := entity.Type |
| 519 | nextName := entity.Name |
| 520 | nextStatus := entity.Status |
| 521 | if req.Type != nil { |
| 522 | nextType = strings.TrimSpace(*req.Type) |
| 523 | } |
| 524 | if req.Name != nil { |
| 525 | nextName = strings.TrimSpace(*req.Name) |
| 526 | } |
| 527 | if req.Status != nil { |
| 528 | nextStatus = strings.TrimSpace(*req.Status) |
| 529 | } |
| 530 | if nextType == "" || nextName == "" { |
| 531 | c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(errors.New("type 或 name 不能为空"))) |
| 532 | return |
| 533 | } |
| 534 | if nextStatus == "" { |
| 535 | nextStatus = "active" |
| 536 | } |
| 537 | |
| 538 | var dupCount int64 |
| 539 | if err := model.DB.Model(&model.Entity{}). |
| 540 | Where("eid = ? AND id <> ? AND type = ? AND name = ?", eid, entity.ID, nextType, nextName). |
| 541 | Count(&dupCount).Error; err != nil { |
| 542 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
nothing calls this directly
no test coverage detected