225 lines
6.1 KiB
Go
225 lines
6.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"be.ems/src/framework/i18n"
|
|
"be.ems/src/framework/utils/ctx"
|
|
"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"
|
|
)
|
|
|
|
// 实例化控制层 NeLicenseController 结构体
|
|
var NewNeLicense = &NeLicenseController{
|
|
neLicenseService: neService.NewNeLicenseImpl,
|
|
neInfoService: neService.NewNeInfoImpl,
|
|
}
|
|
|
|
// 网元授权激活信息请求
|
|
//
|
|
// PATH /license
|
|
type NeLicenseController struct {
|
|
// 网元授权激活信息服务
|
|
neLicenseService neService.INeLicense
|
|
// 网元信息服务
|
|
neInfoService neService.INeInfo
|
|
}
|
|
|
|
// 网元授权激活信息列表
|
|
//
|
|
// GET /list
|
|
func (s *NeLicenseController) List(c *gin.Context) {
|
|
querys := ctx.QueryMap(c)
|
|
data := s.neLicenseService.SelectPage(querys)
|
|
|
|
// 过滤屏蔽授权文件
|
|
rows := data["rows"].([]model.NeLicense)
|
|
arr := &rows
|
|
for i := range *arr {
|
|
(*arr)[i].ActivationRequestCode = "-"
|
|
(*arr)[i].LicensePath = "-"
|
|
}
|
|
|
|
c.JSON(200, result.Ok(data))
|
|
}
|
|
|
|
// 网元授权激活信息
|
|
//
|
|
// GET /:licenseId
|
|
func (s *NeLicenseController) Info(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
licenseId := c.Param("licenseId")
|
|
if licenseId == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
neLicense := s.neLicenseService.SelectById(licenseId)
|
|
if neLicense.ID != licenseId {
|
|
// 没有可访问网元授权激活数据!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
|
|
return
|
|
}
|
|
|
|
c.JSON(200, result.OkData(neLicense))
|
|
}
|
|
|
|
// 网元neType和neID查询
|
|
//
|
|
// GET /byTypeAndID
|
|
func (s *NeLicenseController) 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
|
|
}
|
|
|
|
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(querys.NeType, querys.NeId)
|
|
if neLicense.NeId != querys.NeId {
|
|
// 没有可访问网元授权激活数据!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
|
|
return
|
|
}
|
|
|
|
c.JSON(200, result.OkData(neLicense))
|
|
}
|
|
|
|
// 网元授权激活授权申请码
|
|
//
|
|
// GET /code
|
|
func (s *NeLicenseController) Code(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
|
|
}
|
|
|
|
// 检查是否存在授权记录
|
|
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(querys.NeType, querys.NeId)
|
|
if neLicense.NeId != querys.NeId {
|
|
// 没有可访问网元授权激活数据!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
|
|
return
|
|
}
|
|
|
|
// 更新授权码
|
|
code, licensePath := s.neLicenseService.ReadLicenseInfo(neLicense)
|
|
neLicense.ActivationRequestCode = code
|
|
if licensePath != "" {
|
|
neLicense.LicensePath = licensePath
|
|
} else {
|
|
neLicense.SerialNum = ""
|
|
neLicense.ExpiryDate = ""
|
|
neLicense.Status = "0"
|
|
}
|
|
neLicense.UpdateBy = ctx.LoginUserToUserName(c)
|
|
s.neLicenseService.Update(neLicense)
|
|
|
|
c.JSON(200, result.OkData(code))
|
|
}
|
|
|
|
// 网元授权激活授权文件替换
|
|
//
|
|
// POST /change
|
|
func (s *NeLicenseController) Change(c *gin.Context) {
|
|
language := ctx.AcceptLanguage(c)
|
|
var body model.NeLicense
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil || body.LicensePath == "" {
|
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
|
return
|
|
}
|
|
|
|
// 检查是否存在授权记录
|
|
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(body.NeType, body.NeId)
|
|
if neLicense.NeId != body.NeId {
|
|
// 没有可访问网元授权激活数据!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
|
|
return
|
|
}
|
|
|
|
// 更新授权记录
|
|
if body.Remark != "" {
|
|
neLicense.Remark = body.Remark
|
|
}
|
|
neLicense.LicensePath = body.LicensePath
|
|
neLicense.Status = "0"
|
|
neLicense.UpdateBy = ctx.LoginUserToUserName(c)
|
|
upRows := s.neLicenseService.Update(neLicense)
|
|
if upRows > 0 {
|
|
// 进行上传替换
|
|
err = s.neLicenseService.UploadLicense(body)
|
|
if err != nil {
|
|
c.JSON(200, result.ErrMsg(err.Error()))
|
|
return
|
|
}
|
|
c.JSON(200, result.Ok(nil))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 网元授权激活状态
|
|
//
|
|
// GET /state
|
|
func (s *NeLicenseController) State(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
|
|
}
|
|
|
|
// 检查是否存在授权记录
|
|
neLicense := s.neLicenseService.SelectByNeTypeAndNeID(querys.NeType, querys.NeId)
|
|
if neLicense.NeId != querys.NeId {
|
|
// 没有可访问网元授权激活数据!
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neLicense.noData")))
|
|
return
|
|
}
|
|
|
|
// 查询网元获取IP获取网元状态
|
|
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(neLicense.NeType, neLicense.NeId)
|
|
if neInfo.NeId != neLicense.NeId || neInfo.IP == "" {
|
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
|
return
|
|
}
|
|
if neState, err := neService.NeState(neInfo); err == nil {
|
|
neLicense.Status = "1"
|
|
neLicense.SerialNum = fmt.Sprint(neState["sn"])
|
|
neLicense.ExpiryDate = fmt.Sprint(neState["expire"])
|
|
code, licensePath := s.neLicenseService.ReadLicenseInfo(neLicense)
|
|
neLicense.ActivationRequestCode = code
|
|
neLicense.LicensePath = licensePath
|
|
} else {
|
|
neLicense.Status = "0"
|
|
}
|
|
|
|
// 更新授权信息
|
|
neLicense.UpdateBy = ctx.LoginUserToUserName(c)
|
|
s.neLicenseService.Update(neLicense)
|
|
|
|
if neLicense.Status == "1" {
|
|
c.JSON(200, result.OkData(map[string]string{
|
|
"sn": neLicense.SerialNum,
|
|
"expire": neLicense.ExpiryDate,
|
|
}))
|
|
return
|
|
}
|
|
c.JSON(200, result.ErrMsg(fmt.Sprintf("%s service status exception", neLicense.NeType)))
|
|
}
|