实现修改猪营养需求

This commit is contained in:
2025-11-22 17:55:52 +08:00
parent 4405d1f3f1
commit 0637f5fb6c
9 changed files with 427 additions and 20 deletions

View File

@@ -915,3 +915,45 @@ func (c *Controller) ListPigTypes(ctx echo.Context) error {
logger.Infof("%s: 获取猪类型列表成功, 数量: %d", actionType, len(resp.List))
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪类型列表成功", resp, actionType, "获取猪类型列表成功", resp)
}
// UpdatePigTypeNutrientRequirements godoc
// @Summary 全量更新猪类型的营养需求
// @Description 根据猪类型ID替换其所有的营养需求信息。这是一个覆盖操作。
// @Tags 饲料管理
// @Security BearerAuth
// @Accept json
// @Produce json
// @Param id path int true "猪类型ID"
// @Param nutrientRequirements body dto.UpdatePigTypeNutrientRequirementsRequest true "新的营养需求列表"
// @Success 200 {object} controller.Response{data=dto.PigTypeResponse} "业务码为200代表更新成功"
// @Router /api/v1/feed/pig-types/{id}/nutrient-requirements [put]
func (c *Controller) UpdatePigTypeNutrientRequirements(ctx echo.Context) error {
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigTypeNutrientRequirements")
const actionType = "更新猪类型营养需求"
idStr := ctx.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
logger.Errorf("%s: 猪类型ID格式错误: %v, ID: %s", actionType, err, idStr)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪类型ID格式", actionType, "猪类型ID格式错误", idStr)
}
var req dto.UpdatePigTypeNutrientRequirementsRequest
if err := ctx.Bind(&req); err != nil {
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
}
resp, err := c.feedManagementService.UpdatePigTypeNutrientRequirements(reqCtx, uint32(id), &req)
if err != nil {
logger.Errorf("%s: 服务层更新猪类型营养需求失败: %v, ID: %d", actionType, err, id)
if errors.Is(err, service.ErrPigTypeNotFound) {
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪类型不存在", id)
}
// 这里可以根据未来可能从服务层返回的其他特定错误进行处理
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪类型营养需求失败: "+err.Error(), actionType, "服务层更新失败", req)
}
logger.Infof("%s: 猪类型营养需求更新成功, ID: %d", actionType, resp.ID)
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "猪类型营养需求更新成功", resp, actionType, "猪类型营养需求更新成功", resp)
}