feat: 网元配置文件记录结构体变更/导入导出接口声明到路由模块

This commit is contained in:
TsMask
2024-07-24 10:26:15 +08:00
parent 1b6b65c693
commit 68bbe3c750
4 changed files with 93 additions and 10 deletions

View File

@@ -1,19 +1,24 @@
package controller
import (
"path/filepath"
"strings"
"be.ems/src/framework/i18n"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/vo/result"
"be.ems/src/modules/network_element/model"
neService "be.ems/src/modules/network_element/service"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// NewNeConfigBackup 实例化控制层 NeConfigBackupController 结构体
var NewNeConfigBackup = &NeConfigBackupController{
neConfigBackupService: neService.NewNeConfigBackupImpl,
neInfoService: neService.NewNeInfoImpl,
}
// 网元配置文件备份记录
@@ -22,6 +27,8 @@ var NewNeConfigBackup = &NeConfigBackupController{
type NeConfigBackupController struct {
// 网元配置文件备份记录服务
neConfigBackupService neService.INeConfigBackup
// 网元信息服务
neInfoService neService.INeInfo
}
// 网元配置文件备份记录列表
@@ -80,3 +87,77 @@ func (s *NeConfigBackupController) Remove(c *gin.Context) {
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
}
// 网元配置文件备份导入
//
// POST /import
func (s *NeConfigBackupController) Import(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
NeType string `json:"neType" binding:"required"`
NeId string `json:"neId" binding:"required"`
Type string `json:"type" binding:"required,oneof=backup upload"` // 导入类型
Path string `json:"path" binding:"required"` // 文件路径
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
if !strings.HasSuffix(body.Path, ".zip") {
c.JSON(200, result.ErrMsg("Only supports decompression of zip files"))
return
}
// 查网元
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 将zip文件解压到本地后复制到网元端
localFilePath := body.Path
if body.Type == "upload" {
localFilePath = file.ParseUploadFilePath(body.Path)
}
if err := s.neConfigBackupService.NeConfigLocalToNe(neInfo, localFilePath); err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.JSON(200, result.Ok(nil))
}
// 网元配置文件备份导出
//
// POST /export
func (s *NeConfigBackupController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
NeType string `json:"neType" binding:"required"`
NeId string `json:"neId" binding:"required"`
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 查网元
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 将网元文件备份到本地
zipFilePath, err := s.neConfigBackupService.NeConfigNeToLocal(neInfo)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
// 新增备份记录
item := model.NeConfigBackup{
NeType: neInfo.NeType,
NeId: neInfo.NeId,
Name: filepath.Base(zipFilePath),
Path: zipFilePath,
}
s.neConfigBackupService.Insert(item)
c.FileAttachment(item.Path, item.Name)
}

View File

@@ -5,8 +5,7 @@ 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"` // 网元版本
Name string `json:"name" gorm:"name"` // 压缩包名称
Path string `json:"path" gorm:"path"` // 压缩包位置
Remark string `json:"remark" gorm:"remark"` // 备注
CreateBy string `json:"createBy" gorm:"create_by"` // 创建者

View File

@@ -325,6 +325,16 @@ func Setup(router *gin.Engine) {
middleware.PreAuthorize(nil),
controller.NewNeConfigBackup.Remove,
)
neConfigBackupGroup.POST("/import",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_IMPORT)),
controller.NewNeConfigBackup.Import,
)
neConfigBackupGroup.POST("/export",
middleware.PreAuthorize(nil),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigBackup", collectlogs.BUSINESS_TYPE_EXPORT)),
controller.NewNeConfigBackup.Export,
)
}
}

View File

@@ -14,7 +14,7 @@ import (
// 实例化数据层 NewNeConfigBackupImpl 结构体
var NewNeConfigBackupImpl = &NeConfigBackupImpl{
selectSql: `select
id, ne_type, ne_id, name, version, path, remark, create_by, create_time, update_by, update_time
id, ne_type, ne_id, name, path, remark, create_by, create_time, update_by, update_time
from ne_config_backup`,
resultMap: map[string]string{
@@ -22,7 +22,6 @@ var NewNeConfigBackupImpl = &NeConfigBackupImpl{
"ne_type": "NeType",
"ne_id": "NeId",
"name": "Name",
"version": "Version",
"path": "Path",
"remark": "Remark",
"create_by": "CreateBy",
@@ -175,9 +174,6 @@ func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string {
if item.Name != "" {
params["name"] = item.Name
}
if item.Version != "" {
params["version"] = item.Version
}
if item.Path != "" {
params["path"] = item.Path
}
@@ -229,9 +225,6 @@ func (r *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 {
if item.Name != "" {
params["name"] = item.Name
}
if item.Version != "" {
params["version"] = item.Version
}
if item.Path != "" {
params["path"] = item.Path
}