添加callback,迁移文件

This commit is contained in:
2025-12-15 14:37:34 +08:00
parent ad946af5ee
commit 478e37903b
16 changed files with 52 additions and 367 deletions

View File

@@ -44,6 +44,11 @@ func registerGORMCallbacks(deps *Dependencies, stores *stores) error {
return err
}
//注册自动添加创建&更新人 Clalback
if err := pkgGorm.RegisterSetCreatorUpdaterCallback(deps.DB); err != nil {
return err
}
// TODO: 在此添加其他 GORM Callbacks
return nil

View File

@@ -7,14 +7,14 @@ import (
// Account 账号模型
type Account struct {
gorm.Model
BaseModel
Username string `gorm:"uniqueIndex:idx_account_username,where:deleted_at IS NULL;not null;size:50" json:"username"`
Phone string `gorm:"uniqueIndex:idx_account_phone,where:deleted_at IS NULL;not null;size:20" json:"phone"`
Password string `gorm:"not null;size:255" json:"-"` // 不返回给客户端
UserType int `gorm:"not null;index" json:"user_type"` // 1=root, 2=平台, 3=代理, 4=企业
ShopID *uint `gorm:"index" json:"shop_id,omitempty"`
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
Status int `gorm:"not null;default:1" json:"status"` // 0=禁用, 1=启用
BaseModel `gorm:"embedded"`
Username string `gorm:"uniqueIndex:idx_account_username,where:deleted_at IS NULL;not null;size:50" json:"username"`
Phone string `gorm:"uniqueIndex:idx_account_phone,where:deleted_at IS NULL;not null;size:20" json:"phone"`
Password string `gorm:"not null;size:255" json:"-"` // 不返回给客户端
UserType int `gorm:"not null;index" json:"user_type"` // 1=root, 2=平台, 3=代理, 4=企业
ShopID *uint `gorm:"index" json:"shop_id,omitempty"`
ParentID *uint `gorm:"index" json:"parent_id,omitempty"`
Status int `gorm:"not null;default:1" json:"status"` // 0=禁用, 1=启用
}
// TableName 指定表名

View File

@@ -1,17 +1,9 @@
package model
import "gorm.io/gorm"
// atlas:ignore
//
// BaseModel 基础模型,包含通用字段
type BaseModel struct {
Creator uint `gorm:"not null" json:"creator"`
Updater uint `gorm:"not null" json:"updater"`
}
func (b *BaseModel) BeforeCreate(tx *gorm.DB) error {
if userID, ok := tx.Statement.Context.Value("current_user_id").(uint); ok {
b.Creator = userID
b.Updater = userID
}
return nil
}

View File

@@ -7,7 +7,7 @@ import (
// Permission 权限模型
type Permission struct {
gorm.Model
BaseModel
BaseModel `gorm:"embedded"`
PermName string `gorm:"not null;size:50" json:"perm_name"`
PermCode string `gorm:"uniqueIndex:idx_permission_code,where:deleted_at IS NULL;not null;size:100" json:"perm_code"`

View File

@@ -7,7 +7,7 @@ import (
// Role 角色模型
type Role struct {
gorm.Model
BaseModel
BaseModel `gorm:"embedded"`
RoleName string `gorm:"not null;size:50" json:"role_name"`
RoleDesc string `gorm:"size:255" json:"role_desc"`

View File

@@ -7,7 +7,7 @@ import (
// RolePermission 角色-权限关联模型
type RolePermission struct {
gorm.Model
BaseModel
BaseModel `gorm:"embedded"`
RoleID uint `gorm:"not null;index;uniqueIndex:idx_role_permission_unique,where:deleted_at IS NULL" json:"role_id"`
PermID uint `gorm:"not null;index;uniqueIndex:idx_role_permission_unique,where:deleted_at IS NULL" json:"perm_id"`