Files
yunzerwebsiteallinone/go/services/operation_log_user.go
T
2026-11-09 11:42:23 +08:00

70 lines
1.9 KiB
Go

package services
import "server/models"
// OperationLogUser 根据操作日志的租户范围解析操作人信息。
// tid 为空或为 0 时,日志来自平台管理员;否则来自租户用户。
func OperationLogUser(tid *uint64, userID uint64) (account, name string) {
if userID == 0 {
return "", ""
}
if tid != nil && *tid != 0 {
var user models.SystemTenantUser
// userID 是令牌里的 identity_id,查老表前先换算成老 uid
if err := models.Orm.QueryTable(new(models.SystemTenantUser)).
Filter("tid", *tid).
Filter("uid", ResolveTenantUID(*tid, userID)).
One(&user); err == nil {
if user.Account != nil {
account = *user.Account
}
if user.Name != nil {
name = *user.Name
}
}
return account, name
}
var user models.AdminUser
if err := models.Orm.QueryTable(new(models.AdminUser)).
Filter("id", userID).
One(&user); err == nil {
account = user.Account
if user.Name != nil {
name = *user.Name
}
}
return account, name
}
type operationLogUserKey struct {
tid uint64
uid uint64
}
// OperationLogUserResolver 请求级缓存的操作人解析器。
// 日志列表逐行调用 OperationLogUser 时,同一 (tid,userID) 会重复执行
// FindLegacyUID + 用户表查询(N+1),用它按键去重后每个用户只查一次。
type OperationLogUserResolver struct {
cache map[operationLogUserKey][2]string
}
func NewOperationLogUserResolver() *OperationLogUserResolver {
return &OperationLogUserResolver{cache: make(map[operationLogUserKey][2]string)}
}
func (r *OperationLogUserResolver) Resolve(tid *uint64, userID uint64) (account, name string) {
var tidVal uint64
if tid != nil {
tidVal = *tid
}
key := operationLogUserKey{tid: tidVal, uid: userID}
if v, ok := r.cache[key]; ok {
return v[0], v[1]
}
account, name = OperationLogUser(tid, userID)
r.cache[key] = [2]string{account, name}
return account, name
}