36 lines
613 B
Go
36 lines
613 B
Go
package common
|
|
|
|
import (
|
|
"server/config"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CurrentDB 返回带请求上下文的 DB 实例
|
|
func CurrentDB() *gorm.DB {
|
|
if config.DB == nil {
|
|
return nil
|
|
}
|
|
ctx := GetRequestContext()
|
|
if ctx == nil {
|
|
return config.DB
|
|
}
|
|
return config.DB.WithContext(ctx)
|
|
}
|
|
|
|
// WithTenantID 临时覆盖租户ID
|
|
func WithTenantID(db *gorm.DB, tenantID string) *gorm.DB {
|
|
if db == nil {
|
|
return db
|
|
}
|
|
return db.Set(DBSettingTenantID, tenantID)
|
|
}
|
|
|
|
// SkipTenant 临时跳过租户过滤
|
|
func SkipTenant(db *gorm.DB) *gorm.DB {
|
|
if db == nil {
|
|
return db
|
|
}
|
|
return db.Set(DBSettingTenantSkip, true)
|
|
}
|