72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
// 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
|
|
}
|