feat: support ims user, voip auth data and backup UE data
This commit is contained in:
268
features/ue/repository/voip_auth.go
Normal file
268
features/ue/repository/voip_auth.go
Normal file
@@ -0,0 +1,268 @@
|
||||
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/features/ue/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 VoIPAuthRepository 结构体
|
||||
var NewVoIPAuthRepository = &VoIPAuthRepository{
|
||||
selectSql: `select
|
||||
s.id, s.ne_id, s.user_name, s.password,
|
||||
t.tenant_id, t.tenant_name
|
||||
from u_voip_auth s
|
||||
left join sys_tenant t on t.tenant_id = s.tenant_id and t.status = 1`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_id": "NeId",
|
||||
"user_name": "UserName",
|
||||
"password": "Password",
|
||||
|
||||
|
||||
"tenant_id": "TenantID",
|
||||
"tenant_name": "TenantName", // Tenant name for multi-tenancy
|
||||
},
|
||||
}
|
||||
|
||||
// VoIPAuthRepository UDM签约信息表 数据层处理
|
||||
type VoIPAuthRepository struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *VoIPAuthRepository) convertResultRows(rows []map[string]any) []model.VoIPAuth {
|
||||
arr := make([]model.VoIPAuth, 0)
|
||||
for _, row := range rows {
|
||||
item := model.VoIPAuth{}
|
||||
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 *VoIPAuthRepository) ClearAndInsert(neId string, u []model.VoIPAuth) int64 {
|
||||
// 不指定neID时,用 TRUNCATE 清空表快
|
||||
// _, err := datasource.ExecDB("", "TRUNCATE TABLE u_voip_auth", nil)
|
||||
_, err := datasource.ExecDB("", "DELETE FROM u_voip_auth WHERE ne_id = ?", []any{neId})
|
||||
if err != nil {
|
||||
logger.Errorf("TRUNCATE err => %v", err)
|
||||
}
|
||||
|
||||
return r.Inserts(u)
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
func (r *VoIPAuthRepository) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["userName"]; ok && v != "" {
|
||||
conditions = append(conditions, "user_name like concat(concat('%', ?), '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["userNames"]; ok && v != "" {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(v.([]any)))
|
||||
conditions = append(conditions, fmt.Sprintf("user_name in (%s)", placeholder))
|
||||
for _, v := range v.([]any) {
|
||||
params = append(params, v.(string))
|
||||
}
|
||||
}
|
||||
|
||||
// for multi-tenancy solution
|
||||
if v, ok := query["tenantName"]; ok && v != "" {
|
||||
var tenantID []string
|
||||
err := dborm.DefaultDB().Table("sys_tenant").
|
||||
Where("tenant_name=? and status=1", 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)
|
||||
query["neId"] = ""
|
||||
}
|
||||
}
|
||||
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.VoIPAuth{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from u_voip_auth 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 *VoIPAuthRepository) SelectList(u model.VoIPAuth) []model.VoIPAuth {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if u.UserName != "" {
|
||||
conditions = append(conditions, "user_name = ?")
|
||||
params = append(params, u.UserName)
|
||||
}
|
||||
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 user_name asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByUserNameAndNeID 根据用户名和网元ID查询
|
||||
func (r *VoIPAuthRepository) SelectByUserNameAndNeID(userName, neId string) model.VoIPAuth {
|
||||
querySql := r.selectSql + " where user_name = ? and ne_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{userName, neId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.VoIPAuth{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.VoIPAuth{}
|
||||
}
|
||||
|
||||
// Insert 批量添加
|
||||
func (r *VoIPAuthRepository) Inserts(uArr []model.VoIPAuth) 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 *VoIPAuthRepository) Delete(userName, neId string) int64 {
|
||||
tx := datasource.DefaultDB().Where("user_name = ? and ne_id = ?", userName, neId).Delete(&VoIPAuthRepository{})
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("Delete err => %v", err)
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeletePrefixByUserName 删除以指定前缀的用户名
|
||||
func (r *VoIPAuthRepository) DeletePrefixByUserName(userNamePrefix, neId string) int64 {
|
||||
tx := datasource.DefaultDB().Where("user_name like concat(?, '%') and ne_id = ?", userNamePrefix, neId).Delete(&VoIPAuthRepository{})
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("DeletePrefixByUserName err => %v", err)
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
func (r *VoIPAuthRepository) SetTenantID(subArr *[]model.VoIPAuth) {
|
||||
for s := 0; s < len(*subArr); s++ {
|
||||
var tenantID []string
|
||||
err := dborm.DefaultDB().Table("sys_tenant").
|
||||
Where("status='1' and tenancy_type='MSISDN' and ? like tenancy_key", (*subArr)[s].UserName).
|
||||
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]
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user