Merge remote-tracking branch 'origin/main' into multi-tenant

This commit is contained in:
TsMask
2024-09-20 20:23:39 +08:00
142 changed files with 7687 additions and 2096 deletions

View File

@@ -1,26 +1,206 @@
package repository
import (
"fmt"
"strings"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/network_data/model"
)
// UDM鉴权信息 数据层接口
type IUDMAuth interface {
// ClearAndInsert 清空ne_id后新增实体
ClearAndInsert(neId string, uArr []model.UDMAuth) int64
// 实例化数据层 UDMAuthUser 结构体
var NewUDMAuthUser = &UDMAuthUser{
selectSql: `select id, imsi, ne_id, amf, status, ki, algo_index, opc from u_auth_user`,
// SelectPage 根据条件分页查询
SelectPage(query map[string]any) map[string]any
// SelectList 根据实体查询
SelectList(u model.UDMAuth) []model.UDMAuth
// Insert 批量添加
Inserts(uArr []model.UDMAuth) int64
// Delete 删除实体
Delete(neId, imsi string) int64
// DeletePrefixByIMSI 删除前缀匹配的实体
DeletePrefixByIMSI(neId, imsi string) int64
resultMap: map[string]string{
"id": "ID",
"imsi": "IMSI",
"ne_id": "NeId",
"amf": "Amf",
"status": "Status",
"ki": "Ki",
"algo_index": "AlgoIndex",
"opc": "Opc",
},
}
// UDMAuthUser UDM鉴权信息表 数据层处理
type UDMAuthUser struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
resultMap map[string]string
}
// convertResultRows 将结果记录转实体结果组
func (r *UDMAuthUser) convertResultRows(rows []map[string]any) []model.UDMAuthUser {
arr := make([]model.UDMAuthUser, 0)
for _, row := range rows {
item := model.UDMAuthUser{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
}
}
arr = append(arr, item)
}
return arr
}
// ClearAndInsert 清空ne_id后新增实体
func (r *UDMAuthUser) ClearAndInsert(neId string, uArr []model.UDMAuthUser) int64 {
// 不指定neID时用 TRUNCATE 清空表快
_, err := datasource.ExecDB("", "TRUNCATE TABLE u_auth_user", nil)
if err != nil {
logger.Errorf("TRUNCATE err => %v", err)
}
return r.Inserts(uArr)
}
// SelectPage 根据条件分页查询
func (r *UDMAuthUser) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
if v, ok := query["imsi"]; ok && v != "" {
conditions = append(conditions, "imsi like concat(concat('%', ?), '%')")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["neId"]; ok && v != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, v)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.UDMAuthUser{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from u_auth_user"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
// 排序
orderSql := ""
if v, ok := query["sortField"]; ok && v != "" {
sortSql := v.(string)
if o, ok := query["sortOrder"]; ok && o != nil && v != "" {
if o == "desc" {
sortSql += " desc "
} else {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by %s ", sortSql)
}
// 查询数据
querySql := r.selectSql + whereSql + orderSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
}
// SelectList 根据实体查询
func (r *UDMAuthUser) SelectList(u model.UDMAuthUser) []model.UDMAuthUser {
// 查询条件拼接
var conditions []string
var params []any
if u.IMSI != "" {
conditions = append(conditions, "imsi = ?")
params = append(params, u.IMSI)
}
if u.NeId != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, u.NeId)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
// 查询数据
querySql := r.selectSql + whereSql + " order by imsi asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
}
// SelectByIMSIAndNeID 通过imsi和ne_id查询
func (r *UDMAuthUser) SelectByIMSIAndNeID(imsi, neId string) model.UDMAuthUser {
querySql := r.selectSql + " where imsi = ? and ne_id = ?"
results, err := datasource.RawDB("", querySql, []any{imsi, neId})
if err != nil {
logger.Errorf("query err => %v", err)
return model.UDMAuthUser{}
}
// 转换实体
rows := r.convertResultRows(results)
if len(rows) > 0 {
return rows[0]
}
return model.UDMAuthUser{}
}
// Insert 批量添加
func (r *UDMAuthUser) Inserts(uArr []model.UDMAuthUser) int64 {
tx := datasource.DefaultDB().CreateInBatches(uArr, 3000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}
return tx.RowsAffected
}
// Delete 删除实体
func (r *UDMAuthUser) Delete(imsi, neId string) int64 {
tx := datasource.DefaultDB().Where("imsi = ? and ne_id = ?", imsi, neId).Delete(&model.UDMAuthUser{})
if err := tx.Error; err != nil {
logger.Errorf("Delete err => %v", err)
}
return tx.RowsAffected
}
// DeletePrefixByIMSI 删除前缀匹配的实体
func (r *UDMAuthUser) DeletePrefixByIMSI(neId, imsi string) int64 {
tx := datasource.DefaultDB().Where("imsi like concat(?, '%') and ne_id = ?", imsi, neId).Delete(&model.UDMAuthUser{})
if err := tx.Error; err != nil {
logger.Errorf("DeletePrefixByIMSI err => %v", err)
}
return tx.RowsAffected
}

View File

@@ -1,26 +1,288 @@
package repository
import (
"fmt"
"strings"
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"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/network_data/model"
)
// UDM签约信息 数据层接口
type IUDMSub interface {
// ClearAndInsert 清空ne_id后新增实体
ClearAndInsert(neId string, uArr []model.UDMSub) int64
// 实例化数据层 UDMSubUser 结构体
var NewUDMSub = &UDMSubUser{
selectSql: `select
s.id, s.msisdn, s.imsi, s.ambr, s.nssai, s.rat, s.arfb, s.sar, s.cn, s.sm_data, s.smf_sel, s.eps_dat,
s.ne_id, s.eps_flag, s.eps_odb, s.hplmn_odb, s.ard, s.epstpl, s.context_id, s.apn_context, s.static_ip,
s.cag,
t.tenant_id, t.tenant_name
from u_sub_user s
left join sys_tenant t on t.tenant_id = s.tenant_id`,
// SelectPage 根据条件分页查询
SelectPage(query map[string]any) map[string]any
resultMap: map[string]string{
"id": "ID",
"imsi": "IMSI",
"msisdn": "MSISDN",
"ne_id": "NeId",
"ambr": "Ambr",
"nssai": "Nssai",
"rat": "Rat",
"arfb": "Arfb",
"sar": "Sar",
"cn": "Cn",
"sm_data": "SmData",
"smf_sel": "SmfSel",
"eps_dat": "EpsDat",
"eps_flag": "EpsFlag",
"eps_odb": "EpsOdb",
"hplmn_odb": "HplmnOdb",
"ard": "Ard",
"epstpl": "Epstpl",
"context_id": "ContextId",
"apn_context": "ApnContext",
"static_ip": "StaticIp",
"cag": "Cag",
// SelectList 根据实体查询
SelectList(u model.UDMSub) []model.UDMSub
// Insert 批量添加
Inserts(uArr []model.UDMSub) int64
// Delete 删除实体
Delete(neId, imsi string) int64
// DeletePrefixByIMSI 删除前缀匹配的实体
DeletePrefixByIMSI(neId, imsi string) int64
"tenant_id": "TenantID",
"tenant_name": "TenantName", // Tenant name for multi-tenancy
},
}
// UDMSubUser UDM签约信息表 数据层处理
type UDMSubUser struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
resultMap map[string]string
}
// convertResultRows 将结果记录转实体结果组
func (r *UDMSubUser) convertResultRows(rows []map[string]any) []model.UDMSubUser {
arr := make([]model.UDMSubUser, 0)
for _, row := range rows {
item := model.UDMSubUser{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
}
}
arr = append(arr, item)
}
return arr
}
// ClearAndInsert 清空ne_id后新增实体
func (r *UDMSubUser) ClearAndInsert(neID string, u []model.UDMSubUser) int64 {
// 不指定neID时用 TRUNCATE 清空表快
_, err := datasource.ExecDB("", "TRUNCATE TABLE u_sub_user", nil)
if err != nil {
logger.Errorf("TRUNCATE err => %v", err)
}
return r.Inserts(u)
}
// SelectPage 根据条件分页查询字典类型
func (r *UDMSubUser) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
if v, ok := query["msisdn"]; ok && v != "" {
conditions = append(conditions, "msisdn like concat(concat('%', ?), '%')")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["imsi"]; ok && v != "" {
//conditions = append(conditions, "imsi like concat(concat('%', ?), '%')")
//params = append(params, strings.Trim(v.(string), " "))
conditions = append(conditions, "imsi like ?")
params = append(params, v)
}
if v, ok := query["neId"]; ok && v != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, v)
}
// for multi-tenancy solution
if v, ok := query["tenantName"]; ok && v != "" {
var tenantID []string
err := dborm.DefaultDB().Table("sys_tenant").
Where("tenant_name=?", v).Cols("tenant_id").Distinct().Find(&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 {
conditions = append(conditions, "s.tenant_id = ?")
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().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 tenantID != "" {
conditions = append(conditions, "s.tenant_id = ?")
params = append(params, tenantID)
}
// if len(tenantID) > 0 {
// conditions = append(conditions, "s.tenant_id = ?")
// params = append(params, tenantID[0])
// }
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.UDMSubUser{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from u_sub_user s"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
// 排序
orderSql := ""
if v, ok := query["sortField"]; ok && v != "" {
sortSql := v.(string)
if o, ok := query["sortOrder"]; ok && o != nil && v != "" {
if o == "desc" {
sortSql += " desc "
} else {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by %s ", sortSql)
}
// 查询数据
querySql := r.selectSql + whereSql + orderSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
return result
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
}
// SelectList 根据实体查询
func (r *UDMSubUser) SelectList(u model.UDMSubUser) []model.UDMSubUser {
// 查询条件拼接
var conditions []string
var params []any
if u.IMSI != "" {
conditions = append(conditions, "imsi = ?")
params = append(params, u.IMSI)
}
if u.NeId != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, u.NeId)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
// 查询数据
querySql := r.selectSql + whereSql + " order by imsi asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
}
// SelectByIMSIAndNeID 通过imsi和ne_id查询
func (r *UDMSubUser) SelectByIMSIAndNeID(imsi, neId string) model.UDMSubUser {
querySql := r.selectSql + " where imsi = ? and ne_id = ?"
results, err := datasource.RawDB("", querySql, []any{imsi, neId})
if err != nil {
logger.Errorf("query err => %v", err)
return model.UDMSubUser{}
}
// 转换实体
rows := r.convertResultRows(results)
if len(rows) > 0 {
return rows[0]
}
return model.UDMSubUser{}
}
// Insert 批量添加
func (r *UDMSubUser) Inserts(uArr []model.UDMSubUser) int64 {
// multi-tenancy
r.SetTenantID(&uArr)
tx := datasource.DefaultDB().CreateInBatches(uArr, 2000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}
return tx.RowsAffected
}
// Delete 删除实体
func (r *UDMSubUser) Delete(imsi, neId string) int64 {
tx := datasource.DefaultDB().Where("imsi = ? and ne_id = ?", imsi, neId).Delete(&model.UDMSubUser{})
if err := tx.Error; err != nil {
logger.Errorf("Delete err => %v", err)
}
return tx.RowsAffected
}
// DeletePrefixByIMSI 删除前缀匹配的实体
func (r *UDMSubUser) DeletePrefixByIMSI(imsiPrefix, neId string) int64 {
tx := datasource.DefaultDB().Where("imsi like concat(?, '%') and ne_id = ?", imsiPrefix, neId).Delete(&model.UDMSubUser{})
if err := tx.Error; err != nil {
logger.Errorf("DeletePrefixByIMSI err => %v", err)
}
return tx.RowsAffected
}
func (r *UDMSubUser) SetTenantID(subArr *[]model.UDMSubUser) {
for s := 0; s < len(*subArr); s++ {
var tenantID []string
err := dborm.DefaultDB().Table("sys_tenant").
Where("status='1' and tenancy_type='IMSI' and ? like tenancy_key", (*subArr)[s].IMSI).
Cols("parent_id").Distinct().Find(&tenantID)
if err != nil {
logger.Errorf("Find tenant_id err => %v", err)
continue
}
if len(tenantID) > 0 {
(*subArr)[s].TenantID = tenantID[0]
}
}
}

View File

@@ -1,268 +0,0 @@
package repository
import (
"fmt"
"strings"
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"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/network_data/model"
)
// 实例化数据层 UDMSubImpl 结构体
var NewUDMSubImpl = &UDMSubImpl{
selectSql: `select
s.id, s.msisdn, s.imsi, s.ambr, s.nssai, s.rat, s.arfb, s.sar, s.cn, s.sm_data, s.smf_sel, s.eps_dat,
s.ne_id, s.eps_flag, s.eps_odb, s.hplmn_odb, s.ard, s.epstpl, s.context_id, s.apn_context, s.static_ip,
t.tenant_id, t.tenant_name
from u_sub_user s
left join sys_tenant t on t.tenant_id = s.tenant_id`,
resultMap: map[string]string{
"id": "ID",
"msisdn": "Msisdn",
"imsi": "IMSI",
"ambr": "Ambr",
"nssai": "Nssai",
"rat": "Rat",
"arfb": "Arfb",
"sar": "Sar",
"cn": "Cn",
"sm_data": "SmData",
"smf_sel": "SmfSel",
"eps_dat": "EpsDat",
"ne_id": "NeId",
"eps_flag": "EpsFlag",
"eps_odb": "EpsOdb",
"hplmn_odb": "HplmnOdb",
"ard": "Ard",
"epstpl": "Epstpl",
"context_id": "ContextId",
"apn_context": "ApnContext",
"static_ip": "StaticIp",
"tenant_id": "TenantID",
"tenant_name": "TenantName", // Tenant name for multi-tenancy
},
}
// UDMSubImpl UDM签约信息表 数据层处理
type UDMSubImpl struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
resultMap map[string]string
}
// convertResultRows 将结果记录转实体结果组
func (r *UDMSubImpl) convertResultRows(rows []map[string]any) []model.UDMSub {
arr := make([]model.UDMSub, 0)
for _, row := range rows {
item := model.UDMSub{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
}
}
arr = append(arr, item)
}
return arr
}
// ClearAndInsert 清空ne_id后新增实体
func (r *UDMSubImpl) ClearAndInsert(neID string, u []model.UDMSub) int64 {
// 不指定neID时用 TRUNCATE 清空表快
_, err := datasource.ExecDB("", "TRUNCATE TABLE u_sub_user", nil)
if err != nil {
logger.Errorf("TRUNCATE err => %v", err)
}
return r.Inserts(u)
}
// SelectPage 根据条件分页查询字典类型
func (r *UDMSubImpl) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
if v, ok := query["msisdn"]; ok && v != "" {
conditions = append(conditions, "msisdn like concat(concat('%', ?), '%')")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["imsi"]; ok && v != "" {
//conditions = append(conditions, "imsi like concat(concat('%', ?), '%')")
//params = append(params, strings.Trim(v.(string), " "))
conditions = append(conditions, "imsi like ?")
params = append(params, v)
}
if v, ok := query["neId"]; ok && v != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, v)
}
// for multi-tenancy solution
if v, ok := query["tenantName"]; ok && v != "" {
var tenantID []string
err := dborm.DefaultDB().Table("sys_tenant").
Where("tenant_name=?", v).Cols("tenant_id").Distinct().Find(&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 {
conditions = append(conditions, "s.tenant_id = ?")
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().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 tenantID != "" {
conditions = append(conditions, "s.tenant_id = ?")
params = append(params, tenantID)
}
// if len(tenantID) > 0 {
// conditions = append(conditions, "s.tenant_id = ?")
// params = append(params, tenantID[0])
// }
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.UDMSub{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from u_sub_user s"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
// 排序
orderSql := ""
if v, ok := query["sortField"]; ok && v != "" {
sortSql := v.(string)
if o, ok := query["sortOrder"]; ok && o != nil && v != "" {
if o == "desc" {
sortSql += " desc "
} else {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by %s ", sortSql)
}
// 查询数据
querySql := r.selectSql + whereSql + orderSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
return result
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
}
// SelectList 根据实体查询
func (r *UDMSubImpl) SelectList(u model.UDMSub) []model.UDMSub {
// 查询条件拼接
var conditions []string
var params []any
if u.IMSI != "" {
conditions = append(conditions, "imsi = ?")
params = append(params, u.IMSI)
}
if u.NeId != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, u.NeId)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
// 查询数据
querySql := r.selectSql + whereSql + " order by imsi asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
}
// Insert 批量添加
func (r *UDMSubImpl) Inserts(uArr []model.UDMSub) int64 {
// multi-tenancy
r.SetTenantID(&uArr)
tx := datasource.DefaultDB().CreateInBatches(uArr, 2000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}
return tx.RowsAffected
}
// Delete 删除实体
func (r *UDMSubImpl) Delete(neId, imsi string) int64 {
tx := datasource.DefaultDB().Where("imsi = ? and ne_id = ?", imsi, neId).Delete(&model.UDMSub{})
if err := tx.Error; err != nil {
logger.Errorf("Delete err => %v", err)
}
return tx.RowsAffected
}
// DeletePrefixByIMSI 删除前缀匹配的实体
func (r *UDMSubImpl) DeletePrefixByIMSI(neId, imsi string) int64 {
tx := datasource.DefaultDB().Where("imsi like concat(?, '%') and ne_id = ?", imsi, neId).Delete(&model.UDMSub{})
if err := tx.Error; err != nil {
logger.Errorf("DeletePrefixByIMSI err => %v", err)
}
return tx.RowsAffected
}
func (r *UDMSubImpl) SetTenantID(subArr *[]model.UDMSub) {
for s := 0; s < len(*subArr); s++ {
var tenantID []string
err := dborm.DefaultDB().Table("sys_tenant").
Where("status='1' and tenancy_type='IMSI' and ? like tenancy_key", (*subArr)[s].IMSI).
Cols("parent_id").Distinct().Find(&tenantID)
if err != nil {
logger.Errorf("Find tenant_id err => %v", err)
continue
}
if len(tenantID) > 0 {
(*subArr)[s].TenantID = tenantID[0]
}
}
}

View File

@@ -11,24 +11,21 @@ import (
"be.ems/src/modules/network_data/model"
)
// 实例化数据层 UDMAuthImpl 结构体
var NewUDMAuthImpl = &UDMAuthImpl{
selectSql: `select id, imsi, amf, status, ki, algo_index, opc, ne_id from u_auth_user`,
// 实例化数据层 UDMUserInfo 结构体
var NewUDMUserInfo = &UDMUserInfo{
selectSql: `select id, imsi, msisdn, ne_id, remark from u_user_info`,
resultMap: map[string]string{
"id": "ID",
"imsi": "IMSI",
"amf": "Amf",
"status": "Status",
"ki": "Ki",
"algo_index": "AlgoIndex",
"opc": "Opc",
"ne_id": "NeId",
"id": "ID",
"imsi": "IMSI",
"msisdn": "MSISDN",
"ne_id": "NeId",
"remark": "Remark",
},
}
// UDMAuthImpl UDM鉴权信息表 数据层处理
type UDMAuthImpl struct {
// UDMUserInfo UDM鉴权信息表 数据层处理
type UDMUserInfo struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
@@ -36,10 +33,10 @@ type UDMAuthImpl struct {
}
// convertResultRows 将结果记录转实体结果组
func (r *UDMAuthImpl) convertResultRows(rows []map[string]any) []model.UDMAuth {
arr := make([]model.UDMAuth, 0)
func (r *UDMUserInfo) convertResultRows(rows []map[string]any) []model.UDMUserInfo {
arr := make([]model.UDMUserInfo, 0)
for _, row := range rows {
item := model.UDMAuth{}
item := model.UDMUserInfo{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
@@ -50,18 +47,8 @@ func (r *UDMAuthImpl) convertResultRows(rows []map[string]any) []model.UDMAuth {
return arr
}
// ClearAndInsert 清空ne_id后新增实体
func (r *UDMAuthImpl) ClearAndInsert(neId string, uArr []model.UDMAuth) int64 {
// 不指定neID时用 TRUNCATE 清空表快
_, err := datasource.ExecDB("", "TRUNCATE TABLE u_auth_user", nil)
if err != nil {
logger.Errorf("TRUNCATE err => %v", err)
}
return r.Inserts(uArr)
}
// SelectPage 根据条件分页查询
func (r *UDMAuthImpl) SelectPage(query map[string]any) map[string]any {
func (r *UDMUserInfo) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
@@ -82,11 +69,11 @@ func (r *UDMAuthImpl) SelectPage(query map[string]any) map[string]any {
result := map[string]any{
"total": 0,
"rows": []model.UDMAuth{},
"rows": []model.UDMUserInfo{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from u_auth_user"
totalSql := "select count(1) as 'total' from u_user_info"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
@@ -132,7 +119,7 @@ func (r *UDMAuthImpl) SelectPage(query map[string]any) map[string]any {
}
// SelectList 根据实体查询
func (r *UDMAuthImpl) SelectList(u model.UDMAuth) []model.UDMAuth {
func (r *UDMUserInfo) SelectList(u model.UDMUserInfo) []model.UDMUserInfo {
// 查询条件拼接
var conditions []string
var params []any
@@ -162,8 +149,24 @@ func (r *UDMAuthImpl) SelectList(u model.UDMAuth) []model.UDMAuth {
return r.convertResultRows(results)
}
// SelectByIMSIAndNeID 通过imsi和ne_id查询
func (r *UDMUserInfo) SelectByIMSIAndNeID(imsi, neId string) model.UDMUserInfo {
querySql := r.selectSql + " where imsi = ? and ne_id = ?"
results, err := datasource.RawDB("", querySql, []any{imsi, neId})
if err != nil {
logger.Errorf("query err => %v", err)
return model.UDMUserInfo{}
}
// 转换实体
rows := r.convertResultRows(results)
if len(rows) > 0 {
return rows[0]
}
return model.UDMUserInfo{}
}
// Insert 批量添加
func (r *UDMAuthImpl) Inserts(uArr []model.UDMAuth) int64 {
func (r *UDMUserInfo) Inserts(uArr []model.UDMUserInfo) int64 {
tx := datasource.DefaultDB().CreateInBatches(uArr, 3000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
@@ -172,8 +175,8 @@ func (r *UDMAuthImpl) Inserts(uArr []model.UDMAuth) int64 {
}
// Delete 删除实体
func (r *UDMAuthImpl) Delete(neId, imsi string) int64 {
tx := datasource.DefaultDB().Where("imsi = ? and ne_id = ?", imsi, neId).Delete(&model.UDMAuth{})
func (r *UDMUserInfo) Delete(imsi, neId string) int64 {
tx := datasource.DefaultDB().Where("imsi = ? and ne_id = ?", imsi, neId).Delete(&model.UDMUserInfo{})
if err := tx.Error; err != nil {
logger.Errorf("Delete err => %v", err)
}
@@ -181,8 +184,8 @@ func (r *UDMAuthImpl) Delete(neId, imsi string) int64 {
}
// DeletePrefixByIMSI 删除前缀匹配的实体
func (r *UDMAuthImpl) DeletePrefixByIMSI(neId, imsi string) int64 {
tx := datasource.DefaultDB().Where("imsi like concat(?, '%') and ne_id = ?", imsi, neId).Delete(&model.UDMAuth{})
func (r *UDMUserInfo) DeletePrefixByIMSI(imsiPrefix, neId string) int64 {
tx := datasource.DefaultDB().Where("imsi like concat(?, '%') and ne_id = ?", imsiPrefix, neId).Delete(&model.UDMUserInfo{})
if err := tx.Error; err != nil {
logger.Errorf("DeletePrefixByIMSI err => %v", err)
}