猪群领域其他方法映射到api
This commit is contained in:
@@ -29,7 +29,7 @@ func NewPigBatchController(logger *logs.Logger, service service.PigBatchService)
|
||||
// CreatePigBatch godoc
|
||||
// @Summary 创建猪批次
|
||||
// @Description 创建一个新的猪批次
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body dto.PigBatchCreateDTO true "猪批次信息"
|
||||
@@ -58,7 +58,7 @@ func (c *PigBatchController) CreatePigBatch(ctx *gin.Context) {
|
||||
// GetPigBatch godoc
|
||||
// @Summary 获取单个猪批次
|
||||
// @Description 根据ID获取单个猪批次信息
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "获取成功"
|
||||
@@ -88,7 +88,7 @@ func (c *PigBatchController) GetPigBatch(ctx *gin.Context) {
|
||||
// UpdatePigBatch godoc
|
||||
// @Summary 更新猪批次
|
||||
// @Description 更新一个已存在的猪批次信息
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
@@ -126,7 +126,7 @@ func (c *PigBatchController) UpdatePigBatch(ctx *gin.Context) {
|
||||
// DeletePigBatch godoc
|
||||
// @Summary 删除猪批次
|
||||
// @Description 根据ID删除一个猪批次
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
@@ -155,7 +155,7 @@ func (c *PigBatchController) DeletePigBatch(ctx *gin.Context) {
|
||||
// ListPigBatches godoc
|
||||
// @Summary 获取猪批次列表
|
||||
// @Description 获取所有猪批次的列表,支持按活跃状态筛选
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Produce json
|
||||
// @Param is_active query bool false "是否活跃 (true/false)"
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PigBatchResponseDTO} "获取成功"
|
||||
@@ -182,7 +182,7 @@ func (c *PigBatchController) ListPigBatches(ctx *gin.Context) {
|
||||
// AssignEmptyPensToBatch godoc
|
||||
// @Summary 为猪批次分配空栏
|
||||
// @Description 将一个或多个空闲猪栏分配给指定的猪批次
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
@@ -225,7 +225,7 @@ func (c *PigBatchController) AssignEmptyPensToBatch(ctx *gin.Context) {
|
||||
// ReclassifyPenToNewBatch godoc
|
||||
// @Summary 将猪栏划拨到新批次
|
||||
// @Description 将一个猪栏(连同其中的猪只)从一个批次整体划拨到另一个批次
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param fromBatchID path int true "源猪批次ID"
|
||||
@@ -268,7 +268,7 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx *gin.Context) {
|
||||
// RemoveEmptyPenFromBatch godoc
|
||||
// @Summary 从猪批次移除空栏
|
||||
// @Description 将一个空闲猪栏从指定的猪批次中移除
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Produce json
|
||||
// @Param batchID path int true "猪批次ID"
|
||||
// @Param penID path int true "待移除的猪栏ID"
|
||||
@@ -308,7 +308,7 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx *gin.Context) {
|
||||
// MovePigsIntoPen godoc
|
||||
// @Summary 将猪只从“虚拟库存”移入指定猪栏
|
||||
// @Description 将指定数量的猪只从批次的“虚拟库存”移入一个已分配的猪栏
|
||||
// @Tags 猪批次管理
|
||||
// @Tags 猪群管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
|
||||
@@ -0,0 +1,269 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// RecordSickPigs godoc
|
||||
// @Summary 记录新增病猪事件
|
||||
// @Description 记录猪批次中新增病猪的数量、治疗地点和发生时间
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.RecordSickPigsRequest true "记录病猪请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/{id}/record-sick-pigs [post]
|
||||
func (c *PigBatchController) RecordSickPigs(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.RecordSickPigsRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.RecordSickPigs(operatorID, uint(batchID), req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, 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) || 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)
|
||||
}
|
||||
|
||||
// RecordSickPigRecovery godoc
|
||||
// @Summary 记录病猪康复事件
|
||||
// @Description 记录猪批次中病猪康复的数量、治疗地点和发生时间
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.RecordSickPigRecoveryRequest true "记录病猪康复请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/{id}/record-sick-pig-recovery [post]
|
||||
func (c *PigBatchController) RecordSickPigRecovery(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.RecordSickPigRecoveryRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.RecordSickPigRecovery(operatorID, uint(batchID), req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, 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) || 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)
|
||||
}
|
||||
|
||||
// RecordSickPigDeath godoc
|
||||
// @Summary 记录病猪死亡事件
|
||||
// @Description 记录猪批次中病猪死亡的数量、治疗地点和发生时间
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.RecordSickPigDeathRequest true "记录病猪死亡请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/{id}/record-sick-pig-death [post]
|
||||
func (c *PigBatchController) RecordSickPigDeath(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.RecordSickPigDeathRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.RecordSickPigDeath(operatorID, uint(batchID), req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, 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) || 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)
|
||||
}
|
||||
|
||||
// RecordSickPigCull godoc
|
||||
// @Summary 记录病猪淘汰事件
|
||||
// @Description 记录猪批次中病猪淘汰的数量、治疗地点和发生时间
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.RecordSickPigCullRequest true "记录病猪淘汰请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/{id}/record-sick-pig-cull [post]
|
||||
func (c *PigBatchController) RecordSickPigCull(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.RecordSickPigCullRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.RecordSickPigCull(operatorID, uint(batchID), req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, 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) || 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)
|
||||
}
|
||||
|
||||
// RecordDeath godoc
|
||||
// @Summary 记录正常猪只死亡事件
|
||||
// @Description 记录猪批次中正常猪只死亡的数量和发生时间
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.RecordDeathRequest true "记录正常猪只死亡请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/{id}/record-death [post]
|
||||
func (c *PigBatchController) RecordDeath(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.RecordDeathRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.RecordDeath(operatorID, uint(batchID), req.PenID, req.Quantity, req.HappenedAt, 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) || 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)
|
||||
}
|
||||
|
||||
// RecordCull godoc
|
||||
// @Summary 记录正常猪只淘汰事件
|
||||
// @Description 记录猪批次中正常猪只淘汰的数量和发生时间
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.RecordCullRequest true "记录正常猪只淘汰请求信息"
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/{id}/record-cull [post]
|
||||
func (c *PigBatchController) RecordCull(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.RecordCullRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.RecordCull(operatorID, uint(batchID), req.PenID, req.Quantity, req.HappenedAt, 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) || 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)
|
||||
}
|
||||
@@ -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"
|
||||
)
|
||||
|
||||
// SellPigs godoc
|
||||
// @Summary 处理卖猪的业务逻辑
|
||||
// @Description 记录猪批次中的猪只出售事件
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.SellPigsRequest true "卖猪请求信息"
|
||||
// @Success 200 {object} controller.Response "卖猪成功"
|
||||
// @Router /api/v1/pig-batches/{id}/sell-pigs [post]
|
||||
func (c *PigBatchController) SellPigs(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.SellPigsRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.SellPigs(uint(batchID), req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
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) || 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)
|
||||
}
|
||||
|
||||
// BuyPigs godoc
|
||||
// @Summary 处理买猪的业务逻辑
|
||||
// @Description 记录猪批次中的猪只购买事件
|
||||
// @Tags 猪批次管理
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param id path int true "猪批次ID"
|
||||
// @Param body body dto.BuyPigsRequest true "买猪请求信息"
|
||||
// @Success 200 {object} controller.Response "买猪成功"
|
||||
// @Router /api/v1/pig-batches/{id}/buy-pigs [post]
|
||||
func (c *PigBatchController) BuyPigs(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.BuyPigsRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
return
|
||||
}
|
||||
|
||||
operatorID, err := controller.GetOperatorIDFromContext(ctx)
|
||||
|
||||
err = c.service.BuyPigs(uint(batchID), req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
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)
|
||||
}
|
||||
@@ -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