fix: multi-tenant phase 2

This commit is contained in:
2024-06-26 10:30:53 +08:00
parent 267904be29
commit f514818e5d
16 changed files with 56 additions and 37 deletions

View File

@@ -4,6 +4,8 @@ import (
"strings"
"time"
dborm "be.ems/lib/core/datasource"
"be.ems/lib/log"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/parse"
@@ -14,9 +16,11 @@ import (
// 实例化数据层 SysLogOperateImpl 结构体
var NewSysLogOperateImpl = &SysLogOperateImpl{
selectSql: `select
oper_id, title, business_type, method, request_method, operator_type, oper_name, dept_name,
oper_url, oper_ip, oper_location, oper_param, oper_msg, status, oper_time, cost_time
from sys_log_operate`,
l.oper_id, l.title, l.business_type, l.method, l.request_method, l.operator_type, l.oper_name, l.dept_name,
l.oper_url, l.oper_ip, l.oper_location, l.oper_param, l.oper_msg, l.status, l.oper_time, l.cost_time,
t.tenant_name
from sys_log_operate l
left join sys_tenant t on t.tenant_id = l.tenant_id`,
resultMap: map[string]string{
"oper_id": "OperID",
@@ -35,6 +39,7 @@ var NewSysLogOperateImpl = &SysLogOperateImpl{
"status": "Status",
"oper_time": "OperTime",
"cost_time": "CostTime",
"tenant_name": "TenantName",
},
}
@@ -100,9 +105,18 @@ func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[st
}
// multi-tenancy, only filter user setting tenant_id
if v, ok := query["operName"]; ok && v != "" {
conditions = append(conditions, "oper_name=?")
params = append(params, v)
if v, ok := query["tenantName"]; ok && v != "" {
var tenantID string
_, err := dborm.DefaultDB().Table("sys_tenant").
Where("tenant_name=?", v).Cols("tenant_id").Distinct().Get(&tenantID)
if err != nil {
log.Errorf("get tenant_id err => %v", err)
}
if tenantID != "" {
conditions = append(conditions, "l.tenant_id = ?")
params = append(params, tenantID)
}
log.Tracef("tenantName=%v, tenantID=%v", v, tenantID)
}
// 构建查询条件语句
@@ -118,7 +132,7 @@ func (r *SysLogOperateImpl) SelectSysLogOperatePage(query map[string]any) map[st
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from sys_log_operate"
totalSql := "select count(1) as 'total' from sys_log_operate l"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
@@ -253,8 +267,8 @@ func (r *SysLogOperateImpl) InsertSysLogOperate(SysLogOperate model.SysLogOperat
if SysLogOperate.CostTime > 0 {
params["cost_time"] = SysLogOperate.CostTime
}
if SysLogOperate.TenantName != "" {
params["tenant_name"] = SysLogOperate.TenantName
if SysLogOperate.TenantID != "" {
params["tenant_id"] = SysLogOperate.TenantID
}
// 构建执行语句