feat: refact id column define

This commit is contained in:
engigu
2026-03-03 17:25:38 +08:00
parent 117ae126f7
commit 4589c64923
62 changed files with 881 additions and 454 deletions
+7 -9
View File
@@ -1,8 +1,6 @@
package models
import (
"strconv"
"github.com/engigu/baihu-panel/internal/constant"
"gorm.io/gorm"
@@ -10,7 +8,7 @@ import (
// Agent 远程执行代理
type Agent struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Name string `json:"name" gorm:"size:100;not null"` // Agent 名称
Token string `json:"token" gorm:"size:64;index"` // 认证 Token(可重复使用)
MachineID string `json:"machine_id" gorm:"size:64;uniqueIndex"` // 机器识别码(唯一)
@@ -36,7 +34,7 @@ func (Agent) TableName() string {
// AgentToken Agent 令牌
type AgentToken struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Token string `json:"token" gorm:"size:64;uniqueIndex;not null"` // 令牌
Remark string `json:"remark" gorm:"size:255"` // 备注
MaxUses int `json:"max_uses" gorm:"default:0"` // 最大使用次数,0 表示无限制
@@ -54,7 +52,7 @@ func (AgentToken) TableName() string {
// AgentTask Agent 任务配置(用于下发给 Agent)
type AgentTask struct {
ID uint `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Command string `json:"command"`
Schedule string `json:"schedule"`
@@ -67,7 +65,7 @@ type AgentTask struct {
}
func (t AgentTask) GetID() string {
return strconv.FormatUint(uint64(t.ID), 10)
return t.ID
}
func (t AgentTask) GetName() string {
@@ -88,9 +86,9 @@ func (t AgentTask) GetRandomRange() int {
// AgentTaskResult Agent 上报的任务执行结果
type AgentTaskResult struct {
TaskID uint `json:"task_id"`
LogID uint `json:"log_id"`
AgentID uint `json:"agent_id"`
TaskID string `json:"task_id"`
LogID string `json:"log_id"`
AgentID string `json:"agent_id"`
Command string `json:"command"`
Output string `json:"output"`
Error string `json:"error"` // 额外的系统错误信息
+1 -1
View File
@@ -8,7 +8,7 @@ import (
// Dependency 依赖包模型
type Dependency struct {
ID int `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Name string `json:"name" gorm:"size:100;not null"`
Version string `json:"version" gorm:"size:50"`
Language string `json:"language" gorm:"size:100;index"` // 关联语言 (node, python...)
+4 -4
View File
@@ -8,12 +8,12 @@ import (
// EnvironmentVariable represents an environment variable
type EnvironmentVariable struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Name string `json:"name" gorm:"size:255;not null"`
Value string `json:"value" gorm:"type:text"`
Remark string `json:"remark" gorm:"size:500"`
Hidden bool `json:"hidden" gorm:"default:true"`
UserID uint `json:"user_id" gorm:"index"`
UserID string `json:"user_id" gorm:"size:20;index"`
CreatedAt LocalTime `json:"created_at"`
UpdatedAt LocalTime `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
@@ -25,10 +25,10 @@ func (EnvironmentVariable) TableName() string {
// Script represents a script file
type Script struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Name string `json:"name" gorm:"size:255;not null"`
Content string `json:"content" gorm:"type:text"`
UserID uint `json:"user_id" gorm:"index"`
UserID string `json:"user_id" gorm:"size:20;index"`
CreatedAt LocalTime `json:"created_at"`
UpdatedAt LocalTime `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
+1 -1
View File
@@ -6,7 +6,7 @@ import (
)
type Language struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Plugin string `json:"plugin" gorm:"size:100;not null;index"`
Version string `json:"version" gorm:"size:100;not null;index"`
InstallPath string `json:"install_path" gorm:"size:255"`
+1 -1
View File
@@ -6,7 +6,7 @@ import (
// LoginLog 登录日志
type LoginLog struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Username string `json:"username" gorm:"size:100;index;not null"`
IP string `json:"ip" gorm:"size:50"`
UserAgent string `json:"user_agent" gorm:"size:500"`
+2 -2
View File
@@ -6,8 +6,8 @@ import (
// SendStats 任务执行统计
type SendStats struct {
ID uint `json:"id" gorm:"primaryKey"`
TaskID uint `json:"task_id" gorm:"uniqueIndex:idx_task_day_status"`
ID string `json:"id" gorm:"primaryKey;size:20"`
TaskID string `json:"task_id" gorm:"size:20;uniqueIndex:idx_task_day_status"`
Day string `json:"day" gorm:"size:10;uniqueIndex:idx_task_day_status"` // 格式: 2006-01-02
Status string `json:"status" gorm:"size:20;uniqueIndex:idx_task_day_status"`
Num int `json:"num" gorm:"default:0"`
+1 -1
View File
@@ -6,7 +6,7 @@ import (
// Setting 系统设置
type Setting struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Section string `json:"section" gorm:"size:50;not null;index:idx_section_key"`
Key string `json:"key" gorm:"size:100;not null;index:idx_section_key"`
Value string `json:"value" gorm:"type:text"`
+8 -10
View File
@@ -1,8 +1,6 @@
package models
import (
"fmt"
"github.com/engigu/baihu-panel/internal/constant"
"gorm.io/gorm"
@@ -34,7 +32,7 @@ type TaskConfig struct {
// Task 代表一个计划任务
type Task struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Name string `json:"name" gorm:"size:255;not null"`
Command string `json:"command" gorm:"type:text"` // 普通任务的命令
Tags string `json:"tags" gorm:"size:255;default:''"` // 标签,逗号分隔
@@ -45,9 +43,9 @@ type Task struct {
Timeout int `json:"timeout" gorm:"default:30"` // 超时时间(分钟),默认30分钟
WorkDir string `json:"work_dir" gorm:"size:255;default:''"` // 工作目录,为空则使用 scripts 目录
CleanConfig string `json:"clean_config" gorm:"size:255;default:''"` // 清理配置 JSON
Envs string `json:"envs" gorm:"size:255;default:''"` // 环境变量ID列表,逗号分隔
Envs string `json:"envs" gorm:"type:text"` // 环境变量ID列表,逗号分隔
Languages []map[string]string `json:"languages" gorm:"serializer:json;type:text"` // 针对本地任务的语言配置列表
AgentID *uint `json:"agent_id" gorm:"index"` // Agent ID,为空表示本地执行
AgentID *string `json:"agent_id" gorm:"size:20;index"` // Agent ID,为空表示本地执行
RetryCount int `json:"retry_count" gorm:"default:0"` // 失败重试次数
RetryInterval int `json:"retry_interval" gorm:"default:0"` // 失败重试间隔(秒)
RandomRange int `json:"random_range" gorm:"default:0"` // 随机延迟范围(秒)
@@ -65,7 +63,7 @@ func (Task) TableName() string {
}
func (t *Task) GetID() string {
return fmt.Sprintf("%d", t.ID)
return t.ID
}
func (t *Task) GetName() string {
@@ -93,7 +91,7 @@ func (t *Task) GetLanguages() []map[string]string {
}
func (t *Task) GetUseMise() bool {
return t.AgentID == nil || *t.AgentID == 0
return t.AgentID == nil || *t.AgentID == ""
}
func (t *Task) UseMise() bool {
@@ -110,9 +108,9 @@ func (t *Task) GetRandomRange() int {
// TaskLog 代表任务执行的日志记录
type TaskLog struct {
ID uint `json:"id" gorm:"primaryKey"`
TaskID uint `json:"task_id" gorm:"index"`
AgentID *uint `json:"agent_id" gorm:"index"` // Agent ID,为空表示本地执行
ID string `json:"id" gorm:"primaryKey;size:20"`
TaskID string `json:"task_id" gorm:"size:20;index"`
AgentID *string `json:"agent_id" gorm:"size:20;index"` // Agent ID,为空表示本地执行
Command string `json:"command" gorm:"type:text"`
Output string `json:"-" gorm:"type:longtext"` // gzip+base64 压缩后的日志
Error string `json:"error" gorm:"type:text"` // 额外的系统错误信息
+1 -1
View File
@@ -8,7 +8,7 @@ import (
// User represents a system user
type User struct {
ID uint `json:"id" gorm:"primaryKey"`
ID string `json:"id" gorm:"primaryKey;size:20"`
Username string `json:"username" gorm:"size:100;uniqueIndex;not null"`
Password string `json:"-" gorm:"size:255;not null"`
Email string `json:"email" gorm:"size:255"`
+2 -2
View File
@@ -6,7 +6,7 @@ import (
// AgentVO 代理视图对象
type AgentVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Status string `json:"status"`
@@ -71,7 +71,7 @@ func ToAgentVOListFromModels(agents []models.Agent) []*AgentVO {
// AgentTokenVO 代理令牌视图对象
type AgentTokenVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Token string `json:"token"`
Remark string `json:"remark"`
MaxUses int `json:"max_uses"`
+1 -1
View File
@@ -8,7 +8,7 @@ import (
// DependencyVO 依赖包视图对象
type DependencyVO struct {
ID int `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Version string `json:"version"`
Language string `json:"language"`
+1 -1
View File
@@ -6,7 +6,7 @@ import (
// ScriptVO 脚本视图对象
type ScriptVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Content string `json:"content,omitempty"` // 仅在拉取详情时返回
CreatedAt models.LocalTime `json:"created_at"`
+3 -3
View File
@@ -6,7 +6,7 @@ import (
// UserVO 用户视图对象
type UserVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
Role string `json:"role"`
@@ -31,7 +31,7 @@ func ToUserVO(user *models.User) *UserVO {
// EnvVO 环境变量视图对象
type EnvVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Value string `json:"value"`
Remark string `json:"remark"`
@@ -79,7 +79,7 @@ func ToEnvVOListFromModels(envs []models.EnvironmentVariable) []*EnvVO {
// LoginLogVO 登录日志视图对象
type LoginLogVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Username string `json:"username"`
IP string `json:"ip"`
UserAgent string `json:"user_agent"`
+6 -6
View File
@@ -7,7 +7,7 @@ import (
// TaskVO 任务视图对象
type TaskVO struct {
ID uint `json:"id"`
ID string `json:"id"`
Name string `json:"name"`
Command string `json:"command"`
Tags string `json:"tags"`
@@ -20,7 +20,7 @@ type TaskVO struct {
CleanConfig string `json:"clean_config"`
Envs string `json:"envs"`
Languages []map[string]string `json:"languages"`
AgentID *uint `json:"agent_id"`
AgentID *string `json:"agent_id"`
Enabled bool `json:"enabled"`
RetryCount int `json:"retry_count"`
RetryInterval int `json:"retry_interval"`
@@ -85,11 +85,11 @@ func ToTaskVOListFromModels(tasks []models.Task) []*TaskVO {
// TaskLogVO 任务历史视图对象
type TaskLogVO struct {
ID uint `json:"id"`
TaskID uint `json:"task_id"`
ID string `json:"id"`
TaskID string `json:"task_id"`
TaskName string `json:"task_name"`
TaskType string `json:"task_type"`
AgentID *uint `json:"agent_id"`
AgentID *string `json:"agent_id"`
Command string `json:"command"`
Error string `json:"error"`
Status string `json:"status"`
@@ -148,7 +148,7 @@ func ToTaskLogVOListFromModels(logs []models.TaskLog) []*TaskLogVO {
// ExecutionResultVO 任务执行结果视图对象
type ExecutionResultVO struct {
TaskID string `json:"task_id"`
LogID uint `json:"log_id,omitempty"`
LogID string `json:"log_id,omitempty"`
Success bool `json:"success"`
Status string `json:"status"`
Output string `json:"output,omitempty"`