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