init: init refactor
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"sundynix-micro-go/common/utils/uniqueid"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// BaseModel 基础模型,所有表的公共字段
|
||||
type BaseModel struct {
|
||||
ID string `gorm:"size:50;primaryKey" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updatedAt" gorm:"autoCreateTime;autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
// BeforeCreate 创建前自动生成雪花ID
|
||||
func (m *BaseModel) BeforeCreate(db *gorm.DB) (err error) {
|
||||
db.Statement.SetColumn("id", uniqueid.GenerateID())
|
||||
return
|
||||
}
|
||||
|
||||
// BeforeUpdate 更新前自动更新时间
|
||||
func (m *BaseModel) BeforeUpdate(db *gorm.DB) (err error) {
|
||||
db.Statement.SetColumn("updated_at", time.Now())
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user