feat: 更新多个模块以支持新的数据结构和日志格式

This commit is contained in:
TsMask
2025-02-20 10:08:27 +08:00
parent 045a2b6b01
commit f3c33b31ac
272 changed files with 13246 additions and 15885 deletions

View File

@@ -1,19 +1,20 @@
package controller
import (
"fmt"
"os"
"path/filepath"
"strings"
"be.ems/src/framework/i18n"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/reqctx"
"be.ems/src/framework/resp"
"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 结构体
@@ -33,33 +34,32 @@ type NeConfigBackupController struct {
// 网元配置文件备份记录列表
//
// GET /list
func (s *NeConfigBackupController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.neConfigBackupService.SelectPage(querys)
c.JSON(200, result.Ok(data))
func (s NeConfigBackupController) List(c *gin.Context) {
query := reqctx.QueryMap(c)
rows, total := s.neConfigBackupService.FindByPage(query)
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
}
// 网元配置文件备份记录信息
//
// GET /download?id=xx
func (s *NeConfigBackupController) Download(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")))
func (s NeConfigBackupController) Download(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Query("id"))
if id <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
return
}
item := s.neConfigBackupService.SelectById(id)
item := s.neConfigBackupService.FindById(id)
if item.ID != id {
// 没有可访问主机命令数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfigBackup.noData")))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neConfigBackup.noData")))
return
}
if _, err := os.Stat(item.Path); err != nil {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfigBackup.notFoundFile")))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neConfigBackup.notFoundFile")))
return
}
c.FileAttachment(item.Path, item.Name)
@@ -68,125 +68,129 @@ func (s *NeConfigBackupController) Download(c *gin.Context) {
// 网元配置文件备份记录修改
//
// PUT /
func (s *NeConfigBackupController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
func (s NeConfigBackupController) Edit(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body struct {
ID string `json:"id" binding:"required"` // 记录ID
ID int64 `json:"id" binding:"required"` // 记录ID
Name string `json:"name" binding:"required"` // 名称
Remark string `json:"remark" binding:"required"` // 备注
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.ID == "" {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
// 检查是否存在
data := s.neConfigBackupService.SelectById(body.ID)
data := s.neConfigBackupService.FindById(body.ID)
if data.ID != body.ID {
// 没有可访问主机命令数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfig.noData")))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neConfig.noData")))
return
}
data.Name = body.Name
data.Remark = body.Remark
data.UpdateBy = ctx.LoginUserToUserName(c)
data.UpdateBy = reqctx.LoginUserToUserName(c)
rows := s.neConfigBackupService.Update(data)
if rows > 0 {
c.JSON(200, result.Ok(nil))
c.JSON(200, resp.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
c.JSON(200, resp.Err(nil))
}
// 网元配置文件备份记录删除
//
// 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")))
func (s NeConfigBackupController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Query("id")
if id == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
return
}
// 处理字符转id数组后去重
ids := strings.Split(id, ",")
uniqueIDs := parse.RemoveDuplicates(ids)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
return
uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
// 转换成int64数组类型
ids := make([]int64, 0)
for _, v := range uniqueIDs {
ids = append(ids, parse.Number(v))
}
rows, err := s.neConfigBackupService.DeleteByIds(uniqueIDs)
rows, err := s.neConfigBackupService.DeleteByIds(ids)
if err != nil {
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
c.JSON(200, resp.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))
c.JSON(200, resp.OkMsg(msg))
}
// 网元配置文件备份导入
//
// POST /import
func (s *NeConfigBackupController) Import(c *gin.Context) {
language := ctx.AcceptLanguage(c)
func (s NeConfigBackupController) Import(c *gin.Context) {
language := reqctx.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")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
if !strings.HasSuffix(body.Path, ".zip") {
c.JSON(200, result.ErrMsg("Only supports decompression of zip files"))
c.JSON(200, resp.ErrMsg("Only supports decompression of zip files"))
return
}
// 查网元
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
neInfo := s.neInfoService.FindByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 将zip文件解压到本地后复制到网元端
localFilePath := body.Path
if body.Type == "upload" {
localFilePath = file.ParseUploadFilePath(body.Path)
localFilePath = file.ParseUploadFileAbsPath(body.Path)
}
if err := s.neConfigBackupService.NeConfigLocalToNe(neInfo, localFilePath); err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
if err := s.neConfigBackupService.FileLocalToNe(neInfo, localFilePath); err != nil {
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
c.JSON(200, result.Ok(nil))
c.JSON(200, resp.Ok(nil))
}
// 网元配置文件备份导出
//
// POST /export
func (s *NeConfigBackupController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
func (s NeConfigBackupController) Export(c *gin.Context) {
language := reqctx.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")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
return
}
// 查网元
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeId)
neInfo := s.neInfoService.FindByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 将网元文件备份到本地
zipFilePath, err := s.neConfigBackupService.NeConfigNeToLocal(neInfo)
zipFilePath, err := s.neConfigBackupService.FileNeToLocal(neInfo)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
// 新增备份记录
@@ -195,7 +199,7 @@ func (s *NeConfigBackupController) Export(c *gin.Context) {
NeId: neInfo.NeId,
Name: filepath.Base(zipFilePath),
Path: zipFilePath,
CreateBy: ctx.LoginUserToUserName(c),
CreateBy: reqctx.LoginUserToUserName(c),
}
s.neConfigBackupService.Insert(item)
c.FileAttachment(item.Path, item.Name)