feat: 网元_参数配置可用属性值缓存并支持数据修改的功能接口

This commit is contained in:
TsMask
2024-07-23 11:56:30 +08:00
parent eccc563f2d
commit fb4c6b483d
5 changed files with 211 additions and 73 deletions

View File

@@ -60,7 +60,7 @@ func (s *NeConfigController) Info(c *gin.Context) {
}
// 将字符串转json数据
if err := json.Unmarshal([]byte(data.ParamJSONStr), &data.ParamData); err != nil {
if err := json.Unmarshal([]byte(data.ParamJson), &data.ParamData); err != nil {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
@@ -84,7 +84,7 @@ func (s *NeConfigController) Add(c *gin.Context) {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
body.ParamJSONStr = string(paramDataByte)
body.ParamJson = string(paramDataByte)
insertId := s.neConfigService.Insert(body)
if insertId != "" {
@@ -120,7 +120,7 @@ func (s *NeConfigController) Edit(c *gin.Context) {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
body.ParamJSONStr = string(paramDataByte)
body.ParamJson = string(paramDataByte)
rows := s.neConfigService.Update(body)
if rows > 0 {
@@ -132,16 +132,16 @@ func (s *NeConfigController) Edit(c *gin.Context) {
// 网元参数配置可用属性值删除
//
// DELETE /:ids
// DELETE /
func (s *NeConfigController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
ids := c.Param("ids")
if ids == "" {
id, okId := c.GetQuery("id")
if id == "" || !okId {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
idsArr := strings.Split(ids, ",")
idsArr := strings.Split(id, ",")
uniqueIDs := parse.RemoveDuplicates(idsArr)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
@@ -173,26 +173,26 @@ func (s *NeConfigController) ListByNeType(c *gin.Context) {
// 网元参数配置数据信息
//
// GET /data
func (s *NeConfigController) Data(c *gin.Context) {
func (s *NeConfigController) DataInfo(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
var query struct {
NeType string `form:"neType" binding:"required"` // 网元类型
NeId string `form:"neId" binding:"required"` // 网元ID
TopTag string `form:"topTag" binding:"required"` // 可用属性
ParamName string `form:"paramName" binding:"required"` // 可用属性
}
if err := c.ShouldBindQuery(&querys); err != nil {
if err := c.ShouldBindQuery(&query); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeId)
if neInfo.NeId != querys.NeId || neInfo.IP == "" {
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(query.NeType, query.NeId)
if neInfo.NeId != query.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元直连
resData, err := neFetchlink.NeConfigInfo(neInfo, querys.TopTag)
resData, err := neFetchlink.NeConfigInfo(neInfo, query.ParamName)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
@@ -200,3 +200,118 @@ func (s *NeConfigController) Data(c *gin.Context) {
c.JSON(200, result.Ok(resData))
}
// 网元参数配置数据修改
//
// PUT /data
func (s *NeConfigController) DataEdit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
NeType string `json:"neType" binding:"required"` // 网元类型
NeId string `json:"neId" binding:"required"` // 网元ID
ParamName string `json:"paramName" binding:"required"`
ParamData map[string]any `json:"paramData" binding:"required"`
Loc string `json:"loc"` // 仅array使用与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元直连
resData, err := neFetchlink.NeConfigUpdate(neInfo, body.ParamName, body.Loc, body.ParamData)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.OkData(resData))
}
// 网元参数配置数据新增array
//
// POST /data
func (s *NeConfigController) DataAdd(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
NeType string `json:"neType" binding:"required"` // 网元类型
NeId string `json:"neId" binding:"required"` // 网元ID
ParamName string `json:"paramName" binding:"required"` // 根据配置可选值
ParamData map[string]any `json:"paramData" binding:"required"` // 数据对象
Loc string `json:"loc" binding:"required"` // 与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否array
info := s.neConfigService.SelectNeConfigByNeTypeAndParamName(body.NeType, body.ParamName)
if info.ParamType != "array" {
c.JSON(400, result.CodeMsg(400, "this attribute does not support adding"))
return
}
// 必须含有index
_, idxOk := body.ParamData["index"]
if info.ParamType == "array" && !idxOk {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元直连
resData, err := neFetchlink.NeConfigInstall(neInfo, body.ParamName, body.Loc, body.ParamData)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.OkData(resData))
}
// 网元参数配置数据删除array
//
// DELETE /data
func (s *NeConfigController) DataRemove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var query struct {
NeType string `form:"neType" binding:"required"` // 网元类型
NeId string `form:"neId" binding:"required"` // 网元ID
ParamName string `form:"paramName" binding:"required"`
Loc string `form:"loc" binding:"required"` // 与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
}
if err := c.ShouldBindQuery(&query); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否array
info := s.neConfigService.SelectNeConfigByNeTypeAndParamName(query.NeType, query.ParamName)
if info.ParamType != "array" {
c.JSON(400, result.CodeMsg(400, "this attribute does not support adding"))
return
}
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(query.NeType, query.NeId)
if neInfo.NeId != query.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元直连
resData, err := neFetchlink.NeConfigDelete(neInfo, query.ParamName, query.Loc)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.OkData(resData))
}

View File

@@ -1,21 +1,23 @@
package model
// NeConfig 网元参数配置可用属性值
// NeConfig 网元_参数配置可用属性值
type NeConfig struct {
ID string `json:"id" gorm:"id"`
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
NeType string `json:"neType" binding:"required" gorm:"ne_type"` // 网元类型
NeId string `json:"-" gorm:"ne_id"`
TopTag string `json:"topTag" binding:"required" gorm:"top_tag"`
TopDisplay string `json:"topDisplay" binding:"required" gorm:"top_display"`
Method string `json:"method" gorm:"method"` // 操作属性 get只读强制不可编辑删除 put可编辑 delete可删除 post可新增
ParamJSONStr string `json:"-" gorm:"param_json"` // accesss属性控制只读read-only/read/ro 读写read-write
ParamName string `json:"paramName" binding:"required" gorm:"param_name"` // 参数名
ParamDisplay string `json:"paramDisplay" binding:"required" gorm:"param_display"` // 参数显示名
ParamType string `json:"paramType" gorm:"param_type"` // 参数类型 list列表单层 array数组多层
ParamJson string `json:"-" gorm:"param_json"` // accesss属性控制只读read-only/read/ro 读写read-write
ParamSort int64 `json:"paramSort" gorm:"param_sort"` // 参数排序
ParamPerms string `json:"paramPerms" gorm:"param_perms"` // 操作权限 get只读 put可编辑 delete可删除 post可新增
UpdateTime int64 `json:"updateTime" gorm:"update_time"` // 更新时间
// ====== 非数据库字段属性 ======
ParamData map[string]any `json:"paramData,omitempty" binding:"required" gorm:"-"` // 与ParamJSONStr配合转换
ParamData []map[string]any `json:"paramData,omitempty" binding:"required" gorm:"-"` // 与ParamJSONStr配合转换
}
// TableName 表名称
func (*NeConfig) TableName() string {
return "param_config"
return "ne_config"
}

View File

@@ -2,6 +2,7 @@ package repository
import (
"strings"
"time"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
@@ -12,18 +13,18 @@ import (
// NewNeConfigImpl 网元参数配置可用属性值 实例化数据层
var NewNeConfigImpl = &NeConfigImpl{
selectSql: `select
id, ne_type, ne_id, top_tag, top_display, method, param_json
from param_config`,
selectSql: `select id, ne_type, param_name, param_display, param_type, param_json, param_sort, param_perms, update_time from ne_config`,
resultMap: map[string]string{
"id": "ID",
"ne_type": "NeType",
"ne_id": "NeId",
"top_tag": "TopTag",
"top_display": "TopDisplay",
"method": "Method",
"param_json": "ParamJSONStr",
"param_name": "ParamName",
"param_display": "ParamDisplay",
"param_type": "ParamType",
"param_json": "ParamJson",
"param_sort": "ParamSort",
"param_perms": "ParamPerms",
"update_time": "UpdateTime",
},
}
@@ -59,8 +60,8 @@ func (r *NeConfigImpl) SelectPage(query map[string]any) map[string]any {
conditions = append(conditions, "ne_type = ?")
params = append(params, v)
}
if v, ok := query["topTag"]; ok && v != "" {
conditions = append(conditions, "top_tag = ?")
if v, ok := query["paramName"]; ok && v != "" {
conditions = append(conditions, "param_name = ?")
params = append(params, v)
}
@@ -76,7 +77,7 @@ func (r *NeConfigImpl) SelectPage(query map[string]any) map[string]any {
}
// 查询数量 长度为0直接返回
totalSql := "select count(id) as 'total' from param_config"
totalSql := "select count(id) as 'total' from ne_config"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
@@ -117,9 +118,9 @@ func (r *NeConfigImpl) SelectList(param model.NeConfig) []model.NeConfig {
conditions = append(conditions, "ne_type = ?")
params = append(params, param.NeType)
}
if param.TopTag != "" {
conditions = append(conditions, "top_tag = ?")
params = append(params, param.TopTag)
if param.ParamName != "" {
conditions = append(conditions, "param_name = ?")
params = append(params, param.ParamName)
}
// 构建查询条件语句
@@ -129,7 +130,7 @@ func (r *NeConfigImpl) SelectList(param model.NeConfig) []model.NeConfig {
}
// 查询数据
querySql := r.selectSql + whereSql + " order by id asc "
querySql := r.selectSql + whereSql + " order by param_sort asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
@@ -160,25 +161,27 @@ func (r *NeConfigImpl) Insert(param model.NeConfig) string {
if param.NeType != "" {
params["ne_type"] = param.NeType
}
if param.NeId != "" {
params["ne_id"] = param.NeId
if param.ParamName != "" {
params["param_name"] = param.ParamName
}
if param.TopTag != "" {
params["top_tag"] = param.TopTag
if param.ParamDisplay != "" {
params["param_display"] = param.ParamDisplay
}
if param.TopDisplay != "" {
params["top_display"] = param.TopDisplay
if param.ParamType != "" {
params["param_type"] = param.ParamType
}
if param.Method != "" {
params["method"] = param.Method
if param.ParamJson != "" {
params["param_json"] = param.ParamJson
}
if param.ParamJSONStr != "" {
params["param_json"] = param.ParamJSONStr
params["param_sort"] = param.ParamSort
if param.ParamPerms != "" {
params["param_perms"] = param.ParamPerms
}
params["update_time"] = time.Now().UnixMilli()
// 构建执行语句
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
sql := "insert into param_config (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
sql := "insert into ne_config (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
db := datasource.DefaultDB()
// 开启事务
@@ -210,25 +213,27 @@ func (r *NeConfigImpl) Update(param model.NeConfig) int64 {
if param.NeType != "" {
params["ne_type"] = param.NeType
}
if param.NeId != "" {
params["ne_id"] = param.NeId
if param.ParamName != "" {
params["param_name"] = param.ParamName
}
if param.TopTag != "" {
params["top_tag"] = param.TopTag
if param.ParamDisplay != "" {
params["param_display"] = param.ParamDisplay
}
if param.TopDisplay != "" {
params["top_display"] = param.TopDisplay
if param.ParamType != "" {
params["param_type"] = param.ParamType
}
if param.Method != "" {
params["method"] = param.Method
if param.ParamJson != "" {
params["param_json"] = param.ParamJson
}
if param.ParamJSONStr != "" {
params["param_json"] = param.ParamJSONStr
params["param_sort"] = param.ParamSort
if param.ParamPerms != "" {
params["param_perms"] = param.ParamPerms
}
params["update_time"] = time.Now().UnixMilli()
// 构建执行语句
keys, values := repo.KeyValueByUpdate(params)
sql := "update param_config set " + strings.Join(keys, ",") + " where id = ?"
sql := "update ne_config set " + strings.Join(keys, ",") + " where id = ?"
// 执行更新
values = append(values, param.ID)
@@ -243,7 +248,7 @@ func (r *NeConfigImpl) Update(param model.NeConfig) int64 {
// DeleteByIds 批量删除信息
func (r *NeConfigImpl) DeleteByIds(ids []string) int64 {
placeholder := repo.KeyPlaceholderByQuery(len(ids))
sql := "delete from param_config where id in (" + placeholder + ")"
sql := "delete from ne_config where id in (" + placeholder + ")"
parameters := repo.ConvertIdsSlice(ids)
results, err := datasource.ExecDB("", sql, parameters)
if err != nil {

View File

@@ -13,6 +13,9 @@ type INeConfig interface {
// SelectNeConfigByNeType 查询网元类型参数配置
SelectNeConfigByNeType(neType string) []model.NeConfig
// SelectNeConfigByNeTypeAndParamName 查询网元类型参数配置By参数名
SelectNeConfigByNeTypeAndParamName(neType, paramName string) model.NeConfig
// SelectNeHostPage 分页查询列表数据
SelectPage(query map[string]any) map[string]any

View File

@@ -37,11 +37,11 @@ func (r *NeConfigImpl) RefreshByNeTypeAndNeID(neType string) []model.NeConfig {
}
}
for k, v := range neConfigGroup {
key := fmt.Sprintf("%sparam_config:%s", cachekey.NE_DATA_KEY, strings.ToUpper(k))
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.ParamJSONStr), &item.ParamData); err != nil {
if err := json.Unmarshal([]byte(item.ParamJson), &item.ParamData); err != nil {
continue
}
v[i] = item
@@ -54,14 +54,14 @@ func (r *NeConfigImpl) RefreshByNeTypeAndNeID(neType string) []model.NeConfig {
return neConfigList
}
// 单个
key := fmt.Sprintf("%sparam_config:%s", cachekey.NE_DATA_KEY, strings.ToUpper(neType))
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.ParamJSONStr), &v.ParamData); err != nil {
if err := json.Unmarshal([]byte(v.ParamJson), &v.ParamData); err != nil {
continue
}
neConfigList[i] = v
@@ -74,9 +74,9 @@ func (r *NeConfigImpl) RefreshByNeTypeAndNeID(neType string) []model.NeConfig {
// ClearNeCacheByNeType 清除网元类型参数配置缓存
func (r *NeConfigImpl) ClearNeCacheByNeType(neType string) bool {
key := fmt.Sprintf("%sparam_config:%s", cachekey.NE_DATA_KEY, neType)
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, neType)
if neType == "*" {
key = fmt.Sprintf("%sparam_config:*", cachekey.NE_DATA_KEY)
key = fmt.Sprintf("%sNeConfig:*", cachekey.NE_DATA_KEY)
}
keys, err := redis.GetKeys("", key)
if err != nil {
@@ -89,7 +89,7 @@ func (r *NeConfigImpl) ClearNeCacheByNeType(neType string) bool {
// SelectNeConfigByNeType 查询网元类型参数配置
func (r *NeConfigImpl) SelectNeConfigByNeType(neType string) []model.NeConfig {
var neConfigList []model.NeConfig
key := fmt.Sprintf("%sparam_config:%s", cachekey.NE_DATA_KEY, strings.ToUpper(neType))
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)
@@ -102,6 +102,19 @@ func (r *NeConfigImpl) SelectNeConfigByNeType(neType string) []model.NeConfig {
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)