add: multi-tenant
This commit is contained in:
@@ -24,7 +24,7 @@ type UdmSubUser struct {
|
|||||||
ContextId string `json:"contextId" xorm:"context_id"`
|
ContextId string `json:"contextId" xorm:"context_id"`
|
||||||
ApnContext string `json:"apnContext" xorm:"apn_context"`
|
ApnContext string `json:"apnContext" xorm:"apn_context"`
|
||||||
StaticIp string `json:"staticIp" xorm:"static_ip"`
|
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:"-"` // 批量数
|
SubNum string `json:"subNum,omitempty" xorm:"-"` // 批量数
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
var NewRepoUdmSubUser = &RepoUdmSubUser{
|
var NewRepoUdmSubUser = &RepoUdmSubUser{
|
||||||
selectSql: `select
|
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,
|
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`,
|
from u_sub_user`,
|
||||||
|
|
||||||
resultMap: map[string]string{
|
resultMap: map[string]string{
|
||||||
@@ -40,7 +40,7 @@ var NewRepoUdmSubUser = &RepoUdmSubUser{
|
|||||||
"context_id": "ContextId",
|
"context_id": "ContextId",
|
||||||
"apn_context": "ApnContext",
|
"apn_context": "ApnContext",
|
||||||
"static_ip": "StaticIp",
|
"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)
|
params = append(params, v)
|
||||||
}
|
}
|
||||||
// for multi-tenancy solution
|
// for multi-tenancy solution
|
||||||
if v, ok := query["tenantIDs"]; ok && v != "" {
|
if v, ok := query["tenantName"]; ok && v != "" {
|
||||||
conditions = append(conditions, "tenant_ids like concat(concat('%', ?), '%')")
|
conditions = append(conditions, "tenant_name = '?'")
|
||||||
params = append(params, v)
|
params = append(params, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ func (r *RepoUdmSubUser) SelectPage(query map[string]any) map[string]any {
|
|||||||
|
|
||||||
result := map[string]any{
|
result := map[string]any{
|
||||||
"total": 0,
|
"total": 0,
|
||||||
"rows": []model.UdmAuthUser{},
|
"rows": []model.UdmSubUser{},
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询数量 长度为0直接返回
|
// 查询数量 长度为0直接返回
|
||||||
@@ -165,9 +165,9 @@ func (r *RepoUdmSubUser) SelectList(auth model.UdmSubUser) []model.UdmSubUser {
|
|||||||
conditions = append(conditions, "ne_id = ?")
|
conditions = append(conditions, "ne_id = ?")
|
||||||
params = append(params, auth.NeID)
|
params = append(params, auth.NeID)
|
||||||
}
|
}
|
||||||
if auth.TenantIDs != "" {
|
if auth.TenantName != "" {
|
||||||
conditions = append(conditions, "tenant_ids like concat(concat('%', ?), '%')")
|
conditions = append(conditions, "tenant_name = '?'")
|
||||||
params = append(params, auth.TenantIDs)
|
params = append(params, auth.TenantName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建查询条件语句
|
// 构建查询条件语句
|
||||||
@@ -208,7 +208,7 @@ func (r *RepoUdmSubUser) ClearAndInsert(neID string, subArr []model.UdmSubUser)
|
|||||||
batch := subArr[i:end]
|
batch := subArr[i:end]
|
||||||
|
|
||||||
// multi-tenancy
|
// multi-tenancy
|
||||||
r.SetTenantIDs(batch)
|
r.SetTenantName(batch)
|
||||||
|
|
||||||
// 调用 InsertMulti 函数将批量数据插入数据库
|
// 调用 InsertMulti 函数将批量数据插入数据库
|
||||||
results, err := datasource.DefaultDB().Table("u_sub_user").InsertMulti(batch)
|
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]
|
batch := subUser[i:end]
|
||||||
|
|
||||||
// multi-tenancy
|
// multi-tenancy
|
||||||
r.SetTenantIDs(batch)
|
r.SetTenantName(batch)
|
||||||
|
|
||||||
// 调用 InsertMulti 函数将批量数据插入数据库
|
// 调用 InsertMulti 函数将批量数据插入数据库
|
||||||
results, err := datasource.DefaultDB().Table("u_sub_user").InsertMulti(batch)
|
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
|
// multi-tenancy
|
||||||
subUserSlice := []model.UdmSubUser{subUser}
|
subUserSlice := []model.UdmSubUser{subUser}
|
||||||
r.SetTenantIDs(subUserSlice)
|
r.SetTenantName(subUserSlice)
|
||||||
|
|
||||||
results, err := datasource.DefaultDB().Table("u_sub_user").Insert(subUser)
|
results, err := datasource.DefaultDB().Table("u_sub_user").Insert(subUser)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -358,8 +358,8 @@ func (r *RepoUdmSubUser) Update(neID string, authUser model.UdmSubUser) int64 {
|
|||||||
user.StaticIp = authUser.StaticIp
|
user.StaticIp = authUser.StaticIp
|
||||||
}
|
}
|
||||||
// for multi-tenancy solution
|
// for multi-tenancy solution
|
||||||
if authUser.TenantIDs != "" && authUser.TenantIDs != user.TenantIDs {
|
if authUser.TenantName != "" && authUser.TenantName != user.TenantName {
|
||||||
user.TenantIDs = authUser.TenantIDs
|
user.TenantName = authUser.TenantName
|
||||||
}
|
}
|
||||||
|
|
||||||
results, err := datasource.DefaultDB().Table("u_sub_user").Where("imsi = ? and ne_id = ?", user.Imsi, user.NeID).Update(user)
|
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
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
// multi-tenancy solution, get tenant_ids by imsi
|
// multi-tenancy solution, get tenant_name by imsi
|
||||||
func (r *RepoUdmSubUser) SetTenantIDs(subArr []model.UdmSubUser) {
|
func (r *RepoUdmSubUser) SetTenantName(subArr []model.UdmSubUser) {
|
||||||
for s := 0; s < len(subArr); s++ {
|
for s := 0; s < len(subArr); s++ {
|
||||||
var tenantIDs []string
|
var tenantName string
|
||||||
err := datasource.DefaultDB().Table("sys_tenant_map").
|
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 {
|
if err != nil {
|
||||||
log.Errorf("Find tenant_ids err => %v", err)
|
log.Errorf("Find tenant_name err => %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
subArr[s].TenantIDs = strings.Join(tenantIDs, ",")
|
subArr[s].TenantName = tenantName
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type UDMSub struct {
|
|||||||
ContextId string `json:"contextId" gorm:"column:context_id"`
|
ContextId string `json:"contextId" gorm:"column:context_id"`
|
||||||
ApnContext string `json:"apnContext" gorm:"column:apn_context"`
|
ApnContext string `json:"apnContext" gorm:"column:apn_context"`
|
||||||
StaticIp string `json:"staticIp" gorm:"column:static_ip"`
|
StaticIp string `json:"staticIp" gorm:"column:static_ip"`
|
||||||
TenantIDs string `json:"tenantIDs" gorm:"column:tenant_ids"`
|
TenantName string `json:"tenantName" gorm:"column:tenant_name"`
|
||||||
|
|
||||||
// ====== 非数据库字段属性 ======
|
// ====== 非数据库字段属性 ======
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import (
|
|||||||
var NewUDMSubImpl = &UDMSubImpl{
|
var NewUDMSubImpl = &UDMSubImpl{
|
||||||
selectSql: `select
|
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,
|
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`,
|
from u_sub_user`,
|
||||||
|
|
||||||
resultMap: map[string]string{
|
resultMap: map[string]string{
|
||||||
@@ -40,7 +40,7 @@ var NewUDMSubImpl = &UDMSubImpl{
|
|||||||
"context_id": "ContextId",
|
"context_id": "ContextId",
|
||||||
"apn_context": "ApnContext",
|
"apn_context": "ApnContext",
|
||||||
"static_ip": "StaticIp",
|
"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)
|
params = append(params, v)
|
||||||
}
|
}
|
||||||
// for multi-tenancy solution
|
// for multi-tenancy solution
|
||||||
if v, ok := query["tenantIDs"]; ok && v != "" {
|
if v, ok := query["tenantName"]; ok && v != "" {
|
||||||
conditions = append(conditions, "tenant_ids like concat(concat('%', ?), '%')")
|
conditions = append(conditions, "tenant_name = '?'")
|
||||||
params = append(params, v)
|
params = append(params, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user