Files
pig-farm-controller/internal/model/device.go
huang bab44e7e3a 1. 增加程序启动时自动迁移表
2. 增加设备信息model和基础操作函数
2025-09-08 10:58:26 +08:00

100 lines
2.7 KiB
Go

// Package model 提供数据模型定义
// 包含设备控制等相关数据结构
package model
import (
"time"
"gorm.io/gorm"
)
// DeviceType 设备类型枚举
type DeviceType string
const (
// DeviceTypeFan 风机
DeviceTypeFan DeviceType = "fan"
// DeviceTypeWaterCurtain 水帘
DeviceTypeWaterCurtain DeviceType = "water_curtain"
// DeviceTypePigPenController 猪舍主控
DeviceTypePigPenController DeviceType = "pig_pen_controller"
// DeviceTypeFeedMillController 做料车间主控
DeviceTypeFeedMillController DeviceType = "feed_mill_controller"
)
// Device 代表设备信息
type Device struct {
// ID 设备ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// Name 设备名称
Name string `gorm:"not null;column:name" json:"name"`
// Type 设备类型
Type DeviceType `gorm:"not null;column:type" json:"type"`
// ParentID 上级设备ID(用于设备层级关系,指向区域主控设备)
ParentID *uint `gorm:"column:parent_id;index" json:"parent_id"`
// Status 设备状态
Status string `gorm:"not null;column:status" json:"status"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}
// TableName 指定Device模型对应的数据库表名
func (Device) TableName() string {
return "devices"
}
// DeviceControl 代表设备控制记录
type DeviceControl struct {
// ID 记录ID
ID uint `gorm:"primaryKey;column:id" json:"id"`
// UserID 用户ID
UserID uint `gorm:"not null;column:user_id;index" json:"user_id"`
// Location 设备安装位置描述
Location string `gorm:"not null;column:location" json:"location"`
// DeviceType 设备类型
DeviceType DeviceType `gorm:"not null;column:device_type" json:"device_type"`
// DeviceID 设备编号
DeviceID string `gorm:"not null;column:device_id" json:"device_id"`
// Action 控制动作(开/关)
Action string `gorm:"not null;column:action" json:"action"`
// Status 控制状态(成功/失败)
Status string `gorm:"not null;column:status" json:"status"`
// Result 控制结果详情(可选)
Result string `gorm:"column:result" json:"result"`
// CreatedAt 创建时间
CreatedAt time.Time `gorm:"column:created_at" json:"created_at"`
// UpdatedAt 更新时间
UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"`
// DeletedAt 删除时间(用于软删除)
DeletedAt gorm.DeletedAt `gorm:"index;column:deleted_at" json:"-"`
}
// TableName 指定DeviceControl模型对应的数据库表名
func (DeviceControl) TableName() string {
return "device_controls"
}