package model import ( "gitee.ltd/lxh/wechat-robot/internal/utils" "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;index:deleted,unique,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 DeviceId string `gorm:"column:device_id;index:deleted,unique;" json:"deviceId"` // 设备Id DeviceName string `gorm:"column:device_name" json:"deviceName"` // 设备名称 WechatID string `gorm:"column:wechat_id;index:deleted,unique,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"` 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 } // CheckDevice // @description: 检查设备信息 // @receiver r // @return bool 是否重新处理过,如果是,就返回true func (r *Robot) CheckDevice() (flag bool) { // 如果是空的,就获取环境变量配置的值,如果还是空的,就生成一个新的 if r.DeviceId == "" { r.DeviceId = utils.GetDeviceId() flag = true } // 如果是空的,就获取环境变量配置的值,如果还是空的,就生成一个新的 if r.DeviceName == "" { r.DeviceName = utils.GetDeviceName() flag = true } return }