147 lines
3.6 KiB
Go
147 lines
3.6 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
|
||
"be.ems/src/framework/redis"
|
||
"be.ems/src/modules/network_data/model"
|
||
"be.ems/src/modules/network_data/repository"
|
||
)
|
||
|
||
// 实例化服务层 UDMAuthImpl 结构体
|
||
var NewUDMAuthImpl = &UDMAuthImpl{
|
||
udmAuthRepository: repository.NewUDMAuthImpl,
|
||
}
|
||
|
||
// UDM鉴权信息 服务层处理
|
||
type UDMAuthImpl struct {
|
||
// UDM鉴权信息数据信息
|
||
udmAuthRepository repository.IUDMAuth
|
||
}
|
||
|
||
// dataByRedis UDM鉴权用户 db:0 中 ausf:*
|
||
func (r *UDMAuthImpl) dataByRedis(imsi, neId string) []model.UDMAuth {
|
||
arr := []model.UDMAuth{}
|
||
key := fmt.Sprintf("ausf:%s", imsi)
|
||
ausfArr, err := redis.GetKeys("udmuser", key)
|
||
if err != nil {
|
||
return arr
|
||
}
|
||
for _, key := range ausfArr {
|
||
m, err := redis.GetHash("udmuser", key)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
|
||
// 跳过-号数据
|
||
imsi := key[5:]
|
||
if strings.Contains(imsi, "-") {
|
||
continue
|
||
}
|
||
|
||
amf := ""
|
||
if v, ok := m["amf"]; ok {
|
||
amf = strings.Replace(v, "\r\n", "", 1)
|
||
}
|
||
a := model.UDMAuth{
|
||
IMSI: imsi,
|
||
Amf: amf,
|
||
Status: "1", // 默认给1
|
||
Ki: m["ki"],
|
||
AlgoIndex: m["algo"],
|
||
Opc: m["opc"],
|
||
NeId: neId,
|
||
}
|
||
arr = append(arr, a)
|
||
}
|
||
return arr
|
||
}
|
||
|
||
// ResetData 重置鉴权用户数据,清空数据库重新同步Redis数据
|
||
func (r *UDMAuthImpl) ResetData(neId string) int64 {
|
||
authArr := r.dataByRedis("*", neId)
|
||
// 数据清空后添加
|
||
go r.udmAuthRepository.ClearAndInsert(neId, authArr)
|
||
return int64(len(authArr))
|
||
}
|
||
|
||
// SelectPage 分页查询数据库
|
||
func (r *UDMAuthImpl) SelectPage(query map[string]any) map[string]any {
|
||
return r.udmAuthRepository.SelectPage(query)
|
||
}
|
||
|
||
// SelectList 查询数据库
|
||
func (r *UDMAuthImpl) SelectList(u model.UDMAuth) []model.UDMAuth {
|
||
return r.udmAuthRepository.SelectList(u)
|
||
}
|
||
|
||
// Insert 从数据中读取后删除imsi再存入数据库
|
||
// imsi长度15,ki长度32,opc长度0或者32
|
||
func (r *UDMAuthImpl) Insert(neId string, u model.UDMAuth) int64 {
|
||
uArr := r.dataByRedis(u.IMSI, neId)
|
||
if len(uArr) > 0 {
|
||
r.udmAuthRepository.Delete(neId, u.IMSI)
|
||
return r.udmAuthRepository.Inserts(uArr)
|
||
}
|
||
return 0
|
||
}
|
||
|
||
// InsertData 导入文件数据 dataType目前两种:txt/csv
|
||
func (r *UDMAuthImpl) InsertData(neId, dataType string, data any) int64 {
|
||
// imsi截取前缀,重新获取部分数据
|
||
prefixes := make(map[string]struct{})
|
||
|
||
if dataType == "csv" {
|
||
for _, v := range data.([]map[string]string) {
|
||
imsi := v["imsi"]
|
||
if len(imsi) < 6 {
|
||
continue
|
||
}
|
||
prefix := imsi[:len(imsi)-4]
|
||
prefixes[prefix] = struct{}{}
|
||
}
|
||
}
|
||
if dataType == "txt" {
|
||
for _, v := range data.([][]string) {
|
||
imsi := v[0]
|
||
if len(imsi) < 6 {
|
||
continue
|
||
}
|
||
prefix := imsi[:len(imsi)-4]
|
||
prefixes[prefix] = struct{}{}
|
||
}
|
||
}
|
||
|
||
// 根据前缀重新加载插入
|
||
var num int64 = 0
|
||
for prefix := range prefixes {
|
||
// 直接删除前缀的记录
|
||
r.udmAuthRepository.DeletePrefixByIMSI(neId, prefix)
|
||
// keys ausf:4600001000004*
|
||
authArr := r.dataByRedis(prefix+"*", neId)
|
||
if len(authArr) > 0 {
|
||
num += r.udmAuthRepository.Inserts(authArr)
|
||
}
|
||
}
|
||
return num
|
||
}
|
||
|
||
// Delete 删除单个不重新加载
|
||
func (r *UDMAuthImpl) Delete(neId, imsi string) int64 {
|
||
return r.udmAuthRepository.Delete(neId, imsi)
|
||
}
|
||
|
||
// LoadData 删除范围后重新加载 num表示imsi后几位
|
||
func (r *UDMAuthImpl) LoadData(neId, imsi, num string) int64 {
|
||
prefix := imsi[:len(imsi)-len(num)-1]
|
||
// 直接删除前缀的记录
|
||
delNum := r.udmAuthRepository.DeletePrefixByIMSI(neId, prefix)
|
||
// keys ausf:4600001000004*
|
||
authArr := r.dataByRedis(prefix+"*", neId)
|
||
if len(authArr) > 0 {
|
||
return r.udmAuthRepository.Inserts(authArr)
|
||
}
|
||
return delNum
|
||
}
|