Files
be.ems/src/modules/ne/repository/ne_info.go
TsMask 45e226ce1a Refactor API tags in swagger.yaml to use shortened prefixes
- Updated tags from 'network_data' to 'ne_data' for consistency and brevity.
- Changed 'network_element' to 'ne' across various endpoints for improved readability.
- Adjusted related descriptions in the tags section to reflect the new naming conventions.
2025-08-21 14:30:09 +08:00

241 lines
5.4 KiB
Go

package repository
import (
"fmt"
"time"
"be.ems/src/framework/database/db"
"be.ems/src/framework/logger"
"be.ems/src/modules/ne/model"
)
// 实例化数据层 NeInfo 结构体
var NewNeInfo = &NeInfo{}
// NeInfo 网元信息表 数据层处理
type NeInfo struct{}
// neListSort 网元列表预设排序
func (r NeInfo) neListSort(arr []model.NeInfo) []model.NeInfo {
// neListSort 网元列表预设排序
var list = []string{
"OMC",
"IMS",
"AMF",
"AUSF",
"UDR",
"UDM",
"SMF",
"PCF",
"NSSF",
"NRF",
"UPF",
"LMF",
"NEF",
"MME",
"N3IWF",
"MOCNGW",
"SMSC",
"SMSF",
"CBC",
"CHF",
"HLR",
"SGWC",
"IP-SM-GW",
"MMTel-AS",
"I-CSCF",
"P-CSCF",
"S-CSCF",
}
// 创建优先级映射
priority := make(map[string]int)
for i, v := range list {
priority[v] = i
}
// 冒泡排序
n := len(arr)
for i := 0; i < n-1; i++ {
for j := 0; j < n-i-1; j++ {
if priority[arr[j].NeType] > priority[arr[j+1].NeType] {
// 交换元素
arr[j], arr[j+1] = arr[j+1], arr[j]
}
}
}
return arr
}
// SelectByPage 分页查询集合
func (r NeInfo) SelectByPage(query map[string]string) ([]model.NeInfo, int64) {
tx := db.DB("").Model(&model.NeInfo{})
// 查询条件拼接
if v, ok := query["neType"]; ok && v != "" {
tx = tx.Where("ne_type = ?", v)
}
if v, ok := query["neId"]; ok && v != "" {
tx = tx.Where("ne_id = ?", v)
}
if v, ok := query["rmUid"]; ok && v != "" {
tx = tx.Where("rmUid like ?", fmt.Sprintf("%s%%", v))
}
// 查询结果
var total int64 = 0
rows := []model.NeInfo{}
// 查询数量为0直接返回
if err := tx.Count(&total).Error; err != nil || total <= 0 {
return rows, total
}
// 查询数据分页
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
err := tx.Order("ne_type asc, ne_id asc").Find(&rows).Error
if err != nil {
logger.Errorf("query find err => %v", err.Error())
return rows, total
}
return r.neListSort(rows), total
}
// SelectByIds 通过ID查询
func (r NeInfo) SelectByIds(ids []int64) []model.NeInfo {
rows := []model.NeInfo{}
if len(ids) <= 0 {
return rows
}
tx := db.DB("").Model(&model.NeInfo{})
// 构建查询条件
tx = tx.Where("id in ?", ids)
// 查询数据
if err := tx.Find(&rows).Error; err != nil {
logger.Errorf("query find err => %v", err.Error())
return rows
}
return rows
}
// Insert 新增信息
func (r NeInfo) Insert(neInfo model.NeInfo) int64 {
if neInfo.CreateBy != "" {
ms := time.Now().UnixMilli()
neInfo.CreateTime = ms
neInfo.UpdateTime = ms
neInfo.UpdateBy = neInfo.CreateBy
}
tx := db.DB("").Create(&neInfo)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}
return neInfo.ID
}
// Update 修改信息
func (r NeInfo) Update(neInfo model.NeInfo) int64 {
if neInfo.ID <= 0 {
return 0
}
if neInfo.UpdateBy != "" {
neInfo.UpdateTime = time.Now().UnixMilli()
}
neInfo.UpdateTime = time.Now().UnixMilli()
tx := db.DB("").Model(&model.NeInfo{})
// 构建查询条件
tx = tx.Where("id = ?", neInfo.ID)
tx = tx.Omit("id", "create_by", "create_time")
// 执行更新
if err := tx.Updates(neInfo).Error; err != nil {
logger.Errorf("update err => %v", err.Error())
return 0
}
return tx.RowsAffected
}
// DeleteByIds 批量删除信息
func (r NeInfo) DeleteByIds(ids []int64) int64 {
if len(ids) <= 0 {
return 0
}
tx := db.DB("").Where("id in ?", ids)
if err := tx.Delete(&model.NeInfo{}).Error; err != nil {
logger.Errorf("delete err => %v", err.Error())
return 0
}
return tx.RowsAffected
}
// SelectList 查询列表
func (r NeInfo) SelectList(neInfo model.NeInfo) []model.NeInfo {
tx := db.DB("").Model(&model.NeInfo{})
// 构建查询条件
if neInfo.NeType != "" {
tx = tx.Where("ne_type = ?", neInfo.NeType)
}
if neInfo.NeId != "" {
tx = tx.Where("ne_id = ?", neInfo.NeId)
}
// 查询数据
rows := []model.NeInfo{}
if err := tx.Order("ne_type asc, ne_id asc").Find(&rows).Error; err != nil {
logger.Errorf("query find err => %v", err.Error())
return rows
}
return r.neListSort(rows)
}
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
func (r NeInfo) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
tx := db.DB("").Model(&model.NeInfo{})
// 构建查询条件
tx = tx.Where("ne_type = ? and ne_id = ?", neType, neID)
// 查询数据
row := model.NeInfo{}
if err := tx.Limit(1).Find(&row).Error; err != nil {
logger.Errorf("query find err => %v", err.Error())
return row
}
return row
}
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
func (r NeInfo) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) int64 {
tx := db.DB("").Model(&model.NeInfo{})
// 查询条件拼接
if neInfo.NeType != "" {
tx = tx.Where("ne_type = ?", neInfo.NeType)
}
if neInfo.NeId != "" {
tx = tx.Where("ne_id = ?", neInfo.NeType)
}
// 查询数据
var id int64 = 0
if err := tx.Limit(1).Select("id").Find(&id).Error; err != nil {
logger.Errorf("query find err => %v", err.Error())
return id
}
return id
}
// UpdateState 修改状态
func (r NeInfo) UpdateState(id int64, status int64) int64 {
if id <= 0 {
return 0
}
tx := db.DB("").Model(&model.NeInfo{})
// 构建查询条件
tx = tx.Where("id = ?", id)
tx.Updates(map[string]any{
"status": status,
"update_time": time.Now().UnixMilli(),
})
// 执行更新
if err := tx.Error; err != nil {
logger.Errorf("update err => %v", err.Error())
return 0
}
return tx.RowsAffected
}