Files
NetworkAuth/models/function.go

66 lines
2.1 KiB
Go
Raw Permalink Normal View History

package models
import (
"fmt"
"strings"
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
2025-10-27 23:12:15 +08:00
// ============================================================================
// 结构体定义
// ============================================================================
// Function 函数表模型
// 用于管理应用程序的函数代码
// CreatedAt/UpdatedAt 由 GORM 自动维护
type Function struct {
// ID主键自增
ID uint `gorm:"primaryKey;comment:函数ID自增主键" json:"id"`
// UUID函数的唯一标识符36位字符串
UUID string `gorm:"uniqueIndex;size:36;not null;comment:函数的唯一标识符" json:"uuid"`
// Number函数编号13位Unix时间戳毫秒级
Number string `gorm:"uniqueIndex;size:13;not null;comment:函数编号13位Unix时间戳" json:"number"`
// AppUUID应用绑定标识符"0"表示全局函数其他UUID表示绑定到特定应用
AppUUID string `gorm:"size:36;not null;default:'0';comment:应用绑定标识符" json:"app_uuid"`
// Alias函数别名便于识别和管理
Alias string `gorm:"uniqueIndex;size:100;not null;comment:函数别名" json:"alias"`
// Code函数代码内容
Code string `gorm:"type:text;comment:函数代码" json:"code"`
// Remark备注信息用于描述函数用途
Remark string `gorm:"type:text;comment:备注信息" json:"remark"`
// 时间字段
CreatedAt time.Time `gorm:"comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"comment:更新时间" json:"updated_at"`
}
2025-10-27 23:12:15 +08:00
// ============================================================================
// 结构体方法
// ============================================================================
// BeforeCreate 在创建记录前自动生成UUID和Number
func (function *Function) BeforeCreate(tx *gorm.DB) error {
// 生成UUID
if function.UUID == "" {
function.UUID = strings.ToUpper(uuid.New().String())
}
// 生成Number使用13位Unix时间戳毫秒级
function.Number = fmt.Sprintf("%d", time.Now().UnixMilli())
return nil
}
// TableName 指定表名
func (Function) TableName() string {
return "functions"
2025-10-27 23:12:15 +08:00
}