package model import ( "github.com/bwmarrin/snowflake" "gorm.io/gorm" "picgo/configs" "time" ) type BaseModel struct { ID int64 `gorm:"primary_key;unique;not null;comment:'主键'" json:"id"` CreatedAt time.Time `gorm:"not null;type:timestamp;default:CURRENT_TIMESTAMP;comment:'创建时间'"` UpdatedAt time.Time `gorm:"not null;type:timestamp;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;comment:'更新时间'"` DeletedAt gorm.DeletedAt `gorm:"index"` } // BeforeCreate 在创建用户之前为其生成分布式ID func (base *BaseModel) BeforeCreate(tx *gorm.DB) (err error) { var node *snowflake.Node nodeId := configs.Settings.Server.NodeId if node, err = snowflake.NewNode(nodeId); err != nil { return err } base.ID = node.Generate().Int64() return nil }