add gorm update create hook

This commit is contained in:
landaiqing
2024-09-10 22:45:03 +08:00
parent e87a511ece
commit 5c30db51f1
7 changed files with 154 additions and 3 deletions

View File

@@ -1,6 +1,9 @@
package model
import (
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"time"
)
@@ -25,7 +28,7 @@ type ScaAuthUserDevice struct {
Platform *string `gorm:"column:platform;type:varchar(20);comment:平台" json:"platform"` // 平台
EngineName *string `gorm:"column:engine_name;type:varchar(20);comment:引擎名称" json:"engine_name"` // 引擎名称
EngineVersion *string `gorm:"column:engine_version;type:varchar(20);comment:引擎版本" json:"engine_version"` // 引擎版本
CreatedBy *string `gorm:"column:created_by;type:varchar(32);comment:创建人" json:"created_by"` // 创建人
CreatedBy *string `gorm:"column:created_by;type:varchar(32);default:system;comment:创建人" json:"created_by"` // 创建人
UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"`
}
@@ -33,3 +36,19 @@ type ScaAuthUserDevice struct {
func (*ScaAuthUserDevice) TableName() string {
return TableNameScaAuthUserDevice
}
func (device *ScaAuthUserDevice) BeforeUpdate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
global.LOG.Error("user_id is not found in global.DB")
return fmt.Errorf("user_id is not found in global.DB")
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
device.UpdateBy = userIdStr
return nil
}