修复报错
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"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"
|
||||
)
|
||||
|
||||
// mapAndSendError 统一映射服务层错误并发送响应。
|
||||
// 这个函数将服务层返回的错误转换为控制器层应返回的HTTP状态码和审计信息。
|
||||
func mapAndSendError(c *PigBatchController, ctx echo.Context, action string, err error, id uint) error {
|
||||
func mapAndSendError(reqContext context.Context, c *PigBatchController, ctx echo.Context, action string, err error, id uint) error {
|
||||
if errors.Is(err, service.ErrPigBatchNotFound) ||
|
||||
errors.Is(err, service.ErrPenNotFound) ||
|
||||
errors.Is(err, service.ErrPenNotAssociatedWithBatch) {
|
||||
@@ -26,7 +28,7 @@ func mapAndSendError(c *PigBatchController, ctx echo.Context, action string, err
|
||||
errors.Is(err, service.ErrPenNotEmpty) {
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), action, err.Error(), id)
|
||||
} else {
|
||||
c.logger.Errorf("操作[%s]业务逻辑失败: %v", action, err)
|
||||
logs.GetLogger(reqContext).Errorf("操作[%s]业务逻辑失败: %v", action, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, fmt.Sprintf("操作失败: %v", err), action, err.Error(), id)
|
||||
}
|
||||
}
|
||||
@@ -86,6 +88,7 @@ func extractOperatorAndPrimaryID(
|
||||
// handleAPIRequest 封装了控制器中处理带有请求体和路径参数的API请求的通用逻辑。
|
||||
// 它负责请求体绑定、操作员ID获取、服务层调用、错误映射和响应发送。
|
||||
func handleAPIRequest[Req any](
|
||||
reqContext context.Context,
|
||||
c *PigBatchController,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
@@ -108,7 +111,7 @@ func handleAPIRequest[Req any](
|
||||
// 3. 执行服务层逻辑
|
||||
err = serviceExecutor(ctx, operatorID, primaryID, reqDTO)
|
||||
if err != nil {
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 4. 发送成功响应
|
||||
@@ -117,6 +120,7 @@ func handleAPIRequest[Req any](
|
||||
|
||||
// handleNoBodyAPIRequest 封装了处理不带请求体,但有路径参数和操作员ID的API请求的通用逻辑。
|
||||
func handleNoBodyAPIRequest(
|
||||
reqContext context.Context,
|
||||
c *PigBatchController,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
@@ -133,7 +137,7 @@ func handleNoBodyAPIRequest(
|
||||
// 2. 执行服务层逻辑
|
||||
err = serviceExecutor(ctx, operatorID, primaryID)
|
||||
if err != nil {
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 3. 发送成功响应
|
||||
@@ -142,6 +146,7 @@ func handleNoBodyAPIRequest(
|
||||
|
||||
// handleAPIRequestWithResponse 封装了控制器中处理带有请求体、路径参数并返回响应DTO的API请求的通用逻辑。
|
||||
func handleAPIRequestWithResponse[Req any, Resp any](
|
||||
reqContext context.Context,
|
||||
c *PigBatchController,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
@@ -164,7 +169,7 @@ func handleAPIRequestWithResponse[Req any, Resp any](
|
||||
// 3. 执行服务层逻辑
|
||||
respDTO, err := serviceExecutor(ctx, operatorID, primaryID, reqDTO)
|
||||
if err != nil {
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 4. 发送成功响应
|
||||
@@ -173,6 +178,7 @@ func handleAPIRequestWithResponse[Req any, Resp any](
|
||||
|
||||
// handleNoBodyAPIRequestWithResponse 封装了处理不带请求体,但有路径参数和操作员ID,并返回响应DTO的API请求的通用逻辑。
|
||||
func handleNoBodyAPIRequestWithResponse[Resp any](
|
||||
reqContext context.Context,
|
||||
c *PigBatchController,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
@@ -189,7 +195,7 @@ func handleNoBodyAPIRequestWithResponse[Resp any](
|
||||
// 2. 执行服务层逻辑
|
||||
respDTO, err := serviceExecutor(ctx, operatorID, primaryID)
|
||||
if err != nil {
|
||||
return mapAndSendError(c, ctx, action, err, primaryID)
|
||||
return mapAndSendError(reqContext, c, ctx, action, err, primaryID)
|
||||
}
|
||||
|
||||
// 3. 发送成功响应
|
||||
@@ -198,6 +204,7 @@ func handleNoBodyAPIRequestWithResponse[Resp any](
|
||||
|
||||
// handleQueryAPIRequestWithResponse 封装了处理带有查询参数并返回响应DTO的API请求的通用逻辑。
|
||||
func handleQueryAPIRequestWithResponse[Query any, Resp any](
|
||||
reqContext context.Context,
|
||||
c *PigBatchController,
|
||||
ctx echo.Context,
|
||||
action string,
|
||||
@@ -220,7 +227,7 @@ func handleQueryAPIRequestWithResponse[Query any, Resp any](
|
||||
respDTO, err := serviceExecutor(ctx, operatorID, queryDTO)
|
||||
if err != nil {
|
||||
// 对于列表查询,通常没有primaryID,所以传递0
|
||||
return mapAndSendError(c, ctx, action, err, 0)
|
||||
return mapAndSendError(reqContext, c, ctx, action, err, 0)
|
||||
}
|
||||
|
||||
// 4. 发送成功响应
|
||||
|
||||
@@ -42,7 +42,7 @@ func (c *PigBatchController) CreatePigBatch(ctx echo.Context) error {
|
||||
var req dto.PigBatchCreateDTO
|
||||
|
||||
return handleAPIRequestWithResponse(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchCreateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
// 对于创建操作,primaryID通常不从路径中获取,而是由服务层生成
|
||||
return c.service.CreatePigBatch(reqCtx, operatorID, req)
|
||||
@@ -67,7 +67,7 @@ func (c *PigBatchController) GetPigBatch(ctx echo.Context) error {
|
||||
const action = "获取猪批次"
|
||||
|
||||
return handleNoBodyAPIRequestWithResponse(
|
||||
c, ctx, action,
|
||||
reqCtx, c, ctx, action,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) (*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.GetPigBatch(reqCtx, primaryID)
|
||||
},
|
||||
@@ -94,7 +94,7 @@ func (c *PigBatchController) UpdatePigBatch(ctx echo.Context) error {
|
||||
var req dto.PigBatchUpdateDTO
|
||||
|
||||
return handleAPIRequestWithResponse(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.PigBatchUpdateDTO) (*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.UpdatePigBatch(reqCtx, primaryID, req)
|
||||
},
|
||||
@@ -118,7 +118,7 @@ func (c *PigBatchController) DeletePigBatch(ctx echo.Context) error {
|
||||
const action = "删除猪批次"
|
||||
|
||||
return handleNoBodyAPIRequest(
|
||||
c, ctx, action,
|
||||
reqCtx, c, ctx, action,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
||||
return c.service.DeletePigBatch(reqCtx, primaryID)
|
||||
},
|
||||
@@ -143,7 +143,7 @@ func (c *PigBatchController) ListPigBatches(ctx echo.Context) error {
|
||||
var query dto.PigBatchQueryDTO
|
||||
|
||||
return handleQueryAPIRequestWithResponse(
|
||||
c, ctx, action, &query,
|
||||
reqCtx, c, ctx, action, &query,
|
||||
func(ctx echo.Context, operatorID uint, query *dto.PigBatchQueryDTO) ([]*dto.PigBatchResponseDTO, error) {
|
||||
return c.service.ListPigBatches(reqCtx, query.IsActive)
|
||||
},
|
||||
@@ -169,7 +169,7 @@ func (c *PigBatchController) AssignEmptyPensToBatch(ctx echo.Context) error {
|
||||
var req dto.AssignEmptyPensToBatchRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.AssignEmptyPensToBatchRequest) error {
|
||||
return c.service.AssignEmptyPensToBatch(reqCtx, primaryID, req.PenIDs, operatorID)
|
||||
},
|
||||
@@ -196,7 +196,7 @@ func (c *PigBatchController) ReclassifyPenToNewBatch(ctx echo.Context) error {
|
||||
var req dto.ReclassifyPenToNewBatchRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.ReclassifyPenToNewBatchRequest) error {
|
||||
// primaryID 在这里是 fromBatchID
|
||||
return c.service.ReclassifyPenToNewBatch(reqCtx, primaryID, req.ToBatchID, req.PenID, operatorID, req.Remarks)
|
||||
@@ -229,7 +229,7 @@ func (c *PigBatchController) RemoveEmptyPenFromBatch(ctx echo.Context) error {
|
||||
const action = "从猪批次移除空栏"
|
||||
|
||||
return handleNoBodyAPIRequest(
|
||||
c, ctx, action,
|
||||
reqCtx, c, ctx, action,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint) error {
|
||||
// primaryID 在这里是 batchID
|
||||
penIDParam := ctx.Param("penID")
|
||||
@@ -269,7 +269,7 @@ func (c *PigBatchController) MovePigsIntoPen(ctx echo.Context) error {
|
||||
var req dto.MovePigsIntoPenRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.MovePigsIntoPenRequest) error {
|
||||
return c.service.MovePigsIntoPen(reqCtx, primaryID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
},
|
||||
|
||||
@@ -25,9 +25,9 @@ func (c *PigBatchController) RecordSickPigs(ctx echo.Context) error {
|
||||
var req dto.RecordSickPigsRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigsRequest) error {
|
||||
return c.service.RecordSickPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
return c.service.RecordSickPigs(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
"记录成功",
|
||||
nil, // 默认从 ":id" 路径参数提取ID
|
||||
@@ -52,7 +52,7 @@ func (c *PigBatchController) RecordSickPigRecovery(ctx echo.Context) error {
|
||||
var req dto.RecordSickPigRecoveryRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigRecoveryRequest) error {
|
||||
return c.service.RecordSickPigRecovery(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
@@ -79,7 +79,7 @@ func (c *PigBatchController) RecordSickPigDeath(ctx echo.Context) error {
|
||||
var req dto.RecordSickPigDeathRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigDeathRequest) error {
|
||||
return c.service.RecordSickPigDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
@@ -106,7 +106,7 @@ func (c *PigBatchController) RecordSickPigCull(ctx echo.Context) error {
|
||||
var req dto.RecordSickPigCullRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordSickPigCullRequest) error {
|
||||
return c.service.RecordSickPigCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.TreatmentLocation, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
@@ -133,7 +133,7 @@ func (c *PigBatchController) RecordDeath(ctx echo.Context) error {
|
||||
var req dto.RecordDeathRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordDeathRequest) error {
|
||||
return c.service.RecordDeath(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
@@ -160,7 +160,7 @@ func (c *PigBatchController) RecordCull(ctx echo.Context) error {
|
||||
var req dto.RecordCullRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.RecordCullRequest) error {
|
||||
return c.service.RecordCull(reqCtx, operatorID, primaryID, req.PenID, req.Quantity, req.HappenedAt, req.Remarks)
|
||||
},
|
||||
|
||||
@@ -24,7 +24,7 @@ func (c *PigBatchController) SellPigs(ctx echo.Context) error {
|
||||
var req dto.SellPigsRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.SellPigsRequest) error {
|
||||
return c.service.SellPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
},
|
||||
@@ -50,7 +50,7 @@ func (c *PigBatchController) BuyPigs(ctx echo.Context) error {
|
||||
var req dto.BuyPigsRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.BuyPigsRequest) error {
|
||||
return c.service.BuyPigs(reqCtx, primaryID, req.PenID, req.Quantity, req.UnitPrice, req.TotalPrice, req.TraderName, req.TradeDate, req.Remarks, operatorID)
|
||||
},
|
||||
|
||||
@@ -27,7 +27,7 @@ func (c *PigBatchController) TransferPigsAcrossBatches(ctx echo.Context) error {
|
||||
var req dto.TransferPigsAcrossBatchesRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsAcrossBatchesRequest) error {
|
||||
// primaryID 在这里是 sourceBatchID
|
||||
return c.service.TransferPigsAcrossBatches(reqCtx, primaryID, req.DestBatchID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
@@ -62,7 +62,7 @@ func (c *PigBatchController) TransferPigsWithinBatch(ctx echo.Context) error {
|
||||
var req dto.TransferPigsWithinBatchRequest
|
||||
|
||||
return handleAPIRequest(
|
||||
c, ctx, action, &req,
|
||||
reqCtx, c, ctx, action, &req,
|
||||
func(ctx echo.Context, operatorID uint, primaryID uint, req *dto.TransferPigsWithinBatchRequest) error {
|
||||
// primaryID 在这里是 batchID
|
||||
return c.service.TransferPigsWithinBatch(reqCtx, primaryID, req.FromPenID, req.ToPenID, req.Quantity, operatorID, req.Remarks)
|
||||
|
||||
@@ -3,7 +3,7 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/audit"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
// AuditLogMiddleware 创建一个Echo中间件,用于在请求结束后记录用户操作审计日志。
|
||||
// 它依赖于控制器通过调用 SendSuccessWithAudit 或 SendErrorWithAudit 在上下文中设置的审计信息。
|
||||
func AuditLogMiddleware(ctx context.Context, auditService audit.Service) echo.MiddlewareFunc {
|
||||
func AuditLogMiddleware(ctx context.Context, auditService service.AuditService) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
newCtx := logs.AddFuncName(ctx, c.Request().Context(), "AuditLogMiddleware")
|
||||
@@ -35,8 +35,8 @@ func AuditLogMiddleware(ctx context.Context, auditService audit.Service) echo.Mi
|
||||
user, _ = userCtx.(*models.User)
|
||||
}
|
||||
|
||||
// 构建 RequestContext
|
||||
reqCtx := audit.RequestContext{
|
||||
// 构建 AuditRequestContext
|
||||
reqCtx := service.AuditRequestContext{
|
||||
ClientIP: c.RealIP(),
|
||||
HTTPPath: c.Request().URL.Path,
|
||||
HTTPMethod: c.Request().Method,
|
||||
|
||||
@@ -8,10 +8,10 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/domain/token"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/token"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"gorm.io/gorm"
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
|
||||
// AuthMiddleware 创建一个Echo中间件,用于JWT身份验证
|
||||
// 它依赖于 TokenService 来解析和验证 token,并使用 UserRepository 来获取完整的用户信息
|
||||
func AuthMiddleware(ctx context.Context, tokenService token.Service, userRepo repository.UserRepository) echo.MiddlewareFunc {
|
||||
func AuthMiddleware(ctx context.Context, tokenGenerator token.Generator, userRepo repository.UserRepository) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
reqCtx := logs.AddFuncName(ctx, c.Request().Context(), "AuthMiddleware")
|
||||
@@ -39,7 +39,7 @@ func AuthMiddleware(ctx context.Context, tokenService token.Service, userRepo re
|
||||
tokenString := parts[1]
|
||||
|
||||
// 解析和验证 token
|
||||
claims, err := tokenService.ParseToken(tokenString)
|
||||
claims, err := tokenGenerator.ParseToken(tokenString)
|
||||
if err != nil {
|
||||
return controller.SendErrorWithStatus(c, http.StatusUnauthorized, controller.CodeUnauthorized, "无效的Token")
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
)
|
||||
|
||||
// RequestContext 封装了审计日志所需的请求上下文信息
|
||||
type RequestContext struct {
|
||||
// AuditRequestContext 封装了审计日志所需的请求上下文信息
|
||||
type AuditRequestContext struct {
|
||||
ClientIP string
|
||||
HTTPPath string
|
||||
HTTPMethod string
|
||||
@@ -19,7 +19,7 @@ type RequestContext struct {
|
||||
|
||||
// AuditService 定义了审计服务的接口
|
||||
type AuditService interface {
|
||||
LogAction(ctx context.Context, user *models.User, reqCtx RequestContext, actionType, description string, targetResource interface{}, status models.AuditStatus, resultDetails string)
|
||||
LogAction(ctx context.Context, user *models.User, reqCtx AuditRequestContext, actionType, description string, targetResource interface{}, status models.AuditStatus, resultDetails string)
|
||||
}
|
||||
|
||||
// auditService 是 AuditService 接口的实现
|
||||
@@ -37,7 +37,7 @@ func NewAuditService(ctx context.Context, repo repository.UserActionLogRepositor
|
||||
}
|
||||
|
||||
// LogAction 记录一个用户操作。它在一个新的 goroutine 中异步执行,以避免阻塞主请求。
|
||||
func (s *auditService) LogAction(ctx context.Context, user *models.User, reqCtx RequestContext, actionType, description string, targetResource interface{}, status models.AuditStatus, resultDetails string) {
|
||||
func (s *auditService) LogAction(ctx context.Context, user *models.User, reqCtx AuditRequestContext, actionType, description string, targetResource interface{}, status models.AuditStatus, resultDetails string) {
|
||||
serviceCtx, logger := logs.Trace(ctx, s.ctx, "LogAction")
|
||||
|
||||
// 不再从 context 中获取用户信息,直接使用传入的 user 对象
|
||||
|
||||
@@ -330,7 +330,7 @@ func (s *pigFarmService) DeletePen(ctx context.Context, id uint) error {
|
||||
func (s *pigFarmService) UpdatePenStatus(ctx context.Context, id uint, newStatus models.PenStatus) (*dto.PenResponse, error) {
|
||||
serviceCtx, logger := logs.Trace(ctx, s.ctx, "UpdatePenStatus")
|
||||
var updatedPen *models.Pen
|
||||
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
err := s.uow.ExecuteInTransaction(serviceCtx, func(tx *gorm.DB) error {
|
||||
pen, err := s.penRepository.GetPenByIDTx(serviceCtx, tx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
||||
@@ -89,7 +89,7 @@ func (s *userService) Login(ctx context.Context, req *dto.LoginRequest) (*dto.Lo
|
||||
}
|
||||
|
||||
// 登录成功,生成 JWT token
|
||||
tokenString, err := s.tokenService.GenerateToken(user.ID)
|
||||
tokenString, err := s.tokenGenerator.GenerateToken(user.ID)
|
||||
if err != nil {
|
||||
logger.Errorf("登录: 生成令牌失败: %v", err)
|
||||
return nil, errors.New("登录失败,无法生成认证信息")
|
||||
|
||||
Reference in New Issue
Block a user