feat: 内转请求的网元参数配置接口

This commit is contained in:
TsMask
2024-06-25 17:03:56 +08:00
parent d92ca5f2e4
commit c816b64b31
9 changed files with 610 additions and 2 deletions

View File

@@ -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
}

View File

@@ -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
}