194 lines
9.3 KiB
Go
194 lines
9.3 KiB
Go
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"
|
|
)
|
|
|
|
// PigBreedController 定义了猪品种相关的控制器
|
|
type PigBreedController struct {
|
|
ctx context.Context
|
|
feedManagementService service.FeedManagementService
|
|
}
|
|
|
|
// NewPigBreedController 创建一个新的 PigBreedController 实例
|
|
func NewPigBreedController(ctx context.Context, feedManagementService service.FeedManagementService) *PigBreedController {
|
|
return &PigBreedController{
|
|
ctx: ctx,
|
|
feedManagementService: feedManagementService,
|
|
}
|
|
}
|
|
|
|
// CreatePigBreed godoc
|
|
// @Summary 创建猪品种
|
|
// @Description 创建一个新的猪品种。
|
|
// @Tags 饲料管理-猪
|
|
// @Security BearerAuth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param pigBreed body dto.CreatePigBreedRequest true "猪品种信息"
|
|
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为201代表创建成功"
|
|
// @Router /api/v1/feed/pig-breeds [post]
|
|
func (c *PigBreedController) CreatePigBreed(ctx echo.Context) error {
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigBreed")
|
|
var req dto.CreatePigBreedRequest
|
|
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.feedManagementService.CreatePigBreed(reqCtx, &req)
|
|
if err != nil {
|
|
logger.Errorf("%s: 服务层创建猪品种失败: %v", actionType, err)
|
|
// 猪品种没有名称冲突的领域错误,这里直接返回内部错误
|
|
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)
|
|
}
|
|
|
|
// UpdatePigBreed godoc
|
|
// @Summary 更新猪品种
|
|
// @Description 根据ID更新猪品种信息。
|
|
// @Tags 饲料管理-猪
|
|
// @Security BearerAuth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "猪品种ID"
|
|
// @Param pigBreed body dto.UpdatePigBreedRequest true "更新后的猪品种信息"
|
|
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为200代表更新成功"
|
|
// @Router /api/v1/feed/pig-breeds/{id} [put]
|
|
func (c *PigBreedController) UpdatePigBreed(ctx echo.Context) error {
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigBreed")
|
|
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.UpdatePigBreedRequest
|
|
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.UpdatePigBreed(reqCtx, uint32(id), &req)
|
|
if err != nil {
|
|
logger.Errorf("%s: 服务层更新猪品种失败: %v, ID: %d", actionType, err, id)
|
|
if errors.Is(err, service.ErrPigBreedNotFound) {
|
|
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)
|
|
}
|
|
|
|
// DeletePigBreed 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/pig-breeds/{id} [delete]
|
|
func (c *PigBreedController) DeletePigBreed(ctx echo.Context) error {
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigBreed")
|
|
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.feedManagementService.DeletePigBreed(reqCtx, uint32(id))
|
|
if err != nil {
|
|
logger.Errorf("%s: 服务层删除猪品种失败: %v, ID: %d", actionType, err, id)
|
|
if errors.Is(err, service.ErrPigBreedNotFound) {
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), actionType, "猪品种不存在", id)
|
|
}
|
|
if errors.Is(err, service.ErrPigBreedInUse) {
|
|
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, 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)
|
|
}
|
|
|
|
// GetPigBreed godoc
|
|
// @Summary 获取猪品种详情
|
|
// @Description 根据ID获取单个猪品种的详细信息。
|
|
// @Tags 饲料管理-猪
|
|
// @Security BearerAuth
|
|
// @Produce json
|
|
// @Param id path int true "猪品种ID"
|
|
// @Success 200 {object} controller.Response{data=dto.PigBreedResponse} "业务码为200代表成功获取"
|
|
// @Router /api/v1/feed/pig-breeds/{id} [get]
|
|
func (c *PigBreedController) GetPigBreed(ctx echo.Context) error {
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigBreed")
|
|
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.feedManagementService.GetPigBreed(reqCtx, uint32(id))
|
|
if err != nil {
|
|
logger.Errorf("%s: 服务层获取猪品种详情失败: %v, ID: %d", actionType, err, id)
|
|
if errors.Is(err, service.ErrPigBreedNotFound) {
|
|
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)
|
|
}
|
|
|
|
// ListPigBreeds godoc
|
|
// @Summary 获取猪品种列表
|
|
// @Description 获取所有猪品种的列表,支持分页和过滤。
|
|
// @Tags 饲料管理-猪
|
|
// @Security BearerAuth
|
|
// @Produce json
|
|
// @Param query query dto.ListPigBreedRequest false "查询参数"
|
|
// @Success 200 {object} controller.Response{data=dto.ListPigBreedResponse} "业务码为200代表成功获取列表"
|
|
// @Router /api/v1/feed/pig-breeds [get]
|
|
func (c *PigBreedController) ListPigBreeds(ctx echo.Context) error {
|
|
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigBreeds")
|
|
const actionType = "获取猪品种列表"
|
|
var req dto.ListPigBreedRequest
|
|
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.ListPigBreeds(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)
|
|
}
|