1. 增加程序启动时自动迁移表
2. 增加设备信息model和基础操作函数
This commit is contained in:
@@ -8,10 +8,19 @@ import (
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// migrateModels 需要自动迁移的数据库模型列表
|
||||
var migrateModels = []interface{}{
|
||||
&model.User{},
|
||||
&model.OperationHistory{},
|
||||
&model.Device{},
|
||||
&model.DeviceControl{},
|
||||
}
|
||||
|
||||
// PostgresStorage 代表基于PostgreSQL的存储实现
|
||||
// 使用GORM作为ORM库
|
||||
type PostgresStorage struct {
|
||||
@@ -75,6 +84,14 @@ func (ps *PostgresStorage) Connect() error {
|
||||
sqlDB.SetMaxIdleConns(ps.maxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(ps.connMaxLifetime) * time.Second)
|
||||
|
||||
// 自动迁移数据库表结构
|
||||
ps.logger.Info("正在自动迁移数据库表结构")
|
||||
if err = ps.db.AutoMigrate(migrateModels...); err != nil {
|
||||
ps.logger.Error(fmt.Sprintf("数据库表结构迁移失败: %v", err))
|
||||
return fmt.Errorf("数据库表结构迁移失败: %v", err)
|
||||
}
|
||||
ps.logger.Info("数据库表结构迁移完成")
|
||||
|
||||
ps.logger.Info("PostgreSQL数据库连接成功")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -7,6 +7,27 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeviceRepo 设备仓库接口
|
||||
type DeviceRepo interface {
|
||||
// Create 创建设备
|
||||
Create(device *model.Device) error
|
||||
|
||||
// FindByID 根据ID查找设备
|
||||
FindByID(id uint) (*model.Device, error)
|
||||
|
||||
// FindByParentID 根据上级设备ID查找设备
|
||||
FindByParentID(parentID uint) ([]*model.Device, error)
|
||||
|
||||
// FindByType 根据设备类型查找设备
|
||||
FindByType(deviceType model.DeviceType) ([]*model.Device, error)
|
||||
|
||||
// Update 更新设备信息
|
||||
Update(device *model.Device) error
|
||||
|
||||
// Delete 删除设备
|
||||
Delete(id uint) error
|
||||
}
|
||||
|
||||
// DeviceControlRepo 设备控制仓库接口
|
||||
type DeviceControlRepo interface {
|
||||
// Create 创建设备控制记录
|
||||
@@ -22,11 +43,23 @@ type DeviceControlRepo interface {
|
||||
List(offset, limit int) ([]*model.DeviceControl, error)
|
||||
}
|
||||
|
||||
// deviceRepo 设备仓库实现
|
||||
type deviceRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// deviceControlRepo 设备控制仓库实现
|
||||
type deviceControlRepo struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewDeviceRepo 创建设备仓库实例
|
||||
func NewDeviceRepo(db *gorm.DB) DeviceRepo {
|
||||
return &deviceRepo{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
// NewDeviceControlRepo 创建设备控制仓库实例
|
||||
func NewDeviceControlRepo(db *gorm.DB) DeviceControlRepo {
|
||||
return &deviceControlRepo{
|
||||
@@ -34,6 +67,54 @@ func NewDeviceControlRepo(db *gorm.DB) DeviceControlRepo {
|
||||
}
|
||||
}
|
||||
|
||||
// Create 创建设备
|
||||
func (r *deviceRepo) Create(device *model.Device) error {
|
||||
result := r.db.Create(device)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// FindByID 根据ID查找设备
|
||||
func (r *deviceRepo) FindByID(id uint) (*model.Device, error) {
|
||||
var device model.Device
|
||||
result := r.db.First(&device, id)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
// FindByParentID 根据上级设备ID查找设备
|
||||
func (r *deviceRepo) FindByParentID(parentID uint) ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
result := r.db.Where("parent_id = ?", parentID).Find(&devices)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// FindByType 根据设备类型查找设备
|
||||
func (r *deviceRepo) FindByType(deviceType model.DeviceType) ([]*model.Device, error) {
|
||||
var devices []*model.Device
|
||||
result := r.db.Where("type = ?", deviceType).Find(&devices)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return devices, nil
|
||||
}
|
||||
|
||||
// Update 更新设备信息
|
||||
func (r *deviceRepo) Update(device *model.Device) error {
|
||||
result := r.db.Save(device)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Delete 删除设备
|
||||
func (r *deviceRepo) Delete(id uint) error {
|
||||
result := r.db.Delete(&model.Device{}, id)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// Create 创建设备控制记录
|
||||
func (r *deviceControlRepo) Create(control *model.DeviceControl) error {
|
||||
result := r.db.Create(control)
|
||||
|
||||
Reference in New Issue
Block a user