112 lines
3.9 KiB
Go
112 lines
3.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"time"
|
|
|
|
"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"
|
|
)
|
|
|
|
// NotificationListOptions 定义了查询通知列表时的可选参数
|
|
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"
|
|
}
|
|
|
|
// NotificationRepository 定义了与通知记录相关的数据库操作接口。
|
|
type NotificationRepository interface {
|
|
// Create 将一条新的通知记录插入数据库。
|
|
Create(notification *models.Notification) error
|
|
// CreateInTx 在给定的事务中插入一条新的通知记录。
|
|
CreateInTx(tx *gorm.DB, notification *models.Notification) error
|
|
// BatchCreate 批量插入多条通知记录。
|
|
BatchCreate(notifications []*models.Notification) error
|
|
// List 支持分页和过滤的通知列表查询。
|
|
// 返回通知列表、总记录数和错误。
|
|
List(opts NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error)
|
|
}
|
|
|
|
// gormNotificationRepository 是 NotificationRepository 的 GORM 实现。
|
|
type gormNotificationRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewGormNotificationRepository 创建一个新的 NotificationRepository GORM 实现实例。
|
|
func NewGormNotificationRepository(db *gorm.DB) NotificationRepository {
|
|
return &gormNotificationRepository{db: db}
|
|
}
|
|
|
|
// Create 将一条新的通知记录插入数据库。
|
|
func (r *gormNotificationRepository) Create(notification *models.Notification) error {
|
|
return r.db.Create(notification).Error
|
|
}
|
|
|
|
// CreateInTx 在给定的事务中插入一条新的通知记录。
|
|
func (r *gormNotificationRepository) CreateInTx(tx *gorm.DB, notification *models.Notification) error {
|
|
return tx.Create(notification).Error
|
|
}
|
|
|
|
// BatchCreate 批量插入多条通知记录。
|
|
func (r *gormNotificationRepository) BatchCreate(notifications []*models.Notification) error {
|
|
// GORM 的 Create 方法在传入切片时会自动进行批量插入
|
|
return r.db.Create(¬ifications).Error
|
|
}
|
|
|
|
// List 实现了分页和过滤查询通知记录的功能
|
|
func (r *gormNotificationRepository) List(opts NotificationListOptions, page, pageSize int) ([]models.Notification, int64, error) {
|
|
// --- 校验分页参数 ---
|
|
if page <= 0 || pageSize <= 0 {
|
|
return nil, 0, ErrInvalidPagination // 复用已定义的错误
|
|
}
|
|
|
|
var results []models.Notification
|
|
var total int64
|
|
|
|
query := r.db.Model(&models.Notification{})
|
|
|
|
// --- 应用过滤条件 ---
|
|
if opts.UserID != nil {
|
|
query = query.Where("user_id = ?", *opts.UserID)
|
|
}
|
|
if opts.NotifierType != nil {
|
|
query = query.Where("notifier_type = ?", *opts.NotifierType)
|
|
}
|
|
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)
|
|
}
|
|
if opts.EndTime != nil {
|
|
query = query.Where("alarm_timestamp <= ?", *opts.EndTime)
|
|
}
|
|
|
|
// --- 计算总数 ---
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
// --- 应用排序条件 ---
|
|
orderBy := "alarm_timestamp DESC" // 默认按时间倒序
|
|
if opts.OrderBy != "" {
|
|
orderBy = opts.OrderBy
|
|
}
|
|
query = query.Order(orderBy)
|
|
|
|
// --- 分页 ---
|
|
offset := (page - 1) * pageSize
|
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
|
|
|
return results, total, err
|
|
}
|