feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -3,7 +3,7 @@ package repository
|
||||
import (
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
@@ -16,7 +16,7 @@ type NeConfig struct{}
|
||||
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeConfig) SelectByPage(query map[string]string) ([]model.NeConfig, int64) {
|
||||
tx := datasource.DB("").Model(&model.NeConfig{})
|
||||
tx := db.DB("").Model(&model.NeConfig{})
|
||||
// 查询条件拼接
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
@@ -35,7 +35,7 @@ func (r NeConfig) SelectByPage(query map[string]string) ([]model.NeConfig, int64
|
||||
}
|
||||
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := datasource.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
@@ -45,9 +45,9 @@ func (r NeConfig) SelectByPage(query map[string]string) ([]model.NeConfig, int64
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *NeConfig) SelectList(param model.NeConfig) []model.NeConfig {
|
||||
tx := datasource.DB("").Model(&model.NeConfig{})
|
||||
// Select 查询集合
|
||||
func (r NeConfig) Select(param model.NeConfig) []model.NeConfig {
|
||||
tx := db.DB("").Model(&model.NeConfig{})
|
||||
// 查询条件拼接
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", param.NeType)
|
||||
@@ -66,12 +66,12 @@ func (r *NeConfig) SelectList(param model.NeConfig) []model.NeConfig {
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeConfig) SelectByIds(ids []string) []model.NeConfig {
|
||||
func (r NeConfig) SelectByIds(ids []int64) []model.NeConfig {
|
||||
rows := []model.NeConfig{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := datasource.DB("").Model(&model.NeConfig{})
|
||||
tx := db.DB("").Model(&model.NeConfig{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
@@ -83,23 +83,23 @@ func (r *NeConfig) SelectByIds(ids []string) []model.NeConfig {
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeConfig) Insert(param model.NeConfig) string {
|
||||
func (r NeConfig) Insert(param model.NeConfig) int64 {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
// 执行插入
|
||||
if err := datasource.DB("").Create(¶m).Error; err != nil {
|
||||
if err := db.DB("").Create(¶m).Error; err != nil {
|
||||
logger.Errorf("insert err => %v", err.Error())
|
||||
return ""
|
||||
return 0
|
||||
}
|
||||
return param.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *NeConfig) Update(param model.NeConfig) int64 {
|
||||
if param.ID == "" {
|
||||
func (r NeConfig) Update(param model.NeConfig) int64 {
|
||||
if param.ID == 0 {
|
||||
return 0
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := datasource.DB("").Model(&model.NeConfig{})
|
||||
tx := db.DB("").Model(&model.NeConfig{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id")
|
||||
@@ -112,11 +112,11 @@ func (r *NeConfig) Update(param model.NeConfig) int64 {
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeConfig) DeleteByIds(ids []string) int64 {
|
||||
func (r NeConfig) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := datasource.DB("").Where("id in ?", ids)
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeConfig{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
|
||||
@@ -1,262 +1,118 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 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`,
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
var NewNeConfigBackup = &NeConfigBackup{}
|
||||
|
||||
// NeConfigBackup 网元配置文件备份记录 数据层处理
|
||||
type NeConfigBackup struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeConfigBackup struct{}
|
||||
|
||||
// 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 {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeConfigBackup) SelectByPage(query map[string]string) ([]model.NeConfigBackup, int64) {
|
||||
tx := db.DB("").Model(&model.NeConfigBackup{})
|
||||
// 查询条件拼接
|
||||
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), " "))
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("neId = ?", v)
|
||||
}
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
conditions = append(conditions, "name like concat(concat('%', ?), '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("name like concat(concat('%', ?), '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeConfigBackup{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
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)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Order("id desc").Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询信息
|
||||
func (r NeConfigBackup) SelectByIds(ids []int64) []model.NeConfigBackup {
|
||||
rows := []model.NeConfigBackup{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by id desc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
tx := db.DB("").Model(&model.NeConfigBackup{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
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
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
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)
|
||||
// Insert 新增信息 返回新增数据ID
|
||||
func (r NeConfigBackup) Insert(item model.NeConfigBackup) int64 {
|
||||
if item.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
item.UpdateBy = item.CreateBy
|
||||
item.UpdateTime = ms
|
||||
item.CreateTime = ms
|
||||
}
|
||||
// 执行插入
|
||||
if err := db.DB("").Create(&item).Error; err != nil {
|
||||
logger.Errorf("insert err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
return item.ID
|
||||
}
|
||||
|
||||
// Update 修改信息 返回受影响行数
|
||||
func (r NeConfigBackup) Update(item model.NeConfigBackup) int64 {
|
||||
if item.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
if item.UpdateBy != "" {
|
||||
item.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
tx := db.DB("").Model(&model.NeConfigBackup{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", item.ID)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(item).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息 返回受影响行数
|
||||
func (r NeConfigBackup) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeConfigBackup{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
@@ -2,117 +2,47 @@ package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 NeHost 结构体
|
||||
var NewNeHost = &NeHost{
|
||||
selectSql: `select
|
||||
host_id, host_type, group_id, title, addr, port, user, auth_mode, password, private_key, pass_phrase, db_name, 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",
|
||||
"db_name": "DBName",
|
||||
"remark": "Remark",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
var NewNeHost = &NeHost{}
|
||||
|
||||
// NeHost 网元主机连接 数据层处理
|
||||
type NeHost struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeHost struct{}
|
||||
|
||||
// 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 {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeHost) SelectByPage(query map[string]string) ([]model.NeHost, int64) {
|
||||
tx := db.DB("").Model(&model.NeHost{})
|
||||
// 查询条件拼接
|
||||
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), " "))
|
||||
tx = tx.Where("host_type = ?", v)
|
||||
}
|
||||
if v, ok := query["groupId"]; ok && v != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("group_id = ?", v)
|
||||
}
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("title like concat(?, '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeHost{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
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)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
|
||||
// 排序
|
||||
orderSql := ""
|
||||
if sv, ok := query["sortField"]; ok && sv != "" {
|
||||
sortSql := fmt.Sprint(sv)
|
||||
if sortSql == "updateTime" {
|
||||
@@ -128,276 +58,120 @@ func (r *NeHost) SelectPage(query map[string]any) map[string]any {
|
||||
sortSql += " asc "
|
||||
}
|
||||
}
|
||||
orderSql = fmt.Sprintf(" order by %s ", sortSql)
|
||||
tx = tx.Order(sortSql)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + orderSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
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)
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// 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{}
|
||||
func (r NeHost) SelectByIds(ids []int64) []model.NeHost {
|
||||
rows := []model.NeHost{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
// 转换实体
|
||||
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 ""
|
||||
}
|
||||
|
||||
tx := db.DB("").Model(&model.NeHost{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
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.DBName != "" {
|
||||
params["db_name"] = neHost.DBName
|
||||
}
|
||||
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
|
||||
}
|
||||
if neHost.DBName != "" {
|
||||
params["db_name"] = neHost.DBName
|
||||
}
|
||||
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
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
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)
|
||||
// Insert 新增信息
|
||||
func (r NeHost) Insert(param model.NeHost) int64 {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
// 根据认证模式清除不必要的信息
|
||||
if param.AuthMode == "0" {
|
||||
param.PrivateKey = ""
|
||||
param.PassPhrase = ""
|
||||
}
|
||||
if param.AuthMode == "1" {
|
||||
param.Password = ""
|
||||
}
|
||||
if param.AuthMode == "2" {
|
||||
param.Password = ""
|
||||
param.PrivateKey = ""
|
||||
param.PassPhrase = ""
|
||||
}
|
||||
// 执行插入
|
||||
if err := db.DB("").Create(¶m).Error; err != nil {
|
||||
logger.Errorf("insert err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
return param.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeHost) Update(param model.NeHost) int64 {
|
||||
if param.ID == 0 {
|
||||
return 0
|
||||
}
|
||||
// 根据认证模式清除不必要的信息
|
||||
if param.AuthMode == "0" {
|
||||
param.PrivateKey = ""
|
||||
param.PassPhrase = ""
|
||||
}
|
||||
if param.AuthMode == "1" {
|
||||
param.Password = ""
|
||||
}
|
||||
if param.AuthMode == "2" {
|
||||
param.Password = ""
|
||||
param.PrivateKey = ""
|
||||
param.PassPhrase = ""
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeHost{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeHost) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeHost{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// CheckUnique 检查信息是否唯一 返回数据ID
|
||||
func (r NeHost) CheckUnique(param model.NeHost) int64 {
|
||||
tx := db.DB("").Model(&model.NeHost{})
|
||||
// 查询条件拼接
|
||||
if param.HostType != "" {
|
||||
tx = tx.Where("host_type = ?", param.HostType)
|
||||
}
|
||||
if param.GroupID != "" {
|
||||
tx = tx.Where("group_id = ?", param.GroupID)
|
||||
}
|
||||
if param.Title != "" {
|
||||
tx = tx.Where("title = ?", param.Title)
|
||||
}
|
||||
// 查询数据
|
||||
var id int64 = 0
|
||||
if err := tx.Select("id").Limit(1).Find(&id).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return id
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -1,306 +1,130 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 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`,
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
var NewNeHostCmd = &NeHostCmd{}
|
||||
|
||||
// NeHostCmd 网元主机连接 数据层处理
|
||||
type NeHostCmd struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeHostCmd struct{}
|
||||
|
||||
// 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 {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeHostCmd) SelectByPage(query map[string]string) ([]model.NeHostCmd, int64) {
|
||||
tx := db.DB("").Model(&model.NeHostCmd{})
|
||||
// 查询条件拼接
|
||||
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), " "))
|
||||
tx = tx.Where("cmd_type = ?", v)
|
||||
}
|
||||
if v, ok := query["groupId"]; ok && v != "" {
|
||||
conditions = append(conditions, "group_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("group_id = ?", v)
|
||||
}
|
||||
if v, ok := query["title"]; ok && v != "" {
|
||||
conditions = append(conditions, "title like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("title like concat(?, '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeHostCmd{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
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)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
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)
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// 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{}
|
||||
func (r NeHostCmd) SelectByIds(ids []int64) []model.NeHostCmd {
|
||||
rows := []model.NeHostCmd{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
// 转换实体
|
||||
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 ""
|
||||
}
|
||||
|
||||
tx := db.DB("").Model(&model.NeHostCmd{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
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
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
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)
|
||||
// Insert 新增信息
|
||||
func (r NeHostCmd) Insert(param model.NeHostCmd) int64 {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
// 执行插入
|
||||
if err := db.DB("").Create(¶m).Error; err != nil {
|
||||
logger.Errorf("insert err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
return param.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeHostCmd) Update(param model.NeHostCmd) int64 {
|
||||
if param.ID == 0 {
|
||||
return 0
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeHostCmd{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeHostCmd) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeHostCmd{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// CheckUnique 检查信息是否唯一 返回数据ID
|
||||
func (r NeHostCmd) CheckUnique(param model.NeHostCmd) int64 {
|
||||
tx := db.DB("").Model(&model.NeHostCmd{})
|
||||
// 查询条件拼接
|
||||
if param.CmdType != "" {
|
||||
tx = tx.Where("cmd_type = ?", param.CmdType)
|
||||
}
|
||||
if param.GroupID != "" {
|
||||
tx = tx.Where("group_id = ?", param.GroupID)
|
||||
}
|
||||
if param.Title != "" {
|
||||
tx = tx.Where("title = ?", param.Title)
|
||||
}
|
||||
// 查询数据
|
||||
var id int64 = 0
|
||||
if err := tx.Select("id").Limit(1).Find(&id).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return id
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package repository
|
||||
import (
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
@@ -62,7 +62,7 @@ func (r NeInfo) neListSort(arr []model.NeInfo) []model.NeInfo {
|
||||
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeInfo) SelectByPage(query map[string]string) ([]model.NeInfo, int64) {
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 查询条件拼接
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
@@ -84,7 +84,7 @@ func (r NeInfo) SelectByPage(query map[string]string) ([]model.NeInfo, int64) {
|
||||
}
|
||||
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := datasource.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Order("ne_type asc, ne_id asc").Find(&rows).Error
|
||||
if err != nil {
|
||||
@@ -94,23 +94,75 @@ func (r NeInfo) SelectByPage(query map[string]string) ([]model.NeInfo, int64) {
|
||||
return r.neListSort(rows), total
|
||||
}
|
||||
|
||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||
func (r NeInfo) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("ne_type = ? and ne_id = ?", neType, neID)
|
||||
// 查询数据
|
||||
row := model.NeInfo{}
|
||||
if err := tx.Limit(1).Find(&row).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return row
|
||||
// SelectByIds 通过ID查询
|
||||
func (r NeInfo) SelectByIds(ids []int64) []model.NeInfo {
|
||||
rows := []model.NeInfo{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
return row
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r NeInfo) Insert(neInfo model.NeInfo) int64 {
|
||||
if neInfo.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
neInfo.CreateTime = ms
|
||||
neInfo.UpdateTime = ms
|
||||
neInfo.UpdateBy = neInfo.CreateBy
|
||||
}
|
||||
tx := db.DB("").Create(&neInfo)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return neInfo.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeInfo) Update(neInfo model.NeInfo) int64 {
|
||||
if neInfo.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
if neInfo.UpdateBy != "" {
|
||||
neInfo.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
neInfo.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", neInfo.ID)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(neInfo).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeInfo) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeInfo{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// SelectList 查询列表
|
||||
func (r NeInfo) SelectList(neInfo model.NeInfo) []model.NeInfo {
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
if neInfo.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", neInfo.NeType)
|
||||
@@ -128,26 +180,23 @@ func (r NeInfo) SelectList(neInfo model.NeInfo) []model.NeInfo {
|
||||
return r.neListSort(rows)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r NeInfo) SelectByIds(ids []string) []model.NeInfo {
|
||||
rows := []model.NeInfo{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||
func (r NeInfo) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
tx = tx.Where("ne_type = ? and ne_id = ?", neType, neID)
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
row := model.NeInfo{}
|
||||
if err := tx.Limit(1).Find(&row).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
return row
|
||||
}
|
||||
return rows
|
||||
return row
|
||||
}
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
func (r NeInfo) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string {
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
func (r NeInfo) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) int64 {
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 查询条件拼接
|
||||
if neInfo.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", neInfo.NeType)
|
||||
@@ -156,7 +205,7 @@ func (r NeInfo) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string {
|
||||
tx = tx.Where("ne_id = ?", neInfo.NeType)
|
||||
}
|
||||
// 查询数据
|
||||
id := ""
|
||||
var id int64 = 0
|
||||
if err := tx.Limit(1).Select("id").Find(&id).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return id
|
||||
@@ -164,48 +213,12 @@ func (r NeInfo) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string {
|
||||
return id
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeInfo) Insert(neInfo model.NeInfo) string {
|
||||
if neInfo.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
neInfo.CreateTime = ms
|
||||
neInfo.UpdateTime = ms
|
||||
neInfo.UpdateBy = neInfo.CreateBy
|
||||
}
|
||||
tx := datasource.DefaultDB().Create(&neInfo)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return neInfo.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *NeInfo) Update(neInfo model.NeInfo) int64 {
|
||||
if neInfo.ID == "" {
|
||||
// UpdateState 修改状态
|
||||
func (r NeInfo) UpdateState(id int64, status int64) int64 {
|
||||
if id <= 0 {
|
||||
return 0
|
||||
}
|
||||
if neInfo.UpdateBy != "" {
|
||||
neInfo.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
neInfo.UpdateTime = time.Now().UnixMilli()
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", neInfo.ID)
|
||||
tx = tx.Omit("id")
|
||||
// 执行更新
|
||||
if err := tx.Updates(neInfo).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *NeInfo) UpdateState(id, status string) int64 {
|
||||
if id == "" {
|
||||
return 0
|
||||
}
|
||||
tx := datasource.DB("").Model(&model.NeInfo{})
|
||||
tx := db.DB("").Model(&model.NeInfo{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", id)
|
||||
// 执行更新
|
||||
@@ -215,16 +228,3 @@ func (r *NeInfo) UpdateState(id, status string) int64 {
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除网元信息
|
||||
func (r NeInfo) DeleteByIds(ids []string) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := datasource.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeInfo{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
@@ -1,297 +1,148 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 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`,
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
var NewNeLicense = &NeLicense{}
|
||||
|
||||
// NeLicense 网元授权激活信息 数据层处理
|
||||
type NeLicense struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeLicense struct{}
|
||||
|
||||
// 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 {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeLicense) SelectByPage(query map[string]string) ([]model.NeLicense, int64) {
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 查询条件拼接
|
||||
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), " "))
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("ne_id = ?", v)
|
||||
}
|
||||
if v, ok := query["expiryDate"]; ok && v != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("expiry_date = ?", v)
|
||||
}
|
||||
if v, ok := query["serialNum"]; ok && v != "" {
|
||||
tx = tx.Where("serial_num = ?", v)
|
||||
}
|
||||
if v, ok := query["createBy"]; ok && v != "" {
|
||||
conditions = append(conditions, "create_by like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("create_by like concat(?, '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeLicense{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
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)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
tx = tx.Order("id desc")
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
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
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *NeLicense) SelectList(neLicense model.NeLicense) []model.NeLicense {
|
||||
// Select 查询集合
|
||||
func (r NeLicense) Select(param model.NeLicense) []model.NeLicense {
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neLicense.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neLicense.NeType)
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", param.NeType)
|
||||
}
|
||||
if neLicense.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neLicense.NeId)
|
||||
if param.NeId != "" {
|
||||
tx = tx.Where("ne_id = ?", param.NeId)
|
||||
}
|
||||
if neLicense.ExpiryDate != "" {
|
||||
conditions = append(conditions, "expiry_date = ?")
|
||||
params = append(params, neLicense.ExpiryDate)
|
||||
if param.ExpiryDate != "" {
|
||||
tx = tx.Where("expiry_date = ?", param.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 ")
|
||||
if param.CreateBy != "" {
|
||||
tx = tx.Where("create_by like concat(?, '%')", param.CreateBy)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
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
|
||||
rows := []model.NeLicense{}
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
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)
|
||||
// SelectByIds 通过ID查询
|
||||
func (r NeLicense) SelectByIds(ids []int64) []model.NeLicense {
|
||||
rows := []model.NeLicense{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r NeLicense) Insert(neInfo model.NeLicense) int64 {
|
||||
if neInfo.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
neInfo.CreateTime = ms
|
||||
neInfo.UpdateTime = ms
|
||||
neInfo.UpdateBy = neInfo.CreateBy
|
||||
}
|
||||
tx := db.DB("").Create(&neInfo)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return neInfo.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeLicense) Update(param model.NeLicense) int64 {
|
||||
if param.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
if param.UpdateBy != "" {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeLicense{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeLicense) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeLicense{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
@@ -1,317 +1,170 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 NeSoftware 结构体
|
||||
var NewNeSoftware = &NeSoftware{
|
||||
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",
|
||||
},
|
||||
}
|
||||
var NewNeSoftware = &NeSoftware{}
|
||||
|
||||
// NeSoftware 网元软件包信息 数据层处理
|
||||
type NeSoftware struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeSoftware struct{}
|
||||
|
||||
// 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 {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeSoftware) SelectByPage(query map[string]string) ([]model.NeSoftware, int64) {
|
||||
tx := db.DB("").Model(&model.NeSoftware{})
|
||||
// 查询条件拼接
|
||||
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...)
|
||||
if strings.Contains(v, ",") {
|
||||
softwareTypeArr := strings.Split(v, ",")
|
||||
tx = tx.Where("ne_type in ?", softwareTypeArr)
|
||||
} else {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, strings.Trim(softwareType, " "))
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
}
|
||||
if v, ok := query["name"]; ok && v != "" {
|
||||
conditions = append(conditions, "name like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("name like concat(?, '%')", v)
|
||||
}
|
||||
if v, ok := query["version"]; ok && v != "" {
|
||||
conditions = append(conditions, "version like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("version like concat(?, '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeSoftware{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
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)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
tx = tx.Order("id desc")
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
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
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *NeSoftware) SelectList(neSoftware model.NeSoftware) []model.NeSoftware {
|
||||
// Select 查询集合
|
||||
func (r NeSoftware) Select(param model.NeSoftware) []model.NeSoftware {
|
||||
tx := db.DB("").Model(&model.NeSoftware{})
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neSoftware.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neSoftware.NeType)
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", param.NeType)
|
||||
}
|
||||
if neSoftware.Path != "" {
|
||||
conditions = append(conditions, "path = ?")
|
||||
params = append(params, neSoftware.Path)
|
||||
if param.Path != "" {
|
||||
tx = tx.Where("path = ?", param.Path)
|
||||
}
|
||||
if neSoftware.Version != "" {
|
||||
conditions = append(conditions, "version = ?")
|
||||
params = append(params, neSoftware.Version)
|
||||
if param.Version != "" {
|
||||
tx = tx.Where("version = ?", param.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 ")
|
||||
if param.Name != "" {
|
||||
tx = tx.Where("name like concat(?, '%')", param.Name)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
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
|
||||
rows := []model.NeSoftware{}
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
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)
|
||||
// SelectByIds 通过ID查询
|
||||
func (r NeSoftware) SelectByIds(ids []int64) []model.NeSoftware {
|
||||
rows := []model.NeSoftware{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := db.DB("").Model(&model.NeSoftware{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r NeSoftware) Insert(param model.NeSoftware) int64 {
|
||||
if param.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
param.CreateTime = ms
|
||||
param.UpdateTime = ms
|
||||
param.UpdateBy = param.CreateBy
|
||||
}
|
||||
tx := db.DB("").Create(¶m)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return param.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeSoftware) Update(param model.NeSoftware) int64 {
|
||||
if param.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
if param.UpdateBy != "" {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeSoftware{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeSoftware) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeSoftware{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// CheckUnique 检查信息是否唯一 返回数据ID
|
||||
func (r NeSoftware) CheckUnique(param model.NeSoftware) int64 {
|
||||
tx := db.DB("").Model(&model.NeSoftware{})
|
||||
// 查询条件拼接
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", param.NeType)
|
||||
}
|
||||
if param.Path != "" {
|
||||
tx = tx.Where("path = ?", param.Path)
|
||||
}
|
||||
if param.Version != "" {
|
||||
tx = tx.Where("version = ?", param.Version)
|
||||
}
|
||||
// 查询数据
|
||||
var id int64 = 0
|
||||
if err := tx.Select("id").Limit(1).Find(&id).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return id
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -1,329 +1,148 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/network_element/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 NeVersion 结构体
|
||||
var NewNeVersion = &NeVersion{
|
||||
selectSql: `select
|
||||
id, ne_type, ne_id, name, version, path, pre_name, pre_version, pre_path, new_name, new_version, new_path, status, create_by, create_time, update_by, update_time
|
||||
from ne_version`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"ne_type": "NeType",
|
||||
"ne_id": "NeId",
|
||||
"name": "name",
|
||||
"version": "Version",
|
||||
"path": "Path",
|
||||
"pre_name": "preName",
|
||||
"pre_version": "PreVersion",
|
||||
"pre_path": "PrePath",
|
||||
"new_name": "NewName",
|
||||
"new_version": "NewVersion",
|
||||
"new_path": "NewPath",
|
||||
"status": "Status",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
},
|
||||
}
|
||||
var NewNeVersion = &NeVersion{}
|
||||
|
||||
// NeVersion 网元版本信息 数据层处理
|
||||
type NeVersion struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type NeVersion struct{}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *NeVersion) convertResultRows(rows []map[string]any) []model.NeVersion {
|
||||
arr := make([]model.NeVersion, 0)
|
||||
for _, row := range rows {
|
||||
item := model.NeVersion{}
|
||||
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 *NeVersion) SelectPage(query map[string]any) map[string]any {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r NeVersion) SelectByPage(query map[string]string) ([]model.NeVersion, int64) {
|
||||
tx := db.DB("").Model(&model.NeVersion{})
|
||||
// 查询条件拼接
|
||||
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), " "))
|
||||
tx = tx.Where("ne_type = ?", v)
|
||||
}
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("ne_id = ?", v)
|
||||
}
|
||||
if v, ok := query["version"]; ok && v != "" {
|
||||
conditions = append(conditions, "version like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("version like concat(?, '%')", v)
|
||||
}
|
||||
if v, ok := query["path"]; ok && v != "" {
|
||||
conditions = append(conditions, "path like concat(?, '%')")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("path like concat(?, '%')", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.NeVersion{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.NeHost{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_version"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
tx = tx.Order("update_time desc")
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
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 update_time 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
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *NeVersion) SelectList(neVersion model.NeVersion) []model.NeVersion {
|
||||
// Select 查询集合
|
||||
func (r NeVersion) Select(param model.NeVersion) []model.NeVersion {
|
||||
tx := db.DB("").Model(&model.NeSoftware{})
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neVersion.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neVersion.NeType)
|
||||
if param.NeType != "" {
|
||||
tx = tx.Where("ne_type = ?", param.NeType)
|
||||
}
|
||||
if neVersion.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neVersion.NeId)
|
||||
if param.NeId != "" {
|
||||
tx = tx.Where("ne_id = ?", param.NeId)
|
||||
}
|
||||
if neVersion.Version != "" {
|
||||
conditions = append(conditions, "version like concat(?, '%')")
|
||||
params = append(params, neVersion.Version)
|
||||
if param.Version != "" {
|
||||
tx = tx.Where("version like concat(?, '%')", param.Version)
|
||||
}
|
||||
if neVersion.Path != "" {
|
||||
conditions = append(conditions, "path like concat(?, '%')")
|
||||
params = append(params, neVersion.Path)
|
||||
if param.Path != "" {
|
||||
tx = tx.Where("path like concat(?, '%')", param.Path)
|
||||
}
|
||||
if neVersion.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, neVersion.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
if param.Status != "" {
|
||||
tx = tx.Where("status = ?", param.Status)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
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 *NeVersion) SelectByIds(cmdIds []string) []model.NeVersion {
|
||||
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.NeVersion{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeVersion) Insert(neVersion model.NeVersion) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neVersion.NeType != "" {
|
||||
params["ne_type"] = neVersion.NeType
|
||||
}
|
||||
if neVersion.NeId != "" {
|
||||
params["ne_id"] = neVersion.NeId
|
||||
}
|
||||
if neVersion.Name != "" {
|
||||
params["name"] = neVersion.Name
|
||||
}
|
||||
if neVersion.Version != "" {
|
||||
params["version"] = neVersion.Version
|
||||
}
|
||||
if neVersion.Path != "" {
|
||||
params["path"] = neVersion.Path
|
||||
}
|
||||
if neVersion.PreName != "" {
|
||||
params["pre_name"] = neVersion.PreName
|
||||
}
|
||||
if neVersion.PreVersion != "" {
|
||||
params["pre_version"] = neVersion.PreVersion
|
||||
}
|
||||
if neVersion.PrePath != "" {
|
||||
params["pre_path"] = neVersion.PrePath
|
||||
}
|
||||
if neVersion.NewName != "" {
|
||||
params["new_name"] = neVersion.NewName
|
||||
}
|
||||
if neVersion.NewVersion != "" {
|
||||
params["new_version"] = neVersion.NewVersion
|
||||
}
|
||||
if neVersion.NewPath != "" {
|
||||
params["new_path"] = neVersion.NewPath
|
||||
}
|
||||
if neVersion.Status != "" {
|
||||
params["status"] = neVersion.Status
|
||||
}
|
||||
if neVersion.CreateBy != "" {
|
||||
params["create_by"] = neVersion.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_version (" + 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 *NeVersion) Update(neVersion model.NeVersion) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neVersion.NeType != "" {
|
||||
params["ne_type"] = neVersion.NeType
|
||||
}
|
||||
if neVersion.NeId != "" {
|
||||
params["ne_id"] = neVersion.NeId
|
||||
}
|
||||
if neVersion.Name != "" {
|
||||
params["name"] = neVersion.Name
|
||||
}
|
||||
if neVersion.Version != "" {
|
||||
params["version"] = neVersion.Version
|
||||
}
|
||||
if neVersion.Path != "" {
|
||||
params["path"] = neVersion.Path
|
||||
}
|
||||
if neVersion.PreName != "" {
|
||||
params["pre_name"] = neVersion.PreName
|
||||
}
|
||||
if neVersion.PreVersion != "" {
|
||||
params["pre_version"] = neVersion.PreVersion
|
||||
}
|
||||
if neVersion.PrePath != "" {
|
||||
params["pre_path"] = neVersion.PrePath
|
||||
}
|
||||
if neVersion.NewName != "" {
|
||||
params["new_name"] = neVersion.NewName
|
||||
}
|
||||
if neVersion.NewVersion != "" {
|
||||
params["new_version"] = neVersion.NewVersion
|
||||
}
|
||||
if neVersion.NewPath != "" {
|
||||
params["new_path"] = neVersion.NewPath
|
||||
}
|
||||
if neVersion.Status != "" {
|
||||
params["status"] = neVersion.Status
|
||||
}
|
||||
if neVersion.UpdateBy != "" {
|
||||
params["update_by"] = neVersion.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_version set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neVersion.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
rows := []model.NeVersion{}
|
||||
if err := tx.Order("id asc").Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeVersion) DeleteByIds(cmdIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||
sql := "delete from ne_version where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
// SelectByIds 通过ID查询
|
||||
func (r NeVersion) SelectByIds(ids []int64) []model.NeVersion {
|
||||
rows := []model.NeVersion{}
|
||||
if len(ids) <= 0 {
|
||||
return rows
|
||||
}
|
||||
tx := db.DB("").Model(&model.NeVersion{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id in ?", ids)
|
||||
// 查询数据
|
||||
if err := tx.Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r NeVersion) Insert(param model.NeVersion) int64 {
|
||||
if param.CreateBy != "" {
|
||||
ms := time.Now().UnixMilli()
|
||||
param.CreateTime = ms
|
||||
param.UpdateTime = ms
|
||||
param.UpdateBy = param.CreateBy
|
||||
}
|
||||
tx := db.DB("").Create(¶m)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
return param.ID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r NeVersion) Update(param model.NeVersion) int64 {
|
||||
if param.ID <= 0 {
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
if param.UpdateBy != "" {
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
}
|
||||
param.UpdateTime = time.Now().UnixMilli()
|
||||
tx := db.DB("").Model(&model.NeVersion{})
|
||||
// 构建查询条件
|
||||
tx = tx.Where("id = ?", param.ID)
|
||||
tx = tx.Omit("id", "create_by", "create_time")
|
||||
// 执行更新
|
||||
if err := tx.Updates(param).Error; err != nil {
|
||||
logger.Errorf("update err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r NeVersion) DeleteByIds(ids []int64) int64 {
|
||||
if len(ids) <= 0 {
|
||||
return 0
|
||||
}
|
||||
tx := db.DB("").Where("id in ?", ids)
|
||||
if err := tx.Delete(&model.NeVersion{}).Error; err != nil {
|
||||
logger.Errorf("delete err => %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return tx.RowsAffected
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user