feat: UDM数据操作调整

This commit is contained in:
TsMask
2024-11-12 10:17:45 +08:00
parent 1a9e5735a4
commit 729f03d2fe
6 changed files with 75 additions and 275 deletions

View File

@@ -55,8 +55,8 @@ func (s *UDMAuthController) ResetData(c *gin.Context) {
// GET /list
func (s *UDMAuthController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.udmAuthService.SelectPage(querys)
c.JSON(200, result.Ok(data))
total, rows := s.udmAuthService.SelectPage(querys)
c.JSON(200, result.Ok(map[string]any{"total": total, "rows": rows}))
}
// UDM鉴权用户信息
@@ -364,13 +364,12 @@ func (s *UDMAuthController) Export(c *gin.Context) {
querys["pageNum"] = 1
querys["pageSize"] = 10000
data := s.udmAuthService.SelectPage(querys)
if parse.Number(data["total"]) == 0 {
total, rows := s.udmAuthService.SelectPage(querys)
if total == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
return
}
rows := data["rows"].([]model.UDMAuthUser)
// rows := s.udmAuthService.SelectList(model.UDMAuthUser{NeId: neId})
if len(rows) <= 0 {

View File

@@ -54,8 +54,8 @@ func (s *UDMSubController) ResetData(c *gin.Context) {
// GET /list
func (s *UDMSubController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.udmSubService.SelectPage(querys)
c.JSON(200, result.Ok(data))
total, rows := s.udmSubService.SelectPage(querys)
c.JSON(200, result.Ok(map[string]any{"total": total, "rows": rows}))
}
// UDM签约用户信息
@@ -369,13 +369,12 @@ func (s *UDMSubController) Export(c *gin.Context) {
querys["pageNum"] = 1
querys["pageSize"] = 10000
data := s.udmSubService.SelectPage(querys)
if parse.Number(data["total"]) == 0 {
total, rows := s.udmSubService.SelectPage(querys)
if total == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
return
}
rows := data["rows"].([]model.UDMSubUser)
// rows := s.udmSubService.SelectList(model.UDMSubUser{NeId: neId})
if len(rows) <= 0 {

View File

@@ -1,120 +1,57 @@
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"
)
// 实例化数据层 UDMAuthUser 结构体
var NewUDMAuthUser = &UDMAuthUser{
selectSql: `select id, imsi, ne_id, amf, status, ki, algo_index, opc from u_auth_user`,
resultMap: map[string]string{
"id": "ID",
"imsi": "IMSI",
"ne_id": "NeId",
"amf": "Amf",
"status": "Status",
"ki": "Ki",
"algo_index": "AlgoIndex",
"opc": "Opc",
},
}
var NewUDMAuthUser = &UDMAuthUser{}
// 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
}
type UDMAuthUser struct{}
// ClearAndInsert 清空ne_id后新增实体
func (r *UDMAuthUser) ClearAndInsert(neId string, uArr []model.UDMAuthUser) int64 {
// 不指定neID时用 TRUNCATE 清空表快
// _, err := datasource.ExecDB("", "TRUNCATE TABLE u_auth_user", nil)
_, err := datasource.ExecDB("", "DELETE FROM u_auth_user WHERE ne_id = ?", []any{neId})
if err != nil {
logger.Errorf("TRUNCATE err => %v", err)
result := datasource.DB("").Where("ne_id = ?", neId).Unscoped().Delete(&model.UDMAuthUser{})
if result.Error != nil {
logger.Errorf("Delete err => %v", result.Error)
}
return r.Inserts(uArr)
}
// SelectPage 根据条件分页查询
func (r *UDMAuthUser) SelectPage(query map[string]any) map[string]any {
func (r *UDMAuthUser) SelectPage(query map[string]any) (int64, []model.UDMAuthUser) {
tx := datasource.DB("").Model(&model.UDMAuthUser{})
// 查询条件拼接
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), " "))
tx = tx.Where("imsi like concat(concat('%',?), '%')", v)
}
if v, ok := query["neId"]; ok && v != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, v)
tx = tx.Where("ne_id =?", v)
}
if v, ok := query["imsis"]; ok && v != "" {
placeholder := repo.KeyPlaceholderByQuery(len(v.([]any)))
conditions = append(conditions, fmt.Sprintf("imsi in (%s)", placeholder))
for _, v := range v.([]any) {
params = append(params, v.(string))
}
tx = tx.Where("imsi in ?", v)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.UDMAuthUser{},
}
var total int64 = 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 {
if err := tx.Count(&total).Error; err != nil || total <= 0 {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
return total, rows
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
tx = tx.Offset(int(pageNum * pageSize)).Limit(int(pageSize))
// 排序
orderSql := ""
if v, ok := query["sortField"]; ok && v != "" {
sortSql := v.(string)
if o, ok := query["sortOrder"]; ok && o != nil && v != "" {
@@ -124,71 +61,52 @@ func (r *UDMAuthUser) SelectPage(query map[string]any) map[string]any {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by %s ", sortSql)
tx = tx.Order(sortSql)
}
// 查询数据
querySql := r.selectSql + whereSql + orderSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
if err := tx.Find(&rows).Error; err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
return total, rows
}
// SelectList 根据实体查询
func (r *UDMAuthUser) SelectList(u model.UDMAuthUser) []model.UDMAuthUser {
tx := datasource.DB("").Model(&model.UDMAuthUser{})
// 查询条件拼接
var conditions []string
var params []any
if u.IMSI != "" {
conditions = append(conditions, "imsi = ?")
params = append(params, u.IMSI)
tx = tx.Where("imsi = ?", 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 ")
tx = tx.Where("ne_id = ?", u.NeId)
}
// 查询数据
querySql := r.selectSql + whereSql + " order by imsi asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
arr := []model.UDMAuthUser{}
if err := tx.Order("imsi asc").Find(&arr).Error; err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
return arr
}
// 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 {
tx := datasource.DB("").Model(&model.UDMAuthUser{})
item := model.UDMAuthUser{}
// 查询条件拼接
tx = tx.Where("imsi = ? and ne_id = ?", imsi, neId)
// 查询数据
if err := tx.Order("imsi asc").Limit(1).Find(&item).Error; 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{}
return item
}
// Insert 批量添加
func (r *UDMAuthUser) Inserts(uArr []model.UDMAuthUser) int64 {
tx := datasource.DefaultDB().CreateInBatches(uArr, 3000)
tx := datasource.DB("").CreateInBatches(uArr, 3000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}

View File

@@ -1,156 +1,60 @@
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"
)
// 实例化数据层 UDMSubUser 结构体
var NewUDMSub = &UDMSubUser{
selectSql: `select
id, imsi, msisdn, ne_id,
am_dat, ambr, nssai, rat, arfb, sar, cn_type, rfsp_index, reg_timer, ue_usage_type, active_time, mico, odb_ps, group_id,
eps_dat, eps_flag, eps_odb, hplmn_odb, ard, epstpl, context_id, apn_mum, apn_context, static_ip,
sm_data, smf_sel, cag
from u_sub_user`,
resultMap: map[string]string{
"id": "ID",
"imsi": "IMSI",
"msisdn": "MSISDN",
"ne_id": "NeId",
"am_dat": "AmDat",
"ambr": "UeAmbrTpl",
"nssai": "NssaiTpl",
"rat": "RatRestrictions",
"arfb": "AreaForbiddenTpl",
"sar": "ServiceAreaRestrictionTpl",
"cn_type": "CnTypeRestrictions",
"rfsp_index": "RfspIndex",
"reg_timer": "SubsRegTime",
"ue_usage_type": "UeUsageType",
"active_time": "ActiveTime",
"mico": "MicoAllowed",
"odb_ps": "OdbPs",
"group_id": "GroupId",
"eps_dat": "EpsDat",
"eps_flag": "EpsFlag",
"eps_odb": "EpsOdb",
"hplmn_odb": "HplmnOdb",
"ard": "Ard",
"epstpl": "Epstpl",
"context_id": "ContextId",
"apn_mum": "ApnNum",
"apn_context": "ApnContext",
"static_ip": "StaticIp",
"sm_data": "SmData",
"smf_sel": "SmfSel",
"cag": "Cag",
},
}
var NewUDMSub = &UDMSubUser{}
// 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
}
type UDMSubUser struct{}
// ClearAndInsert 清空ne_id后新增实体
func (r *UDMSubUser) ClearAndInsert(neId string, u []model.UDMSubUser) int64 {
// 不指定neID时用 TRUNCATE 清空表快
// _, err := datasource.ExecDB("", "TRUNCATE TABLE u_sub_user", nil)
_, err := datasource.ExecDB("", "DELETE FROM u_sub_user WHERE ne_id = ?", []any{neId})
if err != nil {
logger.Errorf("TRUNCATE err => %v", err)
result := datasource.DB("").Where("ne_id = ?", neId).Unscoped().Delete(&model.UDMSubUser{})
if result.Error != nil {
logger.Errorf("Delete err => %v", result.Error)
}
return r.Inserts(u)
}
// SelectPage 根据条件分页查询字典类型
func (r *UDMSubUser) SelectPage(query map[string]any) map[string]any {
func (r *UDMSubUser) SelectPage(query map[string]any) (int64, []model.UDMSubUser) {
tx := datasource.DB("").Model(&model.UDMSubUser{})
// 查询条件拼接
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), " "))
tx = tx.Where("imsi like concat(concat('%', ?), '%')", v)
}
if v, ok := query["msisdn"]; ok && v != "" {
conditions = append(conditions, "msisdn like concat(concat('%', ?), '%')")
params = append(params, strings.Trim(v.(string), " "))
tx = tx.Where("msisdn like concat(concat('%', ?), '%')", v)
}
if v, ok := query["neId"]; ok && v != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, v)
tx = tx.Where("ne_id =?", v)
}
if v, ok := query["imsis"]; ok && v != "" {
placeholder := repo.KeyPlaceholderByQuery(len(v.([]any)))
conditions = append(conditions, fmt.Sprintf("imsi in (%s)", placeholder))
for _, v := range v.([]any) {
params = append(params, v.(string))
}
tx = tx.Where("imsi in ?", v)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.UDMSubUser{},
}
var total int64 = 0
rows := []model.UDMSubUser{}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from u_sub_user"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
if err := tx.Count(&total).Error; err != nil || total <= 0 {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
return total, rows
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
tx = tx.Offset(int(pageNum * pageSize)).Limit(int(pageSize))
// 排序
orderSql := ""
if v, ok := query["sortField"]; ok && v != "" {
sortSql := v.(string)
if o, ok := query["sortOrder"]; ok && o != nil && v != "" {
@@ -160,72 +64,52 @@ func (r *UDMSubUser) SelectPage(query map[string]any) map[string]any {
sortSql += " asc "
}
}
orderSql = fmt.Sprintf(" order by %s ", sortSql)
tx = tx.Order(sortSql)
}
// 查询数据
querySql := r.selectSql + whereSql + orderSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
if err := tx.Find(&rows).Error; err != nil {
logger.Errorf("query err => %v", err)
return result
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
return total, rows
}
// SelectList 根据实体查询
func (r *UDMSubUser) SelectList(u model.UDMSubUser) []model.UDMSubUser {
tx := datasource.DB("").Model(&model.UDMSubUser{})
// 查询条件拼接
var conditions []string
var params []any
if u.IMSI != "" {
conditions = append(conditions, "imsi = ?")
params = append(params, u.IMSI)
tx = tx.Where("imsi = ?", 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 ")
tx = tx.Where("ne_id = ?", u.NeId)
}
// 查询数据
querySql := r.selectSql + whereSql + " order by imsi asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
arr := []model.UDMSubUser{}
if err := tx.Order("imsi asc").Find(&arr).Error; err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
return arr
}
// 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 {
tx := datasource.DB("").Model(&model.UDMSubUser{})
item := model.UDMSubUser{}
// 查询条件拼接
tx = tx.Where("imsi = ? and ne_id = ?", imsi, neId)
// 查询数据
if err := tx.Order("imsi asc").Limit(1).Find(&item).Error; 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{}
return item
}
// Insert 批量添加
func (r *UDMSubUser) Inserts(uArr []model.UDMSubUser) int64 {
tx := datasource.DefaultDB().CreateInBatches(uArr, 2000)
tx := datasource.DB("").CreateInBatches(uArr, 2000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}

View File

@@ -101,7 +101,7 @@ func (r *UDMAuthUser) ParseInfo(imsi, neId string, data map[string]string) model
}
// SelectPage 分页查询数据库
func (r *UDMAuthUser) SelectPage(query map[string]any) map[string]any {
func (r *UDMAuthUser) SelectPage(query map[string]any) (int64, []model.UDMAuthUser) {
return r.udmAuthRepository.SelectPage(query)
}

View File

@@ -180,7 +180,7 @@ func (r *UDMSubUser) ParseInfo(imsi, neId string, data map[string]string) model.
}
// SelectPage 分页查询数据库
func (r *UDMSubUser) SelectPage(query map[string]any) map[string]any {
func (r *UDMSubUser) SelectPage(query map[string]any) (int64, []model.UDMSubUser) {
return r.udmSubRepository.SelectPage(query)
}