131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"ems.agt/src/framework/i18n"
|
|
"ems.agt/src/framework/utils/ctx"
|
|
"ems.agt/src/framework/utils/parse"
|
|
"ems.agt/src/framework/vo/result"
|
|
"ems.agt/src/modules/network_element/model"
|
|
neService "ems.agt/src/modules/network_element/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// 实例化控制层 NeInfoController 结构体
|
|
var NewNeInfo = &NeInfoController{
|
|
neInfoService: neService.NewNeInfoImpl,
|
|
}
|
|
|
|
// 网元信息请求
|
|
//
|
|
// PATH /
|
|
type NeInfoController struct {
|
|
// 网元信息服务
|
|
neInfoService neService.INeInfo
|
|
}
|
|
|
|
// neStateCacheMap 网元状态缓存最后一次成功的信息
|
|
var neStateCacheMap map[string]map[string]any = make(map[string]map[string]any)
|
|
|
|
// 网元状态
|
|
//
|
|
// GET /state
|
|
func (s *NeInfoController) NeState(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var querys struct {
|
|
NeType string `form:"neType" binding:"required"`
|
|
NeID string `form:"neId" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindQuery(&querys); err != nil {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
|
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
neKey := fmt.Sprintf("%s_%s", neInfo.NeType, neInfo.NeId)
|
|
|
|
// 网元直连
|
|
resData, err := neService.NeState(neInfo)
|
|
if err != nil {
|
|
// 异常取上次缓存
|
|
if v, ok := neStateCacheMap[neKey]; ok && v != nil {
|
|
v["online"] = false
|
|
neStateCacheMap[neKey] = v
|
|
} else {
|
|
neStateCacheMap[neKey] = map[string]any{
|
|
"online": false,
|
|
"neId": neInfo.NeId,
|
|
"neName": neInfo.NeName,
|
|
"neType": neInfo.NeType,
|
|
"neIP": neInfo.IP,
|
|
}
|
|
}
|
|
c.JSON(200, result.OkData(neStateCacheMap[neKey]))
|
|
return
|
|
}
|
|
|
|
// 存入缓存
|
|
resData["online"] = true
|
|
neStateCacheMap[neKey] = resData
|
|
c.JSON(200, result.OkData(resData))
|
|
}
|
|
|
|
// 网元neType和neID查询
|
|
//
|
|
// GET /info
|
|
func (s *NeInfoController) NeTypeAndID(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var querys struct {
|
|
NeType string `form:"neType" binding:"required"`
|
|
NeID string `form:"neId" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindQuery(&querys); err != nil {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
|
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
c.JSON(200, result.OkData(neInfo))
|
|
}
|
|
|
|
// 网元列表
|
|
//
|
|
// GET /list
|
|
func (s *NeInfoController) NeList(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var querys struct {
|
|
NeType string `form:"neType"`
|
|
NeId string `form:"neId"`
|
|
BandStatus string `form:"bandStatus"`
|
|
}
|
|
if err := c.ShouldBindQuery(&querys); err != nil {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 查询实体参数
|
|
ne := model.NeInfo{}
|
|
if querys.NeType != "" {
|
|
ne.NeType = querys.NeType
|
|
}
|
|
if querys.NeId != "" {
|
|
ne.NeId = querys.NeId
|
|
}
|
|
bandStatus := parse.Boolean(querys.BandStatus)
|
|
neList := s.neInfoService.SelectNeList(ne, bandStatus)
|
|
if len(neList) == 0 {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
c.JSON(200, result.OkData(neList))
|
|
}
|