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

@@ -33,7 +33,7 @@ CREATE TABLE `kpi_report` (
`granularity` tinyint(4) NULL DEFAULT 60 COMMENT 'Time granualarity: 5/10/.../60/300 (second)',
`kpi_values` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
`created_at` bigint(20) NULL DEFAULT NULL,
`tenant_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`tenant_id` varchar(32) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_timestamp`(`created_at`) USING BTREE,
INDEX `idx_uid_datetime`(`rm_uid`, `date`, `start_time`) USING BTREE

View File

@@ -38,7 +38,7 @@ CREATE TABLE `sys_log_operate` (
`status` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '0' COMMENT '操作状态0异常 1正常',
`oper_time` bigint(20) NULL DEFAULT 0 COMMENT '操作时间',
`cost_time` bigint(20) NULL DEFAULT 0 COMMENT '消耗时间(毫秒)',
`tenant_name` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`tenant_id` varchar(32) NULL DEFAULT NULL COMMENT 'Tenant ID refer to sys_tenant',
PRIMARY KEY (`oper_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 30 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '系统操作日志表' ROW_FORMAT = Dynamic;

View File

@@ -284,9 +284,9 @@ INSERT INTO `sys_role_menu` VALUES (5, 2082);
INSERT INTO `sys_role_menu` VALUES (5, 2087);
INSERT INTO `sys_role_menu` VALUES (5, 2131);
INSERT INTO `sys_role_menu` VALUES (5, 2132);
INSERT INTO `sys_role_menu` VALUES (100, 2153);
INSERT INTO `sys_role_menu` VALUES (100, 2154);
INSERT INTO `sys_role_menu` VALUES (100, 2155);
INSERT INTO `sys_role_menu` VALUES (100, 2156);
INSERT INTO `sys_role_menu` VALUES (100, 2157);
INSERT INTO `sys_role_menu` VALUES (100, 2158);
INSERT INTO `sys_role_menu` VALUES (100, 2159);
INSERT INTO `sys_role_menu` VALUES (100, 2160);

View File

@@ -41,6 +41,6 @@ CREATE TABLE `sys_tenant` (
-- ----------------------------
-- Records of sys_tenant
-- ----------------------------
INSERT INTO `sys_tenant` VALUES (1, 0, '0', 'TenantRoot', 0, '', '', '1', '0', 'admin', 1699348237468, '', 1718783333041);
-- INSERT INTO `sys_tenant` VALUES (1, 0, '0', 'TenantRoot', 0, '', '', '1', '0', 'admin', 1699348237468, '', 1718783333041);
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -37,8 +37,7 @@ CREATE TABLE IF NOT EXISTS `kpi_report` (
INDEX `idx_uid_datetime`(`rm_uid`, `date`, `start_time`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 10660 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
ALTER TABLE `kpi_report`
ADD COLUMN IF NOT EXISTS `tenant_name` varchar(128) NULL AFTER `created_at`;
ALTER TABLE `kpi_report` ADD COLUMN IF NOT EXISTS `tenant_id` varchar(32) NULL DEFAULT NULL AFTER `created_at`;
ALTER TABLE `kpi_report`
ADD INDEX IF NOT EXISTS `idx_timestamp`(`created_at`) USING BTREE,

View File

@@ -30,5 +30,6 @@ CREATE TABLE IF NOT EXISTS `sys_log_operate` (
PRIMARY KEY (`oper_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='系统操作日志表';
ALTER TABLE `sys_log_operate` ADD COLUMN IF NOT EXISTS `tenant_name` varchar(128) NULL AFTER `cost_time`;
ALTER TABLE `sys_log_operate` ADD COLUMN IF NOT EXISTS `tenant_id` varchar(32) NULL DEFAULT NULL AFTER `cost_time`;

View File

@@ -37,12 +37,9 @@ CREATE TABLE IF NOT EXISTS `sys_tenant` (
PRIMARY KEY (`tenant_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '部门表' ROW_FORMAT = Dynamic;
-- alert table
ALTER TABLE `tenants_db`.`sys_tenant` DROP INDEX IF EXISTS `uni_idx_tenant_name`;
-- ----------------------------
-- Records of sys_tenant
-- ----------------------------
INSERT IGNORE INTO `sys_tenant` VALUES (1, 0, '0', 'TenantRoot', 0, '', '', '1', '0', 'admin', 1699348237468, 'supervisor', 1718783333041);
-- INSERT IGNORE INTO `sys_tenant` VALUES (1, 0, '0', 'TenantRoot', 0, '', '', '1', '0', 'admin', 1699348237468, 'supervisor', 1718783333041);
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -39,6 +39,6 @@ ALTER TABLE `sys_user` MODIFY COLUMN `phonenumber` varchar(16) CHARACTER SET utf
ALTER TABLE `sys_user` MODIFY COLUMN `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '备注' AFTER `update_time`;
-- multi-tenancy
ALTER TABLE `tenants_db`.`sys_user` ADD COLUMN IF NOT EXISTS `tenant_id` bigint(20) NULL DEFAULT NULL COMMENT 'Tenant ID' AFTER `dept_id`;
ALTER TABLE `sys_user` ADD COLUMN IF NOT EXISTS `tenant_id` bigint(20) NULL DEFAULT NULL DEFAULT NULL COMMENT 'Tenant ID' AFTER `dept_id`;
SET FOREIGN_KEY_CHECKS=1;

View File

@@ -76,8 +76,8 @@ type KpiData struct {
Granularity int8 `json:"granularity" xorm:"granularity"`
KPIValues []KPIVal `json:"kpiValues" xorm:"json 'kpi_values'"`
//CreatedAt int64 `json:"createdAt" xorm:"created 'created_at'"`
CreatedAt int64 `json:"createdAt" xorm:"'created_at'"`
TenantName string `json:"tenantName" xorm:"tenant_name"`
CreatedAt int64 `json:"createdAt" xorm:"'created_at'"`
TenantID string `json:"tenantId" xorm:"tenant_id"`
}
type KPIVal struct {
KPIID string `json:"kpi_id" xorm:"kpi_id"`
@@ -259,8 +259,8 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
}
// set tenant_name if exist
where := fmt.Sprintf("status='1' and tenant_id=(select parent_id from sys_tenant where status='1' and tenancy_type='UPF' and tenancy_key='%s')", kpiData.RmUid)
kpiData.TenantName, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "tenant_name", where)
where := fmt.Sprintf("status='1' and tenancy_type='UPF' and tenancy_key='%s'", kpiData.RmUid)
kpiData.TenantID, _ = dborm.XormGetSingleColStringByWhere("sys_tenant", "parent_id", where)
// insert kpi_report table, no session
tableName := "kpi_report_" + strings.ToLower(kpiReport.Task.NE.NeType)
affected, err := xEngine.Table(tableName).Insert(kpiData)

View File

@@ -131,7 +131,7 @@ for ne_type in ${ne_types}; do
if [ $? = 0 ]; then
echo "done"
fi
SQL="ALTER TABLE ${TABLE_NAME} ADD COLUMN IF NOT EXISTS `tenant_name` varchar(128) NULL AFTER `created_at`;"
SQL="ALTER TABLE ${TABLE_NAME} ADD COLUMN IF NOT EXISTS `tenant_id` varchar(32) NULL DEFAULT NULL AFTER `created_at`;"
echo -n "Alter table ${TABLE_NAME} ..."
mysql -u${USER} -p${PASSWORD} -P ${PORT} --protocol tcp -D ${DBNAME} -e "${SQL}"
if [ $? = 0 ]; then

View File

@@ -121,6 +121,7 @@ func OperateLog(options Options) gin.HandlerFunc {
OperName: loginUser.User.UserName,
DeptName: loginUser.User.Dept.DeptName,
TenantName: loginUser.User.Tenant.TenantName,
TenantID: loginUser.User.Tenant.TenantID,
}
if loginUser.User.UserType == "sys" {

View File

@@ -114,17 +114,21 @@ func (r *UDMSubImpl) SelectPage(query map[string]any) map[string]any {
params = append(params, tenantID[0])
}
} else if v, ok := query["userName"]; ok && v != "" {
var tenantID []string
err := dborm.DefaultDB().Table("sys_user").
Where("user_name=?", v).Cols("tenant_id").Distinct().Find(&tenantID)
var tenantID string
_, err := dborm.DefaultDB().Table("sys_user").
Where("user_name=?", v).Cols("tenant_id").Distinct().Get(&tenantID)
if err != nil {
log.Errorf("Find tenant_id from sys_user err => %v", err)
}
log.Tracef("userName=%v, tenantID=%v", v, tenantID)
if len(tenantID) > 0 {
if tenantID != "" {
conditions = append(conditions, "s.tenant_id = ?")
params = append(params, tenantID[0])
params = append(params, tenantID)
}
// if len(tenantID) > 0 {
// conditions = append(conditions, "s.tenant_id = ?")
// params = append(params, tenantID[0])
// }
}
// 构建查询条件语句

View File

@@ -132,7 +132,7 @@ func (s *SysTenantController) Add(c *gin.Context) {
}
// 检查租户名称唯一
if body.ParentID == "1" {
if body.ParentID == "0" {
uniqueTenantName := s.sysTenantService.CheckUniqueTenantName(body.TenantName, body.ParentID, "")
if !uniqueTenantName {
// 租户新增【%s】失败租户名称已存在
@@ -197,7 +197,7 @@ func (s *SysTenantController) Edit(c *gin.Context) {
}
// 检查租户名称唯一
if body.ParentID == "1" {
if body.ParentID == "0" {
uniqueTenantName := s.sysTenantService.CheckUniqueTenantName(body.TenantName, body.ParentID, body.TenantID)
if !uniqueTenantName {
// 租户修改【%s】失败租户名称已存在

View File

@@ -34,6 +34,7 @@ type SysLogOperate struct {
OperTime int64 `json:"operTime"`
// 消耗时间(毫秒)
CostTime int64 `json:"costTime"`
// Tenant name refer to tenant_name of sys_tenant
// TenantName and TenantID refer to sys_tenant
TenantName string `json:"tenantName"`
TenantID string `json:"tenantId"`
}

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
}
// 构建执行语句

View File

@@ -236,6 +236,7 @@ func (r *SysTenantImpl) IsUniqueTenancy(sysTenant model.SysTenant) bool {
// 查询条件拼接
var conditions []string
var params []any
if sysTenant.TenantID != "" {
conditions = append(conditions, "tenant_id != ?")
params = append(params, sysTenant.TenantID)
@@ -245,8 +246,9 @@ func (r *SysTenantImpl) IsUniqueTenancy(sysTenant model.SysTenant) bool {
params = append(params, sysTenant.TenancyType)
}
if sysTenant.TenancyKey != "" {
conditions = append(conditions, "? like tenancy_key")
params = append(params, sysTenant.TenancyKey)
conditions = append(conditions, "(? like tenancy_key or tenancy_key like ?)")
//conditions = append(conditions, "(LOCATE(?, tenancy_key)>0 or LOCATE(tenancy_key, ?)>0)")
params = append(params, sysTenant.TenancyKey, sysTenant.TenancyKey)
}
// 构建查询条件语句