猪群领域其他方法映射到api
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// TransferPigsAcrossBatches godoc
|
||||
// @Summary 跨猪群调栏
|
||||
// @Description 将指定数量的猪只从一个猪群的猪栏调动到另一个猪群的猪栏
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param sourceBatchID path int true "源猪批次ID"
|
||||
// @Param body body dto.TransferPigsAcrossBatchesRequest true "跨群调栏请求信息"
|
||||
// @Success 200 {object} controller.Response "调栏成功"
|
||||
// @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)
|
||||
}
|
||||
|
||||
// TransferPigsWithinBatch godoc
|
||||
// @Summary 群内调栏
|
||||
// @Description 将指定数量的猪只在同一个猪群的不同猪栏间调动
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.TransferPigsWithinBatchRequest true "群内调栏请求信息"
|
||||
// @Success 200 {object} controller.Response "调栏成功"
|
||||
// @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)
|
||||
}
|
||||
Reference in New Issue
Block a user