实现service层

This commit is contained in:
2025-10-25 14:36:24 +08:00
parent f6d2069e1a
commit f62cc1c4a9
2 changed files with 14 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -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)
}