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