issue_22 #41
							
								
								
									
										106
									
								
								internal/infra/repository/notification_repository.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										106
									
								
								internal/infra/repository/notification_repository.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,106 @@ | ||||
| package repository | ||||
|  | ||||
| import ( | ||||
| 	"time" | ||||
|  | ||||
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" | ||||
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/notify" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
|  | ||||
| // NotificationListOptions 定义了查询通知列表时的可选参数 | ||||
| type NotificationListOptions struct { | ||||
| 	UserID       *uint                // 按用户ID过滤 | ||||
| 	NotifierType *notify.NotifierType // 按通知器类型过滤 | ||||
| 	Status       *string              // 按通知状态过滤 (例如:"success", "failed", "pending") | ||||
| 	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.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 | ||||
| } | ||||
							
								
								
									
										6
									
								
								internal/infra/repository/repository.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/infra/repository/repository.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,6 @@ | ||||
| package repository | ||||
|  | ||||
| import "errors" | ||||
|  | ||||
| // ErrInvalidPagination 表示分页参数无效 | ||||
| var ErrInvalidPagination = errors.New("无效的分页参数:page和pageSize必须为大于0") | ||||
| @@ -1,16 +1,12 @@ | ||||
| package repository | ||||
|  | ||||
| import ( | ||||
| 	"errors" | ||||
| 	"time" | ||||
|  | ||||
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" | ||||
| 	"gorm.io/gorm" | ||||
| ) | ||||
|  | ||||
| // ErrInvalidPagination 表示分页参数无效 | ||||
| var ErrInvalidPagination = errors.New("无效的分页参数:page和pageSize必须为大于0") | ||||
|  | ||||
| // SensorDataListOptions 定义了查询传感器数据列表时的可选参数 | ||||
| type SensorDataListOptions struct { | ||||
| 	DeviceID   *uint | ||||
|   | ||||
		Reference in New Issue
	
	Block a user