From c816b64b310307e8231c8140ff1dd5d7f7d7b0bf Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 25 Jun 2024 17:03:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=86=85=E8=BD=AC=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E7=BD=91=E5=85=83=E5=8F=82=E6=95=B0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/param_config.go | 188 +++++++++++++ .../network_element/fetch_link/ne_config.go | 2 +- .../network_element/model/ne_config.go | 21 ++ .../network_element/model/ne_software.go | 1 - .../network_element/network_element.go | 31 +++ .../network_element/repository/ne_config.go | 24 ++ .../repository/ne_config.impl.go | 254 ++++++++++++++++++ .../network_element/service/ne_config.go | 24 ++ .../network_element/service/ne_config.impl.go | 67 +++++ 9 files changed, 610 insertions(+), 2 deletions(-) create mode 100644 src/modules/network_element/controller/param_config.go create mode 100644 src/modules/network_element/model/ne_config.go create mode 100644 src/modules/network_element/repository/ne_config.go create mode 100644 src/modules/network_element/repository/ne_config.impl.go create mode 100644 src/modules/network_element/service/ne_config.go create mode 100644 src/modules/network_element/service/ne_config.impl.go diff --git a/src/modules/network_element/controller/param_config.go b/src/modules/network_element/controller/param_config.go new file mode 100644 index 00000000..2f0d273e --- /dev/null +++ b/src/modules/network_element/controller/param_config.go @@ -0,0 +1,188 @@ +package controller + +import ( + "encoding/json" + "strings" + + "be.ems/src/framework/i18n" + "be.ems/src/framework/utils/ctx" + "be.ems/src/framework/utils/parse" + "be.ems/src/framework/vo/result" + neFetchlink "be.ems/src/modules/network_element/fetch_link" + "be.ems/src/modules/network_element/model" + neService "be.ems/src/modules/network_element/service" + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" +) + +// NewNeConfig 网元参数配置 实例化控制层 +var NewNeConfig = &NeConfigController{ + neConfigService: neService.NewNeConfigImpl, + neInfoService: neService.NewNeInfoImpl, +} + +// 网元参数配置 +// +// PATH /config +type NeConfigController struct { + // 网元参数配置可用属性值服务 + neConfigService neService.INeConfig + // 网元信息服务 + neInfoService neService.INeInfo +} + +// 网元参数配置可用属性值列表 +// +// GET /list +func (s *NeConfigController) List(c *gin.Context) { + querys := ctx.QueryMap(c) + data := s.neConfigService.SelectPage(querys) + + c.JSON(200, result.Ok(data)) +} + +// 网元参数配置可用属性值信息 +// +// GET /info/:id +func (s *NeConfigController) Info(c *gin.Context) { + language := ctx.AcceptLanguage(c) + id := c.Param("id") + if id == "" { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + + data := s.neConfigService.SelectById(id) + if data.ID != id { + // 没有可访问参数配置数据! + c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfig.noData"))) + return + } + + // 将字符串转json数据 + if err := json.Unmarshal([]byte(data.ParamJSONStr), &data.ParamData); err != nil { + c.JSON(400, result.CodeMsg(400, err.Error())) + return + } + c.JSON(200, result.OkData(data)) +} + +// 网元参数配置可用属性值新增 +// +// POST / +func (s *NeConfigController) Add(c *gin.Context) { + language := ctx.AcceptLanguage(c) + var body model.NeConfig + if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + + // 将json数据转字符串存储 + paramDataByte, err := json.Marshal(body.ParamData) + if err != nil { + c.JSON(400, result.CodeMsg(400, err.Error())) + return + } + body.ParamJSONStr = string(paramDataByte) + + insertId := s.neConfigService.Insert(body) + if insertId != "" { + c.JSON(200, result.Ok(nil)) + return + } + c.JSON(200, result.Err(nil)) +} + +// 网元参数配置可用属性值修改 +// +// PUT / +func (s *NeConfigController) Edit(c *gin.Context) { + language := ctx.AcceptLanguage(c) + var body model.NeConfig + err := c.ShouldBindBodyWith(&body, binding.JSON) + if err != nil || body.ID == "" { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + + // 检查是否存在 + data := s.neConfigService.SelectById(body.ID) + if data.ID != body.ID { + // 没有可访问主机命令数据! + c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfig.noData"))) + return + } + + // 将json数据转字符串存储 + paramDataByte, err := json.Marshal(body.ParamData) + if err != nil { + c.JSON(400, result.CodeMsg(400, err.Error())) + return + } + body.ParamJSONStr = string(paramDataByte) + + rows := s.neConfigService.Update(body) + if rows > 0 { + c.JSON(200, result.Ok(nil)) + return + } + c.JSON(200, result.Err(nil)) +} + +// 网元参数配置可用属性值删除 +// +// DELETE /:ids +func (s *NeConfigController) Remove(c *gin.Context) { + language := ctx.AcceptLanguage(c) + ids := c.Param("ids") + if ids == "" { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + // 处理字符转id数组后去重 + idsArr := strings.Split(ids, ",") + uniqueIDs := parse.RemoveDuplicates(idsArr) + if len(uniqueIDs) <= 0 { + c.JSON(200, result.Err(nil)) + return + } + rows, err := s.neConfigService.DeleteByIds(uniqueIDs) + if err != nil { + c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error()))) + return + } + msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows}) + c.JSON(200, result.OkMsg(msg)) +} + +// 网元参数配置数据信息 +// +// GET /data +func (s *NeConfigController) Data(c *gin.Context) { + language := ctx.AcceptLanguage(c) + var querys struct { + NeType string `form:"neType" binding:"required"` // 网元类型 + NeId string `form:"neId" binding:"required"` // 网元ID + TopTag string `form:"topTag" binding:"required"` // 可用属性 + } + if err := c.ShouldBindQuery(&querys); 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 == "" { + c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo"))) + return + } + + // 网元直连 + resData, err := neFetchlink.NeConfigInfo(neInfo, querys.TopTag) + if err != nil { + c.JSON(200, result.ErrMsg(err.Error())) + return + } + + c.JSON(200, result.Ok(resData)) +} diff --git a/src/modules/network_element/fetch_link/ne_config.go b/src/modules/network_element/fetch_link/ne_config.go index 2b19c44e..cb93a2b2 100644 --- a/src/modules/network_element/fetch_link/ne_config.go +++ b/src/modules/network_element/fetch_link/ne_config.go @@ -55,7 +55,7 @@ func NeConfigInfo(neInfo model.NeInfo, name string) (map[string]any, error) { neUrl := fmt.Sprintf("http://%s:%d/api/rest/systemManagement/v1/elementType/%s/objectType/config/%s", neInfo.IP, neInfo.Port, strings.ToLower(neInfo.NeType), name) resBytes, err := fetch.Get(neUrl, nil, 1000) if err != nil { - logger.Warnf("NeConfigInfo %s", err.Error()) + logger.Warnf("NeConfigInfo %s Get \"%s\"", err.Error(), neUrl) return nil, err } diff --git a/src/modules/network_element/model/ne_config.go b/src/modules/network_element/model/ne_config.go new file mode 100644 index 00000000..dcc7e054 --- /dev/null +++ b/src/modules/network_element/model/ne_config.go @@ -0,0 +1,21 @@ +package model + +// NeConfig 网元参数配置可用属性值 +type NeConfig struct { + ID string `json:"id" gorm:"id"` + 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 + + // ====== 非数据库字段属性 ====== + + ParamData map[string]any `json:"paramData,omitempty" binding:"required" gorm:"-"` // 与ParamJSONStr配合转换 +} + +// TableName 表名称 +func (*NeConfig) TableName() string { + return "param_config" +} diff --git a/src/modules/network_element/model/ne_software.go b/src/modules/network_element/model/ne_software.go index 3eaf9569..53a5321d 100644 --- a/src/modules/network_element/model/ne_software.go +++ b/src/modules/network_element/model/ne_software.go @@ -15,7 +15,6 @@ type NeSoftware struct { // ====== 非数据库字段属性 ====== - NeId string `json:"neId,omitempty" gorm:"-"` // 网元ID } // TableName 表名称 diff --git a/src/modules/network_element/network_element.go b/src/modules/network_element/network_element.go index 43cef165..49dea873 100644 --- a/src/modules/network_element/network_element.go +++ b/src/modules/network_element/network_element.go @@ -257,6 +257,37 @@ func Setup(router *gin.Engine) { ) } + // 网元参数配置 + neConfigGroup := neGroup.Group("/config") + { + neConfigGroup.GET("/list", + middleware.PreAuthorize(nil), + controller.NewNeConfig.List, + ) + neConfigGroup.GET("/info/:id", + middleware.PreAuthorize(nil), + controller.NewNeConfig.Info, + ) + neConfigGroup.POST("", + middleware.PreAuthorize(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_INSERT)), + controller.NewNeConfig.Add, + ) + neConfigGroup.PUT("", + middleware.PreAuthorize(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_UPDATE)), + controller.NewNeConfig.Edit, + ) + neConfigGroup.DELETE("/:ids", + middleware.PreAuthorize(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfig", collectlogs.BUSINESS_TYPE_DELETE)), + controller.NewNeConfig.Remove, + ) + neConfigGroup.GET("/data", + middleware.PreAuthorize(nil), + controller.NewNeConfig.Data, + ) + } } // InitLoad 初始参数 diff --git a/src/modules/network_element/repository/ne_config.go b/src/modules/network_element/repository/ne_config.go new file mode 100644 index 00000000..5f555b3e --- /dev/null +++ b/src/modules/network_element/repository/ne_config.go @@ -0,0 +1,24 @@ +package repository + +import "be.ems/src/modules/network_element/model" + +// INeConfig 网元参数配置可用属性值 数据层接口 +type INeConfig interface { + // SelectPage 根据条件分页查询字典类型 + SelectPage(query map[string]any) map[string]any + + // SelectList 根据实体查询 + SelectList(param model.NeConfig) []model.NeConfig + + // SelectByIds 通过ID查询 + SelectByIds(ids []string) []model.NeConfig + + // Insert 新增信息 + Insert(param model.NeConfig) string + + // Update 修改信息 + Update(param model.NeConfig) int64 + + // DeleteByIds 批量删除信息 + DeleteByIds(ids []string) int64 +} diff --git a/src/modules/network_element/repository/ne_config.impl.go b/src/modules/network_element/repository/ne_config.impl.go new file mode 100644 index 00000000..8ea0db53 --- /dev/null +++ b/src/modules/network_element/repository/ne_config.impl.go @@ -0,0 +1,254 @@ +package repository + +import ( + "strings" + + "be.ems/src/framework/datasource" + "be.ems/src/framework/logger" + "be.ems/src/framework/utils/parse" + "be.ems/src/framework/utils/repo" + "be.ems/src/modules/network_element/model" +) + +// NewNeConfigImpl 网元参数配置可用属性值 实例化数据层 +var NewNeConfigImpl = &NeConfigImpl{ + selectSql: `select + id, ne_type, ne_id, top_tag, top_display, method, param_json + from param_config`, + + resultMap: map[string]string{ + "id": "ID", + "ne_type": "NeType", + "ne_id": "NeId", + "top_tag": "TopTag", + "top_display": "TopDisplay", + "method": "Method", + "param_json": "ParamJSONStr", + }, +} + +// NeConfigImpl 网元参数配置可用属性值 数据层处理 +type NeConfigImpl struct { + // 查询视图对象SQL + selectSql string + // 结果字段与实体映射 + resultMap map[string]string +} + +// convertResultRows 将结果记录转实体结果组 +func (r *NeConfigImpl) convertResultRows(rows []map[string]any) []model.NeConfig { + arr := make([]model.NeConfig, 0) + for _, row := range rows { + item := model.NeConfig{} + for key, value := range row { + if keyMapper, ok := r.resultMap[key]; ok { + repo.SetFieldValue(&item, keyMapper, value) + } + } + arr = append(arr, item) + } + return arr +} + +// SelectPage 根据条件分页查询字典类型 +func (r *NeConfigImpl) SelectPage(query map[string]any) map[string]any { + // 查询条件拼接 + var conditions []string + var params []any + if v, ok := query["neType"]; ok && v != "" { + conditions = append(conditions, "ne_type = ?") + params = append(params, v) + } + if v, ok := query["topTag"]; ok && v != "" { + conditions = append(conditions, "top_tag = ?") + params = append(params, v) + } + + // 构建查询条件语句 + whereSql := "" + if len(conditions) > 0 { + whereSql += " where " + strings.Join(conditions, " and ") + } + + result := map[string]any{ + "total": 0, + "rows": []model.NeHost{}, + } + + // 查询数量 长度为0直接返回 + totalSql := "select count(id) as 'total' from param_config" + totalRows, err := datasource.RawDB("", totalSql+whereSql, params) + if err != nil { + logger.Errorf("total err => %v", err) + return result + } + total := parse.Number(totalRows[0]["total"]) + if total == 0 { + return result + } else { + result["total"] = total + } + + // 分页 + pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"]) + pageSql := " limit ?,? " + params = append(params, pageNum*pageSize) + params = append(params, pageSize) + + // 查询数据 + querySql := r.selectSql + whereSql + pageSql + results, err := datasource.RawDB("", querySql, params) + if err != nil { + logger.Errorf("query err => %v", err) + return result + } + + // 转换实体 + result["rows"] = r.convertResultRows(results) + return result +} + +// SelectList 根据实体查询 +func (r *NeConfigImpl) SelectList(param model.NeConfig) []model.NeConfig { + // 查询条件拼接 + var conditions []string + var params []any + if param.NeType != "" { + conditions = append(conditions, "ne_type = ?") + params = append(params, param.NeType) + } + if param.TopTag != "" { + conditions = append(conditions, "top_tag = ?") + params = append(params, param.TopTag) + } + + // 构建查询条件语句 + whereSql := "" + if len(conditions) > 0 { + whereSql += " where " + strings.Join(conditions, " and ") + } + + // 查询数据 + querySql := r.selectSql + whereSql + " order by id asc " + results, err := datasource.RawDB("", querySql, params) + if err != nil { + logger.Errorf("query err => %v", err) + } + + // 转换实体 + return r.convertResultRows(results) +} + +// SelectByIds 通过ID查询 +func (r *NeConfigImpl) SelectByIds(ids []string) []model.NeConfig { + placeholder := repo.KeyPlaceholderByQuery(len(ids)) + querySql := r.selectSql + " where id in (" + placeholder + ")" + parameters := repo.ConvertIdsSlice(ids) + results, err := datasource.RawDB("", querySql, parameters) + if err != nil { + logger.Errorf("query err => %v", err) + return []model.NeConfig{} + } + // 转换实体 + return r.convertResultRows(results) +} + +// Insert 新增信息 +func (r *NeConfigImpl) Insert(param model.NeConfig) string { + // 参数拼接 + params := make(map[string]any) + if param.NeType != "" { + params["ne_type"] = param.NeType + } + if param.NeId != "" { + params["ne_id"] = param.NeId + } + if param.TopTag != "" { + params["top_tag"] = param.TopTag + } + if param.TopDisplay != "" { + params["top_display"] = param.TopDisplay + } + if param.Method != "" { + params["method"] = param.Method + } + if param.ParamJSONStr != "" { + params["param_json"] = param.ParamJSONStr + } + + // 构建执行语句 + keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params) + sql := "insert into param_config (" + strings.Join(keys, ",") + ")values(" + placeholder + ")" + + db := datasource.DefaultDB() + // 开启事务 + tx := db.Begin() + // 执行插入 + err := tx.Exec(sql, values...).Error + if err != nil { + logger.Errorf("insert row : %v", err.Error()) + tx.Rollback() + return "" + } + // 获取生成的自增 ID + var insertedID string + err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID) + if err != nil { + logger.Errorf("insert last id : %v", err.Error()) + tx.Rollback() + return "" + } + // 提交事务 + tx.Commit() + return insertedID +} + +// Update 修改信息 +func (r *NeConfigImpl) Update(param model.NeConfig) int64 { + // 参数拼接 + params := make(map[string]any) + if param.NeType != "" { + params["ne_type"] = param.NeType + } + if param.NeId != "" { + params["ne_id"] = param.NeId + } + if param.TopTag != "" { + params["top_tag"] = param.TopTag + } + if param.TopDisplay != "" { + params["top_display"] = param.TopDisplay + } + if param.Method != "" { + params["method"] = param.Method + } + if param.ParamJSONStr != "" { + params["param_json"] = param.ParamJSONStr + } + + // 构建执行语句 + keys, values := repo.KeyValueByUpdate(params) + sql := "update param_config set " + strings.Join(keys, ",") + " where id = ?" + + // 执行更新 + values = append(values, param.ID) + rows, err := datasource.ExecDB("", sql, values) + if err != nil { + logger.Errorf("update row : %v", err.Error()) + return 0 + } + return rows +} + +// DeleteByIds 批量删除信息 +func (r *NeConfigImpl) DeleteByIds(ids []string) int64 { + placeholder := repo.KeyPlaceholderByQuery(len(ids)) + sql := "delete from param_config where id in (" + placeholder + ")" + parameters := repo.ConvertIdsSlice(ids) + results, err := datasource.ExecDB("", sql, parameters) + if err != nil { + logger.Errorf("delete err => %v", err) + return 0 + } + return results +} diff --git a/src/modules/network_element/service/ne_config.go b/src/modules/network_element/service/ne_config.go new file mode 100644 index 00000000..4b1f0eed --- /dev/null +++ b/src/modules/network_element/service/ne_config.go @@ -0,0 +1,24 @@ +package service + +import "be.ems/src/modules/network_element/model" + +// INeConfig 网元参数配置可用属性值 服务层接口 +type INeConfig interface { + // SelectPage 根据条件分页查询字典类型 + SelectPage(query map[string]any) map[string]any + + // SelectList 根据实体查询 + SelectList(param model.NeConfig) []model.NeConfig + + // SelectByIds 通过ID查询 + SelectById(id string) model.NeConfig + + // Insert 新增信息 + Insert(param model.NeConfig) string + + // Update 修改信息 + Update(param model.NeConfig) int64 + + // DeleteByIds 批量删除信息 + DeleteByIds(ids []string) (int64, error) +} diff --git a/src/modules/network_element/service/ne_config.impl.go b/src/modules/network_element/service/ne_config.impl.go new file mode 100644 index 00000000..b129fb6f --- /dev/null +++ b/src/modules/network_element/service/ne_config.impl.go @@ -0,0 +1,67 @@ +package service + +import ( + "fmt" + + "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 +} + +// 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") +}