picgo/model/base.go

27 lines
816 B
Go
Raw Permalink Normal View History

2024-07-12 20:32:33 +08:00
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
}