新增数据库主键处理

This commit is contained in:
2023-07-14 17:36:14 +08:00
parent 593ea49554
commit c9efce7bb6
28 changed files with 730 additions and 52 deletions
+20
View File
@@ -1,5 +1,11 @@
package model
import (
"github.com/bwmarrin/snowflake"
"gorm.io/gorm"
"hospital-admin-api/config"
)
// UserCaCert 医师/药师ca监管证书表
type UserCaCert struct {
CertId int64 `gorm:"column:cert_id;type:bigint(19);primary_key;comment:主键id" json:"cert_id"`
@@ -18,3 +24,17 @@ type UserCaCert struct {
func (m *UserCaCert) TableName() string {
return "gdxz_user_ca_cert"
}
func (m *UserCaCert) BeforeCreate(tx *gorm.DB) error {
if m.CertId == 0 {
// 创建雪花算法实例
sf, err := snowflake.NewNode(config.C.Snowflake)
if err != nil {
return err
}
// 生成新的雪花算法 ID
m.CertId = sf.Generate().Int64()
}
return nil
}