修改controller包
This commit is contained in:
@@ -1,25 +1,25 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// PigBatchController 负责处理猪批次相关的API请求
|
||||
type PigBatchController struct {
|
||||
logger *logs.Logger
|
||||
ctx context.Context
|
||||
service service.PigBatchService
|
||||
}
|
||||
|
||||
// NewPigBatchController 创建一个新的 PigBatchController 实例
|
||||
func NewPigBatchController(logger *logs.Logger, service service.PigBatchService) *PigBatchController {
|
||||
func NewPigBatchController(ctx context.Context, service service.PigBatchService) *PigBatchController {
|
||||
return &PigBatchController{
|
||||
logger: logger,
|
||||
ctx: ctx,
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,8 @@ func NewPigBatchController(logger *logs.Logger, service service.PigBatchService)
|
||||
// @Success 201 {object} controller.Response{data=dto.PigBatchResponseDTO} "创建成功"
|
||||
// @Router /api/v1/pig-batches [post]
|
||||
func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "CreatePigBatch")
|
||||
|
||||
const action = "创建猪批次"
|
||||
var req dto.PigBatchCreateDTO
|
||||
|
||||
@@ -42,7 +44,7 @@ func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
// 对于创建操作,primaryID通常不从路径中获取,而是由服务层生成
|
||||
return c.service.CreatePigBatch(operatorID, req)
|
||||
return c.service.CreatePigBatch(reqCtx, operatorID, req)
|
||||
},
|
||||
"创建成功",
|
||||
nil, // 无需自定义ID提取器,primaryID将为0
|
||||
@@ -59,12 +61,14 @@ func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "获取成功"
|
||||
// @Router /api/v1/pig-batches/{id} [get]
|
||||
func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "GetPigBatch")
|
||||
|
||||
const action = "获取猪批次"
|
||||
|
||||
return handleNoBodyAPIRequestWithResponse(
|
||||
c, ctx, action,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.GetPigBatch(primaryID)
|
||||
return c.service.GetPigBatch(reqCtx, primaryID)
|
||||
},
|
||||
"获取成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -83,13 +87,15 @@ func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PigBatchResponseDTO} "更新成功"
|
||||
// @Router /api/v1/pig-batches/{id} [put]
|
||||
func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "UpdatePigBatch")
|
||||
|
||||
const action = "更新猪批次"
|
||||
var req dto.PigBatchUpdateDTO
|
||||
|
||||
return handleAPIRequestWithResponse(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.UpdatePigBatch(primaryID, req)
|
||||
return c.service.UpdatePigBatch(reqCtx, primaryID, req)
|
||||
},
|
||||
"更新成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -106,12 +112,14 @@ func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
// @Router /api/v1/pig-batches/{id} [delete]
|
||||
func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "DeletePigBatch")
|
||||
|
||||
const action = "删除猪批次"
|
||||
|
||||
return handleNoBodyAPIRequest(
|
||||
c, ctx, action,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
||||
return c.service.DeletePigBatch(primaryID)
|
||||
return c.service.DeletePigBatch(reqCtx, primaryID)
|
||||
},
|
||||
"删除成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -128,13 +136,15 @@ func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PigBatchResponseDTO} "获取成功"
|
||||
// @Router /api/v1/pig-batches [get]
|
||||
func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "ListPigBatches")
|
||||
|
||||
const action = "获取猪批次列表"
|
||||
var query dto.PigBatchQueryDTO
|
||||
|
||||
return handleQueryAPIRequestWithResponse(
|
||||
c, ctx, action, &query,
|
||||
func(ctx echo.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.ListPigBatches(query.IsActive)
|
||||
return c.service.ListPigBatches(reqCtx, query.IsActive)
|
||||
},
|
||||
"获取成功",
|
||||
)
|
||||
@@ -152,13 +162,15 @@ func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "分配成功"
|
||||
// @Router /api/v1/pig-batches/assign-pens/{id} [post]
|
||||
func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "AssignEmptyPensToBatch")
|
||||
|
||||
const action = "为猪批次分配空栏"
|
||||
var req dto.AssignEmptyPensToBatchRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
|
||||
return c.service.AssignEmptyPensToBatch(primaryID, req.PenIDs, operatorID)
|
||||
return c.service.AssignEmptyPensToBatch(reqCtx, primaryID, req.PenIDs, operatorID)
|
||||
},
|
||||
"分配成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -177,6 +189,8 @@ func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "划拨成功"
|
||||
// @Router /api/v1/pig-batches/reclassify-pen/{fromBatchID} [post]
|
||||
func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "ReclassifyPenToNewBatch")
|
||||
|
||||
const action = "划拨猪栏到新批次"
|
||||
var req dto.ReclassifyPenToNewBatchRequest
|
||||
|
||||
@@ -184,7 +198,7 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.ReclassifyPenToNewBatchRequest) error {
|
||||
// primaryID 在这里是 fromBatchID
|
||||
return c.service.ReclassifyPenToNewBatch(primaryID, req.ToBatchID, req.PenID, operatorID, req.Remarks)
|
||||
return c.service.ReclassifyPenToNewBatch(reqCtx, primaryID, req.ToBatchID, req.PenID, operatorID, req.Remarks)
|
||||
},
|
||||
"划拨成功",
|
||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":fromBatchID" 路径参数提取
|
||||
@@ -209,6 +223,8 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "移除成功"
|
||||
// @Router /api/v1/pig-batches/remove-pen/{penID}/{batchID} [delete]
|
||||
func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RemoveEmptyPenFromBatch")
|
||||
|
||||
const action = "从猪批次移除空栏"
|
||||
|
||||
return handleNoBodyAPIRequest(
|
||||
@@ -220,7 +236,7 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
||||
if err != nil {
|
||||
return err // 返回错误,因为 penID 格式无效
|
||||
}
|
||||
return c.service.RemoveEmptyPenFromBatch(primaryID, uint(parsedPenID))
|
||||
return c.service.RemoveEmptyPenFromBatch(reqCtx, primaryID, uint(parsedPenID))
|
||||
},
|
||||
"移除成功",
|
||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":batchID" 路径参数提取
|
||||
@@ -246,13 +262,15 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "移入成功"
|
||||
// @Router /api/v1/pig-batches/move-pigs-into-pen/{id} [post]
|
||||
func (c *PigBatchController) MovePigsIntoPen(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "MovePigsIntoPen")
|
||||
|
||||
const action = "将猪只移入猪栏"
|
||||
var req dto.MovePigsIntoPenRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
|
||||
return c.service.MovePigsIntoPen(primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
return c.service.MovePigsIntoPen(reqCtx, primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
"移入成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
|
||||
@@ -2,6 +2,7 @@ package management
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@@ -17,13 +18,15 @@ import (
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pigs/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigs")
|
||||
|
||||
const action = "记录新增病猪事件"
|
||||
var req dto.RecordSickPigsRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigsRequest) error {
|
||||
return c.service.RecordSickPigs(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordSickPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -42,13 +45,15 @@ func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pig-recovery/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigRecovery")
|
||||
|
||||
const action = "记录病猪康复事件"
|
||||
var req dto.RecordSickPigRecoveryRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigRecoveryRequest) error {
|
||||
return c.service.RecordSickPigRecovery(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordSickPigRecovery(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -67,13 +72,15 @@ func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pig-death/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigDeath")
|
||||
|
||||
const action = "记录病猪死亡事件"
|
||||
var req dto.RecordSickPigDeathRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigDeathRequest) error {
|
||||
return c.service.RecordSickPigDeath(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordSickPigDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -92,13 +99,15 @@ func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-sick-pig-cull/{id} [post]
|
||||
func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordSickPigCull")
|
||||
|
||||
const action = "记录病猪淘汰事件"
|
||||
var req dto.RecordSickPigCullRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigCullRequest) error {
|
||||
return c.service.RecordSickPigCull(operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordSickPigCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -117,13 +126,15 @@ func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-death/{id} [post]
|
||||
func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordDeath")
|
||||
|
||||
const action = "记录正常猪只死亡事件"
|
||||
var req dto.RecordDeathRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordDeathRequest) error {
|
||||
return c.service.RecordDeath(operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -142,13 +153,15 @@ func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "记录成功"
|
||||
// @Router /api/v1/pig-batches/record-cull/{id} [post]
|
||||
func (c *PigBatchController) RecordCull(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "RecordCull")
|
||||
|
||||
const action = "记录正常猪只淘汰事件"
|
||||
var req dto.RecordCullRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordCullRequest) error {
|
||||
return c.service.RecordCull(operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
|
||||
@@ -2,6 +2,7 @@ package management
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@@ -17,13 +18,14 @@ import (
|
||||
// @Success 200 {object} controller.Response "卖猪成功"
|
||||
// @Router /api/v1/pig-batches/sell-pigs/{id} [post]
|
||||
func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "SellPigs")
|
||||
const action = "卖猪"
|
||||
var req dto.SellPigsRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.SellPigsRequest) error {
|
||||
return c.service.SellPigs(primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
return c.service.SellPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
},
|
||||
"卖猪成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -42,13 +44,14 @@ func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "买猪成功"
|
||||
// @Router /api/v1/pig-batches/buy-pigs/{id} [post]
|
||||
func (c *PigBatchController) BuyPigs(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "BuyPigs")
|
||||
const action = "买猪"
|
||||
var req dto.BuyPigsRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.BuyPigsRequest) error {
|
||||
return c.service.BuyPigs(primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
return c.service.BuyPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
},
|
||||
"买猪成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
@@ -19,6 +20,8 @@ import (
|
||||
// @Success 200 {object} controller.Response "调栏成功"
|
||||
// @Router /api/v1/pig-batches/transfer-across-batches/{sourceBatchID} [post]
|
||||
func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "TransferPigsAcrossBatches")
|
||||
|
||||
const action = "跨猪群调栏"
|
||||
var req dto.TransferPigsAcrossBatchesRequest
|
||||
|
||||
@@ -26,7 +29,7 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsAcrossBatchesRequest) error {
|
||||
// primaryID 在这里是 sourceBatchID
|
||||
return c.service.TransferPigsAcrossBatches(primaryID, req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
return c.service.TransferPigsAcrossBatches(reqCtx, primaryID, req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
"调栏成功",
|
||||
func(ctx echo.Context) (uint, error) { // 自定义ID提取器,从 ":sourceBatchID" 路径参数提取
|
||||
@@ -52,6 +55,8 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "调栏成功"
|
||||
// @Router /api/v1/pig-batches/transfer-within-batch/{id} [post]
|
||||
func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx.Request().Context(), c.ctx, "TransferPigsWithinBatch")
|
||||
|
||||
const action = "群内调栏"
|
||||
var req dto.TransferPigsWithinBatchRequest
|
||||
|
||||
@@ -59,7 +64,7 @@ func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
||||
c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsWithinBatchRequest) error {
|
||||
// primaryID 在这里是 batchID
|
||||
return c.service.TransferPigsWithinBatch(primaryID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
return c.service.TransferPigsWithinBatch(reqCtx, primaryID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
"调栏成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
@@ -15,14 +16,14 @@ import (
|
||||
|
||||
// PigFarmController 负责处理猪舍和猪栏相关的API请求
|
||||
type PigFarmController struct {
|
||||
logger *logs.Logger
|
||||
ctx context.Context
|
||||
service service.PigFarmService
|
||||
}
|
||||
|
||||
// NewPigFarmController 创建一个新的 PigFarmController 实例
|
||||
func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *PigFarmController {
|
||||
func NewPigFarmController(ctx context.Context, service service.PigFarmService) *PigFarmController {
|
||||
return &PigFarmController{
|
||||
logger: logger,
|
||||
ctx: ctx,
|
||||
service: service,
|
||||
}
|
||||
}
|
||||
@@ -40,16 +41,17 @@ func NewPigFarmController(logger *logs.Logger, service service.PigFarmService) *
|
||||
// @Success 201 {object} controller.Response{data=dto.PigHouseResponse} "创建成功"
|
||||
// @Router /api/v1/pig-houses [post]
|
||||
func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePigHouse")
|
||||
const action = "创建猪舍"
|
||||
var req dto.CreatePigHouseRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
c.logger.Errorf("%s: 参数绑定失败: %v", action, err)
|
||||
logger.Errorf("%s: 参数绑定失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
house, err := c.service.CreatePigHouse(req.Name, req.Description)
|
||||
house, err := c.service.CreatePigHouse(reqCtx, req.Name, req.Description)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪舍失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
@@ -66,18 +68,19 @@ func (c *PigFarmController) CreatePigHouse(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "获取成功"
|
||||
// @Router /api/v1/pig-houses/{id} [get]
|
||||
func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPigHouse")
|
||||
const action = "获取猪舍"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
house, err := c.service.GetPigHouseByID(uint(id))
|
||||
house, err := c.service.GetPigHouseByID(reqCtx, uint(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪舍失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
@@ -93,10 +96,11 @@ func (c *PigFarmController) GetPigHouse(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PigHouseResponse} "获取成功"
|
||||
// @Router /api/v1/pig-houses [get]
|
||||
func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPigHouses")
|
||||
const action = "获取猪舍列表"
|
||||
houses, err := c.service.ListPigHouses()
|
||||
houses, err := c.service.ListPigHouses(reqCtx)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
}
|
||||
|
||||
@@ -115,6 +119,7 @@ func (c *PigFarmController) ListPigHouses(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PigHouseResponse} "更新成功"
|
||||
// @Router /api/v1/pig-houses/{id} [put]
|
||||
func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePigHouse")
|
||||
const action = "更新猪舍"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
@@ -126,12 +131,12 @@ func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
house, err := c.service.UpdatePigHouse(uint(id), req.Name, req.Description)
|
||||
house, err := c.service.UpdatePigHouse(reqCtx, uint(id), req.Name, req.Description)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
@@ -148,13 +153,14 @@ func (c *PigFarmController) UpdatePigHouse(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
// @Router /api/v1/pig-houses/{id} [delete]
|
||||
func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePigHouse")
|
||||
const action = "删除猪舍"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
if err := c.service.DeletePigHouse(uint(id)); err != nil {
|
||||
if err := c.service.DeletePigHouse(reqCtx, uint(id)); err != nil {
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪舍不存在", action, "猪舍不存在", id)
|
||||
}
|
||||
@@ -162,7 +168,7 @@ func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
||||
if errors.Is(err, service.ErrHouseContainsPens) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
@@ -182,19 +188,20 @@ func (c *PigFarmController) DeletePigHouse(ctx echo.Context) error {
|
||||
// @Success 201 {object} controller.Response{data=dto.PenResponse} "创建成功"
|
||||
// @Router /api/v1/pens [post]
|
||||
func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreatePen")
|
||||
const action = "创建猪栏"
|
||||
var req dto.CreatePenRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
pen, err := c.service.CreatePen(req.PenNumber, req.HouseID, req.Capacity)
|
||||
pen, err := c.service.CreatePen(reqCtx, req.PenNumber, req.HouseID, req.Capacity)
|
||||
if err != nil {
|
||||
// 检查是否是业务逻辑错误
|
||||
if errors.Is(err, service.ErrHouseNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), req)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建猪栏失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
@@ -211,18 +218,19 @@ func (c *PigFarmController) CreatePen(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "获取成功"
|
||||
// @Router /api/v1/pens/{id} [get]
|
||||
func (c *PigFarmController) GetPen(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetPen")
|
||||
const action = "获取猪栏"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
pen, err := c.service.GetPenByID(uint(id))
|
||||
pen, err := c.service.GetPenByID(reqCtx, uint(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取猪栏失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
@@ -238,10 +246,11 @@ func (c *PigFarmController) GetPen(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=[]dto.PenResponse} "获取成功"
|
||||
// @Router /api/v1/pens [get]
|
||||
func (c *PigFarmController) ListPens(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListPens")
|
||||
const action = "获取猪栏列表"
|
||||
pens, err := c.service.ListPens()
|
||||
pens, err := c.service.ListPens(reqCtx)
|
||||
if err != nil {
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取列表失败", action, "业务逻辑失败", nil)
|
||||
}
|
||||
|
||||
@@ -260,6 +269,7 @@ func (c *PigFarmController) ListPens(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||
// @Router /api/v1/pens/{id} [put]
|
||||
func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePen")
|
||||
const action = "更新猪栏"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
@@ -271,13 +281,13 @@ func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
pen, err := c.service.UpdatePen(uint(id), req.PenNumber, req.HouseID, req.Capacity, req.Status)
|
||||
pen, err := c.service.UpdatePen(reqCtx, uint(id), req.PenNumber, req.HouseID, req.Capacity, req.Status)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
}
|
||||
// 其他业务逻辑错误可以在这里添加处理
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败", action, "业务逻辑失败", req)
|
||||
}
|
||||
|
||||
@@ -294,13 +304,14 @@ func (c *PigFarmController) UpdatePen(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response "删除成功"
|
||||
// @Router /api/v1/pens/{id} [delete]
|
||||
func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeletePen")
|
||||
const action = "删除猪栏"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID格式", action, "ID格式错误", ctx.Param("id"))
|
||||
}
|
||||
|
||||
if err := c.service.DeletePen(uint(id)); err != nil {
|
||||
if err := c.service.DeletePen(reqCtx, uint(id)); err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "猪栏不存在", action, "猪栏不存在", id)
|
||||
}
|
||||
@@ -308,7 +319,7 @@ func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
||||
if errors.Is(err, service.ErrPenInUse) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败", action, "业务逻辑失败", id)
|
||||
}
|
||||
|
||||
@@ -327,6 +338,7 @@ func (c *PigFarmController) DeletePen(ctx echo.Context) error {
|
||||
// @Success 200 {object} controller.Response{data=dto.PenResponse} "更新成功"
|
||||
// @Router /api/v1/pens/{id}/status [put]
|
||||
func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdatePenStatus")
|
||||
const action = "更新猪栏状态"
|
||||
id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
@@ -338,14 +350,14 @@ func (c *PigFarmController) UpdatePenStatus(ctx echo.Context) error {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体", action, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
pen, err := c.service.UpdatePenStatus(uint(id), req.Status)
|
||||
pen, err := c.service.UpdatePenStatus(reqCtx, uint(id), req.Status)
|
||||
if err != nil {
|
||||
if errors.Is(err, service.ErrPenNotFound) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, err.Error(), action, err.Error(), id)
|
||||
} else if errors.Is(err, service.ErrPenStatusInvalidForOccupiedPen) || errors.Is(err, service.ErrPenStatusInvalidForUnoccupiedPen) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
}
|
||||
c.logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
logger.Errorf("%s: 业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新猪栏状态失败", action, err.Error(), id)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user