350 lines
15 KiB
Go
350 lines
15 KiB
Go
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"
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// PigBatchController 负责处理猪批次相关的API请求
|
|
type PigBatchController struct {
|
|
logger *logs.Logger
|
|
service service.PigBatchService
|
|
}
|
|
|
|
// NewPigBatchController 创建一个新的 PigBatchController 实例
|
|
func NewPigBatchController(logger *logs.Logger, service service.PigBatchService) *PigBatchController {
|
|
return &PigBatchController{
|
|
logger: logger,
|
|
service: service,
|
|
}
|
|
}
|
|
|
|
// CreatePigBatch godoc
|
|
// @Summary 创建猪批次
|
|
// @Description 创建一个新的猪批次
|
|
// @Tags 猪批次管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @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) {
|
|
const action = "创建猪批次"
|
|
var req dto.PigBatchCreateDTO
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
|
return
|
|
}
|
|
|
|
userID, err := controller.GetOperatorIDFromContext(ctx)
|
|
|
|
respDTO, err := c.service.CreatePigBatch(userID, &req)
|
|
if err != nil {
|
|
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪批次失败", action, "业务逻辑失败", req)
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "创建成功", respDTO, action, "创建成功", respDTO)
|
|
}
|
|
|
|
// GetPigBatch godoc
|
|
// @Summary 获取单个猪批次
|
|
// @Description 根据ID获取单个猪批次信息
|
|
// @Tags 猪批次管理
|
|
// @Produce json
|
|
// @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) {
|
|
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
|
|
}
|
|
|
|
respDTO, err := c.service.GetPigBatch(uint(id))
|
|
if err != nil {
|
|
if errors.Is(err, service.ErrPigBatchNotFound) {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪批次不存在", action, "猪批次不存在", id)
|
|
return
|
|
}
|
|
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪批次失败", action, "业务逻辑失败", id)
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", respDTO, action, "获取成功", respDTO)
|
|
}
|
|
|
|
// UpdatePigBatch godoc
|
|
// @Summary 更新猪批次
|
|
// @Description 更新一个已存在的猪批次信息
|
|
// @Tags 猪批次管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "猪批次ID"
|
|
// @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) {
|
|
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
|
|
}
|
|
|
|
var req dto.PigBatchUpdateDTO
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
|
return
|
|
}
|
|
|
|
respDTO, err := c.service.UpdatePigBatch(uint(id), &req)
|
|
if err != nil {
|
|
if errors.Is(err, service.ErrPigBatchNotFound) {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪批次不存在", action, "猪批次不存在", id)
|
|
return
|
|
}
|
|
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪批次失败", action, "业务逻辑失败", req)
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", respDTO, action, "更新成功", respDTO)
|
|
}
|
|
|
|
// DeletePigBatch godoc
|
|
// @Summary 删除猪批次
|
|
// @Description 根据ID删除一个猪批次
|
|
// @Tags 猪批次管理
|
|
// @Produce json
|
|
// @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) {
|
|
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
|
|
}
|
|
|
|
if err := c.service.DeletePigBatch(uint(id)); err != nil {
|
|
if errors.Is(err, service.ErrPigBatchNotFound) {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪批次不存在", action, "猪批次不存在", id)
|
|
return
|
|
}
|
|
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除猪批次失败", action, "业务逻辑失败", id)
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, action, "删除成功", id)
|
|
}
|
|
|
|
// ListPigBatches godoc
|
|
// @Summary 获取猪批次列表
|
|
// @Description 获取所有猪批次的列表,支持按活跃状态筛选
|
|
// @Tags 猪批次管理
|
|
// @Produce json
|
|
// @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) {
|
|
const action = "获取猪批次列表"
|
|
var query dto.PigBatchQueryDTO
|
|
// ShouldBindQuery 会自动处理 URL 查询参数,例如 ?is_active=true
|
|
if err := ctx.ShouldBindQuery(&query); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数", action, "查询参数绑定失败", nil)
|
|
return
|
|
}
|
|
|
|
respDTOs, err := c.service.ListPigBatches(query.IsActive)
|
|
if err != nil {
|
|
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪批次列表失败", action, "业务逻辑失败", nil)
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", respDTOs, action, "获取成功", respDTOs)
|
|
}
|
|
|
|
// AssignEmptyPensToBatch godoc
|
|
// @Summary 为猪批次分配空栏
|
|
// @Description 将一个或多个空闲猪栏分配给指定的猪批次
|
|
// @Tags 猪批次管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "猪批次ID"
|
|
// @Param body body dto.AssignEmptyPensToBatchRequest true "待分配的猪栏ID列表"
|
|
// @Success 200 {object} controller.Response "分配成功"
|
|
// @Router /api/v1/pig-batches/{id}/assign-pens [post]
|
|
func (c *PigBatchController) AssignEmptyPensToBatch(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.AssignEmptyPensToBatchRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
|
return
|
|
}
|
|
|
|
userID, err := controller.GetOperatorIDFromContext(ctx)
|
|
|
|
err = c.service.AssignEmptyPensToBatch(uint(batchID), req.PenIDs, userID)
|
|
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.ErrPigBatchNotActive) || errors.Is(err, service.ErrPenOccupiedByOtherBatch) || errors.Is(err, service.ErrPenStatusInvalidForAllocation) {
|
|
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)
|
|
}
|
|
|
|
// ReclassifyPenToNewBatch godoc
|
|
// @Summary 将猪栏划拨到新批次
|
|
// @Description 将一个猪栏(连同其中的猪只)从一个批次整体划拨到另一个批次
|
|
// @Tags 猪批次管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param fromBatchID path int true "源猪批次ID"
|
|
// @Param body body dto.ReclassifyPenToNewBatchRequest true "划拨请求信息 (包含目标批次ID、猪栏ID和备注)"
|
|
// @Success 200 {object} controller.Response "划拨成功"
|
|
// @Router /api/v1/pig-batches/{fromBatchID}/reclassify-pen [post]
|
|
func (c *PigBatchController) ReclassifyPenToNewBatch(ctx *gin.Context) {
|
|
const action = "划拨猪栏到新批次"
|
|
fromBatchID, err := strconv.ParseUint(ctx.Param("fromBatchID"), 10, 32)
|
|
if err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的源猪批次ID格式", action, "ID格式错误", ctx.Param("fromBatchID"))
|
|
return
|
|
}
|
|
|
|
var req dto.ReclassifyPenToNewBatchRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
|
return
|
|
}
|
|
|
|
userID, err := controller.GetOperatorIDFromContext(ctx)
|
|
|
|
err = c.service.ReclassifyPenToNewBatch(uint(fromBatchID), req.ToBatchID, req.PenID, userID, 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(), fromBatchID)
|
|
return
|
|
} else if errors.Is(err, service.ErrPigBatchNotActive) || errors.Is(err, service.ErrPenOccupiedByOtherBatch) || errors.Is(err, service.ErrPenNotAssociatedWithBatch) || errors.Is(err, service.ErrInvalidOperation) {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), fromBatchID)
|
|
return
|
|
}
|
|
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "划拨猪栏失败", action, err.Error(), fromBatchID)
|
|
return
|
|
}
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "划拨成功", nil, action, "划拨成功", fromBatchID)
|
|
}
|
|
|
|
// RemoveEmptyPenFromBatch godoc
|
|
// @Summary 从猪批次移除空栏
|
|
// @Description 将一个空闲猪栏从指定的猪批次中移除
|
|
// @Tags 猪批次管理
|
|
// @Produce json
|
|
// @Param batchID path int true "猪批次ID"
|
|
// @Param penID path int true "待移除的猪栏ID"
|
|
// @Success 200 {object} controller.Response "移除成功"
|
|
// @Router /api/v1/pig-batches/{batchID}/remove-pen/{penID} [delete]
|
|
func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx *gin.Context) {
|
|
const action = "从猪批次移除空栏"
|
|
batchID, err := strconv.ParseUint(ctx.Param("batchID"), 10, 32)
|
|
if err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪批次ID格式", action, "ID格式错误", ctx.Param("batchID"))
|
|
return
|
|
}
|
|
|
|
penID, err := strconv.ParseUint(ctx.Param("penID"), 10, 32)
|
|
if err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的猪栏ID格式", action, "ID格式错误", ctx.Param("penID"))
|
|
return
|
|
}
|
|
|
|
err = c.service.RemoveEmptyPenFromBatch(uint(batchID), uint(penID))
|
|
if err != nil {
|
|
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(), batchID)
|
|
return
|
|
} else if errors.Is(err, service.ErrPigBatchNotActive) || errors.Is(err, service.ErrPenNotEmpty) {
|
|
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)
|
|
}
|
|
|
|
// MovePigsIntoPen godoc
|
|
// @Summary 将猪只从“虚拟库存”移入指定猪栏
|
|
// @Description 将指定数量的猪只从批次的“虚拟库存”移入一个已分配的猪栏
|
|
// @Tags 猪批次管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "猪批次ID"
|
|
// @Param body body dto.MovePigsIntoPenRequest true "移入猪只请求信息 (包含目标猪栏ID、数量和备注)"
|
|
// @Success 200 {object} controller.Response "移入成功"
|
|
// @Router /api/v1/pig-batches/{id}/move-pigs-into-pen [post]
|
|
func (c *PigBatchController) MovePigsIntoPen(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.MovePigsIntoPenRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
|
return
|
|
}
|
|
|
|
userID, err := controller.GetOperatorIDFromContext(ctx)
|
|
|
|
err = c.service.MovePigsIntoPen(uint(batchID), req.ToPenID, req.Quantity, userID, 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.ErrPigBatchNotActive) || errors.Is(err, service.ErrInvalidOperation) {
|
|
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)
|
|
}
|