diff --git a/internal/app/service/monitor_service.go b/internal/app/service/monitor_service.go index eed0f4d..aa5666b 100644 --- a/internal/app/service/monitor_service.go +++ b/internal/app/service/monitor_service.go @@ -24,6 +24,7 @@ type MonitorService interface { ListPigSickLogs(opts repository.PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error) ListPigPurchases(opts repository.PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) ListPigSales(opts repository.PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error) + ListNotifications(opts repository.NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error) } // monitorService 是 MonitorService 接口的具体实现 @@ -40,6 +41,7 @@ type monitorService struct { pigTransferLogRepo repository.PigTransferLogRepository pigSickLogRepo repository.PigSickLogRepository pigTradeRepo repository.PigTradeRepository + notificationRepo repository.NotificationRepository } // NewMonitorService 创建一个新的 MonitorService 实例 @@ -56,6 +58,7 @@ func NewMonitorService( pigTransferLogRepo repository.PigTransferLogRepository, pigSickLogRepo repository.PigSickLogRepository, pigTradeRepo repository.PigTradeRepository, + notificationRepo repository.NotificationRepository, ) MonitorService { return &monitorService{ sensorDataRepo: sensorDataRepo, @@ -70,6 +73,7 @@ func NewMonitorService( pigTransferLogRepo: pigTransferLogRepo, pigSickLogRepo: pigSickLogRepo, pigTradeRepo: pigTradeRepo, + notificationRepo: notificationRepo, } } @@ -157,3 +161,8 @@ func (s *monitorService) ListPigPurchases(opts repository.PigPurchaseListOptions func (s *monitorService) ListPigSales(opts repository.PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error) { return s.pigTradeRepo.ListPigSales(opts, page, pageSize) } + +// ListNotifications 负责处理查询通知列表的业务逻辑 +func (s *monitorService) ListNotifications(opts repository.NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error) { + return s.notificationRepo.List(opts, page, pageSize) +} diff --git a/internal/infra/repository/notification_repository.go b/internal/infra/repository/notification_repository.go index 43e833b..a5a6d3c 100644 --- a/internal/infra/repository/notification_repository.go +++ b/internal/infra/repository/notification_repository.go @@ -5,6 +5,7 @@ import ( "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "git.huangwc.com/pig/pig-farm-controller/internal/infra/notify" + "go.uber.org/zap/zapcore" "gorm.io/gorm" ) @@ -13,6 +14,7 @@ type NotificationListOptions struct { UserID *uint // 按用户ID过滤 NotifierType *notify.NotifierType // 按通知器类型过滤 Status *string // 按通知状态过滤 (例如:"success", "failed", "pending") + Level *zapcore.Level // 按通知等级过滤 (例如:"info", "warning", "error") StartTime *time.Time // 通知内容生成时间范围 - 开始时间 (对应 AlarmTimestamp) EndTime *time.Time // 通知内容生成时间范围 - 结束时间 (对应 AlarmTimestamp) OrderBy string // 排序字段,例如 "alarm_timestamp DESC" @@ -79,6 +81,9 @@ func (r *gormNotificationRepository) List(opts NotificationListOptions, page, pa if opts.Status != nil { query = query.Where("status = ?", *opts.Status) } + if opts.Level != nil { + query = query.Where("level = ?", opts.Level.String()) + } if opts.StartTime != nil { query = query.Where("alarm_timestamp >= ?", *opts.StartTime) }