236 lines
10 KiB
Go
236 lines
10 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} "创建成功"
|
|
// @Failure 400 {object} controller.Response "请求参数错误"
|
|
// @Failure 500 {object} controller.Response "内部服务器错误"
|
|
// @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
|
|
}
|
|
|
|
respDTO, err := c.service.CreatePigBatch(&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} "获取成功"
|
|
// @Failure 400 {object} controller.Response "无效的ID格式"
|
|
// @Failure 404 {object} controller.Response "猪批次不存在"
|
|
// @Failure 500 {object} controller.Response "内部服务器错误"
|
|
// @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} "更新成功"
|
|
// @Failure 400 {object} controller.Response "请求参数错误或无效的ID格式"
|
|
// @Failure 404 {object} controller.Response "猪批次不存在"
|
|
// @Failure 500 {object} controller.Response "内部服务器错误"
|
|
// @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 "删除成功"
|
|
// @Failure 400 {object} controller.Response "无效的ID格式"
|
|
// @Failure 404 {object} controller.Response "猪批次不存在"
|
|
// @Failure 500 {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} "获取成功"
|
|
// @Failure 500 {object} controller.Response "内部服务器错误"
|
|
// @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)
|
|
}
|
|
|
|
// UpdatePigBatchPens godoc
|
|
// @Summary 更新猪批次关联的猪栏
|
|
// @Description 更新指定猪批次当前关联的猪栏列表
|
|
// @Tags 猪批次管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "猪批次ID"
|
|
// @Param body body dto.PigBatchUpdatePensRequest true "猪批次关联的猪栏ID列表"
|
|
// @Success 200 {object} controller.Response "更新成功"
|
|
// @Failure 400 {object} controller.Response "请求参数错误或无效的ID格式"
|
|
// @Failure 404 {object} controller.Response "猪批次或猪栏不存在"
|
|
// @Failure 409 {object} controller.Response "业务逻辑冲突 (如猪栏已被占用)"
|
|
// @Failure 500 {object} controller.Response "内部服务器错误"
|
|
// @Router /api/v1/pig-batches/{id}/pens [put]
|
|
func (c *PigBatchController) UpdatePigBatchPens(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.PigBatchUpdatePensRequest
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
|
return
|
|
}
|
|
|
|
err = c.service.UpdatePigBatchPens(uint(batchID), req.PenIDs)
|
|
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) || 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)
|
|
}
|