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