From da49a8273ae7587167fb2e14c0c066b64d486855 Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Mon, 17 Jul 2023 16:13:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=9B=AA=E8=8A=B1=E7=AE=97?= =?UTF-8?q?=E6=B3=95=E6=A0=B8=E5=BF=83=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/model/adminApi.go | 12 ++---------- api/model/adminDept.go | 12 ++---------- api/model/adminMenu.go | 12 ++---------- api/model/adminPost.go | 12 ++---------- api/model/adminRole.go | 12 ++---------- api/model/adminUser.go | 12 ++---------- api/model/area.go | 12 ++---------- api/model/basicBank.go | 12 ++---------- api/model/diseaseClassExpertise.go | 12 ++---------- api/model/doctorBankCard.go | 12 ++---------- api/model/doctorExpertise.go | 12 ++---------- api/model/doctorIdenFail.go | 12 ++---------- api/model/hospital.go | 12 ++---------- api/model/hospitalDepartment.go | 12 ++---------- api/model/hospitalDepartmentCustom.go | 12 ++---------- api/model/orderPrescription.go | 12 ++---------- api/model/patientFamily.go | 12 ++---------- api/model/user.go | 12 ++---------- api/model/userCaCert.go | 12 ++---------- api/model/userDoctor.go | 12 ++---------- api/model/userDoctorInfo.go | 12 ++---------- core/snowflake.go | 21 +++++++++++++++++++++ global/global.go | 12 +++++++----- main.go | 3 +++ 24 files changed, 73 insertions(+), 215 deletions(-) create mode 100644 core/snowflake.go diff --git a/api/model/adminApi.go b/api/model/adminApi.go index 39436a7..43fb903 100644 --- a/api/model/adminApi.go +++ b/api/model/adminApi.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -23,14 +22,7 @@ func (m *AdminAPI) TableName() string { func (m *AdminAPI) BeforeCreate(tx *gorm.DB) error { if m.APIID == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.APIID = sf.Generate().Int64() + m.APIID = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/adminDept.go b/api/model/adminDept.go index 9c26b56..0d3abfa 100644 --- a/api/model/adminDept.go +++ b/api/model/adminDept.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -22,14 +21,7 @@ func (m *AdminDept) TableName() string { func (m *AdminDept) BeforeCreate(tx *gorm.DB) error { if m.DeptId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.DeptId = sf.Generate().Int64() + m.DeptId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/adminMenu.go b/api/model/adminMenu.go index 1d1425d..d8ec808 100644 --- a/api/model/adminMenu.go +++ b/api/model/adminMenu.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -29,14 +28,7 @@ func (m *AdminMenu) TableName() string { func (m *AdminMenu) BeforeCreate(tx *gorm.DB) error { if m.MenuId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.MenuId = sf.Generate().Int64() + m.MenuId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/adminPost.go b/api/model/adminPost.go index 4d770aa..02a0ff7 100644 --- a/api/model/adminPost.go +++ b/api/model/adminPost.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -21,14 +20,7 @@ func (m *AdminPost) TableName() string { 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() + m.PostId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/adminRole.go b/api/model/adminRole.go index d9cb11c..2303c20 100644 --- a/api/model/adminRole.go +++ b/api/model/adminRole.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -22,14 +21,7 @@ func (m *AdminRole) TableName() string { func (m *AdminRole) BeforeCreate(tx *gorm.DB) error { if m.RoleId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.RoleId = sf.Generate().Int64() + m.RoleId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/adminUser.go b/api/model/adminUser.go index 146ecae..b116e20 100644 --- a/api/model/adminUser.go +++ b/api/model/adminUser.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -38,14 +37,7 @@ func (m *AdminUser) TableName() string { func (m *AdminUser) BeforeCreate(tx *gorm.DB) error { if m.UserID == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.UserID = sf.Generate().Int64() + m.UserID = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/area.go b/api/model/area.go index 06cb79c..9475fec 100644 --- a/api/model/area.go +++ b/api/model/area.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" ) // Area 地区表 @@ -21,14 +20,7 @@ func (m *Area) TableName() string { func (m *Area) BeforeCreate(tx *gorm.DB) error { if m.AreaId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.AreaId = sf.Generate().Int64() + m.AreaId = global.Snowflake.Generate().Int64() } return nil } diff --git a/api/model/basicBank.go b/api/model/basicBank.go index f9b59ad..ec0d7ec 100644 --- a/api/model/basicBank.go +++ b/api/model/basicBank.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -22,14 +21,7 @@ func (m *BasicBank) TableName() string { 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() + m.BankId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/diseaseClassExpertise.go b/api/model/diseaseClassExpertise.go index 9374a72..8dd7534 100644 --- a/api/model/diseaseClassExpertise.go +++ b/api/model/diseaseClassExpertise.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -21,14 +20,7 @@ func (m *DiseaseClassExpertise) TableName() string { func (m *DiseaseClassExpertise) BeforeCreate(tx *gorm.DB) error { if m.ExpertiseId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.ExpertiseId = sf.Generate().Int64() + m.ExpertiseId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/doctorBankCard.go b/api/model/doctorBankCard.go index 099916c..350f957 100644 --- a/api/model/doctorBankCard.go +++ b/api/model/doctorBankCard.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -29,14 +28,7 @@ func (m *DoctorBankCard) TableName() string { func (m *DoctorBankCard) BeforeCreate(tx *gorm.DB) error { if m.BankCardId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.BankCardId = sf.Generate().Int64() + m.BankCardId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/doctorExpertise.go b/api/model/doctorExpertise.go index 4db52e2..8b6ce66 100644 --- a/api/model/doctorExpertise.go +++ b/api/model/doctorExpertise.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -21,14 +20,7 @@ func (m *DoctorExpertise) TableName() string { func (m *DoctorExpertise) BeforeCreate(tx *gorm.DB) error { if m.DoctorExpertiseId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.DoctorExpertiseId = sf.Generate().Int64() + m.DoctorExpertiseId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/doctorIdenFail.go b/api/model/doctorIdenFail.go index 6024168..b86eb91 100644 --- a/api/model/doctorIdenFail.go +++ b/api/model/doctorIdenFail.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -22,14 +21,7 @@ func (m *DoctorIdenFail) TableName() string { func (m *DoctorIdenFail) BeforeCreate(tx *gorm.DB) error { if m.IdenFailId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.IdenFailId = sf.Generate().Int64() + m.IdenFailId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/hospital.go b/api/model/hospital.go index b77c0d9..d0d5285 100644 --- a/api/model/hospital.go +++ b/api/model/hospital.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -34,14 +33,7 @@ func (m *Hospital) TableName() string { func (m *Hospital) BeforeCreate(tx *gorm.DB) error { if m.HospitalID == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.HospitalID = sf.Generate().Int64() + m.HospitalID = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/hospitalDepartment.go b/api/model/hospitalDepartment.go index d3469c6..3444743 100644 --- a/api/model/hospitalDepartment.go +++ b/api/model/hospitalDepartment.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -22,14 +21,7 @@ func (m *HospitalDepartment) TableName() string { func (m *HospitalDepartment) BeforeCreate(tx *gorm.DB) error { if m.DepartmentId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.DepartmentId = sf.Generate().Int64() + m.DepartmentId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/hospitalDepartmentCustom.go b/api/model/hospitalDepartmentCustom.go index 3ab7528..c70077c 100644 --- a/api/model/hospitalDepartmentCustom.go +++ b/api/model/hospitalDepartmentCustom.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -24,14 +23,7 @@ func (m *HospitalDepartmentCustom) TableName() string { 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() + m.DepartmentCustomId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/orderPrescription.go b/api/model/orderPrescription.go index c6684b8..7aae159 100644 --- a/api/model/orderPrescription.go +++ b/api/model/orderPrescription.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -42,14 +41,7 @@ func (m *OrderPrescription) TableName() string { func (m *OrderPrescription) BeforeCreate(tx *gorm.DB) error { if m.OrderPrescriptionId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.OrderPrescriptionId = sf.Generate().Int64() + m.OrderPrescriptionId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/patientFamily.go b/api/model/patientFamily.go index a0e92d4..836c02a 100644 --- a/api/model/patientFamily.go +++ b/api/model/patientFamily.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -45,14 +44,7 @@ func (m *PatientFamily) TableName() string { func (m *PatientFamily) BeforeCreate(tx *gorm.DB) error { if m.FamilyId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.FamilyId = sf.Generate().Int64() + m.FamilyId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/user.go b/api/model/user.go index 01a65be..74f7c86 100644 --- a/api/model/user.go +++ b/api/model/user.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -34,14 +33,7 @@ func (m *User) TableName() string { func (m *User) BeforeCreate(tx *gorm.DB) error { if m.UserId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.UserId = sf.Generate().Int64() + m.UserId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/userCaCert.go b/api/model/userCaCert.go index bbc5298..54e7b9c 100644 --- a/api/model/userCaCert.go +++ b/api/model/userCaCert.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -28,14 +27,7 @@ func (m *UserCaCert) TableName() string { 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() + m.CertId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/userDoctor.go b/api/model/userDoctor.go index 0ec7a0c..ac2cbc9 100644 --- a/api/model/userDoctor.go +++ b/api/model/userDoctor.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -57,14 +56,7 @@ func (m *UserDoctor) TableName() string { func (m *UserDoctor) BeforeCreate(tx *gorm.DB) error { if m.DoctorId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.DoctorId = sf.Generate().Int64() + m.DoctorId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/api/model/userDoctorInfo.go b/api/model/userDoctorInfo.go index 05488d1..a676dc8 100644 --- a/api/model/userDoctorInfo.go +++ b/api/model/userDoctorInfo.go @@ -1,9 +1,8 @@ package model import ( - "github.com/bwmarrin/snowflake" "gorm.io/gorm" - "hospital-admin-api/config" + "hospital-admin-api/global" "time" ) @@ -34,14 +33,7 @@ func (m *UserDoctorInfo) TableName() string { func (m *UserDoctorInfo) BeforeCreate(tx *gorm.DB) error { if m.DoctorInfoId == 0 { - // 创建雪花算法实例 - sf, err := snowflake.NewNode(config.C.Snowflake) - if err != nil { - return err - } - - // 生成新的雪花算法 ID - m.DoctorInfoId = sf.Generate().Int64() + m.DoctorInfoId = global.Snowflake.Generate().Int64() } m.CreatedAt = LocalTime(time.Now()) diff --git a/core/snowflake.go b/core/snowflake.go new file mode 100644 index 0000000..82fd89e --- /dev/null +++ b/core/snowflake.go @@ -0,0 +1,21 @@ +package core + +import ( + "fmt" + "github.com/bwmarrin/snowflake" + "hospital-admin-api/config" + "hospital-admin-api/global" +) + +// Snowflake 雪花算法 +func Snowflake() { + // 创建雪花算法实例 + node, err := snowflake.NewNode(config.C.Snowflake) + if err != nil { + panic("snowflake初始化失败! " + err.Error()) + } + + global.Snowflake = node + + fmt.Println("初始化snowflake成功......") +} diff --git a/global/global.go b/global/global.go index b15591e..662c098 100644 --- a/global/global.go +++ b/global/global.go @@ -1,6 +1,7 @@ package global import ( + "github.com/bwmarrin/snowflake" ut "github.com/go-playground/universal-translator" "github.com/go-playground/validator/v10" "github.com/go-redis/redis/v8" @@ -10,9 +11,10 @@ import ( // 全局变量 var ( - Db *gorm.DB // 数据库 - Logger *logrus.Logger // 日志 - Redis *redis.Client // redis - Validate *validator.Validate // 验证器 - Trans ut.Translator // Validate/v10 全局验证器 + Db *gorm.DB // 数据库 + Logger *logrus.Logger // 日志 + Redis *redis.Client // redis + Validate *validator.Validate // 验证器 + Trans ut.Translator // Validate/v10 全局验证器 + Snowflake *snowflake.Node // 雪花算法 ) diff --git a/main.go b/main.go index 3d5c251..6109af2 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,9 @@ func main() { // 加载验证器 core.Validator() + // 加载雪花算法 + core.Snowflake() + // 初始化路由-加载中间件 r := router.Init()