71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
package model
|
||
|
||
import (
|
||
"time"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// RobotStatus 表示机器人状态的枚举类型
|
||
type RobotStatus string
|
||
|
||
const (
|
||
RobotStatusOnline RobotStatus = "online"
|
||
RobotStatusOffline RobotStatus = "offline"
|
||
RobotStatusError RobotStatus = "error"
|
||
)
|
||
|
||
// Robot 表示微信机器人实例
|
||
type Robot struct {
|
||
BaseModel
|
||
ContainerID string `gorm:"column:container_id;uniqueIndex:idx_container_id,length:64" json:"container_id"`
|
||
ShortContainerID string `gorm:"column:short_container_id;index" json:"short_container_id"` // 容器ID的前12位
|
||
ContainerHost string `gorm:"column:container_host" json:"container_host"` // 容器访问地址,格式为ip:port
|
||
WechatID string `gorm:"column:wechat_id;index:idx_wechat_id,length:64" json:"wechat_id"`
|
||
Nickname string `gorm:"column:nickname" json:"nickname"`
|
||
Avatar string `gorm:"column:avatar" json:"avatar"`
|
||
Status RobotStatus `gorm:"column:status;default:'offline'" json:"status"`
|
||
LastLoginAt *time.Time `gorm:"column:last_login_at" json:"last_login_at"`
|
||
QRCodePath string `gorm:"column:qrcode_path" json:"qrcode_path"` // 二维码路径,用于登录
|
||
ErrorMessage string `gorm:"column:error_message" json:"error_message"`
|
||
}
|
||
|
||
// TableName 指定表名
|
||
func (Robot) TableName() string {
|
||
return "robots"
|
||
}
|
||
|
||
// BeforeCreate GORM钩子,在创建前处理
|
||
func (r *Robot) BeforeCreate(tx *gorm.DB) error {
|
||
// 设置短容器ID
|
||
if len(r.ContainerID) > 12 {
|
||
r.ShortContainerID = r.ContainerID[:12]
|
||
} else {
|
||
r.ShortContainerID = r.ContainerID
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// BeforeSave GORM钩子,在保存前处理
|
||
func (r *Robot) BeforeSave(tx *gorm.DB) error {
|
||
// 确保短容器ID总是保持同步
|
||
if len(r.ContainerID) > 12 {
|
||
r.ShortContainerID = r.ContainerID[:12]
|
||
} else {
|
||
r.ShortContainerID = r.ContainerID
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// AfterCreate GORM钩子,在创建后处理
|
||
func (r *Robot) AfterCreate(tx *gorm.DB) error {
|
||
// 可以在这里进行一些创建后的操作
|
||
return nil
|
||
}
|
||
|
||
// BeforeDelete GORM钩子,在删除前处理
|
||
func (r *Robot) BeforeDelete(tx *gorm.DB) error {
|
||
// 在删除机器人之前,可以处理相关依赖
|
||
return nil
|
||
}
|