简化控制器层重复代码
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"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"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -22,35 +19,24 @@ import (
|
||||
// @Router /api/v1/pig-batches/{sourceBatchID}/transfer-across-batches [post]
|
||||
func (c *PigBatchController) TransferPigsAcrossBatches(ctx *gin.Context) {
|
||||
const action = "跨猪群调栏"
|
||||
sourceBatchID, err := strconv.ParseUint(ctx.Param("sourceBatchID"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的源猪批次ID格式", action, "ID格式错误", ctx.Param("sourceBatchID"))
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.TransferPigsAcrossBatchesRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.TransferPigsAcrossBatches(uint(sourceBatchID), req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPigBatchNotFound) || errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), sourceBatchID)
|
||||
return
|
||||
} else if errors.Is(err, service.ErrInvalidOperation) || errors.Is(err, service.ErrPigBatchNotActive) || errors.Is(err, service.ErrPenNotAssociatedWithBatch) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), sourceBatchID)
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "跨猪群调栏失败", action, err.Error(), sourceBatchID)
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "调栏成功", nil, action, "调栏成功", sourceBatchID)
|
||||
handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.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" 路径参数提取
|
||||
idParam := ctx.Param("sourceBatchID")
|
||||
parsedID, err := strconv.ParseUint(idParam, 10, 32)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint(parsedID), nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// TransferPigsWithinBatch godoc
|
||||
@@ -65,33 +51,15 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx *gin.Context) {
|
||||
// @Router /api/v1/pig-batches/{id}/transfer-within-batch [post]
|
||||
func (c *PigBatchController) TransferPigsWithinBatch(ctx *gin.Context) {
|
||||
const action = "群内调栏"
|
||||
batchID, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪批次ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.TransferPigsWithinBatchRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.TransferPigsWithinBatch(uint(batchID), req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPigBatchNotFound) || errors.Is(err, service.ErrPenNotFound) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), batchID)
|
||||
return
|
||||
} else if errors.Is(err, service.ErrInvalidOperation) || errors.Is(err, service.ErrPigBatchNotActive) || errors.Is(err, service.ErrPenNotAssociatedWithBatch) {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), batchID)
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "群内调栏失败", action, err.Error(), batchID)
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "调栏成功", nil, action, "调栏成功", batchID)
|
||||
handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx *gin.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)
|
||||
},
|
||||
"调栏成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user