style: 去除Impl接口声明层聚焦服务函数
This commit is contained in:
@@ -1,24 +1,259 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// INeConfig 网元参数配置可用属性值 数据层接口
|
||||
type INeConfig interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
"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"
|
||||
)
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(param model.NeConfig) []model.NeConfig
|
||||
// 实例化数据层 NeConfig 结构体
|
||||
var NewNeConfig = &NeConfig{
|
||||
selectSql: `select id, ne_type, param_name, param_display, param_type, param_json, param_sort, param_perms, update_time from ne_config`,
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(ids []string) []model.NeConfig
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(param model.NeConfig) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(param model.NeConfig) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ids []string) int64
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"param_name": "ParamName",
|
||||
"param_display": "ParamDisplay",
|
||||
"param_type": "ParamType",
|
||||
"param_json": "ParamJson",
|
||||
"param_sort": "ParamSort",
|
||||
"param_perms": "ParamPerms",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeConfig 网元参数配置可用属性值 数据层处理
|
||||
type NeConfig struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeConfig) 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 *NeConfig) 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["paramName"]; ok && v != "" {
|
||||
conditions = append(conditions, "param_name = ?")
|
||||
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 ne_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 *NeConfig) 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.ParamName != "" {
|
||||
conditions = append(conditions, "param_name = ?")
|
||||
params = append(params, param.ParamName)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by param_sort asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeConfig) 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 *NeConfig) Insert(param model.NeConfig) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if param.NeType != "" {
|
||||
params["ne_type"] = param.NeType
|
||||
}
|
||||
if param.ParamName != "" {
|
||||
params["param_name"] = param.ParamName
|
||||
}
|
||||
if param.ParamDisplay != "" {
|
||||
params["param_display"] = param.ParamDisplay
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
params["param_type"] = param.ParamType
|
||||
}
|
||||
if param.ParamJson != "" {
|
||||
params["param_json"] = param.ParamJson
|
||||
}
|
||||
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 ne_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 *NeConfig) Update(param model.NeConfig) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if param.NeType != "" {
|
||||
params["ne_type"] = param.NeType
|
||||
}
|
||||
if param.ParamName != "" {
|
||||
params["param_name"] = param.ParamName
|
||||
}
|
||||
if param.ParamDisplay != "" {
|
||||
params["param_display"] = param.ParamDisplay
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
params["param_type"] = param.ParamType
|
||||
}
|
||||
if param.ParamJson != "" {
|
||||
params["param_json"] = param.ParamJson
|
||||
}
|
||||
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 ne_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 *NeConfig) DeleteByIds(ids []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
||||
sql := "delete from ne_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
|
||||
}
|
||||
|
||||
@@ -1,259 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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, 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",
|
||||
"param_name": "ParamName",
|
||||
"param_display": "ParamDisplay",
|
||||
"param_type": "ParamType",
|
||||
"param_json": "ParamJson",
|
||||
"param_sort": "ParamSort",
|
||||
"param_perms": "ParamPerms",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// 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["paramName"]; ok && v != "" {
|
||||
conditions = append(conditions, "param_name = ?")
|
||||
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 ne_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.ParamName != "" {
|
||||
conditions = append(conditions, "param_name = ?")
|
||||
params = append(params, param.ParamName)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by param_sort 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.ParamName != "" {
|
||||
params["param_name"] = param.ParamName
|
||||
}
|
||||
if param.ParamDisplay != "" {
|
||||
params["param_display"] = param.ParamDisplay
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
params["param_type"] = param.ParamType
|
||||
}
|
||||
if param.ParamJson != "" {
|
||||
params["param_json"] = param.ParamJson
|
||||
}
|
||||
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 ne_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.ParamName != "" {
|
||||
params["param_name"] = param.ParamName
|
||||
}
|
||||
if param.ParamDisplay != "" {
|
||||
params["param_display"] = param.ParamDisplay
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
params["param_type"] = param.ParamType
|
||||
}
|
||||
if param.ParamJson != "" {
|
||||
params["param_json"] = param.ParamJson
|
||||
}
|
||||
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 ne_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 ne_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
|
||||
}
|
||||
@@ -1,24 +1,262 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// INeConfigBackup 网元配置文件备份记录 数据层接口
|
||||
type INeConfigBackup interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
"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"
|
||||
)
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(item model.NeConfigBackup) []model.NeConfigBackup
|
||||
// 实例化数据层 NeConfigBackup 结构体
|
||||
var NewNeConfigBackup = &NeConfigBackup{
|
||||
selectSql: `select
|
||||
id, ne_type, ne_id, name, path, remark, create_by, create_time, update_by, update_time
|
||||
from ne_config_backup`,
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(ids []string) []model.NeConfigBackup
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(item model.NeConfigBackup) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(item model.NeConfigBackup) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ids []string) int64
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"name": "Name",
|
||||
"path": "Path",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeConfigBackup 网元配置文件备份记录 数据层处理
|
||||
type NeConfigBackup struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeConfigBackup) convertResultRows(rows []map[string]any) []model.NeConfigBackup {
|
||||
arr := make([]model.NeConfigBackup, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeConfigBackup{}
|
||||
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 *NeConfigBackup) 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, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
conditions = append(conditions, "name like concat(concat('%', ?), '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_config_backup"
|
||||
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 := " order by id desc 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 *NeConfigBackup) SelectList(item model.NeConfigBackup) []model.NeConfigBackup {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if item.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, item.NeType)
|
||||
}
|
||||
if item.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, item.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by id desc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeConfigBackup) SelectByIds(cmdIds []string) []model.NeConfigBackup {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeConfigBackup{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeConfigBackup) Insert(item model.NeConfigBackup) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if item.NeType != "" {
|
||||
params["ne_type"] = item.NeType
|
||||
}
|
||||
if item.NeId != "" {
|
||||
params["ne_id"] = item.NeId
|
||||
}
|
||||
if item.Name != "" {
|
||||
params["name"] = item.Name
|
||||
}
|
||||
if item.Path != "" {
|
||||
params["path"] = item.Path
|
||||
}
|
||||
if item.Remark != "" {
|
||||
params["remark"] = item.Remark
|
||||
}
|
||||
if item.CreateBy != "" {
|
||||
params["create_by"] = item.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_config_backup (" + 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 *NeConfigBackup) Update(item model.NeConfigBackup) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if item.NeType != "" {
|
||||
params["ne_type"] = item.NeType
|
||||
}
|
||||
if item.NeId != "" {
|
||||
params["ne_id"] = item.NeId
|
||||
}
|
||||
if item.Name != "" {
|
||||
params["name"] = item.Name
|
||||
}
|
||||
if item.Path != "" {
|
||||
params["path"] = item.Path
|
||||
}
|
||||
params["remark"] = item.Remark
|
||||
if item.UpdateBy != "" {
|
||||
params["update_by"] = item.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_config_backup set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, item.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeConfigBackup) DeleteByIds(ids []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
||||
sql := "delete from ne_config_backup 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
|
||||
}
|
||||
|
||||
@@ -1,262 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化数据层 NewNeConfigBackupImpl 结构体
|
||||
var NewNeConfigBackupImpl = &NeConfigBackupImpl{
|
||||
selectSql: `select
|
||||
id, ne_type, ne_id, name, path, remark, create_by, create_time, update_by, update_time
|
||||
from ne_config_backup`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"name": "Name",
|
||||
"path": "Path",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeConfigBackupImpl 网元配置文件备份记录 数据层处理
|
||||
type NeConfigBackupImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeConfigBackupImpl) convertResultRows(rows []map[string]any) []model.NeConfigBackup {
|
||||
arr := make([]model.NeConfigBackup, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeConfigBackup{}
|
||||
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 *NeConfigBackupImpl) 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, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
conditions = append(conditions, "name like concat(concat('%', ?), '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_config_backup"
|
||||
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 := " order by id desc 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 *NeConfigBackupImpl) SelectList(item model.NeConfigBackup) []model.NeConfigBackup {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if item.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, item.NeType)
|
||||
}
|
||||
if item.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, item.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by id desc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeConfigBackupImpl) SelectByIds(cmdIds []string) []model.NeConfigBackup {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeConfigBackup{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if item.NeType != "" {
|
||||
params["ne_type"] = item.NeType
|
||||
}
|
||||
if item.NeId != "" {
|
||||
params["ne_id"] = item.NeId
|
||||
}
|
||||
if item.Name != "" {
|
||||
params["name"] = item.Name
|
||||
}
|
||||
if item.Path != "" {
|
||||
params["path"] = item.Path
|
||||
}
|
||||
if item.Remark != "" {
|
||||
params["remark"] = item.Remark
|
||||
}
|
||||
if item.CreateBy != "" {
|
||||
params["create_by"] = item.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_config_backup (" + 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 *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if item.NeType != "" {
|
||||
params["ne_type"] = item.NeType
|
||||
}
|
||||
if item.NeId != "" {
|
||||
params["ne_id"] = item.NeId
|
||||
}
|
||||
if item.Name != "" {
|
||||
params["name"] = item.Name
|
||||
}
|
||||
if item.Path != "" {
|
||||
params["path"] = item.Path
|
||||
}
|
||||
params["remark"] = item.Remark
|
||||
if item.UpdateBy != "" {
|
||||
params["update_by"] = item.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_config_backup set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, item.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeConfigBackupImpl) DeleteByIds(ids []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(ids))
|
||||
sql := "delete from ne_config_backup 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
|
||||
}
|
||||
@@ -1,27 +1,376 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// INeHost 网元主机连接 数据层接口
|
||||
type INeHost interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
"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"
|
||||
)
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(neHost model.NeHost) []model.NeHost
|
||||
// 实例化数据层 NeHost 结构体
|
||||
var NewNeHost = &NeHost{
|
||||
selectSql: `select
|
||||
host_id, host_type, group_id, title, addr, port, user, auth_mode, password, private_key, pass_phrase, remark, create_by, create_time, update_by, update_time
|
||||
from ne_host`,
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(hostIds []string) []model.NeHost
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neHost model.NeHost) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neHost model.NeHost) int64
|
||||
|
||||
// DeleteByIds 批量删除网元主机连接信息
|
||||
DeleteByIds(hostIds []string) int64
|
||||
|
||||
// CheckUniqueNeHost 校验主机是否唯一
|
||||
CheckUniqueNeHost(neHost model.NeHost) string
|
||||
resultMap: map[string]string{
|
||||
"host_id": "HostID",
|
||||
"host_type": "HostType",
|
||||
"group_id": "GroupID",
|
||||
"title": "Title",
|
||||
"addr": "Addr",
|
||||
"port": "Port",
|
||||
"user": "User",
|
||||
"auth_mode": "AuthMode",
|
||||
"password": "Password",
|
||||
"private_key": "PrivateKey",
|
||||
"private_password": "PassPhrase",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeHost 网元主机连接 数据层处理
|
||||
type NeHost struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeHost) convertResultRows(rows []map[string]any) []model.NeHost {
|
||||
arr := make([]model.NeHost, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeHost{}
|
||||
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 *NeHost) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["hostType"]; ok && v != "" {
|
||||
conditions = append(conditions, "host_type = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["groupId"]; ok && v != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_host"
|
||||
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 *NeHost) SelectList(neHost model.NeHost) []model.NeHost {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHost.HostType != "" {
|
||||
conditions = append(conditions, "host_type = ?")
|
||||
params = append(params, neHost.HostType)
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHost.GroupID)
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, neHost.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by update_time asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeHost) SelectByIds(hostIds []string) []model.NeHost {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(hostIds))
|
||||
querySql := r.selectSql + " where host_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(hostIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeHost{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueNeHost 校验主机是否唯一
|
||||
func (r *NeHost) CheckUniqueNeHost(neHost model.NeHost) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHost.HostType != "" {
|
||||
conditions = append(conditions, "host_type = ?")
|
||||
params = append(params, neHost.HostType)
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHost.GroupID)
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
conditions = append(conditions, "title = ?")
|
||||
params = append(params, neHost.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select host_id as 'str' from ne_host " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeHost) Insert(neHost model.NeHost) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHost.HostType != "" {
|
||||
params["host_type"] = neHost.HostType
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
params["group_id"] = neHost.GroupID
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
params["title"] = neHost.Title
|
||||
}
|
||||
if neHost.Addr != "" {
|
||||
params["addr"] = neHost.Addr
|
||||
}
|
||||
if neHost.Port > 0 {
|
||||
params["port"] = neHost.Port
|
||||
}
|
||||
if neHost.User != "" {
|
||||
params["user"] = neHost.User
|
||||
}
|
||||
if neHost.AuthMode != "" {
|
||||
params["auth_mode"] = neHost.AuthMode
|
||||
}
|
||||
if neHost.Password != "" {
|
||||
params["password"] = neHost.Password
|
||||
}
|
||||
if neHost.PrivateKey != "" {
|
||||
params["private_key"] = neHost.PrivateKey
|
||||
}
|
||||
if neHost.PassPhrase != "" {
|
||||
params["pass_phrase"] = neHost.PassPhrase
|
||||
}
|
||||
if neHost.Remark != "" {
|
||||
params["remark"] = neHost.Remark
|
||||
}
|
||||
if neHost.CreateBy != "" {
|
||||
params["create_by"] = neHost.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 根据认证模式清除不必要的信息
|
||||
if neHost.AuthMode == "0" {
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "1" {
|
||||
params["password"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "2" {
|
||||
params["password"] = ""
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_host (" + 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 *NeHost) Update(neHost model.NeHost) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHost.HostType != "" {
|
||||
params["host_type"] = neHost.HostType
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
params["group_id"] = neHost.GroupID
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
params["title"] = neHost.Title
|
||||
}
|
||||
if neHost.Addr != "" {
|
||||
params["addr"] = neHost.Addr
|
||||
}
|
||||
if neHost.Port > 0 {
|
||||
params["port"] = neHost.Port
|
||||
}
|
||||
if neHost.User != "" {
|
||||
params["user"] = neHost.User
|
||||
}
|
||||
if neHost.AuthMode != "" {
|
||||
params["auth_mode"] = neHost.AuthMode
|
||||
}
|
||||
if neHost.Password != "" {
|
||||
params["password"] = neHost.Password
|
||||
}
|
||||
if neHost.PrivateKey != "" {
|
||||
params["private_key"] = neHost.PrivateKey
|
||||
}
|
||||
if neHost.PassPhrase != "" {
|
||||
params["pass_phrase"] = neHost.PassPhrase
|
||||
}
|
||||
params["remark"] = neHost.Remark
|
||||
if neHost.UpdateBy != "" {
|
||||
params["update_by"] = neHost.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 根据认证模式清除不必要的信息
|
||||
if neHost.AuthMode == "0" {
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "1" {
|
||||
params["password"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "2" {
|
||||
params["password"] = ""
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_host set " + strings.Join(keys, ",") + " where host_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neHost.HostID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除网元主机连接信息
|
||||
func (r *NeHost) DeleteByIds(hostIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(hostIds))
|
||||
sql := "delete from ne_host where host_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(hostIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -1,376 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化数据层 NewNeHostImpl 结构体
|
||||
var NewNeHostImpl = &NeHostImpl{
|
||||
selectSql: `select
|
||||
host_id, host_type, group_id, title, addr, port, user, auth_mode, password, private_key, pass_phrase, remark, create_by, create_time, update_by, update_time
|
||||
from ne_host`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"host_id": "HostID",
|
||||
"host_type": "HostType",
|
||||
"group_id": "GroupID",
|
||||
"title": "Title",
|
||||
"addr": "Addr",
|
||||
"port": "Port",
|
||||
"user": "User",
|
||||
"auth_mode": "AuthMode",
|
||||
"password": "Password",
|
||||
"private_key": "PrivateKey",
|
||||
"private_password": "PassPhrase",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeHostImpl 网元主机连接 数据层处理
|
||||
type NeHostImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeHostImpl) convertResultRows(rows []map[string]any) []model.NeHost {
|
||||
arr := make([]model.NeHost, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeHost{}
|
||||
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 *NeHostImpl) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["hostType"]; ok && v != "" {
|
||||
conditions = append(conditions, "host_type = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["groupId"]; ok && v != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_host"
|
||||
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 *NeHostImpl) SelectList(neHost model.NeHost) []model.NeHost {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHost.HostType != "" {
|
||||
conditions = append(conditions, "host_type = ?")
|
||||
params = append(params, neHost.HostType)
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHost.GroupID)
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, neHost.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by update_time asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeHostImpl) SelectByIds(hostIds []string) []model.NeHost {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(hostIds))
|
||||
querySql := r.selectSql + " where host_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(hostIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeHost{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueNeHost 校验主机是否唯一
|
||||
func (r *NeHostImpl) CheckUniqueNeHost(neHost model.NeHost) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHost.HostType != "" {
|
||||
conditions = append(conditions, "host_type = ?")
|
||||
params = append(params, neHost.HostType)
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHost.GroupID)
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
conditions = append(conditions, "title = ?")
|
||||
params = append(params, neHost.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select host_id as 'str' from ne_host " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeHostImpl) Insert(neHost model.NeHost) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHost.HostType != "" {
|
||||
params["host_type"] = neHost.HostType
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
params["group_id"] = neHost.GroupID
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
params["title"] = neHost.Title
|
||||
}
|
||||
if neHost.Addr != "" {
|
||||
params["addr"] = neHost.Addr
|
||||
}
|
||||
if neHost.Port > 0 {
|
||||
params["port"] = neHost.Port
|
||||
}
|
||||
if neHost.User != "" {
|
||||
params["user"] = neHost.User
|
||||
}
|
||||
if neHost.AuthMode != "" {
|
||||
params["auth_mode"] = neHost.AuthMode
|
||||
}
|
||||
if neHost.Password != "" {
|
||||
params["password"] = neHost.Password
|
||||
}
|
||||
if neHost.PrivateKey != "" {
|
||||
params["private_key"] = neHost.PrivateKey
|
||||
}
|
||||
if neHost.PassPhrase != "" {
|
||||
params["pass_phrase"] = neHost.PassPhrase
|
||||
}
|
||||
if neHost.Remark != "" {
|
||||
params["remark"] = neHost.Remark
|
||||
}
|
||||
if neHost.CreateBy != "" {
|
||||
params["create_by"] = neHost.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 根据认证模式清除不必要的信息
|
||||
if neHost.AuthMode == "0" {
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "1" {
|
||||
params["password"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "2" {
|
||||
params["password"] = ""
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_host (" + 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 *NeHostImpl) Update(neHost model.NeHost) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHost.HostType != "" {
|
||||
params["host_type"] = neHost.HostType
|
||||
}
|
||||
if neHost.GroupID != "" {
|
||||
params["group_id"] = neHost.GroupID
|
||||
}
|
||||
if neHost.Title != "" {
|
||||
params["title"] = neHost.Title
|
||||
}
|
||||
if neHost.Addr != "" {
|
||||
params["addr"] = neHost.Addr
|
||||
}
|
||||
if neHost.Port > 0 {
|
||||
params["port"] = neHost.Port
|
||||
}
|
||||
if neHost.User != "" {
|
||||
params["user"] = neHost.User
|
||||
}
|
||||
if neHost.AuthMode != "" {
|
||||
params["auth_mode"] = neHost.AuthMode
|
||||
}
|
||||
if neHost.Password != "" {
|
||||
params["password"] = neHost.Password
|
||||
}
|
||||
if neHost.PrivateKey != "" {
|
||||
params["private_key"] = neHost.PrivateKey
|
||||
}
|
||||
if neHost.PassPhrase != "" {
|
||||
params["pass_phrase"] = neHost.PassPhrase
|
||||
}
|
||||
params["remark"] = neHost.Remark
|
||||
if neHost.UpdateBy != "" {
|
||||
params["update_by"] = neHost.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 根据认证模式清除不必要的信息
|
||||
if neHost.AuthMode == "0" {
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "1" {
|
||||
params["password"] = ""
|
||||
}
|
||||
if neHost.AuthMode == "2" {
|
||||
params["password"] = ""
|
||||
params["private_key"] = ""
|
||||
params["pass_phrase"] = ""
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_host set " + strings.Join(keys, ",") + " where host_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neHost.HostID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除网元主机连接信息
|
||||
func (r *NeHostImpl) DeleteByIds(hostIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(hostIds))
|
||||
sql := "delete from ne_host where host_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(hostIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
@@ -1,27 +1,306 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// INeHostCmd 网元主机命令 数据层接口
|
||||
type INeHostCmd interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
"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"
|
||||
)
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd
|
||||
// 实例化数据层 NeHostCmd 结构体
|
||||
var NewNeHostCmd = &NeHostCmd{
|
||||
selectSql: `select
|
||||
cmd_id, cmd_type, group_id, title, command, remark, create_by, create_time, update_by, update_time
|
||||
from ne_host_cmd`,
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(cmdIds []string) []model.NeHostCmd
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neHostCmd model.NeHostCmd) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neHostCmd model.NeHostCmd) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(cmdIds []string) int64
|
||||
|
||||
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||
CheckUniqueGroupTitle(neHostCmd model.NeHostCmd) string
|
||||
resultMap: map[string]string{
|
||||
"cmd_id": "CmdID",
|
||||
"cmd_type": "CmdType",
|
||||
"group_id": "GroupID",
|
||||
"title": "Title",
|
||||
"command": "Command",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeHostCmd 网元主机连接 数据层处理
|
||||
type NeHostCmd struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeHostCmd) convertResultRows(rows []map[string]any) []model.NeHostCmd {
|
||||
arr := make([]model.NeHostCmd, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeHostCmd{}
|
||||
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 *NeHostCmd) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["cmdType"]; ok && v != "" {
|
||||
conditions = append(conditions, "cmd_type = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["groupId"]; ok && v != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_host_cmd"
|
||||
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 *NeHostCmd) SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHostCmd.CmdType != "" {
|
||||
conditions = append(conditions, "cmd_type = ?")
|
||||
params = append(params, neHostCmd.CmdType)
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHostCmd.GroupID)
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, neHostCmd.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by update_time asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeHostCmd) SelectByIds(cmdIds []string) []model.NeHostCmd {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where cmd_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeHostCmd{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||
func (r *NeHostCmd) CheckUniqueGroupTitle(neHostCmd model.NeHostCmd) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHostCmd.CmdType != "" {
|
||||
conditions = append(conditions, "cmd_type = ?")
|
||||
params = append(params, neHostCmd.CmdType)
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHostCmd.GroupID)
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
conditions = append(conditions, "title = ?")
|
||||
params = append(params, neHostCmd.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select host_id as 'str' from ne_host " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeHostCmd) Insert(neHostCmd model.NeHostCmd) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHostCmd.CmdType != "" {
|
||||
params["cmd_type"] = neHostCmd.CmdType
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
params["group_id"] = neHostCmd.GroupID
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
params["title"] = neHostCmd.Title
|
||||
}
|
||||
if neHostCmd.Command != "" {
|
||||
params["command"] = neHostCmd.Command
|
||||
}
|
||||
if neHostCmd.Remark != "" {
|
||||
params["remark"] = neHostCmd.Remark
|
||||
}
|
||||
if neHostCmd.CreateBy != "" {
|
||||
params["create_by"] = neHostCmd.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_host_cmd (" + 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 *NeHostCmd) Update(neHostCmd model.NeHostCmd) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHostCmd.CmdType != "" {
|
||||
params["cmd_type"] = neHostCmd.CmdType
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
params["group_id"] = neHostCmd.GroupID
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
params["title"] = neHostCmd.Title
|
||||
}
|
||||
if neHostCmd.Command != "" {
|
||||
params["command"] = neHostCmd.Command
|
||||
}
|
||||
params["remark"] = neHostCmd.Remark
|
||||
if neHostCmd.UpdateBy != "" {
|
||||
params["update_by"] = neHostCmd.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_host_cmd set " + strings.Join(keys, ",") + " where cmd_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neHostCmd.CmdID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeHostCmd) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_host_cmd where cmd_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -1,306 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化数据层 NewNeHostCmdImpl 结构体
|
||||
var NewNeHostCmdImpl = &NeHostCmdImpl{
|
||||
selectSql: `select
|
||||
cmd_id, cmd_type, group_id, title, command, remark, create_by, create_time, update_by, update_time
|
||||
from ne_host_cmd`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"cmd_id": "CmdID",
|
||||
"cmd_type": "CmdType",
|
||||
"group_id": "GroupID",
|
||||
"title": "Title",
|
||||
"command": "Command",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeHostCmdImpl 网元主机连接 数据层处理
|
||||
type NeHostCmdImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeHostCmdImpl) convertResultRows(rows []map[string]any) []model.NeHostCmd {
|
||||
arr := make([]model.NeHostCmd, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeHostCmd{}
|
||||
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 *NeHostCmdImpl) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["cmdType"]; ok && v != "" {
|
||||
conditions = append(conditions, "cmd_type = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["groupId"]; ok && v != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_host_cmd"
|
||||
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 *NeHostCmdImpl) SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHostCmd.CmdType != "" {
|
||||
conditions = append(conditions, "cmd_type = ?")
|
||||
params = append(params, neHostCmd.CmdType)
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHostCmd.GroupID)
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, neHostCmd.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by update_time asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeHostCmdImpl) SelectByIds(cmdIds []string) []model.NeHostCmd {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where cmd_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeHostCmd{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||
func (r *NeHostCmdImpl) CheckUniqueGroupTitle(neHostCmd model.NeHostCmd) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neHostCmd.CmdType != "" {
|
||||
conditions = append(conditions, "cmd_type = ?")
|
||||
params = append(params, neHostCmd.CmdType)
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, neHostCmd.GroupID)
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
conditions = append(conditions, "title = ?")
|
||||
params = append(params, neHostCmd.Title)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select host_id as 'str' from ne_host " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeHostCmdImpl) Insert(neHostCmd model.NeHostCmd) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHostCmd.CmdType != "" {
|
||||
params["cmd_type"] = neHostCmd.CmdType
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
params["group_id"] = neHostCmd.GroupID
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
params["title"] = neHostCmd.Title
|
||||
}
|
||||
if neHostCmd.Command != "" {
|
||||
params["command"] = neHostCmd.Command
|
||||
}
|
||||
if neHostCmd.Remark != "" {
|
||||
params["remark"] = neHostCmd.Remark
|
||||
}
|
||||
if neHostCmd.CreateBy != "" {
|
||||
params["create_by"] = neHostCmd.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_host_cmd (" + 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 *NeHostCmdImpl) Update(neHostCmd model.NeHostCmd) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neHostCmd.CmdType != "" {
|
||||
params["cmd_type"] = neHostCmd.CmdType
|
||||
}
|
||||
if neHostCmd.GroupID != "" {
|
||||
params["group_id"] = neHostCmd.GroupID
|
||||
}
|
||||
if neHostCmd.Title != "" {
|
||||
params["title"] = neHostCmd.Title
|
||||
}
|
||||
if neHostCmd.Command != "" {
|
||||
params["command"] = neHostCmd.Command
|
||||
}
|
||||
params["remark"] = neHostCmd.Remark
|
||||
if neHostCmd.UpdateBy != "" {
|
||||
params["update_by"] = neHostCmd.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_host_cmd set " + strings.Join(keys, ",") + " where cmd_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neHostCmd.CmdID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeHostCmdImpl) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_host_cmd where cmd_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
@@ -1,32 +1,405 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 网元信息 数据层接口
|
||||
type INeInfo interface {
|
||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||
SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 查询列表
|
||||
SelectList(neInfo model.NeInfo) []model.NeInfo
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(infoIds []string) []model.NeInfo
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neInfo model.NeInfo) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neInfo model.NeInfo) int64
|
||||
|
||||
// DeleteByIds 批量删除网元信息
|
||||
DeleteByIds(infoIds []string) int64
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string
|
||||
// neListSort 网元列表预设排序
|
||||
var neListSort = []string{
|
||||
"OMC",
|
||||
"IMS",
|
||||
"AMF",
|
||||
"AUSF",
|
||||
"UDM",
|
||||
"SMF",
|
||||
"PCF",
|
||||
"NSSF",
|
||||
"NRF",
|
||||
"UPF",
|
||||
"LMF",
|
||||
"NEF",
|
||||
"MME",
|
||||
"N3IWF",
|
||||
"MOCNGW",
|
||||
"SMSC",
|
||||
"CBC",
|
||||
}
|
||||
|
||||
// 实例化数据层 NeInfo 结构体
|
||||
var NewNeInfo = &NeInfo{
|
||||
selectSql: `select id, ne_type, ne_id, rm_uid, ne_name, ip, port, pv_flag, province, vendor_name, dn, ne_address, host_ids, status, remark, create_by, create_time, update_by, update_time from ne_info`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"rm_uid": "RmUID",
|
||||
"ne_name": "NeName",
|
||||
"ip": "IP",
|
||||
"port": "Port",
|
||||
"pv_flag": "PvFlag",
|
||||
"province": "Province",
|
||||
"vendor_name": "VendorName",
|
||||
"dn": "Dn",
|
||||
"ne_address": "NeAddress",
|
||||
"host_ids": "HostIDs",
|
||||
"status": "Status",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeInfo 网元信息表 数据层处理
|
||||
type NeInfo struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeInfo) convertResultRows(rows []map[string]any) []model.NeInfo {
|
||||
arr := make([]model.NeInfo, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeInfo{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
|
||||
// 创建优先级映射
|
||||
priority := make(map[string]int)
|
||||
for i, v := range neListSort {
|
||||
priority[v] = i
|
||||
}
|
||||
// 冒泡排序
|
||||
n := len(arr)
|
||||
for i := 0; i < n-1; i++ {
|
||||
for j := 0; j < n-i-1; j++ {
|
||||
if priority[arr[j].NeType] > priority[arr[j+1].NeType] {
|
||||
// 交换元素
|
||||
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||
func (r *NeInfo) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
||||
querySql := r.selectSql + " where ne_type = ? and ne_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{neType, neID})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.NeInfo{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.NeInfo{}
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *NeInfo) 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, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["rmUid"]; ok && v != "" {
|
||||
conditions = append(conditions, "rmUid like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeInfo{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_info"
|
||||
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 + " order by ne_type asc, ne_id asc " + 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 *NeInfo) SelectList(neInfo model.NeInfo) []model.NeInfo {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neInfo.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neInfo.NeType)
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neInfo.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by ne_type asc, ne_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 *NeInfo) SelectByIds(infoIds []string) []model.NeInfo {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(infoIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(infoIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeInfo{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
func (r *NeInfo) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neInfo.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neInfo.NeType)
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neInfo.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select id as 'str' from ne_info " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeInfo) Insert(neInfo model.NeInfo) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neInfo.NeType != "" {
|
||||
params["ne_type"] = neInfo.NeType
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
params["ne_id"] = neInfo.NeId
|
||||
}
|
||||
if neInfo.RmUID != "" {
|
||||
params["rm_uid"] = neInfo.RmUID
|
||||
}
|
||||
if neInfo.NeName != "" {
|
||||
params["ne_name"] = neInfo.NeName
|
||||
}
|
||||
if neInfo.IP != "" {
|
||||
params["ip"] = neInfo.IP
|
||||
}
|
||||
if neInfo.Port > 0 {
|
||||
params["port"] = neInfo.Port
|
||||
}
|
||||
if neInfo.PvFlag != "" {
|
||||
params["pv_flag"] = neInfo.PvFlag
|
||||
}
|
||||
if neInfo.Province != "" {
|
||||
params["province"] = neInfo.Province
|
||||
}
|
||||
if neInfo.VendorName != "" {
|
||||
params["vendor_name"] = neInfo.VendorName
|
||||
}
|
||||
if neInfo.Dn != "" {
|
||||
params["dn"] = neInfo.Dn
|
||||
}
|
||||
if neInfo.NeAddress != "" {
|
||||
params["ne_address"] = neInfo.NeAddress
|
||||
}
|
||||
if neInfo.HostIDs != "" {
|
||||
params["host_ids"] = neInfo.HostIDs
|
||||
}
|
||||
if neInfo.Status != "" {
|
||||
params["status"] = neInfo.Status
|
||||
}
|
||||
if neInfo.Remark != "" {
|
||||
params["remark"] = neInfo.Remark
|
||||
}
|
||||
if neInfo.CreateBy != "" {
|
||||
params["create_by"] = neInfo.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_info (" + 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 *NeInfo) Update(neInfo model.NeInfo) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neInfo.NeType != "" {
|
||||
params["ne_type"] = neInfo.NeType
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
params["ne_id"] = neInfo.NeId
|
||||
}
|
||||
if neInfo.RmUID != "" {
|
||||
params["rm_uid"] = neInfo.RmUID
|
||||
}
|
||||
if neInfo.NeName != "" {
|
||||
params["ne_name"] = neInfo.NeName
|
||||
}
|
||||
if neInfo.IP != "" {
|
||||
params["ip"] = neInfo.IP
|
||||
}
|
||||
if neInfo.Port > 0 {
|
||||
params["port"] = neInfo.Port
|
||||
}
|
||||
if neInfo.PvFlag != "" {
|
||||
params["pv_flag"] = neInfo.PvFlag
|
||||
}
|
||||
params["province"] = neInfo.Province
|
||||
params["vendor_name"] = neInfo.VendorName
|
||||
params["dn"] = neInfo.Dn
|
||||
params["ne_address"] = neInfo.NeAddress
|
||||
if neInfo.HostIDs != "" {
|
||||
params["host_ids"] = neInfo.HostIDs
|
||||
}
|
||||
params["remark"] = neInfo.Remark
|
||||
if neInfo.Status != "" {
|
||||
params["status"] = neInfo.Status
|
||||
}
|
||||
if neInfo.UpdateBy != "" {
|
||||
params["update_by"] = neInfo.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_info set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neInfo.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除网元信息
|
||||
func (r *NeInfo) DeleteByIds(infoIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(infoIds))
|
||||
sql := "delete from ne_info where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(infoIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -1,405 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// neListSort 网元列表预设排序
|
||||
var neListSort = []string{
|
||||
"OMC",
|
||||
"IMS",
|
||||
"AMF",
|
||||
"AUSF",
|
||||
"UDM",
|
||||
"SMF",
|
||||
"PCF",
|
||||
"NSSF",
|
||||
"NRF",
|
||||
"UPF",
|
||||
"LMF",
|
||||
"NEF",
|
||||
"MME",
|
||||
"N3IWF",
|
||||
"MOCNGW",
|
||||
"SMSC",
|
||||
"CBC",
|
||||
}
|
||||
|
||||
// 实例化数据层 NeInfoImpl 结构体
|
||||
var NewNeInfoImpl = &NeInfoImpl{
|
||||
selectSql: `select id, ne_type, ne_id, rm_uid, ne_name, ip, port, pv_flag, province, vendor_name, dn, ne_address, host_ids, status, remark, create_by, create_time, update_by, update_time from ne_info`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"rm_uid": "RmUID",
|
||||
"ne_name": "NeName",
|
||||
"ip": "IP",
|
||||
"port": "Port",
|
||||
"pv_flag": "PvFlag",
|
||||
"province": "Province",
|
||||
"vendor_name": "VendorName",
|
||||
"dn": "Dn",
|
||||
"ne_address": "NeAddress",
|
||||
"host_ids": "HostIDs",
|
||||
"status": "Status",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeInfoImpl 网元信息表 数据层处理
|
||||
type NeInfoImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeInfoImpl) convertResultRows(rows []map[string]any) []model.NeInfo {
|
||||
arr := make([]model.NeInfo, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeInfo{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
|
||||
// 创建优先级映射
|
||||
priority := make(map[string]int)
|
||||
for i, v := range neListSort {
|
||||
priority[v] = i
|
||||
}
|
||||
// 冒泡排序
|
||||
n := len(arr)
|
||||
for i := 0; i < n-1; i++ {
|
||||
for j := 0; j < n-i-1; j++ {
|
||||
if priority[arr[j].NeType] > priority[arr[j+1].NeType] {
|
||||
// 交换元素
|
||||
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||
func (r *NeInfoImpl) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
||||
querySql := r.selectSql + " where ne_type = ? and ne_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{neType, neID})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.NeInfo{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.NeInfo{}
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *NeInfoImpl) 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, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["rmUid"]; ok && v != "" {
|
||||
conditions = append(conditions, "rmUid like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeInfo{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_info"
|
||||
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 + " order by ne_type asc, ne_id asc " + 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 *NeInfoImpl) SelectList(neInfo model.NeInfo) []model.NeInfo {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neInfo.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neInfo.NeType)
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neInfo.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by ne_type asc, ne_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 *NeInfoImpl) SelectByIds(infoIds []string) []model.NeInfo {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(infoIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(infoIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeInfo{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
func (r *NeInfoImpl) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neInfo.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neInfo.NeType)
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neInfo.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select id as 'str' from ne_info " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeInfoImpl) Insert(neInfo model.NeInfo) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neInfo.NeType != "" {
|
||||
params["ne_type"] = neInfo.NeType
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
params["ne_id"] = neInfo.NeId
|
||||
}
|
||||
if neInfo.RmUID != "" {
|
||||
params["rm_uid"] = neInfo.RmUID
|
||||
}
|
||||
if neInfo.NeName != "" {
|
||||
params["ne_name"] = neInfo.NeName
|
||||
}
|
||||
if neInfo.IP != "" {
|
||||
params["ip"] = neInfo.IP
|
||||
}
|
||||
if neInfo.Port > 0 {
|
||||
params["port"] = neInfo.Port
|
||||
}
|
||||
if neInfo.PvFlag != "" {
|
||||
params["pv_flag"] = neInfo.PvFlag
|
||||
}
|
||||
if neInfo.Province != "" {
|
||||
params["province"] = neInfo.Province
|
||||
}
|
||||
if neInfo.VendorName != "" {
|
||||
params["vendor_name"] = neInfo.VendorName
|
||||
}
|
||||
if neInfo.Dn != "" {
|
||||
params["dn"] = neInfo.Dn
|
||||
}
|
||||
if neInfo.NeAddress != "" {
|
||||
params["ne_address"] = neInfo.NeAddress
|
||||
}
|
||||
if neInfo.HostIDs != "" {
|
||||
params["host_ids"] = neInfo.HostIDs
|
||||
}
|
||||
if neInfo.Status != "" {
|
||||
params["status"] = neInfo.Status
|
||||
}
|
||||
if neInfo.Remark != "" {
|
||||
params["remark"] = neInfo.Remark
|
||||
}
|
||||
if neInfo.CreateBy != "" {
|
||||
params["create_by"] = neInfo.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_info (" + 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 *NeInfoImpl) Update(neInfo model.NeInfo) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neInfo.NeType != "" {
|
||||
params["ne_type"] = neInfo.NeType
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
params["ne_id"] = neInfo.NeId
|
||||
}
|
||||
if neInfo.RmUID != "" {
|
||||
params["rm_uid"] = neInfo.RmUID
|
||||
}
|
||||
if neInfo.NeName != "" {
|
||||
params["ne_name"] = neInfo.NeName
|
||||
}
|
||||
if neInfo.IP != "" {
|
||||
params["ip"] = neInfo.IP
|
||||
}
|
||||
if neInfo.Port > 0 {
|
||||
params["port"] = neInfo.Port
|
||||
}
|
||||
if neInfo.PvFlag != "" {
|
||||
params["pv_flag"] = neInfo.PvFlag
|
||||
}
|
||||
params["province"] = neInfo.Province
|
||||
params["vendor_name"] = neInfo.VendorName
|
||||
params["dn"] = neInfo.Dn
|
||||
params["ne_address"] = neInfo.NeAddress
|
||||
if neInfo.HostIDs != "" {
|
||||
params["host_ids"] = neInfo.HostIDs
|
||||
}
|
||||
params["remark"] = neInfo.Remark
|
||||
if neInfo.Status != "" {
|
||||
params["status"] = neInfo.Status
|
||||
}
|
||||
if neInfo.UpdateBy != "" {
|
||||
params["update_by"] = neInfo.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_info set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neInfo.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除网元信息
|
||||
func (r *NeInfoImpl) DeleteByIds(infoIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(infoIds))
|
||||
sql := "delete from ne_info where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(infoIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
@@ -1,24 +1,297 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// INeLicense 网元授权激活信息 数据层接口
|
||||
type INeLicense interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
"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"
|
||||
)
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(neLicense model.NeLicense) []model.NeLicense
|
||||
// 实例化数据层 NeLicense 结构体
|
||||
var NewNeLicense = &NeLicense{
|
||||
selectSql: `select
|
||||
id, ne_type, ne_id, activation_request_code, license_path, serial_num, expiry_date, status, remark, create_by, create_time, update_by, update_time
|
||||
from ne_license`,
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(ids []string) []model.NeLicense
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neLicense model.NeLicense) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neLicense model.NeLicense) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ids []string) int64
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"activation_request_code": "ActivationRequestCode",
|
||||
"license_path": "LicensePath",
|
||||
"serial_num": "SerialNum",
|
||||
"expiry_date": "ExpiryDate",
|
||||
"status": "Status",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeLicense 网元授权激活信息 数据层处理
|
||||
type NeLicense struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeLicense) convertResultRows(rows []map[string]any) []model.NeLicense {
|
||||
arr := make([]model.NeLicense, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeLicense{}
|
||||
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 *NeLicense) 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, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["expiryDate"]; ok && v != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["createBy"]; ok && v != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_license"
|
||||
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 *NeLicense) SelectList(neLicense model.NeLicense) []model.NeLicense {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neLicense.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neLicense.NeType)
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neLicense.NeId)
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, neLicense.ExpiryDate)
|
||||
}
|
||||
if neLicense.CreateBy != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, neLicense.CreateBy)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
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 *NeLicense) SelectByIds(cmdIds []string) []model.NeLicense {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeLicense{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeLicense) Insert(neLicense model.NeLicense) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neLicense.NeType != "" {
|
||||
params["ne_type"] = neLicense.NeType
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
params["ne_id"] = neLicense.NeId
|
||||
}
|
||||
if neLicense.ActivationRequestCode != "" {
|
||||
params["activation_request_code"] = neLicense.ActivationRequestCode
|
||||
}
|
||||
if neLicense.LicensePath != "" {
|
||||
params["license_path"] = neLicense.LicensePath
|
||||
}
|
||||
if neLicense.SerialNum != "" {
|
||||
params["serial_num"] = neLicense.SerialNum
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
params["expiry_date"] = neLicense.ExpiryDate
|
||||
}
|
||||
if neLicense.Status != "" {
|
||||
params["status"] = neLicense.Status
|
||||
}
|
||||
if neLicense.Remark != "" {
|
||||
params["remark"] = neLicense.Remark
|
||||
}
|
||||
if neLicense.CreateBy != "" {
|
||||
params["create_by"] = neLicense.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_license (" + 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 *NeLicense) Update(neLicense model.NeLicense) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neLicense.NeType != "" {
|
||||
params["ne_type"] = neLicense.NeType
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
params["ne_id"] = neLicense.NeId
|
||||
}
|
||||
if neLicense.ActivationRequestCode != "" {
|
||||
params["activation_request_code"] = neLicense.ActivationRequestCode
|
||||
}
|
||||
if neLicense.LicensePath != "" {
|
||||
params["license_path"] = neLicense.LicensePath
|
||||
}
|
||||
if neLicense.SerialNum != "" {
|
||||
params["serial_num"] = neLicense.SerialNum
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
params["expiry_date"] = neLicense.ExpiryDate
|
||||
}
|
||||
if neLicense.Status != "" {
|
||||
params["status"] = neLicense.Status
|
||||
}
|
||||
if neLicense.Remark != "" {
|
||||
params["remark"] = neLicense.Remark
|
||||
}
|
||||
if neLicense.UpdateBy != "" {
|
||||
params["update_by"] = neLicense.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_license set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neLicense.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeLicense) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_license where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -1,297 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化数据层 NewNeLicense 结构体
|
||||
var NewNeLicenseImpl = &NeLicenseImpl{
|
||||
selectSql: `select
|
||||
id, ne_type, ne_id, activation_request_code, license_path, serial_num, expiry_date, status, remark, create_by, create_time, update_by, update_time
|
||||
from ne_license`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"activation_request_code": "ActivationRequestCode",
|
||||
"license_path": "LicensePath",
|
||||
"serial_num": "SerialNum",
|
||||
"expiry_date": "ExpiryDate",
|
||||
"status": "Status",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeLicenseImpl 网元授权激活信息 数据层处理
|
||||
type NeLicenseImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeLicenseImpl) convertResultRows(rows []map[string]any) []model.NeLicense {
|
||||
arr := make([]model.NeLicense, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeLicense{}
|
||||
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 *NeLicenseImpl) 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, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["expiryDate"]; ok && v != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["createBy"]; ok && v != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_license"
|
||||
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 *NeLicenseImpl) SelectList(neLicense model.NeLicense) []model.NeLicense {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neLicense.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neLicense.NeType)
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neLicense.NeId)
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, neLicense.ExpiryDate)
|
||||
}
|
||||
if neLicense.CreateBy != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, neLicense.CreateBy)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
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 *NeLicenseImpl) SelectByIds(cmdIds []string) []model.NeLicense {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeLicense{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeLicenseImpl) Insert(neLicense model.NeLicense) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neLicense.NeType != "" {
|
||||
params["ne_type"] = neLicense.NeType
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
params["ne_id"] = neLicense.NeId
|
||||
}
|
||||
if neLicense.ActivationRequestCode != "" {
|
||||
params["activation_request_code"] = neLicense.ActivationRequestCode
|
||||
}
|
||||
if neLicense.LicensePath != "" {
|
||||
params["license_path"] = neLicense.LicensePath
|
||||
}
|
||||
if neLicense.SerialNum != "" {
|
||||
params["serial_num"] = neLicense.SerialNum
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
params["expiry_date"] = neLicense.ExpiryDate
|
||||
}
|
||||
if neLicense.Status != "" {
|
||||
params["status"] = neLicense.Status
|
||||
}
|
||||
if neLicense.Remark != "" {
|
||||
params["remark"] = neLicense.Remark
|
||||
}
|
||||
if neLicense.CreateBy != "" {
|
||||
params["create_by"] = neLicense.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_license (" + 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 *NeLicenseImpl) Update(neLicense model.NeLicense) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neLicense.NeType != "" {
|
||||
params["ne_type"] = neLicense.NeType
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
params["ne_id"] = neLicense.NeId
|
||||
}
|
||||
if neLicense.ActivationRequestCode != "" {
|
||||
params["activation_request_code"] = neLicense.ActivationRequestCode
|
||||
}
|
||||
if neLicense.LicensePath != "" {
|
||||
params["license_path"] = neLicense.LicensePath
|
||||
}
|
||||
if neLicense.SerialNum != "" {
|
||||
params["serial_num"] = neLicense.SerialNum
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
params["expiry_date"] = neLicense.ExpiryDate
|
||||
}
|
||||
if neLicense.Status != "" {
|
||||
params["status"] = neLicense.Status
|
||||
}
|
||||
if neLicense.Remark != "" {
|
||||
params["remark"] = neLicense.Remark
|
||||
}
|
||||
if neLicense.UpdateBy != "" {
|
||||
params["update_by"] = neLicense.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_license set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neLicense.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeLicenseImpl) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_license where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
@@ -1,27 +1,317 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/network_element/model"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// INeSoftware 网元软件包信息 数据层接口
|
||||
type INeSoftware interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
"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"
|
||||
)
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(neSoftware model.NeSoftware) []model.NeSoftware
|
||||
// 实例化数据层 NeSoftware 结构体
|
||||
var NewNeSoftware = &NeSoftware{
|
||||
selectSql: `select
|
||||
id, ne_type, name, path, version, description, create_by, create_time, update_by, update_time
|
||||
from ne_software`,
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(ids []string) []model.NeSoftware
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neSoftware model.NeSoftware) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neSoftware model.NeSoftware) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(ids []string) int64
|
||||
|
||||
// CheckUniqueTypeAndNameAndVersion 校验网元类型和文件名版本是否唯一
|
||||
CheckUniqueTypeAndNameAndVersion(neSoftware model.NeSoftware) string
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"name": "Name",
|
||||
"path": "Path",
|
||||
"version": "Version",
|
||||
"description": "Description",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeSoftware 网元软件包信息 数据层处理
|
||||
type NeSoftware struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeSoftware) convertResultRows(rows []map[string]any) []model.NeSoftware {
|
||||
arr := make([]model.NeSoftware, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeSoftware{}
|
||||
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 *NeSoftware) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
softwareType := v.(string)
|
||||
if strings.Contains(softwareType, ",") {
|
||||
softwareTypeArr := strings.Split(softwareType, ",")
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(softwareTypeArr))
|
||||
conditions = append(conditions, "ne_type in ("+placeholder+")")
|
||||
parameters := repo.ConvertIdsSlice(softwareTypeArr)
|
||||
params = append(params, parameters...)
|
||||
} else {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.Trim(softwareType, " "))
|
||||
}
|
||||
}
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
conditions = append(conditions, "name like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["version"]; ok && v != "" {
|
||||
conditions = append(conditions, "version like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_software"
|
||||
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 := " order by id desc 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 *NeSoftware) SelectList(neSoftware model.NeSoftware) []model.NeSoftware {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neSoftware.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neSoftware.NeType)
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
conditions = append(conditions, "path = ?")
|
||||
params = append(params, neSoftware.Path)
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
conditions = append(conditions, "version = ?")
|
||||
params = append(params, neSoftware.Version)
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
conditions = append(conditions, "name like concat(?, '%')")
|
||||
params = append(params, neSoftware.Name)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by id desc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeSoftware) SelectByIds(cmdIds []string) []model.NeSoftware {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeSoftware{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueTypeAndNameAndVersion 校验网元类型和文件名版本是否唯一
|
||||
func (r *NeSoftware) CheckUniqueTypeAndNameAndVersion(neSoftware model.NeSoftware) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neSoftware.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neSoftware.NeType)
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
conditions = append(conditions, "version = ?")
|
||||
params = append(params, neSoftware.Version)
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
conditions = append(conditions, "name = ?")
|
||||
params = append(params, neSoftware.Name)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select id as 'str' from ne_software " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeSoftware) Insert(neSoftware model.NeSoftware) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neSoftware.NeType != "" {
|
||||
params["ne_type"] = neSoftware.NeType
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
params["name"] = neSoftware.Name
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
params["path"] = neSoftware.Path
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
params["version"] = neSoftware.Version
|
||||
}
|
||||
params["description"] = neSoftware.Description
|
||||
if neSoftware.CreateBy != "" {
|
||||
params["create_by"] = neSoftware.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_software (" + 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 *NeSoftware) Update(neSoftware model.NeSoftware) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neSoftware.NeType != "" {
|
||||
params["ne_type"] = neSoftware.NeType
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
params["name"] = neSoftware.Name
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
params["path"] = neSoftware.Path
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
params["version"] = neSoftware.Version
|
||||
}
|
||||
params["description"] = neSoftware.Description
|
||||
if neSoftware.UpdateBy != "" {
|
||||
params["update_by"] = neSoftware.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_software set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neSoftware.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeSoftware) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_software where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -1,317 +0,0 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化数据层 NewNeSoftware 结构体
|
||||
var NewNeSoftwareImpl = &NeSoftwareImpl{
|
||||
selectSql: `select
|
||||
id, ne_type, name, path, version, description, create_by, create_time, update_by, update_time
|
||||
from ne_software`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"name": "Name",
|
||||
"path": "Path",
|
||||
"version": "Version",
|
||||
"description": "Description",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
|
||||
// NeSoftwareImpl 网元软件包信息 数据层处理
|
||||
type NeSoftwareImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeSoftwareImpl) convertResultRows(rows []map[string]any) []model.NeSoftware {
|
||||
arr := make([]model.NeSoftware, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeSoftware{}
|
||||
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 *NeSoftwareImpl) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
softwareType := v.(string)
|
||||
if strings.Contains(softwareType, ",") {
|
||||
softwareTypeArr := strings.Split(softwareType, ",")
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(softwareTypeArr))
|
||||
conditions = append(conditions, "ne_type in ("+placeholder+")")
|
||||
parameters := repo.ConvertIdsSlice(softwareTypeArr)
|
||||
params = append(params, parameters...)
|
||||
} else {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.Trim(softwareType, " "))
|
||||
}
|
||||
}
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
conditions = append(conditions, "name like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["version"]; ok && v != "" {
|
||||
conditions = append(conditions, "version like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_software"
|
||||
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 := " order by id desc 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 *NeSoftwareImpl) SelectList(neSoftware model.NeSoftware) []model.NeSoftware {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neSoftware.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neSoftware.NeType)
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
conditions = append(conditions, "path = ?")
|
||||
params = append(params, neSoftware.Path)
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
conditions = append(conditions, "version = ?")
|
||||
params = append(params, neSoftware.Version)
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
conditions = append(conditions, "name like concat(?, '%')")
|
||||
params = append(params, neSoftware.Name)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by id desc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeSoftwareImpl) SelectByIds(cmdIds []string) []model.NeSoftware {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeSoftware{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueTypeAndNameAndVersion 校验网元类型和文件名版本是否唯一
|
||||
func (r *NeSoftwareImpl) CheckUniqueTypeAndNameAndVersion(neSoftware model.NeSoftware) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neSoftware.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neSoftware.NeType)
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
conditions = append(conditions, "version = ?")
|
||||
params = append(params, neSoftware.Version)
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
conditions = append(conditions, "name = ?")
|
||||
params = append(params, neSoftware.Name)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select id as 'str' from ne_software " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeSoftwareImpl) Insert(neSoftware model.NeSoftware) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neSoftware.NeType != "" {
|
||||
params["ne_type"] = neSoftware.NeType
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
params["name"] = neSoftware.Name
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
params["path"] = neSoftware.Path
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
params["version"] = neSoftware.Version
|
||||
}
|
||||
params["description"] = neSoftware.Description
|
||||
if neSoftware.CreateBy != "" {
|
||||
params["create_by"] = neSoftware.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_software (" + 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 *NeSoftwareImpl) Update(neSoftware model.NeSoftware) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neSoftware.NeType != "" {
|
||||
params["ne_type"] = neSoftware.NeType
|
||||
}
|
||||
if neSoftware.Name != "" {
|
||||
params["name"] = neSoftware.Name
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
params["path"] = neSoftware.Path
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
params["version"] = neSoftware.Version
|
||||
}
|
||||
params["description"] = neSoftware.Description
|
||||
if neSoftware.UpdateBy != "" {
|
||||
params["update_by"] = neSoftware.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_software set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neSoftware.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeSoftwareImpl) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_software where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user