Files
pig-farm-controller/design/provide-logger-with-mothed/task-controller.md
2025-11-05 17:24:19 +08:00

113 lines
9.8 KiB
Markdown

- **`internal/app/controller/user/user_controller.go` (`user.Controller`)**
- **结构体改造**:
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `Ctx context.Context` 成员。
- **构造函数改造 (`NewController`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
- **公共方法改造 (`CreateUser`, `Login`, `SendTestNotification`)**:
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `c.logger.Errorf``c.logger.Infof` 的调用替换为 `logger.Errorf``logger.Infof`
- [x] 确保所有对 `c.userService` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/app/controller/device/device_controller.go` (`device.Controller`)**
- **结构体改造**:
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `Ctx context.Context` 成员。
- **构造函数改造 (`NewController`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
- **公共方法改造 (`CreateDevice`, `GetDevice`, `ListDevices`, `UpdateDevice`, `DeleteDevice`, `ManualControl`,
`CreateAreaController`, `GetAreaController`, `ListAreaControllers`, `UpdateAreaController`,
`DeleteAreaController`, `CreateDeviceTemplate`, `GetDeviceTemplate`, `ListDeviceTemplates`,
`UpdateDeviceTemplate`, `DeleteDeviceTemplate`)**:
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`,
`logger.Warnf`, `logger.Infof`
- [x] 确保所有对 `c.deviceService` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/app/controller/plan/plan_controller.go` (`plan.Controller`)**
- **结构体改造**:
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `Ctx context.Context` 成员。
- **构造函数改造 (`NewController`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
- **公共方法改造 (`CreatePlan`, `GetPlan`, `ListPlans`, `UpdatePlan`, `DeletePlan`, `StartPlan`, `StopPlan`)**:
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `c.logger.Errorf``c.logger.Infof` 的调用替换为 `logger.Errorf``logger.Infof`
- [x] 确保所有对 `c.planService` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/app/controller/management/pig_farm_controller.go` (`management.PigFarmController`)**
- **结构体改造**:
- [x] 移除 `PigFarmController` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `Ctx context.Context` 成员。
- **构造函数改造 (`NewPigFarmController`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 将这个 `Ctx` 赋值给 `PigFarmController` 结构体的 `Ctx` 成员。
- **公共方法改造 (`CreatePigHouse`, `GetPigHouse`, `ListPigHouses`, `UpdatePigHouse`, `DeletePigHouse`, `CreatePen`,
`GetPen`, `ListPens`, `UpdatePen`, `DeletePen`, `UpdatePenStatus`)**:
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, "MethodName")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `c.logger.Errorf``c.logger.Infof` 的调用替换为 `logger.Errorf``logger.Infof`
- [x] 确保所有对 `c.service` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/app/controller/management/pig_batch_controller.go` (`management.PigBatchController`)**
- **结构体改造**:
- [x] 移除 `PigBatchController` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `Ctx context.Context` 成员。
- **构造函数改造 (`NewPigBatchController`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 将这个 `Ctx` 赋值给 `PigBatchController` 结构体的 `Ctx` 成员。
- **公共方法改造 (
所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`,
`AssignEmptyPensToBatch`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`, `MovePigsIntoPen`)**:
- [x] 这些方法通过调用 `controller_helpers.go` 中的 `handleAPIRequest...` 系列函数来处理请求。
- [x] 确保传递给 `handleAPIRequest...` 系列函数的 `serviceExecutor` 匿名函数,其签名与 `controller_helpers.go`
改造后期望的签名一致(即接收 `context.Context` 作为第一个参数)。
- [x] 在 `serviceExecutor` 匿名函数内部,将接收到的 `context.Context` 传递给 `c.service` 的相应方法。
- **`internal/app/controller/management/controller_helpers.go`**
- [x] 修改 `mapAndSendError` 函数中的 `c.logger.Errorf` 调用,改为从 `echo.Context` 中提取 `context.Context`,并使用
`logs.Trace``logs.GetLogger` 获取 `logger` 实例进行日志记录。
- [x] 检查其他函数是否直接或间接依赖 `*PigBatchController``logger` 成员,并进行相应改造。
- **`internal/app/controller/management/pig_batch_trade_controller.go` (`management.PigBatchController`)**
- [x] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `Ctx context.Context`
- [x] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `Ctx`
- [x] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`
- **`internal/app/controller/management/pig_batch_health_controller.go` (`management.PigBatchController`)**
- [x] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `Ctx context.Context`
- [x] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `Ctx`
- [x] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`
- **`internal/app/controller/management/pig_batch_transfer_controller.go` (`management.PigBatchController`)**
- [x] 修改 `NewController` 函数,移除 `logger` 参数,改为接收 `Ctx context.Context`
- [x] 移除 `Controller` 结构体中的 `logger` 成员,改为保存 `Ctx`
- [x] **所有公共方法**: 接收 `echo.Context` 的方法,需要从中提取 `request.Context()` 作为 `upstreamCtx`
- **`internal/app/controller/monitor/monitor_controller.go` (`monitor.Controller`)**
- **结构体改造**:
- [x] 移除 `Controller` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `Ctx context.Context` 成员。
- **构造函数改造 (`NewController`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 将这个 `Ctx` 赋值给 `Controller` 结构体的 `Ctx` 成员。
- **公共方法改造 (
所有方法,例如 `ListSensorData`, `ListDeviceCommandLogs`, `ListPlanExecutionLogs`, `ListTaskExecutionLogs`,
`ListPendingCollections`, `ListUserActionLogs`, `ListRawMaterialPurchases`, `ListRawMaterialStockLogs`,
`ListFeedUsageRecords`, `ListMedicationLogs`, `ListPigBatchLogs`, `ListWeighingBatches`, `ListWeighingRecords`,
`ListPigTransferLogs`, `ListPigSickLogs`, `ListPigPurchases`, `ListPigSales`, `ListNotifications`)**:
- [x] 在每个方法入口处,从 `echo.Context` 中提取 `request.Context()` 作为 `upstreamCtx`
- [x] 使用 `newCtx, logger := logs.Trace(upstreamCtx, c.Ctx, actionType)` 获取新的 `context.Context`
`logger` 实例(`actionType` 为方法内部定义的常量)。
- [x] 将所有对 `c.logger.Errorf`, `c.logger.Warnf`, `c.logger.Infof` 的调用替换为 `logger.Errorf`,
`logger.Warnf`, `logger.Infof`
- [x] 确保所有对 `c.monitorService` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/app/controller/response.go`**
- [x] 修改 `SendSuccessWithAudit``SendErrorWithAudit` 函数签名,使其接收 `ctx context.Context` 作为第一个参数。
- [x] 在 `setAuditDetails` 函数中,如果需要,从传入的 `context.Context` 中提取调用链信息,并将其添加到审计信息中。
- **`internal/app/controller/auth_utils.go`**
- [x] 检查 `GetOperatorIDFromContext``GetOperatorFromContext` 函数,确保它们能够正确地从 `echo.Context` 中获取
`request.Context()`,并从中提取用户信息。