完成任务4
This commit is contained in:
@@ -7,39 +7,39 @@ import (
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// mapAndSendError 统一映射服务层错误并发送响应。
|
||||
// 这个函数将服务层返回的错误转换为控制器层应返回的HTTP状态码和审计信息。
|
||||
func mapAndSendError(c *PigBatchController, ctx *gin.Context, action string, err error, id uint) {
|
||||
func mapAndSendError(c *PigBatchController, ctx echo.Context, action string, err error, id uint) error {
|
||||
if errors.Is(err, service.ErrPigBatchNotFound) ||
|
||||
errors.Is(err, service.ErrPenNotFound) ||
|
||||
errors.Is(err, service.ErrPenNotAssociatedWithBatch) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
||||
} else if errors.Is(err, service.ErrInvalidOperation) ||
|
||||
errors.Is(err, service.ErrPigBatchActive) ||
|
||||
errors.Is(err, service.ErrPigBatchNotActive) ||
|
||||
errors.Is(err, service.ErrPenOccupiedByOtherBatch) ||
|
||||
errors.Is(err, service.ErrPenStatusInvalidForAllocation) ||
|
||||
errors.Is(err, service.ErrPenNotEmpty) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
} else {
|
||||
c.logger.Errorf("操作[%s]业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, fmt.Sprintf("操作失败: %v", err), action, err.Error(), id)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, fmt.Sprintf("操作失败: %v", err), action, err.Error(), id)
|
||||
}
|
||||
}
|
||||
|
||||
// idExtractorFunc 定义了一个函数类型,用于从gin.Context中提取主ID。
|
||||
type idExtractorFunc func(ctx *gin.Context) (uint, error)
|
||||
// idExtractorFunc 定义了一个函数类型,用于从echo.Context中提取主ID。
|
||||
type idExtractorFunc func(ctx echo.Context) (uint, error)
|
||||
|
||||
// extractOperatorAndPrimaryID 封装了从gin.Context中提取操作员ID和主ID的通用逻辑。
|
||||
// extractOperatorAndPrimaryID 封装了从echo.Context中提取操作员ID和主ID的通用逻辑。
|
||||
// 它负责处理ID提取过程中的错误,并发送相应的HTTP响应。
|
||||
//
|
||||
// 参数:
|
||||
//
|
||||
// c: *PigBatchController - 控制器实例,用于访问其日志。
|
||||
// ctx: *gin.Context - Gin上下文。
|
||||
// ctx: echo.Context - Echo上下文。
|
||||
// action: string - 当前操作的描述,用于日志和审计。
|
||||
// idExtractor: idExtractorFunc - 可选函数,用于从ctx中提取主ID。如果为nil,则尝试从":id"路径参数中提取。
|
||||
//
|
||||
@@ -47,26 +47,24 @@ type idExtractorFunc func(ctx *gin.Context) (uint, error)
|
||||
//
|
||||
// operatorID: uint - 提取到的操作员ID。
|
||||
// primaryID: uint - 提取到的主ID。
|
||||
// ok: bool - 如果ID提取成功且没有发送错误响应,则为true。
|
||||
// err: error - 如果ID提取失败或发送错误响应,则返回错误。
|
||||
func extractOperatorAndPrimaryID(
|
||||
c *PigBatchController,
|
||||
ctx *gin.Context,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
idExtractor idExtractorFunc,
|
||||
) (operatorID uint, primaryID uint, ok bool) {
|
||||
) (operatorID uint, primaryID uint, err error) {
|
||||
// 1. 获取操作员ID
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
operatorID, err = controller.GetOperatorIDFromContext(ctx)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeUnauthorized, "未授权", action, "无法获取操作员ID", nil)
|
||||
return 0, 0, false
|
||||
return 0, 0, controller.SendErrorWithAudit(ctx, controller.CodeUnauthorized, "未授权", action, "无法获取操作员ID", nil)
|
||||
}
|
||||
|
||||
// 2. 提取主ID
|
||||
if idExtractor != nil {
|
||||
primaryID, err = idExtractor(ctx)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", err.Error())
|
||||
return 0, 0, false
|
||||
return 0, 0, controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", err.Error())
|
||||
}
|
||||
} else { // 默认从 ":id" 路径参数提取
|
||||
idParam := ctx.Param("id")
|
||||
@@ -75,165 +73,155 @@ func extractOperatorAndPrimaryID(
|
||||
} else {
|
||||
parsedID, err := strconv.ParseUint(idParam, 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", idParam)
|
||||
return 0, 0, false
|
||||
return 0, 0, controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", idParam)
|
||||
}
|
||||
primaryID = uint(parsedID)
|
||||
}
|
||||
}
|
||||
|
||||
return operatorID, primaryID, true
|
||||
return operatorID, primaryID, nil
|
||||
}
|
||||
|
||||
// handleAPIRequest 封装了控制器中处理带有请求体和路径参数的API请求的通用逻辑。
|
||||
// 它负责请求体绑定、操作员ID获取、服务层调用、错误映射和响应发送。
|
||||
func handleAPIRequest[Req any](
|
||||
c *PigBatchController,
|
||||
ctx *gin.Context,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
reqDTO Req,
|
||||
serviceExecutor func(ctx *gin.Context, operatorID uint, primaryID uint, req Req) error,
|
||||
serviceExecutor func(ctx echo.Context, operatorID uint, primaryID uint, req Req) error,
|
||||
successMsg string,
|
||||
idExtractor idExtractorFunc,
|
||||
) {
|
||||
) error {
|
||||
// 1. 绑定请求体
|
||||
if err := ctx.ShouldBindJSON(&reqDTO); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", reqDTO)
|
||||
return
|
||||
if err := ctx.Bind(&reqDTO); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", reqDTO)
|
||||
}
|
||||
|
||||
// 2. 提取操作员ID和主ID
|
||||
operatorID, primaryID, ok := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if !ok {
|
||||
return // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
operatorID, primaryID, err := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if err != nil {
|
||||
return err // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
}
|
||||
|
||||
// 3. 执行服务层逻辑
|
||||
err := serviceExecutor(ctx, operatorID, primaryID, reqDTO)
|
||||
err = serviceExecutor(ctx, operatorID, primaryID, reqDTO)
|
||||
if err != nil {
|
||||
mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 4. 发送成功响应
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, nil, action, successMsg, primaryID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, nil, action, successMsg, primaryID)
|
||||
}
|
||||
|
||||
// handleNoBodyAPIRequest 封装了处理不带请求体,但有路径参数和操作员ID的API请求的通用逻辑。
|
||||
func handleNoBodyAPIRequest(
|
||||
c *PigBatchController,
|
||||
ctx *gin.Context,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
serviceExecutor func(ctx *gin.Context, operatorID uint, primaryID uint) error,
|
||||
serviceExecutor func(ctx echo.Context, operatorID uint, primaryID uint) error,
|
||||
successMsg string,
|
||||
idExtractor idExtractorFunc,
|
||||
) {
|
||||
) error {
|
||||
// 1. 提取操作员ID和主ID
|
||||
operatorID, primaryID, ok := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if !ok {
|
||||
return // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
operatorID, primaryID, err := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if err != nil {
|
||||
return err // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
}
|
||||
|
||||
// 2. 执行服务层逻辑
|
||||
err := serviceExecutor(ctx, operatorID, primaryID)
|
||||
err = serviceExecutor(ctx, operatorID, primaryID)
|
||||
if err != nil {
|
||||
mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 3. 发送成功响应
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, nil, action, successMsg, primaryID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, nil, action, successMsg, primaryID)
|
||||
}
|
||||
|
||||
// handleAPIRequestWithResponse 封装了控制器中处理带有请求体、路径参数并返回响应DTO的API请求的通用逻辑。
|
||||
func handleAPIRequestWithResponse[Req any, Resp any](
|
||||
c *PigBatchController,
|
||||
ctx *gin.Context,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
reqDTO Req,
|
||||
serviceExecutor func(ctx *gin.Context, operatorID uint, primaryID uint, req Req) (Resp, error), // serviceExecutor现在返回Resp
|
||||
serviceExecutor func(ctx echo.Context, operatorID uint, primaryID uint, req Req) (Resp, error), // serviceExecutor现在返回Resp
|
||||
successMsg string,
|
||||
idExtractor idExtractorFunc,
|
||||
) {
|
||||
) error {
|
||||
// 1. 绑定请求体
|
||||
if err := ctx.ShouldBindJSON(&reqDTO); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, fmt.Sprintf("无效的请求体: %v", err), action, fmt.Sprintf("请求体绑定失败: %v", err), reqDTO)
|
||||
return
|
||||
if err := ctx.Bind(&reqDTO); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, fmt.Sprintf("无效的请求体: %v", err), action, fmt.Sprintf("请求体绑定失败: %v", err), reqDTO)
|
||||
}
|
||||
|
||||
// 2. 提取操作员ID和主ID
|
||||
operatorID, primaryID, ok := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if !ok {
|
||||
return // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
operatorID, primaryID, err := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if err != nil {
|
||||
return err // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
}
|
||||
|
||||
// 3. 执行服务层逻辑
|
||||
respDTO, err := serviceExecutor(ctx, operatorID, primaryID, reqDTO)
|
||||
if err != nil {
|
||||
mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 4. 发送成功响应
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, respDTO, action, successMsg, primaryID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, respDTO, action, successMsg, primaryID)
|
||||
}
|
||||
|
||||
// handleNoBodyAPIRequestWithResponse 封装了处理不带请求体,但有路径参数和操作员ID,并返回响应DTO的API请求的通用逻辑。
|
||||
func handleNoBodyAPIRequestWithResponse[Resp any](
|
||||
c *PigBatchController,
|
||||
ctx *gin.Context,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
serviceExecutor func(ctx *gin.Context, operatorID uint, primaryID uint) (Resp, error), // serviceExecutor现在返回Resp
|
||||
serviceExecutor func(ctx echo.Context, operatorID uint, primaryID uint) (Resp, error), // serviceExecutor现在返回Resp
|
||||
successMsg string,
|
||||
idExtractor idExtractorFunc,
|
||||
) {
|
||||
) error {
|
||||
// 1. 提取操作员ID和主ID
|
||||
operatorID, primaryID, ok := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if !ok {
|
||||
return // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
operatorID, primaryID, err := extractOperatorAndPrimaryID(c, ctx, action, idExtractor)
|
||||
if err != nil {
|
||||
return err // 错误已在 extractOperatorAndPrimaryID 中处理
|
||||
}
|
||||
|
||||
// 2. 执行服务层逻辑
|
||||
respDTO, err := serviceExecutor(ctx, operatorID, primaryID)
|
||||
if err != nil {
|
||||
mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 3. 发送成功响应
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, respDTO, action, successMsg, primaryID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, respDTO, action, successMsg, primaryID)
|
||||
}
|
||||
|
||||
// handleQueryAPIRequestWithResponse 封装了处理带有查询参数并返回响应DTO的API请求的通用逻辑。
|
||||
func handleQueryAPIRequestWithResponse[Query any, Resp any](
|
||||
c *PigBatchController,
|
||||
ctx *gin.Context,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
queryDTO Query,
|
||||
serviceExecutor func(ctx *gin.Context, operatorID uint, query Query) (Resp, error), // serviceExecutor现在接收queryDTO
|
||||
serviceExecutor func(ctx echo.Context, operatorID uint, query Query) (Resp, error), // serviceExecutor现在接收queryDTO
|
||||
successMsg string,
|
||||
) {
|
||||
) error {
|
||||
// 1. 绑定查询参数
|
||||
if err := ctx.ShouldBindQuery(&queryDTO); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数", action, "查询参数绑定失败", queryDTO)
|
||||
return
|
||||
if err := ctx.Bind(&queryDTO); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数", action, "查询参数绑定失败", queryDTO)
|
||||
}
|
||||
|
||||
// 2. 获取操作员ID
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeUnauthorized, "未授权", action, "无法获取操作员ID", nil)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeUnauthorized, "未授权", action, "无法获取操作员ID", nil)
|
||||
}
|
||||
|
||||
// 3. 执行服务层逻辑
|
||||
respDTO, err := serviceExecutor(ctx, operatorID, queryDTO)
|
||||
if err != nil {
|
||||
// 对于列表查询,通常没有primaryID,所以传递0
|
||||
mapAndSendError(c, ctx, action, err, 0)
|
||||
return
|
||||
return mapAndSendError(c, ctx, action, err, 0)
|
||||
}
|
||||
|
||||
// 4. 发送成功响应
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, respDTO, action, successMsg, nil)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, successMsg, respDTO, action, successMsg, nil)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// PigBatchController 负责处理猪批次相关的API请求
|
||||
@@ -34,13 +34,13 @@ func NewPigBatchController(logger *logs.Logger, service service.PigBatchService)
|
||||
// @Param body body dto.PigBatchCreateDTO true "猪批次信息"
|
||||
// @Success 201 {object} controller.Response{data=dto.PigBatchResponseDTO} "创建成功"
|
||||
// @Router /api/v1/pig-batches [post]
|
||||
func (c *PigBatchController) CreatePigBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
||||
const action = "创建猪批次"
|
||||
var req dto.PigBatchCreateDTO
|
||||
|
||||
handleAPIRequestWithResponse(
|
||||
return handleAPIRequestWithResponse(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
// 对于创建操作,primaryID通常不从路径中获取,而是由服务层生成
|
||||
return c.service.CreatePigBatch(operatorID, req)
|
||||
},
|
||||
@@ -58,12 +58,12 @@ func (c *PigBatchController) CreatePigBatch(ctx *gin.Context) {
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "获取成功"
|
||||
// @Router /api/v1/pig-batches/{id} [get]
|
||||
func (c *PigBatchController) GetPigBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
||||
const action = "获取猪批次"
|
||||
|
||||
handleNoBodyAPIRequestWithResponse(
|
||||
return handleNoBodyAPIRequestWithResponse(
|
||||
c, ctx, action,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.GetPigBatch(primaryID)
|
||||
},
|
||||
"获取成功",
|
||||
@@ -82,13 +82,13 @@ func (c *PigBatchController) GetPigBatch(ctx *gin.Context) {
|
||||
// @Param body body dto.PigBatchUpdateDTO true "猪批次信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "更新成功"
|
||||
// @Router /api/v1/pig-batches/{id} [put]
|
||||
func (c *PigBatchController) UpdatePigBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
||||
const action = "更新猪批次"
|
||||
var req dto.PigBatchUpdateDTO
|
||||
|
||||
handleAPIRequestWithResponse(
|
||||
return handleAPIRequestWithResponse(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.UpdatePigBatch(primaryID, req)
|
||||
},
|
||||
"更新成功",
|
||||
@@ -105,12 +105,12 @@ func (c *PigBatchController) UpdatePigBatch(ctx *gin.Context) {
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
// @Router /api/v1/pig-batches/{id} [delete]
|
||||
func (c *PigBatchController) DeletePigBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
||||
const action = "删除猪批次"
|
||||
|
||||
handleNoBodyAPIRequest(
|
||||
return handleNoBodyAPIRequest(
|
||||
c, ctx, action,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
||||
return c.service.DeletePigBatch(primaryID)
|
||||
},
|
||||
"删除成功",
|
||||
@@ -127,13 +127,13 @@ func (c *PigBatchController) DeletePigBatch(ctx *gin.Context) {
|
||||
// @Param is_active query bool false "是否活跃 (true/false)"
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PigBatchResponseDTO} "获取成功"
|
||||
// @Router /api/v1/pig-batches [get]
|
||||
func (c *PigBatchController) ListPigBatches(ctx *gin.Context) {
|
||||
func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
||||
const action = "获取猪批次列表"
|
||||
var query dto.PigBatchQueryDTO
|
||||
|
||||
handleQueryAPIRequestWithResponse(
|
||||
return handleQueryAPIRequestWithResponse(
|
||||
c, ctx, action, &query,
|
||||
func(ctx *gin.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
|
||||
func(ctx echo.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.ListPigBatches(query.IsActive)
|
||||
},
|
||||
"获取成功",
|
||||
@@ -151,13 +151,13 @@ func (c *PigBatchController) ListPigBatches(ctx *gin.Context) {
|
||||
// @Param body body dto.AssignEmptyPensToBatchRequest true "待分配的猪栏ID列表"
|
||||
// @Success 200 {object} controller.Response "分配成功"
|
||||
// @Router /api/v1/pig-batches/assign-pens/{id} [post]
|
||||
func (c *PigBatchController) AssignEmptyPensToBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
||||
const action = "为猪批次分配空栏"
|
||||
var req dto.AssignEmptyPensToBatchRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
|
||||
return c.service.AssignEmptyPensToBatch(primaryID, req.PenIDs, operatorID)
|
||||
},
|
||||
"分配成功",
|
||||
@@ -176,18 +176,18 @@ func (c *PigBatchController) AssignEmptyPensToBatch(ctx *gin.Context) {
|
||||
// @Param body body dto.ReclassifyPenToNewBatchRequest true "划拨请求信息 (包含目标批次ID、猪栏ID和备注)"
|
||||
// @Success 200 {object} controller.Response "划拨成功"
|
||||
// @Router /api/v1/pig-batches/reclassify-pen/{fromBatchID} [post]
|
||||
func (c *PigBatchController) ReclassifyPenToNewBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
||||
const action = "划拨猪栏到新批次"
|
||||
var req dto.ReclassifyPenToNewBatchRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.ReclassifyPenToNewBatchRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.ReclassifyPenToNewBatchRequest) error {
|
||||
// primaryID 在这里是 fromBatchID
|
||||
return c.service.ReclassifyPenToNewBatch(primaryID, req.ToBatchID, req.PenID, operatorID, req.Remarks)
|
||||
},
|
||||
"划拨成功",
|
||||
func(ctx *gin.Context) (uint, error) { // 自定义ID提取器,从 ":fromBatchID" 路径参数提取
|
||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":fromBatchID" 路径参数提取
|
||||
idParam := ctx.Param("fromBatchID")
|
||||
parsedID, err := strconv.ParseUint(idParam, 10, 32)
|
||||
if err != nil {
|
||||
@@ -208,22 +208,22 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx *gin.Context) {
|
||||
// @Param penID path int true "待移除的猪栏ID"
|
||||
// @Success 200 {object} controller.Response "移除成功"
|
||||
// @Router /api/v1/pig-batches/remove-pen/{penID}/{batchID} [delete]
|
||||
func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
||||
const action = "从猪批次移除空栏"
|
||||
|
||||
handleNoBodyAPIRequest(
|
||||
return handleNoBodyAPIRequest(
|
||||
c, ctx, action,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
||||
// primaryID 在这里是 batchID
|
||||
penIDParam := ctx.Param("penID")
|
||||
penID, err := strconv.ParseUint(penIDParam, 10, 32)
|
||||
parsedPenID, err := strconv.ParseUint(penIDParam, 10, 32)
|
||||
if err != nil {
|
||||
return err // 返回错误,因为 penID 格式无效
|
||||
}
|
||||
return c.service.RemoveEmptyPenFromBatch(primaryID, uint(penID))
|
||||
return c.service.RemoveEmptyPenFromBatch(primaryID, uint(parsedPenID))
|
||||
},
|
||||
"移除成功",
|
||||
func(ctx *gin.Context) (uint, error) { // 自定义ID提取器,从 ":batchID" 路径参数提取
|
||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":batchID" 路径参数提取
|
||||
idParam := ctx.Param("batchID")
|
||||
parsedID, err := strconv.ParseUint(idParam, 10, 32)
|
||||
if err != nil {
|
||||
@@ -245,13 +245,13 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx *gin.Context) {
|
||||
// @Param body body dto.MovePigsIntoPenRequest true "移入猪只请求信息 (包含目标猪栏ID、数量和备注)"
|
||||
// @Success 200 {object} controller.Response "移入成功"
|
||||
// @Router /api/v1/pig-batches/move-pigs-into-pen/{id} [post]
|
||||
func (c *PigBatchController) MovePigsIntoPen(ctx *gin.Context) {
|
||||
func (c *PigBatchController) MovePigsIntoPen(ctx echo.Context) error {
|
||||
const action = "将猪只移入猪栏"
|
||||
var req dto.MovePigsIntoPenRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
|
||||
return c.service.MovePigsIntoPen(primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
"移入成功",
|
||||
|
||||
@@ -2,7 +2,7 @@ package management
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// RecordSickPigs godoc
|
||||
@@ -16,13 +16,13 @@ import (
|
||||
// @Param body body dto.RecordSickPigsRequest true "记录病猪请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pigs/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigs(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
||||
const action = "记录新增病猪事件"
|
||||
var req dto.RecordSickPigsRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigsRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigsRequest) error {
|
||||
return c.service.RecordSickPigs(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
@@ -41,13 +41,13 @@ func (c *PigBatchController) RecordSickPigs(ctx *gin.Context) {
|
||||
// @Param body body dto.RecordSickPigRecoveryRequest true "记录病猪康复请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pig-recovery/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigRecovery(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
||||
const action = "记录病猪康复事件"
|
||||
var req dto.RecordSickPigRecoveryRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigRecoveryRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigRecoveryRequest) error {
|
||||
return c.service.RecordSickPigRecovery(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
@@ -66,13 +66,13 @@ func (c *PigBatchController) RecordSickPigRecovery(ctx *gin.Context) {
|
||||
// @Param body body dto.RecordSickPigDeathRequest true "记录病猪死亡请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pig-death/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigDeath(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
||||
const action = "记录病猪死亡事件"
|
||||
var req dto.RecordSickPigDeathRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigDeathRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigDeathRequest) error {
|
||||
return c.service.RecordSickPigDeath(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
@@ -91,13 +91,13 @@ func (c *PigBatchController) RecordSickPigDeath(ctx *gin.Context) {
|
||||
// @Param body body dto.RecordSickPigCullRequest true "记录病猪淘汰请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pig-cull/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigCull(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
||||
const action = "记录病猪淘汰事件"
|
||||
var req dto.RecordSickPigCullRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigCullRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigCullRequest) error {
|
||||
return c.service.RecordSickPigCull(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
@@ -116,13 +116,13 @@ func (c *PigBatchController) RecordSickPigCull(ctx *gin.Context) {
|
||||
// @Param body body dto.RecordDeathRequest true "记录正常猪只死亡请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-death/{id} [post]
|
||||
func (c *PigBatchController) RecordDeath(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
||||
const action = "记录正常猪只死亡事件"
|
||||
var req dto.RecordDeathRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.RecordDeathRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordDeathRequest) error {
|
||||
return c.service.RecordDeath(operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
@@ -141,13 +141,13 @@ func (c *PigBatchController) RecordDeath(ctx *gin.Context) {
|
||||
// @Param body body dto.RecordCullRequest true "记录正常猪只淘汰请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-cull/{id} [post]
|
||||
func (c *PigBatchController) RecordCull(ctx *gin.Context) {
|
||||
func (c *PigBatchController) RecordCull(ctx echo.Context) error {
|
||||
const action = "记录正常猪只淘汰事件"
|
||||
var req dto.RecordCullRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.RecordCullRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordCullRequest) error {
|
||||
return c.service.RecordCull(operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
|
||||
@@ -2,7 +2,7 @@ package management
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// SellPigs godoc
|
||||
@@ -16,13 +16,13 @@ import (
|
||||
// @Param body body dto.SellPigsRequest true "卖猪请求信息"
|
||||
// @Success 200 {object} controller.Response "卖猪成功"
|
||||
// @Router /api/v1/pig-batches/sell-pigs/{id} [post]
|
||||
func (c *PigBatchController) SellPigs(ctx *gin.Context) {
|
||||
func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
||||
const action = "卖猪"
|
||||
var req dto.SellPigsRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.SellPigsRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.SellPigsRequest) error {
|
||||
return c.service.SellPigs(primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
},
|
||||
"卖猪成功",
|
||||
@@ -41,13 +41,13 @@ func (c *PigBatchController) SellPigs(ctx *gin.Context) {
|
||||
// @Param body body dto.BuyPigsRequest true "买猪请求信息"
|
||||
// @Success 200 {object} controller.Response "买猪成功"
|
||||
// @Router /api/v1/pig-batches/buy-pigs/{id} [post]
|
||||
func (c *PigBatchController) BuyPigs(ctx *gin.Context) {
|
||||
func (c *PigBatchController) BuyPigs(ctx echo.Context) error {
|
||||
const action = "买猪"
|
||||
var req dto.BuyPigsRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.BuyPigsRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.BuyPigsRequest) error {
|
||||
return c.service.BuyPigs(primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
},
|
||||
"买猪成功",
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// TransferPigsAcrossBatches godoc
|
||||
@@ -18,18 +18,18 @@ import (
|
||||
// @Param body body dto.TransferPigsAcrossBatchesRequest true "跨群调栏请求信息"
|
||||
// @Success 200 {object} controller.Response "调栏成功"
|
||||
// @Router /api/v1/pig-batches/transfer-across-batches/{sourceBatchID} [post]
|
||||
func (c *PigBatchController) TransferPigsAcrossBatches(ctx *gin.Context) {
|
||||
func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
||||
const action = "跨猪群调栏"
|
||||
var req dto.TransferPigsAcrossBatchesRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.TransferPigsAcrossBatchesRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsAcrossBatchesRequest) error {
|
||||
// primaryID 在这里是 sourceBatchID
|
||||
return c.service.TransferPigsAcrossBatches(primaryID, req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
"调栏成功",
|
||||
func(ctx *gin.Context) (uint, error) { // 自定义ID提取器,从 ":sourceBatchID" 路径参数提取
|
||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":sourceBatchID" 路径参数提取
|
||||
idParam := ctx.Param("sourceBatchID")
|
||||
parsedID, err := strconv.ParseUint(idParam, 10, 32)
|
||||
if err != nil {
|
||||
@@ -51,13 +51,13 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx *gin.Context) {
|
||||
// @Param body body dto.TransferPigsWithinBatchRequest true "群内调栏请求信息"
|
||||
// @Success 200 {object} controller.Response "调栏成功"
|
||||
// @Router /api/v1/pig-batches/transfer-within-batch/{id} [post]
|
||||
func (c *PigBatchController) TransferPigsWithinBatch(ctx *gin.Context) {
|
||||
func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
||||
const action = "群内调栏"
|
||||
var req dto.TransferPigsWithinBatchRequest
|
||||
|
||||
handleAPIRequest(
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.TransferPigsWithinBatchRequest) error {
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsWithinBatchRequest) error {
|
||||
// primaryID 在这里是 batchID
|
||||
return c.service.TransferPigsWithinBatch(primaryID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
|
||||
@@ -8,7 +8,7 @@ import (
|
||||
"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/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// --- 控制器定义 ---
|
||||
@@ -31,7 +31,7 @@ func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *
|
||||
|
||||
// CreatePigHouse godoc
|
||||
// @Summary 创建猪舍
|
||||
// @Description 创建一个新的猪舍
|
||||
// @Description 根据提供的信息创建一个新猪舍
|
||||
// @Tags 猪场管理
|
||||
// @Security BearerAuth
|
||||
// @Accept json
|
||||
@@ -39,19 +39,18 @@ func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *
|
||||
// @Param body body dto.CreatePigHouseRequest true "猪舍信息"
|
||||
// @Success 201 {object} controller.Response{data=dto.PigHouseResponse} "创建成功"
|
||||
// @Router /api/v1/pig-houses [post]
|
||||
func (c *PigFarmController) CreatePigHouse(ctx *gin.Context) {
|
||||
func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
||||
const action = "创建猪舍"
|
||||
var req dto.CreatePigHouseRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
house, err := c.service.CreatePigHouse(req.Name, req.Description)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪舍失败", action, "业务逻辑失败", req)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪舍失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PigHouseResponse{
|
||||
@@ -59,7 +58,7 @@ func (c *PigFarmController) CreatePigHouse(ctx *gin.Context) {
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
}
|
||||
|
||||
// GetPigHouse godoc
|
||||
@@ -71,23 +70,20 @@ func (c *PigFarmController) CreatePigHouse(ctx *gin.Context) {
|
||||
// @Param id path int true "猪舍ID"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "获取成功"
|
||||
// @Router /api/v1/pig-houses/{id} [get]
|
||||
func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
|
||||
func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
||||
const action = "获取猪舍"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
house, err := c.service.GetPigHouseByID(uint(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪舍失败", action, "业务逻辑失败", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪舍失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
resp := dto.PigHouseResponse{
|
||||
@@ -95,7 +91,7 @@ func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
}
|
||||
|
||||
// ListPigHouses godoc
|
||||
@@ -106,13 +102,12 @@ func (c *PigFarmController) GetPigHouse(ctx *gin.Context) {
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PigHouseResponse} "获取成功"
|
||||
// @Router /api/v1/pig-houses [get]
|
||||
func (c *PigFarmController) ListPigHouses(ctx *gin.Context) {
|
||||
func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
||||
const action = "获取猪舍列表"
|
||||
houses, err := c.service.ListPigHouses()
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
}
|
||||
|
||||
var resp []dto.PigHouseResponse
|
||||
@@ -124,7 +119,7 @@ func (c *PigFarmController) ListPigHouses(ctx *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, action, "获取成功", resp)
|
||||
}
|
||||
|
||||
// UpdatePigHouse godoc
|
||||
@@ -138,29 +133,25 @@ func (c *PigFarmController) ListPigHouses(ctx *gin.Context) {
|
||||
// @Param body body dto.UpdatePigHouseRequest true "猪舍信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "更新成功"
|
||||
// @Router /api/v1/pig-houses/{id} [put]
|
||||
func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
|
||||
func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
||||
const action = "更新猪舍"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
var req dto.UpdatePigHouseRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
house, err := c.service.UpdatePigHouse(uint(id), req.Name, req.Description)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PigHouseResponse{
|
||||
@@ -168,7 +159,7 @@ func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
|
||||
Name: house.Name,
|
||||
Description: house.Description,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
}
|
||||
|
||||
// DeletePigHouse godoc
|
||||
@@ -180,30 +171,26 @@ func (c *PigFarmController) UpdatePigHouse(ctx *gin.Context) {
|
||||
// @Param id path int true "猪舍ID"
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
// @Router /api/v1/pig-houses/{id} [delete]
|
||||
func (c *PigFarmController) DeletePigHouse(ctx *gin.Context) {
|
||||
func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
||||
const action = "删除猪舍"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
if err := c.service.DeletePigHouse(uint(id)); err != nil {
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
}
|
||||
// 检查是否是业务逻辑错误
|
||||
if errors.Is(err, service.ErrHouseContainsPens) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, action, "删除成功", id)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, action, "删除成功", id)
|
||||
}
|
||||
|
||||
// --- 猪栏 (Pen) API 实现 ---
|
||||
@@ -218,24 +205,21 @@ func (c *PigFarmController) DeletePigHouse(ctx *gin.Context) {
|
||||
// @Param body body dto.CreatePenRequest true "猪栏信息"
|
||||
// @Success 201 {object} controller.Response{data=dto.PenResponse} "创建成功"
|
||||
// @Router /api/v1/pens [post]
|
||||
func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
||||
const action = "创建猪栏"
|
||||
var req dto.CreatePenRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
pen, err := c.service.CreatePen(req.PenNumber, req.HouseID, req.Capacity)
|
||||
if err != nil {
|
||||
// 检查是否是业务逻辑错误
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), req)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), req)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
@@ -245,7 +229,7 @@ func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
Capacity: pen.Capacity,
|
||||
Status: pen.Status,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", resp, action, "创建成功", resp)
|
||||
}
|
||||
|
||||
// GetPen godoc
|
||||
@@ -257,26 +241,23 @@ func (c *PigFarmController) CreatePen(ctx *gin.Context) {
|
||||
// @Param id path int true "猪栏ID"
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "获取成功"
|
||||
// @Router /api/v1/pens/{id} [get]
|
||||
func (c *PigFarmController) GetPen(ctx *gin.Context) {
|
||||
func (c *PigFarmController) GetPen(ctx echo.Context) error {
|
||||
const action = "获取猪栏"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
pen, err := c.service.GetPenByID(uint(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪栏失败", action, "业务逻辑失败", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪栏失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", pen, action, "获取成功", pen)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", pen, action, "获取成功", pen)
|
||||
}
|
||||
|
||||
// ListPens godoc
|
||||
@@ -287,16 +268,15 @@ func (c *PigFarmController) GetPen(ctx *gin.Context) {
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PenResponse} "获取成功"
|
||||
// @Router /api/v1/pens [get]
|
||||
func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
func (c *PigFarmController) ListPens(ctx echo.Context) error {
|
||||
const action = "获取猪栏列表"
|
||||
pens, err := c.service.ListPens()
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", pens, action, "获取成功", pens)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", pens, action, "获取成功", pens)
|
||||
}
|
||||
|
||||
// UpdatePen godoc
|
||||
@@ -310,30 +290,26 @@ func (c *PigFarmController) ListPens(ctx *gin.Context) {
|
||||
// @Param body body dto.UpdatePenRequest true "猪栏信息"
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||
// @Router /api/v1/pens/{id} [put]
|
||||
func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
||||
const action = "更新猪栏"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
var req dto.UpdatePenRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
pen, err := c.service.UpdatePen(uint(id), req.PenNumber, req.HouseID, req.Capacity, req.Status)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
}
|
||||
// 其他业务逻辑错误可以在这里添加处理
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
@@ -344,7 +320,7 @@ func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
}
|
||||
|
||||
// DeletePen godoc
|
||||
@@ -356,30 +332,26 @@ func (c *PigFarmController) UpdatePen(ctx *gin.Context) {
|
||||
// @Param id path int true "猪栏ID"
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
// @Router /api/v1/pens/{id} [delete]
|
||||
func (c *PigFarmController) DeletePen(ctx *gin.Context) {
|
||||
func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
||||
const action = "删除猪栏"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
if err := c.service.DeletePen(uint(id)); err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
}
|
||||
// 检查是否是业务逻辑错误
|
||||
if errors.Is(err, service.ErrPenInUse) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, action, "删除成功", id)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, action, "删除成功", id)
|
||||
}
|
||||
|
||||
// UpdatePenStatus godoc
|
||||
@@ -393,32 +365,27 @@ func (c *PigFarmController) DeletePen(ctx *gin.Context) {
|
||||
// @Param body body dto.UpdatePenStatusRequest true "新的猪栏状态"
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||
// @Router /api/v1/pens/{id}/status [put]
|
||||
func (c *PigFarmController) UpdatePenStatus(ctx *gin.Context) {
|
||||
func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
||||
const action = "更新猪栏状态"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
var req dto.UpdatePenStatusRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
pen, err := c.service.UpdatePenStatus(uint(id), req.Status)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
||||
} else if errors.Is(err, service.ErrPenStatusInvalidForOccupiedPen) || errors.Is(err, service.ErrPenStatusInvalidForUnoccupiedPen) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
|
||||
return
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
|
||||
}
|
||||
|
||||
resp := dto.PenResponse{
|
||||
@@ -429,5 +396,5 @@ func (c *PigFarmController) UpdatePenStatus(ctx *gin.Context) {
|
||||
Status: pen.Status,
|
||||
PigBatchID: pen.PigBatchID,
|
||||
}
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", resp, action, "更新成功", resp)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user