feat: 网元配置文件备份记录功能接口

This commit is contained in:
TsMask
2024-07-23 11:57:10 +08:00
parent fb4c6b483d
commit 0a3a835a85
6 changed files with 490 additions and 0 deletions

View File

@@ -0,0 +1,82 @@
package controller
import (
"strings"
"be.ems/src/framework/i18n"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/vo/result"
neService "be.ems/src/modules/network_element/service"
"github.com/gin-gonic/gin"
)
// NewNeConfigBackup 实例化控制层 NeConfigBackupController 结构体
var NewNeConfigBackup = &NeConfigBackupController{
neConfigBackupService: neService.NewNeConfigBackupImpl,
}
// 网元配置文件备份记录
//
// PATH /config/backup
type NeConfigBackupController struct {
// 网元配置文件备份记录服务
neConfigBackupService neService.INeConfigBackup
}
// 网元配置文件备份记录列表
//
// GET /list
func (s *NeConfigBackupController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.neConfigBackupService.SelectPage(querys)
c.JSON(200, result.Ok(data))
}
// 网元配置文件备份记录信息
//
// GET /?id=xx
func (s *NeConfigBackupController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
id, ok := c.GetQuery("id")
if !ok || id == "" {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
item := s.neConfigBackupService.SelectById(id)
if item.ID != id {
// 没有可访问主机命令数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfigBackup.noData")))
return
}
c.JSON(200, result.OkData(item))
}
// 网元配置文件备份记录删除
//
// DELETE /?id=xx
func (s *NeConfigBackupController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
id, ok := c.GetQuery("id")
if !ok || id == "" {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
ids := strings.Split(id, ",")
uniqueIDs := parse.RemoveDuplicates(ids)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
return
}
rows, err := s.neConfigBackupService.DeleteByIds(uniqueIDs)
if err != nil {
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
return
}
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
}

View File

@@ -0,0 +1,24 @@
package model
// NeConfigBackup 网元配置文件备份记录 ne_config_backup
type NeConfigBackup struct {
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
NeId string `json:"neId" gorm:"ne_id"` // 网元ID
Name string `json:"name" gorm:"name"` // 命名
Version string `json:"version" gorm:"version"` // 网元版本
Path string `json:"path" gorm:"path"` // 压缩包位置
Remark string `json:"remark" gorm:"remark"` // 备注
CreateBy string `json:"createBy" gorm:"create_by"` // 创建者
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
UpdateBy string `json:"updateBy" gorm:"update_by"` // 更新者
UpdateTime int64 `json:"updateTime" gorm:"update_time"` // 更新时间
// ====== 非数据库字段属性 ======
}
// TableName 表名称
func (*NeConfigBackup) TableName() string {
return "ne_config_backup"
}

View File

@@ -0,0 +1,24 @@
package repository
import "be.ems/src/modules/network_element/model"
// INeConfigBackup 网元配置文件备份记录 数据层接口
type INeConfigBackup interface {
// SelectPage 根据条件分页查询字典类型
SelectPage(query map[string]any) map[string]any
// SelectList 根据实体查询
SelectList(item model.NeConfigBackup) []model.NeConfigBackup
// SelectByIds 通过ID查询
SelectByIds(ids []string) []model.NeConfigBackup
// Insert 新增信息
Insert(item model.NeConfigBackup) string
// Update 修改信息
Update(item model.NeConfigBackup) int64
// DeleteByIds 批量删除信息
DeleteByIds(ids []string) int64
}

View File

@@ -0,0 +1,269 @@
package repository
import (
"strings"
"time"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/network_element/model"
)
// 实例化数据层 NewNeConfigBackupImpl 结构体
var NewNeConfigBackupImpl = &NeConfigBackupImpl{
selectSql: `select
id, ne_type, ne_id, name, version, 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",
"version": "Version",
"path": "Path",
"remark": "Remark",
"create_by": "CreateBy",
"create_time": "CreateTime",
"update_by": "UpdateBy",
"update_time": "UpdateTime",
},
}
// NeConfigBackupImpl 网元配置文件备份记录 数据层处理
type NeConfigBackupImpl struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
resultMap map[string]string
}
// convertResultRows 将结果记录转实体结果组
func (r *NeConfigBackupImpl) convertResultRows(rows []map[string]any) []model.NeConfigBackup {
arr := make([]model.NeConfigBackup, 0)
for _, row := range rows {
item := model.NeConfigBackup{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
}
}
arr = append(arr, item)
}
return arr
}
// SelectPage 根据条件分页查询字典类型
func (r *NeConfigBackupImpl) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
if v, ok := query["neType"]; ok && v != "" {
conditions = append(conditions, "ne_type = ?")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["neId"]; ok && v != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["name"]; ok && v != "" {
conditions = append(conditions, "name like concat(?, '%')")
params = append(params, strings.Trim(v.(string), " "))
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.NeHost{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from ne_config_backup"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
// 查询数据
querySql := r.selectSql + whereSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
return result
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
}
// SelectList 根据实体查询
func (r *NeConfigBackupImpl) SelectList(item model.NeConfigBackup) []model.NeConfigBackup {
// 查询条件拼接
var conditions []string
var params []any
if item.NeType != "" {
conditions = append(conditions, "ne_type = ?")
params = append(params, item.NeType)
}
if item.NeId != "" {
conditions = append(conditions, "ne_id = ?")
params = append(params, item.NeId)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
// 查询数据
querySql := r.selectSql + whereSql + " order by id desc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
}
// SelectByIds 通过ID查询
func (r *NeConfigBackupImpl) SelectByIds(cmdIds []string) []model.NeConfigBackup {
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
querySql := r.selectSql + " where id in (" + placeholder + ")"
parameters := repo.ConvertIdsSlice(cmdIds)
results, err := datasource.RawDB("", querySql, parameters)
if err != nil {
logger.Errorf("query err => %v", err)
return []model.NeConfigBackup{}
}
// 转换实体
return r.convertResultRows(results)
}
// Insert 新增信息
func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string {
// 参数拼接
params := make(map[string]any)
if item.NeType != "" {
params["ne_type"] = item.NeType
}
if item.NeId != "" {
params["ne_id"] = item.NeId
}
if item.Name != "" {
params["name"] = item.Name
}
if item.Version != "" {
params["version"] = item.Version
}
if item.Path != "" {
params["path"] = item.Path
}
if item.Remark != "" {
params["remark"] = item.Remark
}
if item.CreateBy != "" {
params["create_by"] = item.CreateBy
params["create_time"] = time.Now().UnixMilli()
}
// 构建执行语句
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
sql := "insert into ne_config_backup (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
db := datasource.DefaultDB()
// 开启事务
tx := db.Begin()
// 执行插入
err := tx.Exec(sql, values...).Error
if err != nil {
logger.Errorf("insert row : %v", err.Error())
tx.Rollback()
return ""
}
// 获取生成的自增 ID
var insertedID string
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
if err != nil {
logger.Errorf("insert last id : %v", err.Error())
tx.Rollback()
return ""
}
// 提交事务
tx.Commit()
return insertedID
}
// Update 修改信息
func (r *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 {
// 参数拼接
params := make(map[string]any)
if item.NeType != "" {
params["ne_type"] = item.NeType
}
if item.NeId != "" {
params["ne_id"] = item.NeId
}
if item.Name != "" {
params["name"] = item.Name
}
if item.Version != "" {
params["version"] = item.Version
}
if item.Path != "" {
params["path"] = item.Path
}
params["remark"] = item.Remark
if item.UpdateBy != "" {
params["update_by"] = item.UpdateBy
params["update_time"] = time.Now().UnixMilli()
}
// 构建执行语句
keys, values := repo.KeyValueByUpdate(params)
sql := "update ne_config_backup set " + strings.Join(keys, ",") + " where id = ?"
// 执行更新
values = append(values, item.ID)
rows, err := datasource.ExecDB("", sql, values)
if err != nil {
logger.Errorf("update row : %v", err.Error())
return 0
}
return rows
}
// DeleteByIds 批量删除信息
func (r *NeConfigBackupImpl) DeleteByIds(ids []string) int64 {
placeholder := repo.KeyPlaceholderByQuery(len(ids))
sql := "delete from ne_config_backup where id in (" + placeholder + ")"
parameters := repo.ConvertIdsSlice(ids)
results, err := datasource.ExecDB("", sql, parameters)
if err != nil {
logger.Errorf("delete err => %v", err)
return 0
}
return results
}

View File

@@ -0,0 +1,24 @@
package service
import "be.ems/src/modules/network_element/model"
// INeConfigBackup 网元配置文件备份记录 服务层接口
type INeConfigBackup interface {
// SelectNeHostPage 分页查询列表数据
SelectPage(query map[string]any) map[string]any
// SelectList 根据实体查询
SelectList(item model.NeConfigBackup) []model.NeConfigBackup
// SelectByIds 通过ID查询
SelectById(id string) model.NeConfigBackup
// Insert 新增信息
Insert(item model.NeConfigBackup) string
// Update 修改信息
Update(item model.NeConfigBackup) int64
// DeleteByIds 批量删除信息
DeleteByIds(ids []string) (int64, error)
}

View File

@@ -0,0 +1,67 @@
package service
import (
"fmt"
"be.ems/src/modules/network_element/model"
"be.ems/src/modules/network_element/repository"
)
// NewNeConfigBackupImpl 网元配置文件备份记录 实例化服务层
var NewNeConfigBackupImpl = &NeConfigBackupImpl{
neConfigBackupRepository: repository.NewNeConfigBackupImpl,
}
// NeConfigBackupImpl 网元配置文件备份记录 服务层处理
type NeConfigBackupImpl struct {
// 网元配置文件备份记录
neConfigBackupRepository repository.INeConfigBackup
}
// SelectNeHostPage 分页查询列表数据
func (r *NeConfigBackupImpl) SelectPage(query map[string]any) map[string]any {
return r.neConfigBackupRepository.SelectPage(query)
}
// SelectConfigList 查询列表
func (r *NeConfigBackupImpl) SelectList(item model.NeConfigBackup) []model.NeConfigBackup {
return r.neConfigBackupRepository.SelectList(item)
}
// SelectByIds 通过ID查询
func (r *NeConfigBackupImpl) SelectById(id string) model.NeConfigBackup {
if id == "" {
return model.NeConfigBackup{}
}
arr := r.neConfigBackupRepository.SelectByIds([]string{id})
if len(arr) > 0 {
return arr[0]
}
return model.NeConfigBackup{}
}
// Insert 新增信息
func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string {
return r.neConfigBackupRepository.Insert(item)
}
// Update 修改信息
func (r *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 {
return r.neConfigBackupRepository.Update(item)
}
// DeleteByIds 批量删除信息
func (r *NeConfigBackupImpl) DeleteByIds(ids []string) (int64, error) {
// 检查是否存在
data := r.neConfigBackupRepository.SelectByIds(ids)
if len(data) <= 0 {
return 0, fmt.Errorf("neConfigBackup.noData")
}
if len(data) == len(ids) {
rows := r.neConfigBackupRepository.DeleteByIds(ids)
return rows, nil
}
// 删除信息失败!
return 0, fmt.Errorf("delete fail")
}