新增雪花算法核心文件
This commit is contained in:
parent
ffa14d1241
commit
da49a8273a
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,14 +22,7 @@ func (m *AdminAPI) TableName() string {
|
|||||||
|
|
||||||
func (m *AdminAPI) BeforeCreate(tx *gorm.DB) error {
|
func (m *AdminAPI) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.APIID == 0 {
|
if m.APIID == 0 {
|
||||||
// 创建雪花算法实例
|
m.APIID = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.APIID = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +21,7 @@ func (m *AdminDept) TableName() string {
|
|||||||
|
|
||||||
func (m *AdminDept) BeforeCreate(tx *gorm.DB) error {
|
func (m *AdminDept) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.DeptId == 0 {
|
if m.DeptId == 0 {
|
||||||
// 创建雪花算法实例
|
m.DeptId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.DeptId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,14 +28,7 @@ func (m *AdminMenu) TableName() string {
|
|||||||
|
|
||||||
func (m *AdminMenu) BeforeCreate(tx *gorm.DB) error {
|
func (m *AdminMenu) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.MenuId == 0 {
|
if m.MenuId == 0 {
|
||||||
// 创建雪花算法实例
|
m.MenuId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.MenuId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,14 +20,7 @@ func (m *AdminPost) TableName() string {
|
|||||||
|
|
||||||
func (m *AdminPost) BeforeCreate(tx *gorm.DB) error {
|
func (m *AdminPost) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.PostId == 0 {
|
if m.PostId == 0 {
|
||||||
// 创建雪花算法实例
|
m.PostId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.PostId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +21,7 @@ func (m *AdminRole) TableName() string {
|
|||||||
|
|
||||||
func (m *AdminRole) BeforeCreate(tx *gorm.DB) error {
|
func (m *AdminRole) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.RoleId == 0 {
|
if m.RoleId == 0 {
|
||||||
// 创建雪花算法实例
|
m.RoleId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.RoleId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -38,14 +37,7 @@ func (m *AdminUser) TableName() string {
|
|||||||
|
|
||||||
func (m *AdminUser) BeforeCreate(tx *gorm.DB) error {
|
func (m *AdminUser) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.UserID == 0 {
|
if m.UserID == 0 {
|
||||||
// 创建雪花算法实例
|
m.UserID = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.UserID = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Area 地区表
|
// Area 地区表
|
||||||
@ -21,14 +20,7 @@ func (m *Area) TableName() string {
|
|||||||
|
|
||||||
func (m *Area) BeforeCreate(tx *gorm.DB) error {
|
func (m *Area) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.AreaId == 0 {
|
if m.AreaId == 0 {
|
||||||
// 创建雪花算法实例
|
m.AreaId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.AreaId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +21,7 @@ func (m *BasicBank) TableName() string {
|
|||||||
|
|
||||||
func (m *BasicBank) BeforeCreate(tx *gorm.DB) error {
|
func (m *BasicBank) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.BankId == 0 {
|
if m.BankId == 0 {
|
||||||
// 创建雪花算法实例
|
m.BankId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.BankId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,14 +20,7 @@ func (m *DiseaseClassExpertise) TableName() string {
|
|||||||
|
|
||||||
func (m *DiseaseClassExpertise) BeforeCreate(tx *gorm.DB) error {
|
func (m *DiseaseClassExpertise) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.ExpertiseId == 0 {
|
if m.ExpertiseId == 0 {
|
||||||
// 创建雪花算法实例
|
m.ExpertiseId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.ExpertiseId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -29,14 +28,7 @@ func (m *DoctorBankCard) TableName() string {
|
|||||||
|
|
||||||
func (m *DoctorBankCard) BeforeCreate(tx *gorm.DB) error {
|
func (m *DoctorBankCard) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.BankCardId == 0 {
|
if m.BankCardId == 0 {
|
||||||
// 创建雪花算法实例
|
m.BankCardId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.BankCardId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,14 +20,7 @@ func (m *DoctorExpertise) TableName() string {
|
|||||||
|
|
||||||
func (m *DoctorExpertise) BeforeCreate(tx *gorm.DB) error {
|
func (m *DoctorExpertise) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.DoctorExpertiseId == 0 {
|
if m.DoctorExpertiseId == 0 {
|
||||||
// 创建雪花算法实例
|
m.DoctorExpertiseId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.DoctorExpertiseId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +21,7 @@ func (m *DoctorIdenFail) TableName() string {
|
|||||||
|
|
||||||
func (m *DoctorIdenFail) BeforeCreate(tx *gorm.DB) error {
|
func (m *DoctorIdenFail) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.IdenFailId == 0 {
|
if m.IdenFailId == 0 {
|
||||||
// 创建雪花算法实例
|
m.IdenFailId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.IdenFailId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,14 +33,7 @@ func (m *Hospital) TableName() string {
|
|||||||
|
|
||||||
func (m *Hospital) BeforeCreate(tx *gorm.DB) error {
|
func (m *Hospital) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.HospitalID == 0 {
|
if m.HospitalID == 0 {
|
||||||
// 创建雪花算法实例
|
m.HospitalID = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.HospitalID = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -22,14 +21,7 @@ func (m *HospitalDepartment) TableName() string {
|
|||||||
|
|
||||||
func (m *HospitalDepartment) BeforeCreate(tx *gorm.DB) error {
|
func (m *HospitalDepartment) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.DepartmentId == 0 {
|
if m.DepartmentId == 0 {
|
||||||
// 创建雪花算法实例
|
m.DepartmentId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.DepartmentId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,14 +23,7 @@ func (m *HospitalDepartmentCustom) TableName() string {
|
|||||||
|
|
||||||
func (m *HospitalDepartmentCustom) BeforeCreate(tx *gorm.DB) error {
|
func (m *HospitalDepartmentCustom) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.DepartmentCustomId == 0 {
|
if m.DepartmentCustomId == 0 {
|
||||||
// 创建雪花算法实例
|
m.DepartmentCustomId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.DepartmentCustomId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -42,14 +41,7 @@ func (m *OrderPrescription) TableName() string {
|
|||||||
|
|
||||||
func (m *OrderPrescription) BeforeCreate(tx *gorm.DB) error {
|
func (m *OrderPrescription) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.OrderPrescriptionId == 0 {
|
if m.OrderPrescriptionId == 0 {
|
||||||
// 创建雪花算法实例
|
m.OrderPrescriptionId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.OrderPrescriptionId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,14 +44,7 @@ func (m *PatientFamily) TableName() string {
|
|||||||
|
|
||||||
func (m *PatientFamily) BeforeCreate(tx *gorm.DB) error {
|
func (m *PatientFamily) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.FamilyId == 0 {
|
if m.FamilyId == 0 {
|
||||||
// 创建雪花算法实例
|
m.FamilyId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.FamilyId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,14 +33,7 @@ func (m *User) TableName() string {
|
|||||||
|
|
||||||
func (m *User) BeforeCreate(tx *gorm.DB) error {
|
func (m *User) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.UserId == 0 {
|
if m.UserId == 0 {
|
||||||
// 创建雪花算法实例
|
m.UserId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.UserId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -28,14 +27,7 @@ func (m *UserCaCert) TableName() string {
|
|||||||
|
|
||||||
func (m *UserCaCert) BeforeCreate(tx *gorm.DB) error {
|
func (m *UserCaCert) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.CertId == 0 {
|
if m.CertId == 0 {
|
||||||
// 创建雪花算法实例
|
m.CertId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.CertId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -57,14 +56,7 @@ func (m *UserDoctor) TableName() string {
|
|||||||
|
|
||||||
func (m *UserDoctor) BeforeCreate(tx *gorm.DB) error {
|
func (m *UserDoctor) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.DoctorId == 0 {
|
if m.DoctorId == 0 {
|
||||||
// 创建雪花算法实例
|
m.DoctorId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.DoctorId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/bwmarrin/snowflake"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/config"
|
"hospital-admin-api/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,14 +33,7 @@ func (m *UserDoctorInfo) TableName() string {
|
|||||||
|
|
||||||
func (m *UserDoctorInfo) BeforeCreate(tx *gorm.DB) error {
|
func (m *UserDoctorInfo) BeforeCreate(tx *gorm.DB) error {
|
||||||
if m.DoctorInfoId == 0 {
|
if m.DoctorInfoId == 0 {
|
||||||
// 创建雪花算法实例
|
m.DoctorInfoId = global.Snowflake.Generate().Int64()
|
||||||
sf, err := snowflake.NewNode(config.C.Snowflake)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 生成新的雪花算法 ID
|
|
||||||
m.DoctorInfoId = sf.Generate().Int64()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m.CreatedAt = LocalTime(time.Now())
|
m.CreatedAt = LocalTime(time.Now())
|
||||||
|
|||||||
21
core/snowflake.go
Normal file
21
core/snowflake.go
Normal file
@ -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成功......")
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
package global
|
package global
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/bwmarrin/snowflake"
|
||||||
ut "github.com/go-playground/universal-translator"
|
ut "github.com/go-playground/universal-translator"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
@ -10,9 +11,10 @@ import (
|
|||||||
|
|
||||||
// 全局变量
|
// 全局变量
|
||||||
var (
|
var (
|
||||||
Db *gorm.DB // 数据库
|
Db *gorm.DB // 数据库
|
||||||
Logger *logrus.Logger // 日志
|
Logger *logrus.Logger // 日志
|
||||||
Redis *redis.Client // redis
|
Redis *redis.Client // redis
|
||||||
Validate *validator.Validate // 验证器
|
Validate *validator.Validate // 验证器
|
||||||
Trans ut.Translator // Validate/v10 全局验证器
|
Trans ut.Translator // Validate/v10 全局验证器
|
||||||
|
Snowflake *snowflake.Node // 雪花算法
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user