修改infra.repository包

This commit is contained in:
2025-11-05 23:00:07 +08:00
parent 97aea66f7c
commit 10b123ab93
25 changed files with 877 additions and 608 deletions

View File

@@ -1,9 +1,12 @@
package repository
import (
"context"
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"gorm.io/gorm"
)
@@ -18,40 +21,44 @@ type DeviceCommandLogListOptions struct {
// DeviceCommandLogRepository 定义了设备下行命令历史记录的数据访问接口
type DeviceCommandLogRepository interface {
Create(record *models.DeviceCommandLog) error
FindByMessageID(messageID string) (*models.DeviceCommandLog, error)
UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error
Create(ctx context.Context, record *models.DeviceCommandLog) error
FindByMessageID(ctx context.Context, messageID string) (*models.DeviceCommandLog, error)
UpdateAcknowledgedAt(ctx context.Context, messageID string, acknowledgedAt time.Time, receivedSuccess bool) error
// List 支持分页和过滤的列表查询
List(opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error)
List(ctx context.Context, opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error)
}
// gormDeviceCommandLogRepository 是 DeviceCommandLogRepository 接口的 GORM 实现
type gormDeviceCommandLogRepository struct {
db *gorm.DB
ctx context.Context
db *gorm.DB
}
// NewGormDeviceCommandLogRepository 创建一个新的 DeviceCommandLogRepository GORM 实现
func NewGormDeviceCommandLogRepository(db *gorm.DB) DeviceCommandLogRepository {
return &gormDeviceCommandLogRepository{db: db}
func NewGormDeviceCommandLogRepository(ctx context.Context, db *gorm.DB) DeviceCommandLogRepository {
return &gormDeviceCommandLogRepository{ctx: ctx, db: db}
}
// Create 实现 DeviceCommandLogRepository 接口的 Create 方法
func (r *gormDeviceCommandLogRepository) Create(record *models.DeviceCommandLog) error {
return r.db.Create(record).Error
func (r *gormDeviceCommandLogRepository) Create(ctx context.Context, record *models.DeviceCommandLog) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "Create")
return r.db.WithContext(repoCtx).Create(record).Error
}
// FindByMessageID 实现 DeviceCommandLogRepository 接口的 FindByMessageID 方法
func (r *gormDeviceCommandLogRepository) FindByMessageID(messageID string) (*models.DeviceCommandLog, error) {
func (r *gormDeviceCommandLogRepository) FindByMessageID(ctx context.Context, messageID string) (*models.DeviceCommandLog, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "FindByMessageID")
var record models.DeviceCommandLog
if err := r.db.Where("message_id = ?", messageID).First(&record).Error; err != nil {
if err := r.db.WithContext(repoCtx).Where("message_id = ?", messageID).First(&record).Error; err != nil {
return nil, err
}
return &record, nil
}
// UpdateAcknowledgedAt 实现 DeviceCommandLogRepository 接口的 UpdateAcknowledgedAt 方法
func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error {
return r.db.Model(&models.DeviceCommandLog{}).
func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(ctx context.Context, messageID string, acknowledgedAt time.Time, receivedSuccess bool) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdateAcknowledgedAt")
return r.db.WithContext(repoCtx).Model(&models.DeviceCommandLog{}).
Where("message_id = ?", messageID).
Updates(map[string]interface{}{
"acknowledged_at": acknowledgedAt,
@@ -60,7 +67,8 @@ func (r *gormDeviceCommandLogRepository) UpdateAcknowledgedAt(messageID string,
}
// List 实现了分页和过滤查询设备命令日志的功能
func (r *gormDeviceCommandLogRepository) List(opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error) {
func (r *gormDeviceCommandLogRepository) List(ctx context.Context, opts DeviceCommandLogListOptions, page, pageSize int) ([]models.DeviceCommandLog, int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "List")
// --- 校验分页参数 ---
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
@@ -69,7 +77,7 @@ func (r *gormDeviceCommandLogRepository) List(opts DeviceCommandLogListOptions,
var results []models.DeviceCommandLog
var total int64
query := r.db.Model(&models.DeviceCommandLog{})
query := r.db.WithContext(repoCtx).Model(&models.DeviceCommandLog{})
// --- 应用过滤条件 ---
if opts.DeviceID != nil {