package service import ( "fmt" "strconv" "strings" "be.ems/features/ue/model" "be.ems/features/ue/repository" "be.ems/src/framework/database/redis" neService "be.ems/src/modules/network_element/service" ) // 实例化服务层 VoIPAuthService 结构体 var NewVoIPAuthService = &VoIPAuthService{ voipAuthRepository: repository.NewVoIPAuthRepository, } // VoLTE用户信息 服务层处理 type VoIPAuthService struct { voipAuthRepository *repository.VoIPAuthRepository // VoLTE用户信息数据信息 } // dataByRedis VoIP鉴权数据 db:0 中 voip:* 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 { userNames := make(map[string]struct{}) if dataType == "csv" { for _, v := range data.([]map[string]string) { userName := v["userName"] // if len(userName) < 6 { // continue // } // prefix := userName[:len(userName)-4] userNames[userName] = struct{}{} } } if dataType == "txt" { for _, v := range data.([][]string) { userName := v[0] // if len(userName) < 6 { // continue // } // prefix := userName[:len(userName)-4] userNames[userName] = struct{}{} } } // 根据前缀重新加载插入 var num int64 = 0 for userName := range userNames { // keys voip:11111 arr := r.dataByRedis(userName, neId) if len(arr) > 0 { r.voipAuthRepository.DeleteByUserName(userName, 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, ",") } // ResetDataWithResult 重置鉴权用户数据,清空数据库重新同步Redis数据 // 通过 channel 返回 ClearAndInsert 的执行结果 func (r *VoIPAuthService) ResetDataWithResult(neId string) chan int64 { arr := r.dataByRedis("*", neId) resultCh := make(chan int64, 1) go func() { resultCh <- r.voipAuthRepository.ClearAndInsert(neId, arr) }() return resultCh }