Files
be.ems/src/modules/practical_training/controller/pt_ne_config_data.go
2024-07-02 10:53:55 +08:00

226 lines
5.9 KiB
Go

package controller
import (
"encoding/json"
"strings"
"be.ems/src/framework/i18n"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/vo/result"
neService "be.ems/src/modules/network_element/service"
"be.ems/src/modules/practical_training/model"
"be.ems/src/modules/practical_training/service"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// NewPtNeConfigData 网元参数配置服务 实例化控制层
var NewPtNeConfigData = &PtNeConfigData{
ptNeConfigDataService: service.NewPtNeConfigDataService,
neInfoService: neService.NewNeInfoImpl,
}
// 网元参数配置服务
//
// PATH /neConfigData
type PtNeConfigData struct {
// 实训教学_网元参数配置服务
ptNeConfigDataService service.IPtNeConfigDataService
// 网元信息服务
neInfoService neService.INeInfo
}
// 网元参数配置信息
//
// GET /
func (s *PtNeConfigData) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
NeType string `form:"neType" binding:"required"`
ParamName string `form:"paramName" binding:"required"`
}
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 优先查询个人的数据,没有就向系统取
param := model.PtNeConfigData{
NeType: querys.NeType,
StubType: "2",
ParamName: querys.ParamName,
}
info := s.ptNeConfigDataService.SelectByStubType(param)
// 输出数据内容
if info.ParamJson != "" {
c.JSON(200, result.Ok(map[string]any{
"data": info.ParamData,
"paramType": info.ParamType,
}))
return
}
c.JSON(200, result.Err(nil))
}
// 网元参数配置新增
//
// POST /
func (s *PtNeConfigData) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.PtNeConfigData
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
currentUserName := ctx.LoginUserToUserName(c)
// 优先查询个人的数据,没有就向系统取
param := model.PtNeConfigData{
CreateBy: currentUserName,
NeType: body.NeType,
StubType: "2",
ParamName: body.ParamName,
}
info := s.ptNeConfigDataService.SelectByStubType(param)
// 要修改的属性
// 将json数据转字符串存储
paramDataByte, err := json.Marshal(info.ParamData)
if err != nil {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
info.ParamJson = string(paramDataByte)
// 个人没数据要新增
if info.StubType == "2" {
info.UpdateBy = currentUserName
s.ptNeConfigDataService.Update(info)
} else {
s.ptNeConfigDataService.Insert(model.PtNeConfigData{
CreateBy: currentUserName,
NeType: info.NeType,
StubType: "2",
ParamName: info.ParamName,
ParamDisplay: info.ParamDisplay,
ParamJson: info.ParamJson,
})
}
c.JSON(204, nil)
}
// 网元参数配置修改
//
// PUT /
func (s *PtNeConfigData) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.PtNeConfigData
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
currentUserName := ctx.LoginUserToUserName(c)
// 优先查询个人的数据,没有就向系统取
param := model.PtNeConfigData{
CreateBy: currentUserName,
NeType: body.NeType,
StubType: "2",
ParamName: body.ParamName,
}
info := s.ptNeConfigDataService.SelectByStubType(param)
// 要修改的属性
// 将json数据转字符串存储
paramDataByte, err := json.Marshal(info.ParamData)
if err != nil {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
info.ParamJson = string(paramDataByte)
// 个人没数据要新增
if info.StubType == "2" {
info.UpdateBy = currentUserName
s.ptNeConfigDataService.Update(info)
} else {
s.ptNeConfigDataService.Insert(model.PtNeConfigData{
CreateBy: currentUserName,
NeType: info.NeType,
StubType: "2",
ParamName: info.ParamName,
ParamDisplay: info.ParamDisplay,
ParamJson: info.ParamJson,
})
}
c.JSON(204, nil)
}
// 网元参数配置删除
//
// DELETE /:ids
func (s *PtNeConfigData) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
ids := c.Param("ids")
if ids == "" {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
idsArr := strings.Split(ids, ",")
uniqueIDs := parse.RemoveDuplicates(idsArr)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
return
}
rows, err := s.ptNeConfigDataService.DeleteByIds(uniqueIDs)
if err != nil {
c.JSON(200, result.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))
}
// 保存为示例配置 (仅管理员/教师操作)
//
// POST /saveAsDefault
func (s *PtNeConfigData) SaveAsDefault(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
NeType string `json:"neType" binding:"required"` // 网元类型
NeId string `json:"neid" binding:"required"` // 网元ID
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 查询网元获取IP获取网元状态
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
}
stubType := "" // 存根数据类型 0系统 1班级 2个人
loginUser, _ := ctx.LoginUser(c)
for _, v := range loginUser.User.Roles {
if v.RoleName == "admin" {
stubType = "0"
} else if v.RoleName == "teacher" {
stubType = "1"
}
}
operaUserName := loginUser.User.NickName
s.ptNeConfigDataService.SaveAsDefaultByType(neInfo, stubType, operaUserName)
c.JSON(200, result.Ok(nil))
}