zhangqian
2023-12-08 5220cfff6d68f24875c5ce832bbe4541b9fe6639
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package model
 
import (
    "apsClient/pkg/snowflake"
    "github.com/jinzhu/gorm"
    "time"
)
 
type CommonModel struct {
    ID        uint `gorm:"primary_key"`
    CreatedAt time.Time
    UpdatedAt time.Time
    DeletedAt *time.Time `sql:"index"`
}
 
func (c *CommonModel) BeforeCreate(db *gorm.DB) {
    if c.ID == 0 {
        id := snowflake.GenerateID()
        if id < 0 {
            id = snowflake.GenerateID()
        }
        c.ID = uint(id)
    }
}