- 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.
126 lines
3.0 KiB
Go
126 lines
3.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"time"
|
|
|
|
"be.ems/src/framework/database/db"
|
|
"be.ems/src/framework/logger"
|
|
"be.ems/src/modules/ne/model"
|
|
)
|
|
|
|
// 实例化数据层 NeConfig 结构体
|
|
var NewNeConfig = &NeConfig{}
|
|
|
|
// NeConfig 网元参数配置可用属性值 数据层处理
|
|
type NeConfig struct{}
|
|
|
|
// SelectByPage 分页查询集合
|
|
func (r NeConfig) SelectByPage(query map[string]string) ([]model.NeConfig, int64) {
|
|
tx := db.DB("").Model(&model.NeConfig{})
|
|
// 查询条件拼接
|
|
if v, ok := query["neType"]; ok && v != "" {
|
|
tx = tx.Where("ne_type = ?", v)
|
|
}
|
|
if v, ok := query["paramName"]; ok && v != "" {
|
|
tx = tx.Where("param_name = ?", v)
|
|
}
|
|
|
|
// 查询结果
|
|
var total int64 = 0
|
|
rows := []model.NeConfig{}
|
|
|
|
// 查询数量为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.Find(&rows).Error
|
|
if err != nil {
|
|
logger.Errorf("query find err => %v", err.Error())
|
|
return rows, total
|
|
}
|
|
return rows, total
|
|
}
|
|
|
|
// Select 查询集合
|
|
func (r NeConfig) Select(param model.NeConfig) []model.NeConfig {
|
|
tx := db.DB("").Model(&model.NeConfig{})
|
|
// 查询条件拼接
|
|
if param.NeType != "" {
|
|
tx = tx.Where("ne_type = ?", param.NeType)
|
|
}
|
|
if param.ParamName != "" {
|
|
tx = tx.Where("param_name = ?", param.ParamName)
|
|
}
|
|
|
|
// 查询数据
|
|
rows := []model.NeConfig{}
|
|
if err := tx.Order("param_sort asc").Find(&rows).Error; err != nil {
|
|
logger.Errorf("query find err => %v", err.Error())
|
|
return rows
|
|
}
|
|
return rows
|
|
}
|
|
|
|
// SelectByIds 通过ID查询
|
|
func (r NeConfig) SelectByIds(ids []int64) []model.NeConfig {
|
|
rows := []model.NeConfig{}
|
|
if len(ids) <= 0 {
|
|
return rows
|
|
}
|
|
tx := db.DB("").Model(&model.NeConfig{})
|
|
// 构建查询条件
|
|
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 NeConfig) Insert(param model.NeConfig) int64 {
|
|
param.UpdateTime = time.Now().UnixMilli()
|
|
// 执行插入
|
|
if err := db.DB("").Create(¶m).Error; err != nil {
|
|
logger.Errorf("insert err => %v", err.Error())
|
|
return 0
|
|
}
|
|
return param.ID
|
|
}
|
|
|
|
// Update 修改信息
|
|
func (r NeConfig) Update(param model.NeConfig) int64 {
|
|
if param.ID == 0 {
|
|
return 0
|
|
}
|
|
param.UpdateTime = time.Now().UnixMilli()
|
|
tx := db.DB("").Model(&model.NeConfig{})
|
|
// 构建查询条件
|
|
tx = tx.Where("id = ?", param.ID)
|
|
tx = tx.Omit("id")
|
|
// 执行更新
|
|
if err := tx.Updates(param).Error; err != nil {
|
|
logger.Errorf("update err => %v", err.Error())
|
|
return 0
|
|
}
|
|
return tx.RowsAffected
|
|
}
|
|
|
|
// DeleteByIds 批量删除信息
|
|
func (r NeConfig) DeleteByIds(ids []int64) int64 {
|
|
if len(ids) <= 0 {
|
|
return 0
|
|
}
|
|
tx := db.DB("").Where("id in ?", ids)
|
|
if err := tx.Delete(&model.NeConfig{}).Error; err != nil {
|
|
logger.Errorf("delete err => %v", err.Error())
|
|
return 0
|
|
}
|
|
return tx.RowsAffected
|
|
}
|