Files
pig-farm-controller/design/archive/2025-11-05-provide-logger-with-mothed/task-domain.md
2025-11-05 23:58:50 +08:00

273 lines
22 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

- **`internal/domain/audit/service.go` (`audit.Service`)**
- **结构体改造**:
- [x] 移除 `service` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewService`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `audit.Service` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "AuditService")`
- [x] 将这个 `selfCtx` 赋值给 `service` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`LogAction`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "LogAction")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `s.logger.Warnw``s.logger.Errorw` 的调用替换为 `logger.Warnw``logger.Errorw`
- [x] 确保所有对 `s.userActionLogRepository.Create` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/device/general_device_service.go` (`device.Service`)**
- **结构体改造**:
- [x] 移除 `GeneralDeviceService` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewGeneralDeviceService`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `GeneralDeviceService` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "GeneralDeviceService")`
- [x] 将这个 `selfCtx` 赋值给 `GeneralDeviceService` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Switch`, `Collect`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, g.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `g.logger.Errorf`, `g.logger.Infof`, `g.logger.Warnf`, `g.logger.Debugf`, `g.logger.DPanicf`
的调用替换为 `logger.Errorf`, `logger.Infof`, `logger.Warnf`, `logger.Debugf`, `logger.DPanicf`
- [x] 确保所有对 `g.deviceRepo`, `g.deviceCommandLogRepo`, `g.pendingCollectionRepo`, `g.comm` 等依赖的调用都将
`newCtx` 作为第一个参数传递。
- **`internal/domain/notify/notify.go` (`domain_notify.Service` - `failoverService` 实现)**
- **结构体改造**:
- [x] 移除 `failoverService` 结构体中的 `log *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewFailoverService`)**:
- [x] 修改函数签名,移除 `log *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `failoverService` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "FailoverService")`
- [x] 将这个 `selfCtx` 赋值给 `failoverService` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`SendBatchAlarm`, `BroadcastAlarm`, `SendTestMessage`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `s.log.Infow`, `s.log.Errorw`, `s.log.Warnw` 的调用替换为 `logger.Infow`, `logger.Errorw`,
`logger.Warnw`
- [x] 确保所有对 `s.userRepo`, `s.primaryNotifier.Send`, `s.notifiers`, `s.notificationRepo` 等依赖的调用都将
`newCtx` 作为第一个参数传递。
- **内部辅助方法改造 (`sendAlarmToUser`, `recordNotificationAttempt`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `s.log.Errorw`, `s.log.Infow`, `s.log.Warnw` 的调用替换为 `logger.Errorw`, `logger.Infow`,
`logger.Warnw`
- [x] 确保所有对 `s.userRepo`, `s.primaryNotifier.Send`, `s.notifiers`, `s.notificationRepo` 等依赖的调用都将
`newCtx` 作为第一个参数传递。
- **`internal/domain/pig/pen_transfer_manager.go` (`pig.PigPenTransferManager`)**
- **结构体改造**:
- [x] 移除 `pigPenTransferManager` 结构体中可能存在的 `logger *logs.Logger` 成员(如果未来添加)。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPigPenTransferManager`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `pigPenTransferManager` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PigPenTransferManager")`
- [x] 将这个 `selfCtx` 赋值给 `pigPenTransferManager` 结构体的 `selfCtx` 成员。
- **公共方法改造 (
所有方法,例如 `LogTransfer`, `GetPenByID`, `GetPensByBatchID`, `UpdatePenFields`, `GetCurrentPigsInPen`,
`GetTotalPigsInPensForBatchTx`, `ReleasePen`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(在 `tx *gorm.DB` 之前)。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `s.logger.Errorf``s.logger.Infof` 的调用替换为 `logger.Errorf``logger.Infof`
- [x] 确保所有对 `s.penRepo`, `s.logRepo`, `s.pigBatchRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/pig/pig_trade_manager.go` (`pig.PigTradeManager`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPigTradeManager`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `pigTradeManager` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PigTradeManager")`
- [x] 将这个 `selfCtx` 赋值给 `pigTradeManager` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`SellPig`, `BuyPig`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(在 `tx *gorm.DB` 之前)。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 确保所有对 `s.tradeRepo` 的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/pig/pig_sick_manager.go` (`pig.SickPigManager`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewSickPigManager`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `sickPigManager` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "SickPigManager")`
- [x] 将这个 `selfCtx` 赋值给 `sickPigManager` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`ProcessSickPigLog`, `GetCurrentSickPigCount`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(在 `tx *gorm.DB` 之前)。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 确保所有对 `s.sickLogRepo`, `s.medicationLogRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/pig/pig_batch_service.go` (`pig.PigBatchService`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPigBatchService`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `pigBatchService` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PigBatchService")`
- [x] 将这个 `selfCtx` 赋值给 `pigBatchService` 结构体的 `selfCtx` 成员。
- **公共方法改造 (
所有方法,例如 `CreatePigBatch`, `GetPigBatch`, `UpdatePigBatch`, `DeletePigBatch`, `ListPigBatches`,
`AssignEmptyPensToBatch`, `MovePigsIntoPen`, `ReclassifyPenToNewBatch`, `RemoveEmptyPenFromBatch`,
`GetCurrentPigQuantity`, `GetCurrentPigsInPen`, `GetTotalPigsInPensForBatch`, `UpdatePigBatchQuantity`,
`SellPigs`, `BuyPigs`, `TransferPigsAcrossBatches`, `TransferPigsWithinBatch`, `RecordSickPigs`,
`RecordSickPigRecovery`, `RecordSickPigDeath`, `RecordSickPigCull`, `RecordDeath`, `RecordCull`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 确保所有对 `s.pigBatchRepo`, `s.pigBatchLogRepo`, `s.uow`, `s.transferSvc`, `s.tradeSvc`, `s.sickSvc`
等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/plan/analysis_plan_task_manager.go` (`plan.AnalysisPlanTaskManager`)**
- **结构体改造**:
- [x] 移除 `analysisPlanTaskManagerImpl` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewAnalysisPlanTaskManager`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `analysisPlanTaskManagerImpl` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "AnalysisPlanTaskManager")`
- [x] 将这个 `selfCtx` 赋值给 `analysisPlanTaskManagerImpl` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Refresh`, `CreateOrUpdateTrigger`, `EnsureAnalysisTaskDefinition`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, m.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `m.logger.Info`, `m.logger.Errorf`, `m.logger.Warnf` 的调用替换为 `logger.Info`,
`logger.Errorf`, `logger.Warnf`
- [x] 确保所有对 `m.planRepo`, `m.pendingTaskRepo`, `m.executionLogRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **内部辅助方法改造 (`getRefreshData`, `cleanupInvalidTasks`, `addOrUpdateTriggers`, `createTriggerTask`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, m.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `m.logger.Info`, `m.logger.Errorf`, `m.logger.Warnf` 的调用替换为 `logger.Info`,
`logger.Errorf`, `logger.Warnf`
- [x] 确保所有对 `m.planRepo`, `m.pendingTaskRepo`, `m.executionLogRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/plan/plan_execution_manager.go` (`plan.ExecutionManager`)**
- **结构体改造**:
- [x] 移除 `planExecutionManagerImpl` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPlanExecutionManager`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `planExecutionManagerImpl` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PlanExecutionManager")`
- [x] 将这个 `selfCtx` 赋值给 `planExecutionManagerImpl` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Start`, `Stop`)**:
- [x] 在 `Start` 方法中,使用 `newCtx, logger := logs.Trace(context.Background(), s.selfCtx, "Start")` 获取
`logger` 实例进行日志记录。
- [x] 在 `Stop` 方法中,使用 `newCtx, logger := logs.Trace(context.Background(), s.selfCtx, "Stop")` 获取
`logger` 实例进行日志记录。
- **内部辅助方法改造 (`run`, `claimAndSubmit`, `handleRequeue`, `processTask`, `runTask`, `analysisPlan`,
`updateTaskExecutionLogStatus`, `handlePlanTermination`, `handlePlanCompletion`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数(如果方法内部需要传递上下文)。
- [x] 在每个方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `s.logger.Errorf`, `s.logger.Warnf`, `s.logger.Infof`, `s.logger.DPanicf` 的调用替换为
`logger.Errorf`, `logger.Warnf`, `logger.Infof`, `logger.DPanicf`
- [x] 确保所有对 `s.pendingTaskRepo`, `s.executionLogRepo`, `s.deviceRepo`, `s.sensorDataRepo`, `s.planRepo`,
`s.analysisPlanTaskManager`, `s.taskFactory`, `s.deviceService` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/plan/plan_service.go` (`plan.Service`)**
- **结构体改造**:
- [x] 移除 `planServiceImpl` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewPlanService`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `planServiceImpl` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "PlanService")`
- [x] 将这个 `selfCtx` 赋值给 `planServiceImpl` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Start`, `Stop`, `RefreshPlanTriggers`, `CreatePlan`, `GetPlanByID`, `ListPlans`, `UpdatePlan`,
`DeletePlan`, `StartPlan`, `StopPlan`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `s.logger.Errorf`, `s.logger.Infof`, `s.logger.Warnf` 的调用替换为 `logger.Errorf`,
`logger.Infof`, `logger.Warnf`
- [x] 确保所有对 `s.executionManager`, `s.taskManager`, `s.planRepo`, `s.deviceRepo`, `s.unitOfWork`,
`s.taskFactory` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/task/task.go` (`task.TaskFactory`)**
- **结构体改造**:
- [x] 移除 `taskFactory` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewTaskFactory`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `taskFactory` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "TaskFactory")`
- [x] 将这个 `selfCtx` 赋值给 `taskFactory` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Production`, `CreateTaskFromModel`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法内部,使用 `newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName")` 获取新的 `context.Context`
`logger` 实例。
- [x] 将所有对 `t.logger.Panicf` 的调用替换为 `logger.Panicf`
- [x] 将所有对 `NewDelayTask`, `NewReleaseFeedWeightTask`, `NewFullCollectionTask` 的调用,传递 `newCtx`
- **`internal/domain/task/release_feed_weight_task.go`**
- **结构体改造**:
- [x] 移除 `ReleaseFeedWeightTask` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewReleaseFeedWeightTask`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `ReleaseFeedWeightTask` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "ReleaseFeedWeightTask")`
- [x] 将这个 `selfCtx` 赋值给 `ReleaseFeedWeightTask` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Execute`, `OnFailure`, `ResolveDeviceIDs`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, r.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `r.logger.Infof`, `r.logger.Errorf`, `r.logger.Warnf`, `r.logger.Debugf` 的调用替换为
`logger.Infof`, `logger.Errorf`, `logger.Warnf`, `logger.Debugf`
- [x] 确保所有对 `r.deviceRepo`, `r.sensorDataRepo`, `r.feedPort` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **内部辅助方法改造 (`getNowWeight`, `parseParameters`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, r.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `r.logger.Errorf`, `r.logger.Warnf` 的调用替换为 `logger.Errorf`, `logger.Warnf`
- [x] 确保所有对 `r.sensorDataRepo`, `r.deviceRepo` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/task/full_collection_task.go`**
- **结构体改造**:
- [x] 移除 `FullCollectionTask` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewFullCollectionTask`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `FullCollectionTask` 创建其专属的 `selfCtx`
`selfCtx := logs.AddCompName(ctx, "FullCollectionTask")`
- [x] 将这个 `selfCtx` 赋值给 `FullCollectionTask` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Execute`, `OnFailure`, `ResolveDeviceIDs`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, t.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `t.logger.Infow`, `t.logger.Errorw` 的调用替换为 `logger.Infow`, `logger.Errorw`
- [x] 确保所有对 `t.deviceRepo`, `t.deviceService` 等依赖的调用都将 `newCtx` 作为第一个参数传递。
- **`internal/domain/task/delay_task.go`**
- **结构体改造**:
- [x] 移除 `DelayTask` 结构体中的 `logger *logs.Logger` 成员。
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewDelayTask`)**:
- [x] 修改函数签名,移除 `logger *logs.Logger` 参数,改为接收 `ctx context.Context`
- [x] 在函数内部,为 `DelayTask` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "DelayTask")`
- [x] 将这个 `selfCtx` 赋值给 `DelayTask` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`Execute`, `OnFailure`, `ResolveDeviceIDs`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, d.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `d.logger.Infof``d.logger.Errorf` 的调用替换为 `logger.Infof``logger.Errorf`
- **内部辅助方法改造 (`parseParameters`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, d.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `d.logger.Errorf` 的调用替换为 `logger.Errorf`
- **`internal/domain/token/token_service.go` (`token.Service`)**
- **结构体改造**:
- [x] 新增 `selfCtx context.Context` 成员。
- **构造函数改造 (`NewTokenService`)**:
- [x] 修改函数签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在函数内部,为 `tokenService` 创建其专属的 `selfCtx``selfCtx := logs.AddCompName(ctx, "TokenService")`
- [x] 将这个 `selfCtx` 赋值给 `tokenService` 结构体的 `selfCtx` 成员。
- **公共方法改造 (`GenerateToken`, `ParseToken`)**:
- [x] 修改方法签名,添加 `ctx context.Context` 作为第一个参数。
- [x] 在方法入口处,使用 `newCtx, logger := logs.Trace(ctx, s.selfCtx, "MethodName")` 获取新的
`context.Context``logger` 实例。
- [x] 将所有对 `s.logger.*` 的调用替换为 `logger.*` (如果存在)。