新增数据库主键处理
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/snowflake"
|
||||
"gorm.io/gorm"
|
||||
"hospital-admin-api/config"
|
||||
)
|
||||
|
||||
// AdminPost 后台-岗位表
|
||||
type AdminPost struct {
|
||||
PostId int64 `gorm:"column:post_id;type:bigint(19);primary_key;comment:主键id" json:"post_id"`
|
||||
@@ -11,3 +17,17 @@ type AdminPost struct {
|
||||
func (m *AdminPost) TableName() string {
|
||||
return "gdxz_admin_post"
|
||||
}
|
||||
|
||||
func (m *AdminPost) BeforeCreate(tx *gorm.DB) error {
|
||||
if m.PostId == 0 {
|
||||
// 创建雪花算法实例
|
||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成新的雪花算法 ID
|
||||
m.PostId = sf.Generate().Int64()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user