diff --git a/design/exceeding-threshold-alarm/index.md b/design/exceeding-threshold-alarm/index.md index bf7f650..06c7319 100644 --- a/design/exceeding-threshold-alarm/index.md +++ b/design/exceeding-threshold-alarm/index.md @@ -131,4 +131,5 @@ 1. 定义告警表和告警历史表 2. 重构部分枚举, 让models包不依赖其他项目中的包 3. 创建仓库层对象(不包含方法) -4. 实现告警发送任务 \ No newline at end of file +4. 实现告警发送任务 +5. 实现告警通知发送计划 \ No newline at end of file diff --git a/internal/core/application.go b/internal/core/application.go index 7c5c3a3..1cc65c2 100644 --- a/internal/core/application.go +++ b/internal/core/application.go @@ -43,7 +43,10 @@ func NewApplication(configPath string) (*Application, error) { if err != nil { return nil, fmt.Errorf("初始化基础设施失败: %w", err) } - domain := initDomainServices(ctx, cfg, infra) + domain, err := initDomainServices(ctx, cfg, infra) + if err != nil { + return nil, fmt.Errorf("初始化领域服务失败: %w", err) + } appServices := initAppServices(ctx, infra, domain) // 3. 初始化 API 入口点 diff --git a/internal/core/component_initializers.go b/internal/core/component_initializers.go index 25ff3fb..8f83bf1 100644 --- a/internal/core/component_initializers.go +++ b/internal/core/component_initializers.go @@ -21,6 +21,7 @@ import ( "git.huangwc.com/pig/pig-farm-controller/internal/infra/transport" "git.huangwc.com/pig/pig-farm-controller/internal/infra/transport/lora" "git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/token" + "gorm.io/gorm" ) @@ -29,7 +30,6 @@ type Infrastructure struct { storage database.Storage repos *Repositories lora *LoraComponents - notifyService domain_notify.Service tokenGenerator token.Generator } @@ -47,18 +47,12 @@ func initInfrastructure(ctx context.Context, cfg *config.Config) (*Infrastructur return nil, err } - notifyService, err := initNotifyService(ctx, cfg.Notify, repos.userRepo, repos.notificationRepo) - if err != nil { - return nil, fmt.Errorf("初始化通知服务失败: %w", err) - } - tokenGenerator := token.NewTokenGenerator([]byte(cfg.App.JWTSecret)) return &Infrastructure{ storage: storage, repos: repos, lora: lora, - notifyService: notifyService, tokenGenerator: tokenGenerator, }, nil } @@ -131,12 +125,18 @@ type DomainServices struct { planExecutionManager plan.ExecutionManager analysisPlanTaskManager plan.AnalysisPlanTaskManager planService plan.Service + notifyService domain_notify.Service } // initDomainServices 初始化所有的领域服务。 -func initDomainServices(ctx context.Context, cfg *config.Config, infra *Infrastructure) *DomainServices { +func initDomainServices(ctx context.Context, cfg *config.Config, infra *Infrastructure) (*DomainServices, error) { baseCtx := context.Background() + notifyService, err := initNotifyService(ctx, cfg.Notify, infra.repos.userRepo, infra.repos.notificationRepo) + if err != nil { + return nil, fmt.Errorf("初始化通知服务失败: %w", err) + } + // 猪群管理相关 pigPenTransferManager := pig.NewPigPenTransferManager(logs.AddCompName(baseCtx, "PigPenTransferManager"), infra.repos.pigPenRepo, infra.repos.pigTransferLogRepo, infra.repos.pigBatchRepo) pigTradeManager := pig.NewPigTradeManager(logs.AddCompName(baseCtx, "PigTradeManager"), infra.repos.pigTradeRepo) @@ -165,7 +165,7 @@ func initDomainServices(ctx context.Context, cfg *config.Config, infra *Infrastr infra.repos.deviceRepo, infra.repos.alarmRepo, generalDeviceService, - infra.notifyService) + notifyService) // 计划任务管理器 analysisPlanTaskManager := plan.NewAnalysisPlanTaskManager(logs.AddCompName(baseCtx, "AnalysisPlanTaskManager"), infra.repos.planRepo, infra.repos.pendingTaskRepo, infra.repos.executionLogRepo) @@ -206,7 +206,8 @@ func initDomainServices(ctx context.Context, cfg *config.Config, infra *Infrastr taskFactory: taskFactory, planExecutionManager: planExecutionManager, planService: planService, - } + notifyService: notifyService, + }, nil } // AppServices 聚合了所有的应用服务实例。 @@ -251,7 +252,7 @@ func initAppServices(ctx context.Context, infra *Infrastructure, domainServices ) auditService := service.NewAuditService(logs.AddCompName(baseCtx, "AuditService"), infra.repos.userActionLogRepo) planService := service.NewPlanService(logs.AddCompName(baseCtx, "AppPlanService"), domainServices.planService) - userService := service.NewUserService(logs.AddCompName(baseCtx, "UserService"), infra.repos.userRepo, infra.tokenGenerator, infra.notifyService) + userService := service.NewUserService(logs.AddCompName(baseCtx, "UserService"), infra.repos.userRepo, infra.tokenGenerator, domainServices.notifyService) return &AppServices{ pigFarmService: pigFarmService,