package feed import ( "context" "errors" "strconv" "git.huangwc.com/pig/pig-farm-controller/internal/app/controller" "git.huangwc.com/pig/pig-farm-controller/internal/app/dto" "git.huangwc.com/pig/pig-farm-controller/internal/app/service" "git.huangwc.com/pig/pig-farm-controller/internal/infra/logs" "github.com/labstack/echo/v4" ) // RawMaterialController 定义了原料相关的控制器 type RawMaterialController struct { ctx context.Context rawMaterialService service.RawMaterialService } // NewRawMaterialController 创建一个新的 RawMaterialController 实例 func NewRawMaterialController(ctx context.Context, feedManagementService service.RawMaterialService) *RawMaterialController { return &RawMaterialController{ ctx: ctx, rawMaterialService: feedManagementService, } } // CreateRawMaterial godoc // @Summary 创建原料 // @Description 创建一个新的原料。 // @Tags 饲料管理-原料 // @Security BearerAuth // @Accept json // @Produce json // @Param rawMaterial body dto.CreateRawMaterialRequest true "原料信息" // @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为201代表创建成功" // @Router /api/v1/feed/raw-materials [post] func (c *RawMaterialController) CreateRawMaterial(ctx echo.Context) error { reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateRawMaterial") var req dto.CreateRawMaterialRequest const actionType = "创建原料" 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.rawMaterialService.CreateRawMaterial(reqCtx, &req) if err != nil { logger.Errorf("%s: 服务层创建原料失败: %v", actionType, err) if errors.Is(err, service.ErrRawMaterialNameConflict) { return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "原料名称已存在", req) } return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建原料失败: "+err.Error(), actionType, "服务层创建原料失败", req) } logger.Infof("%s: 原料创建成功, ID: %d", actionType, resp.ID) return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "原料创建成功", resp, actionType, "原料创建成功", resp) } // UpdateRawMaterial godoc // @Summary 更新原料 // @Description 根据ID更新原料信息。 // @Tags 饲料管理-原料 // @Security BearerAuth // @Accept json // @Produce json // @Param id path int true "原料ID" // @Param rawMaterial body dto.UpdateRawMaterialRequest true "更新后的原料信息" // @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功" // @Router /api/v1/feed/raw-materials/{id} [put] func (c *RawMaterialController) UpdateRawMaterial(ctx echo.Context) error { reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateRawMaterial") 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.UpdateRawMaterialRequest 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.rawMaterialService.UpdateRawMaterial(reqCtx, uint32(id), &req) if err != nil { logger.Errorf("%s: 服务层更新原料失败: %v, ID: %d", actionType, err, id) if errors.Is(err, service.ErrRawMaterialNotFound) { return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id) } if errors.Is(err, service.ErrRawMaterialNameConflict) { return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "原料名称已存在", req) } 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) } // DeleteRawMaterial godoc // @Summary 删除原料 // @Description 根据ID删除原料。 // @Tags 饲料管理-原料 // @Security BearerAuth // @Produce json // @Param id path int true "原料ID" // @Success 200 {object} controller.Response "业务码为200代表删除成功" // @Router /api/v1/feed/raw-materials/{id} [delete] func (c *RawMaterialController) DeleteRawMaterial(ctx echo.Context) error { reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteRawMaterial") 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) } err = c.rawMaterialService.DeleteRawMaterial(reqCtx, uint32(id)) if err != nil { logger.Errorf("%s: 服务层删除原料失败: %v, ID: %d", actionType, err, id) if errors.Is(err, service.ErrRawMaterialNotFound) { return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id) } return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除原料失败: "+err.Error(), actionType, "服务层删除原料失败", id) } logger.Infof("%s: 原料删除成功, ID: %d", actionType, id) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "原料删除成功", nil, actionType, "原料删除成功", id) } // GetRawMaterial godoc // @Summary 获取原料详情 // @Description 根据ID获取单个原料的详细信息。 // @Tags 饲料管理-原料 // @Security BearerAuth // @Produce json // @Param id path int true "原料ID" // @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表成功获取" // @Router /api/v1/feed/raw-materials/{id} [get] func (c *RawMaterialController) GetRawMaterial(ctx echo.Context) error { reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetRawMaterial") 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) } resp, err := c.rawMaterialService.GetRawMaterial(reqCtx, uint32(id)) if err != nil { logger.Errorf("%s: 服务层获取原料详情失败: %v, ID: %d", actionType, err, id) if errors.Is(err, service.ErrRawMaterialNotFound) { return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "原料不存在", id) } return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料详情失败: "+err.Error(), actionType, "服务层获取原料详情失败", id) } logger.Infof("%s: 获取原料详情成功, ID: %d", actionType, id) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料详情成功", resp, actionType, "获取原料详情成功", resp) } // ListRawMaterials godoc // @Summary 获取原料列表 // @Description 获取所有原料的列表,支持分页和过滤。 // @Tags 饲料管理-原料 // @Security BearerAuth // @Produce json // @Param query query dto.ListRawMaterialRequest false "查询参数" // @Success 200 {object} controller.Response{data=dto.ListRawMaterialResponse} "业务码为200代表成功获取列表" // @Router /api/v1/feed/raw-materials [get] func (c *RawMaterialController) ListRawMaterials(ctx echo.Context) error { reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListRawMaterials") const actionType = "获取原料列表" var req dto.ListRawMaterialRequest 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.rawMaterialService.ListRawMaterials(reqCtx, &req) if err != nil { logger.Errorf("%s: 服务层获取原料列表失败: %v", actionType, err) return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取原料列表失败: "+err.Error(), actionType, "服务层获取原料列表失败", nil) } logger.Infof("%s: 获取原料列表成功, 数量: %d", actionType, len(resp.List)) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取原料列表成功", resp, actionType, "获取原料列表成功", resp) } // UpdateRawMaterialNutrients godoc // @Summary 全量更新原料的营养成分 // @Description 根据原料ID,替换其所有的营养成分信息。这是一个覆盖操作。 // @Tags 饲料管理-原料 // @Security BearerAuth // @Accept json // @Produce json // @Param id path int true "原料ID" // @Param nutrients body dto.UpdateRawMaterialNutrientsRequest true "新的营养成分列表" // @Success 200 {object} controller.Response{data=dto.RawMaterialResponse} "业务码为200代表更新成功" // @Router /api/v1/feed/raw-materials/{id}/nutrients [put] func (c *RawMaterialController) UpdateRawMaterialNutrients(ctx echo.Context) error { reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateRawMaterialNutrients") 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.UpdateRawMaterialNutrientsRequest 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.rawMaterialService.UpdateRawMaterialNutrients(reqCtx, uint32(id), &req) if err != nil { logger.Errorf("%s: 服务层更新原料营养成分失败: %v, ID: %d", actionType, err, id) if errors.Is(err, service.ErrRawMaterialNotFound) { 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) }