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) }