Files
be.ems/src/modules/network_element/repository/ne_config.go
TsMask 8cd86a62d6 feat: 网元参数配置数据支持版本区分功能
feat(database): add ne_version and schema columns to ne_info table

feat(database): update ne_info table structure in upgrade script

fix(ne_config): handle id as number in Info and Add methods

fix(ne_config): validate neId and fetch neInfo in ListByNeType and DataAdd methods

fix(ne_info): extract version from ServerState in Add and Edit methods

refactor(ne_config): update NeConfig model to include NeVersion

refactor(ne_info): add NeVersion and Schema fields to NeInfo model

test(ne_config): enhance test cases to include version parsing and saving

refactor(ne_config): improve cache handling and querying by NeType and NeVersion

fix(ne_info): update NeInfo version during status change
2025-10-17 10:17:07 +08:00

127 lines
3.1 KiB
Go

package repository
import (
"time"
"be.ems/src/framework/database/db"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/modules/network_element/model"
)
// 实例化数据层 NeConfig 结构体
var NewNeConfig = &NeConfig{}
// NeConfig 网元参数配置可用属性值 数据层处理
type NeConfig struct{}
// SelectByPage 分页查询集合
func (r NeConfig) SelectByPage(query map[string]string) ([]model.NeConfig, int64) {
tx := datasource.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 := datasource.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 []string) []model.NeConfig {
rows := []model.NeConfig{}
if len(ids) <= 0 {
return rows
}
tx := datasource.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 := datasource.DB("").Create(&param).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 := datasource.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 []string) int64 {
if len(ids) <= 0 {
return 0
}
tx := datasource.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
}