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"
)
@@ -16,7 +19,7 @@ type ScaAuthUserSocial struct {
CreatedTime *time.Time `gorm:"column:created_time;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_time"` // 创建时间
UpdateTime *time.Time `gorm:"column:update_time;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间" json:"update_time"` // 更新时间
Deleted *int64 `gorm:"column:deleted;type:int(11);default:0;comment:是否删除" json:"deleted"` // 是否删除
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"`
}
@@ -24,3 +27,18 @@ type ScaAuthUserSocial struct {
func (*ScaAuthUserSocial) TableName() string {
return TableNameScaAuthUserSocial
}
func (social *ScaAuthUserSocial) 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")
}
social.UpdateBy = userIdStr
return nil
}