165 lines
4.5 KiB
Go
165 lines
4.5 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"be.ems/src/framework/constants/cachekey"
|
|
"be.ems/src/framework/redis"
|
|
"be.ems/src/modules/network_element/model"
|
|
"be.ems/src/modules/network_element/repository"
|
|
)
|
|
|
|
// NewNeConfigImpl 网元参数配置可用属性值 实例化服务层
|
|
var NewNeConfigImpl = &NeConfigImpl{
|
|
neConfigRepository: repository.NewNeConfigImpl,
|
|
}
|
|
|
|
// NeConfigImpl 网元参数配置可用属性值 服务层处理
|
|
type NeConfigImpl struct {
|
|
// 网元参数配置可用属性值表
|
|
neConfigRepository repository.INeConfig
|
|
}
|
|
|
|
// RefreshByNeType 通过ne_type刷新redis中的缓存
|
|
func (r *NeConfigImpl) RefreshByNeTypeAndNeID(neType string) []model.NeConfig {
|
|
// 多个
|
|
if neType == "" || neType == "*" {
|
|
neConfigList := r.neConfigRepository.SelectList(model.NeConfig{})
|
|
if len(neConfigList) > 0 {
|
|
neConfigGroup := map[string][]model.NeConfig{}
|
|
for _, v := range neConfigList {
|
|
if item, ok := neConfigGroup[v.NeType]; ok {
|
|
neConfigGroup[v.NeType] = append(item, v)
|
|
} else {
|
|
neConfigGroup[v.NeType] = []model.NeConfig{v}
|
|
}
|
|
}
|
|
for k, v := range neConfigGroup {
|
|
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, strings.ToUpper(k))
|
|
redis.Del("", key)
|
|
if len(v) > 0 {
|
|
for i, item := range v {
|
|
if err := json.Unmarshal([]byte(item.ParamJson), &item.ParamData); err != nil {
|
|
continue
|
|
}
|
|
v[i] = item
|
|
}
|
|
values, _ := json.Marshal(v)
|
|
redis.Set("", key, string(values))
|
|
}
|
|
}
|
|
}
|
|
return neConfigList
|
|
}
|
|
// 单个
|
|
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, strings.ToUpper(neType))
|
|
redis.Del("", key)
|
|
neConfigList := r.neConfigRepository.SelectList(model.NeConfig{
|
|
NeType: neType,
|
|
})
|
|
if len(neConfigList) > 0 {
|
|
for i, v := range neConfigList {
|
|
if err := json.Unmarshal([]byte(v.ParamJson), &v.ParamData); err != nil {
|
|
continue
|
|
}
|
|
neConfigList[i] = v
|
|
}
|
|
values, _ := json.Marshal(neConfigList)
|
|
redis.Set("", key, string(values))
|
|
}
|
|
return neConfigList
|
|
}
|
|
|
|
// ClearNeCacheByNeType 清除网元类型参数配置缓存
|
|
func (r *NeConfigImpl) ClearNeCacheByNeType(neType string) bool {
|
|
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, neType)
|
|
if neType == "*" {
|
|
key = fmt.Sprintf("%sNeConfig:*", cachekey.NE_DATA_KEY)
|
|
}
|
|
keys, err := redis.GetKeys("", key)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
delOk, _ := redis.DelKeys("", keys)
|
|
return delOk
|
|
}
|
|
|
|
// SelectNeConfigByNeType 查询网元类型参数配置
|
|
func (r *NeConfigImpl) SelectNeConfigByNeType(neType string) []model.NeConfig {
|
|
var neConfigList []model.NeConfig
|
|
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, strings.ToUpper(neType))
|
|
jsonStr, _ := redis.Get("", key)
|
|
if len(jsonStr) > 7 {
|
|
err := json.Unmarshal([]byte(jsonStr), &neConfigList)
|
|
if err != nil {
|
|
neConfigList = []model.NeConfig{}
|
|
}
|
|
} else {
|
|
neConfigList = r.RefreshByNeTypeAndNeID(neType)
|
|
}
|
|
return neConfigList
|
|
}
|
|
|
|
// SelectNeConfigByNeTypeAndParamName 查询网元类型参数配置By参数名
|
|
func (r *NeConfigImpl) SelectNeConfigByNeTypeAndParamName(neType, paramName string) model.NeConfig {
|
|
neConfigList := r.SelectNeConfigByNeType(neType)
|
|
var neConfig model.NeConfig
|
|
for _, v := range neConfigList {
|
|
if v.ParamName == paramName {
|
|
neConfig = v
|
|
break
|
|
}
|
|
}
|
|
return neConfig
|
|
}
|
|
|
|
// SelectNeHostPage 分页查询列表数据
|
|
func (r *NeConfigImpl) SelectPage(query map[string]any) map[string]any {
|
|
return r.neConfigRepository.SelectPage(query)
|
|
}
|
|
|
|
// SelectConfigList 查询列表
|
|
func (r *NeConfigImpl) SelectList(param model.NeConfig) []model.NeConfig {
|
|
return r.neConfigRepository.SelectList(param)
|
|
}
|
|
|
|
// SelectByIds 通过ID查询
|
|
func (r *NeConfigImpl) SelectById(id string) model.NeConfig {
|
|
if id == "" {
|
|
return model.NeConfig{}
|
|
}
|
|
neHosts := r.neConfigRepository.SelectByIds([]string{id})
|
|
if len(neHosts) > 0 {
|
|
return neHosts[0]
|
|
}
|
|
return model.NeConfig{}
|
|
}
|
|
|
|
// Insert 新增信息
|
|
func (r *NeConfigImpl) Insert(param model.NeConfig) string {
|
|
return r.neConfigRepository.Insert(param)
|
|
}
|
|
|
|
// Update 修改信息
|
|
func (r *NeConfigImpl) Update(param model.NeConfig) int64 {
|
|
return r.neConfigRepository.Update(param)
|
|
}
|
|
|
|
// DeleteByIds 批量删除信息
|
|
func (r *NeConfigImpl) DeleteByIds(ids []string) (int64, error) {
|
|
// 检查是否存在
|
|
data := r.neConfigRepository.SelectByIds(ids)
|
|
if len(data) <= 0 {
|
|
return 0, fmt.Errorf("param.noData")
|
|
}
|
|
|
|
if len(data) == len(ids) {
|
|
rows := r.neConfigRepository.DeleteByIds(ids)
|
|
return rows, nil
|
|
}
|
|
// 删除信息失败!
|
|
return 0, fmt.Errorf("delete fail")
|
|
}
|