add: multi-tenant

This commit is contained in:
2024-06-05 11:16:22 +08:00
parent 2139bbe9ee
commit 6ce288c3ef
4 changed files with 25 additions and 25 deletions

View File

@@ -24,7 +24,7 @@ type UdmSubUser struct {
ContextId string `json:"contextId" xorm:"context_id"`
ApnContext string `json:"apnContext" xorm:"apn_context"`
StaticIp string `json:"staticIp" xorm:"static_ip"`
TenantIDs string `json:"tenantIDs" xorm:"tenant_ids"`
TenantName string `json:"tenantName" xorm:"tenant_name"`
SubNum string `json:"subNum,omitempty" xorm:"-"` // 批量数
}

View File

@@ -15,7 +15,7 @@ import (
var NewRepoUdmSubUser = &RepoUdmSubUser{
selectSql: `select
id, msisdn, imsi, ambr, nssai, rat, arfb, sar, cn, sm_data, smf_sel, eps_dat, ne_id, eps_flag, eps_odb, hplmn_odb, ard, epstpl, context_id, apn_context, static_ip,
tenant_ids
tenant_name
from u_sub_user`,
resultMap: map[string]string{
@@ -40,7 +40,7 @@ var NewRepoUdmSubUser = &RepoUdmSubUser{
"context_id": "ContextId",
"apn_context": "ApnContext",
"static_ip": "StaticIp",
"tenant_ids": "TenantIDs",
"tenant_name": "TenantName",
},
}
@@ -85,8 +85,8 @@ func (r *RepoUdmSubUser) SelectPage(query map[string]any) map[string]any {
params = append(params, v)
}
// for multi-tenancy solution
if v, ok := query["tenantIDs"]; ok && v != "" {
conditions = append(conditions, "tenant_ids like concat(concat('%', ?), '%')")
if v, ok := query["tenantName"]; ok && v != "" {
conditions = append(conditions, "tenant_name = '?'")
params = append(params, v)
}
@@ -98,7 +98,7 @@ func (r *RepoUdmSubUser) SelectPage(query map[string]any) map[string]any {
result := map[string]any{
"total": 0,
"rows": []model.UdmAuthUser{},
"rows": []model.UdmSubUser{},
}
// 查询数量 长度为0直接返回
@@ -165,9 +165,9 @@ func (r *RepoUdmSubUser) SelectList(auth model.UdmSubUser) []model.UdmSubUser {
conditions = append(conditions, "ne_id = ?")
params = append(params, auth.NeID)
}
if auth.TenantIDs != "" {
conditions = append(conditions, "tenant_ids like concat(concat('%', ?), '%')")
params = append(params, auth.TenantIDs)
if auth.TenantName != "" {
conditions = append(conditions, "tenant_name = '?'")
params = append(params, auth.TenantName)
}
// 构建查询条件语句
@@ -208,7 +208,7 @@ func (r *RepoUdmSubUser) ClearAndInsert(neID string, subArr []model.UdmSubUser)
batch := subArr[i:end]
// multi-tenancy
r.SetTenantIDs(batch)
r.SetTenantName(batch)
// 调用 InsertMulti 函数将批量数据插入数据库
results, err := datasource.DefaultDB().Table("u_sub_user").InsertMulti(batch)
@@ -253,7 +253,7 @@ func (r *RepoUdmSubUser) Inserts(subUser []model.UdmSubUser) int64 {
batch := subUser[i:end]
// multi-tenancy
r.SetTenantIDs(batch)
r.SetTenantName(batch)
// 调用 InsertMulti 函数将批量数据插入数据库
results, err := datasource.DefaultDB().Table("u_sub_user").InsertMulti(batch)
@@ -285,7 +285,7 @@ func (r *RepoUdmSubUser) Insert4G(neID string, subUser model.UdmSubUser) int64 {
// multi-tenancy
subUserSlice := []model.UdmSubUser{subUser}
r.SetTenantIDs(subUserSlice)
r.SetTenantName(subUserSlice)
results, err := datasource.DefaultDB().Table("u_sub_user").Insert(subUser)
if err == nil {
@@ -358,8 +358,8 @@ func (r *RepoUdmSubUser) Update(neID string, authUser model.UdmSubUser) int64 {
user.StaticIp = authUser.StaticIp
}
// for multi-tenancy solution
if authUser.TenantIDs != "" && authUser.TenantIDs != user.TenantIDs {
user.TenantIDs = authUser.TenantIDs
if authUser.TenantName != "" && authUser.TenantName != user.TenantName {
user.TenantName = authUser.TenantName
}
results, err := datasource.DefaultDB().Table("u_sub_user").Where("imsi = ? and ne_id = ?", user.Imsi, user.NeID).Update(user)
@@ -474,16 +474,16 @@ func (r *RepoUdmSubUser) Deletes(neID, imsi, num string) int64 {
return results
}
// multi-tenancy solution, get tenant_ids by imsi
func (r *RepoUdmSubUser) SetTenantIDs(subArr []model.UdmSubUser) {
// multi-tenancy solution, get tenant_name by imsi
func (r *RepoUdmSubUser) SetTenantName(subArr []model.UdmSubUser) {
for s := 0; s < len(subArr); s++ {
var tenantIDs []string
var tenantName string
err := datasource.DefaultDB().Table("sys_tenant_map").
Where("mapping_type='UE' and mapping_key like ?", subArr[s].Imsi).Cols("tenant_id").Find(tenantIDs)
Where("mapping_type='udm_sub' and mapping_key='?'", subArr[s].Imsi).Cols("tenant_name").Find(tenantName)
if err != nil {
log.Errorf("Find tenant_ids err => %v", err)
log.Errorf("Find tenant_name err => %v", err)
continue
}
subArr[s].TenantIDs = strings.Join(tenantIDs, ",")
subArr[s].TenantName = tenantName
}
}

View File

@@ -24,7 +24,7 @@ type UDMSub struct {
ContextId string `json:"contextId" gorm:"column:context_id"`
ApnContext string `json:"apnContext" gorm:"column:apn_context"`
StaticIp string `json:"staticIp" gorm:"column:static_ip"`
TenantIDs string `json:"tenantIDs" gorm:"column:tenant_ids"`
TenantName string `json:"tenantName" gorm:"column:tenant_name"`
// ====== 非数据库字段属性 ======

View File

@@ -15,7 +15,7 @@ import (
var NewUDMSubImpl = &UDMSubImpl{
selectSql: `select
id, msisdn, imsi, ambr, nssai, rat, arfb, sar, cn, sm_data, smf_sel, eps_dat, ne_id, eps_flag, eps_odb, hplmn_odb, ard, epstpl, context_id, apn_context, static_ip,
tenant_ids
tenant_name
from u_sub_user`,
resultMap: map[string]string{
@@ -40,7 +40,7 @@ var NewUDMSubImpl = &UDMSubImpl{
"context_id": "ContextId",
"apn_context": "ApnContext",
"static_ip": "StaticIp",
"tenant_ids": "TenantIDs", // Tenant ID for multi-tenancy
"tenant_name": "TenantName", // Tenant ID for multi-tenancy
},
}
@@ -96,8 +96,8 @@ func (r *UDMSubImpl) SelectPage(query map[string]any) map[string]any {
params = append(params, v)
}
// for multi-tenancy solution
if v, ok := query["tenantIDs"]; ok && v != "" {
conditions = append(conditions, "tenant_ids like concat(concat('%', ?), '%')")
if v, ok := query["tenantName"]; ok && v != "" {
conditions = append(conditions, "tenant_name = '?'")
params = append(params, v)
}