1. 操作历史增删改查
2. 定义设备开关接口(只有定义)
This commit is contained in:
71
internal/storage/repository/device.go
Normal file
71
internal/storage/repository/device.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Package repository 提供数据访问层实现
|
||||
// 包含设备控制等数据实体的仓库接口和实现
|
||||
package repository
|
||||
|
||||
import (
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeviceControlRepo 设备控制仓库接口
|
||||
type DeviceControlRepo interface {
|
||||
// Create 创建设备控制记录
|
||||
Create(control *model.DeviceControl) error
|
||||
|
||||
// FindByUserID 根据用户ID查找设备控制记录
|
||||
FindByUserID(userID uint) ([]*model.DeviceControl, error)
|
||||
|
||||
// FindByID 根据ID查找设备控制记录
|
||||
FindByID(id uint) (*model.DeviceControl, error)
|
||||
|
||||
// List 获取设备控制记录列表(分页)
|
||||
List(offset, limit int) ([]*model.DeviceControl, error)
|
||||
}
|
||||
|
||||
// deviceControlRepo 设备控制仓库实现
|
||||
type deviceControlRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewDeviceControlRepo 创建设备控制仓库实例
|
||||
func NewDeviceControlRepo(db *gorm.DB) DeviceControlRepo {
|
||||
return &deviceControlRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建设备控制记录
|
||||
func (r *deviceControlRepo) Create(control *model.DeviceControl) error {
|
||||
result := r.db.Create(control)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// FindByUserID 根据用户ID查找设备控制记录
|
||||
func (r *deviceControlRepo) FindByUserID(userID uint) ([]*model.DeviceControl, error) {
|
||||
var controls []*model.DeviceControl
|
||||
result := r.db.Where("user_id = ?", userID).Order("created_at DESC").Find(&controls)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return controls, nil
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找设备控制记录
|
||||
func (r *deviceControlRepo) FindByID(id uint) (*model.DeviceControl, error) {
|
||||
var control model.DeviceControl
|
||||
result := r.db.First(&control, id)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &control, nil
|
||||
}
|
||||
|
||||
// List 获取设备控制记录列表(分页)
|
||||
func (r *deviceControlRepo) List(offset, limit int) ([]*model.DeviceControl, error) {
|
||||
var controls []*model.DeviceControl
|
||||
result := r.db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&controls)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return controls, nil
|
||||
}
|
||||
Reference in New Issue
Block a user