feat: support ims user, voip auth data and backup UE data

This commit is contained in:
zhangsz
2025-04-09 14:16:35 +08:00
parent ba6eea0365
commit 7e5a73ffa7
36 changed files with 3569 additions and 62 deletions

View File

@@ -0,0 +1,170 @@
package service
import (
"fmt"
"strconv"
"strings"
"be.ems/src/framework/redis"
neService "be.ems/src/modules/network_element/service"
"be.ems/features/ue/model"
"be.ems/features/ue/repository"
)
// 实例化服务层 VoIPAuthService 结构体
var NewVoIPAuthService = &VoIPAuthService{
voipAuthRepository: repository.NewVoIPAuthRepository,
}
// VoLTE用户信息 服务层处理
type VoIPAuthService struct {
voipAuthRepository *repository.VoIPAuthRepository // VoLTE用户信息数据信息
}
// dataByRedis UDM签约用户 db:0 中 volte:*
func (r *VoIPAuthService) dataByRedis(userName, neId string) []model.VoIPAuth {
arr := []model.VoIPAuth{}
key := fmt.Sprintf("voip:%s", userName)
source := fmt.Sprintf("UDM_%s", neId)
// 网元主机的Redis客户端
redisClient, err := neService.NewNeInfo.NeRunRedisClient("UDM", neId)
if err != nil {
return arr
}
defer func() {
redisClient.Close()
redis.ConnectPush(source, nil)
}()
redis.ConnectPush(source, redisClient.Client)
udmsdArr, err := redis.GetKeys(source, key)
if err != nil {
return arr
}
mkv, err := redis.GetHashBatch(source, udmsdArr)
if err != nil {
return arr
}
for k, m := range mkv {
var userName string
KeyParts := strings.Split(k, ":")
if len(KeyParts) > 1 {
userName = KeyParts[1]
}
a := model.VoIPAuth{
NeId: neId,
UserName: userName, // userName
Password: m["password"], //
}
arr = append(arr, a)
}
return arr
}
// ResetData 重置鉴权用户数据清空数据库重新同步Redis数据
func (r *VoIPAuthService) ResetData(neId string) int64 {
subArr := r.dataByRedis("*", neId)
// 数据清空后添加
go r.voipAuthRepository.ClearAndInsert(neId, subArr)
return int64(len(subArr))
}
// ParseInfo 解析单个用户userName信息 data从命令MML得到的结果
func (r *VoIPAuthService) ParseInfo(userName, neId string, data map[string]string) model.VoIPAuth {
u := r.voipAuthRepository.SelectByUserNameAndNeID(userName, neId)
password := data["password"]
// 用于更新
u.NeId = neId
u.UserName = userName
u.Password = password
return u
}
// SelectPage 分页查询数据库
func (r *VoIPAuthService) SelectPage(query map[string]any) map[string]any {
return r.voipAuthRepository.SelectPage(query)
}
// SelectList 查询数据库
func (r *VoIPAuthService) SelectList(u model.VoIPAuth) []model.VoIPAuth {
return r.voipAuthRepository.SelectList(u)
}
// Insert 从数据中读取后删除userName再存入数据库
func (r *VoIPAuthService) Insert(neId string, u model.VoIPAuth) int64 {
uArr := r.dataByRedis(u.UserName, neId)
if len(uArr) > 0 {
r.voipAuthRepository.Delete(u.UserName, neId)
return r.voipAuthRepository.Inserts(uArr)
}
return 0
}
// InsertData 导入文件数据 dataType目前两种txt/csv
func (r *VoIPAuthService) InsertData(neId, dataType string, data any) int64 {
// userName截取前缀,重新获取部分数据
prefixes := make(map[string]struct{})
if dataType == "csv" {
for _, v := range data.([]map[string]string) {
userName := v["userName"]
prefix := userName[:len(userName)-4]
prefixes[prefix] = struct{}{}
}
}
if dataType == "txt" {
for _, v := range data.([][]string) {
userName := v[0]
prefix := userName[:len(userName)-4]
prefixes[prefix] = struct{}{}
}
}
// 根据前缀重新加载插入
var num int64 = 0
for prefix := range prefixes {
// keys volte:4600001000004*
arr := r.dataByRedis(prefix+"*", neId)
if len(arr) > 0 {
r.voipAuthRepository.DeletePrefixByUserName(prefix, neId)
num += r.voipAuthRepository.Inserts(arr)
}
}
return num
}
// Delete 删除单个不重新加载
func (r *VoIPAuthService) Delete(neId, userName string) int64 {
return r.voipAuthRepository.Delete(userName, neId)
}
// LoadData 重新加载从userName开始num的数据
func (r *VoIPAuthService) LoadData(neId, userName, num string) {
startUserName, _ := strconv.ParseInt(userName, 10, 64)
subNum, _ := strconv.ParseInt(num, 10, 64)
var i int64
for i = 0; i < subNum; i++ {
keyUserName := fmt.Sprintf("%d", startUserName+i)
// 删除原数据
r.voipAuthRepository.Delete(keyUserName, neId)
arr := r.dataByRedis(keyUserName, neId)
if len(arr) < 1 {
continue
}
r.voipAuthRepository.Inserts(arr)
}
}
// ParseCommandParams 解析数据组成命令参数 msisdn=xx,xx=xx,...
func (r *VoIPAuthService) ParseCommandParams(item model.VoIPAuth) string {
var conditions []string
if item.Password != "" {
conditions = append(conditions, fmt.Sprintf("password=%s", item.Password))
}
return strings.Join(conditions, ",")
}