261 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			261 lines
		
	
	
		
			9.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package management
 | ||
| 
 | ||
| import (
 | ||
| 	"strconv"
 | ||
| 
 | ||
| 	"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         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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
 | ||
| 
 | ||
| 	handleAPIRequestWithResponse(
 | ||
| 		c, ctx, action, &req,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
 | ||
| 			// 对于创建操作,primaryID通常不从路径中获取,而是由服务层生成
 | ||
| 			return c.service.CreatePigBatch(operatorID, req)
 | ||
| 		},
 | ||
| 		"创建成功",
 | ||
| 		nil, // 无需自定义ID提取器,primaryID将为0
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // GetPigBatch godoc
 | ||
| // @Summary      获取单个猪批次
 | ||
| // @Description  根据ID获取单个猪批次信息
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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 = "获取猪批次"
 | ||
| 
 | ||
| 	handleNoBodyAPIRequestWithResponse(
 | ||
| 		c, ctx, action,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
 | ||
| 			return c.service.GetPigBatch(primaryID)
 | ||
| 		},
 | ||
| 		"获取成功",
 | ||
| 		nil, // 默认从 ":id" 路径参数提取ID
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // UpdatePigBatch godoc
 | ||
| // @Summary      更新猪批次
 | ||
| // @Description  更新一个已存在的猪批次信息
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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 = "更新猪批次"
 | ||
| 	var req dto.PigBatchUpdateDTO
 | ||
| 
 | ||
| 	handleAPIRequestWithResponse(
 | ||
| 		c, ctx, action, &req,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
 | ||
| 			return c.service.UpdatePigBatch(primaryID, req)
 | ||
| 		},
 | ||
| 		"更新成功",
 | ||
| 		nil, // 默认从 ":id" 路径参数提取ID
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // DeletePigBatch godoc
 | ||
| // @Summary      删除猪批次
 | ||
| // @Description  根据ID删除一个猪批次
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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 = "删除猪批次"
 | ||
| 
 | ||
| 	handleNoBodyAPIRequest(
 | ||
| 		c, ctx, action,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint) error {
 | ||
| 			return c.service.DeletePigBatch(primaryID)
 | ||
| 		},
 | ||
| 		"删除成功",
 | ||
| 		nil, // 默认从 ":id" 路径参数提取ID
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // ListPigBatches godoc
 | ||
| // @Summary      获取猪批次列表
 | ||
| // @Description  获取所有猪批次的列表,支持按活跃状态筛选
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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
 | ||
| 
 | ||
| 	handleQueryAPIRequestWithResponse(
 | ||
| 		c, ctx, action, &query,
 | ||
| 		func(ctx *gin.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
 | ||
| 			return c.service.ListPigBatches(query.IsActive)
 | ||
| 		},
 | ||
| 		"获取成功",
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // AssignEmptyPensToBatch godoc
 | ||
| // @Summary      为猪批次分配空栏
 | ||
| // @Description  将一个或多个空闲猪栏分配给指定的猪批次
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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/assign-pens/{id} [post]
 | ||
| func (c *PigBatchController) AssignEmptyPensToBatch(ctx *gin.Context) {
 | ||
| 	const action = "为猪批次分配空栏"
 | ||
| 	var req dto.AssignEmptyPensToBatchRequest
 | ||
| 
 | ||
| 	handleAPIRequest(
 | ||
| 		c, ctx, action, &req,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
 | ||
| 			return c.service.AssignEmptyPensToBatch(primaryID, req.PenIDs, operatorID)
 | ||
| 		},
 | ||
| 		"分配成功",
 | ||
| 		nil, // 默认从 ":id" 路径参数提取ID
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // ReclassifyPenToNewBatch godoc
 | ||
| // @Summary      将猪栏划拨到新批次
 | ||
| // @Description  将一个猪栏(连同其中的猪只)从一个批次整体划拨到另一个批次
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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/reclassify-pen/{fromBatchID} [post]
 | ||
| func (c *PigBatchController) ReclassifyPenToNewBatch(ctx *gin.Context) {
 | ||
| 	const action = "划拨猪栏到新批次"
 | ||
| 	var req dto.ReclassifyPenToNewBatchRequest
 | ||
| 
 | ||
| 	handleAPIRequest(
 | ||
| 		c, ctx, action, &req,
 | ||
| 		func(ctx *gin.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" 路径参数提取
 | ||
| 			idParam := ctx.Param("fromBatchID")
 | ||
| 			parsedID, err := strconv.ParseUint(idParam, 10, 32)
 | ||
| 			if err != nil {
 | ||
| 				return 0, err
 | ||
| 			}
 | ||
| 			return uint(parsedID), nil
 | ||
| 		},
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // RemoveEmptyPenFromBatch godoc
 | ||
| // @Summary      从猪批次移除空栏
 | ||
| // @Description  将一个空闲猪栏从指定的猪批次中移除
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @Produce      json
 | ||
| // @Param        batchID path int true "猪批次ID"
 | ||
| // @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) {
 | ||
| 	const action = "从猪批次移除空栏"
 | ||
| 
 | ||
| 	handleNoBodyAPIRequest(
 | ||
| 		c, ctx, action,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint) error {
 | ||
| 			// primaryID 在这里是 batchID
 | ||
| 			penIDParam := ctx.Param("penID")
 | ||
| 			penID, err := strconv.ParseUint(penIDParam, 10, 32)
 | ||
| 			if err != nil {
 | ||
| 				return err // 返回错误,因为 penID 格式无效
 | ||
| 			}
 | ||
| 			return c.service.RemoveEmptyPenFromBatch(primaryID, uint(penID))
 | ||
| 		},
 | ||
| 		"移除成功",
 | ||
| 		func(ctx *gin.Context) (uint, error) { // 自定义ID提取器,从 ":batchID" 路径参数提取
 | ||
| 			idParam := ctx.Param("batchID")
 | ||
| 			parsedID, err := strconv.ParseUint(idParam, 10, 32)
 | ||
| 			if err != nil {
 | ||
| 				return 0, err
 | ||
| 			}
 | ||
| 			return uint(parsedID), nil
 | ||
| 		},
 | ||
| 	)
 | ||
| }
 | ||
| 
 | ||
| // MovePigsIntoPen godoc
 | ||
| // @Summary      将猪只从“虚拟库存”移入指定猪栏
 | ||
| // @Description  将指定数量的猪只从批次的“虚拟库存”移入一个已分配的猪栏
 | ||
| // @Tags         猪群管理
 | ||
| // @Security     BearerAuth
 | ||
| // @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/move-pigs-into-pen/{id} [post]
 | ||
| func (c *PigBatchController) MovePigsIntoPen(ctx *gin.Context) {
 | ||
| 	const action = "将猪只移入猪栏"
 | ||
| 	var req dto.MovePigsIntoPenRequest
 | ||
| 
 | ||
| 	handleAPIRequest(
 | ||
| 		c, ctx, action, &req,
 | ||
| 		func(ctx *gin.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
 | ||
| 			return c.service.MovePigsIntoPen(primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
 | ||
| 		},
 | ||
| 		"移入成功",
 | ||
| 		nil, // 默认从 ":id" 路径参数提取ID
 | ||
| 	)
 | ||
| }
 |