53 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package repository
 | |
| 
 | |
| import (
 | |
| 	"time"
 | |
| 
 | |
| 	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
 | |
| 	"gorm.io/gorm"
 | |
| )
 | |
| 
 | |
| // DeviceCommandLogRepository 定义了设备下行命令历史记录的数据访问接口
 | |
| type DeviceCommandLogRepository interface {
 | |
| 	Create(record *models.DeviceCommandLog) error
 | |
| 	FindByMessageID(messageID string) (*models.DeviceCommandLog, error)
 | |
| 	// UpdateAcknowledgedAt 用于更新指定 MessageID 的下行命令记录的确认时间及接收成功状态。
 | |
| 	// AcknowledgedAt 和 ReceivedSuccess 字段会被更新。
 | |
| 	UpdateAcknowledgedAt(messageID string, acknowledgedAt time.Time, receivedSuccess bool) error
 | |
| }
 | |
| 
 | |
| // gormDeviceCommandLogRepository 是 DeviceCommandLogRepository 接口的 GORM 实现
 | |
| type gormDeviceCommandLogRepository struct {
 | |
| 	db *gorm.DB
 | |
| }
 | |
| 
 | |
| // NewGormDeviceCommandLogRepository 创建一个新的 DeviceCommandLogRepository GORM 实现
 | |
| func NewGormDeviceCommandLogRepository(db *gorm.DB) DeviceCommandLogRepository {
 | |
| 	return &gormDeviceCommandLogRepository{db: db}
 | |
| }
 | |
| 
 | |
| // Create 实现 DeviceCommandLogRepository 接口的 Create 方法
 | |
| func (r *gormDeviceCommandLogRepository) Create(record *models.DeviceCommandLog) error {
 | |
| 	return r.db.Create(record).Error
 | |
| }
 | |
| 
 | |
| // FindByMessageID 实现 DeviceCommandLogRepository 接口的 FindByMessageID 方法
 | |
| func (r *gormDeviceCommandLogRepository) FindByMessageID(messageID string) (*models.DeviceCommandLog, error) {
 | |
| 	var record models.DeviceCommandLog
 | |
| 	if err := r.db.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 {
 | |
| 	// 使用 Updates 方法更新指定字段
 | |
| 	return r.db.Model(&models.DeviceCommandLog{}).
 | |
| 		Where("message_id = ?", messageID).
 | |
| 		Updates(map[string]interface{}{
 | |
| 			"acknowledged_at":  acknowledgedAt,
 | |
| 			"received_success": receivedSuccess,
 | |
| 		}).Error
 | |
| }
 |