518 lines
14 KiB
Go
518 lines
14 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"
|
||
"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 = &PtNeConfigDataController{
|
||
ptNeConfigDataService: service.NewPtNeConfigDataService,
|
||
ptNeConfigDataLogService: service.NewPtNeConfigDataLogService,
|
||
}
|
||
|
||
// 网元参数配置服务
|
||
//
|
||
// PATH /neConfigData
|
||
type PtNeConfigDataController struct {
|
||
// 实训教学_网元参数配置服务
|
||
ptNeConfigDataService service.IPtNeConfigDataService
|
||
// 实训教学_网元参数配置数据变更日志服务
|
||
ptNeConfigDataLogService service.IPtNeConfigDataLogService
|
||
}
|
||
|
||
// 保存为示例配置 (仅管理员操作)
|
||
//
|
||
// POST /saveAsDefault
|
||
func (s *PtNeConfigDataController) 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
|
||
}
|
||
|
||
loginUser, _ := ctx.LoginUser(c)
|
||
for _, v := range loginUser.User.Roles {
|
||
if v.RoleKey == "admin" {
|
||
s.ptNeConfigDataService.SaveAsDefault(body.NeType, body.NeId)
|
||
}
|
||
}
|
||
|
||
c.JSON(200, result.Ok(nil))
|
||
}
|
||
|
||
// 重置为示例配置 (仅学生/教师操作)
|
||
//
|
||
// POST /resetAsDefault
|
||
func (s *PtNeConfigDataController) ResetAsDefault(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body struct {
|
||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||
}
|
||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
loginUser, _ := ctx.LoginUser(c)
|
||
operaUserName := loginUser.User.UserName
|
||
deptId := loginUser.User.DeptID
|
||
for _, v := range loginUser.User.Roles {
|
||
if v.RoleKey == "teacher" {
|
||
s.ptNeConfigDataService.ResetAsDefault(operaUserName, "1", body.NeType, deptId)
|
||
}
|
||
if v.RoleKey == "student" {
|
||
s.ptNeConfigDataService.ResetAsDefault(operaUserName, "2", body.NeType, deptId)
|
||
}
|
||
}
|
||
|
||
c.JSON(200, result.Ok(nil))
|
||
}
|
||
|
||
// 网元参数配置信息
|
||
//
|
||
// GET /
|
||
func (s *PtNeConfigDataController) Info(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var querys struct {
|
||
NeType string `form:"neType" binding:"required"`
|
||
ParamName string `form:"paramName" binding:"required"`
|
||
Student string `form:"student"` // 教师携带学生账号查询
|
||
}
|
||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
loginUser, _ := ctx.LoginUser(c)
|
||
currentUserName := loginUser.User.UserName
|
||
deptId := loginUser.User.DeptID
|
||
stubType := "2" // 存根数据类型 0系统 1班级 2个人
|
||
for _, v := range loginUser.User.Roles {
|
||
if v.RoleKey == "admin" {
|
||
stubType = "0"
|
||
}
|
||
if v.RoleKey == "teacher" {
|
||
stubType = "1"
|
||
// 查看学生数据
|
||
if querys.Student != "" {
|
||
currentUserName = querys.Student
|
||
stubType = "2"
|
||
}
|
||
}
|
||
if v.RoleKey == "student" {
|
||
stubType = "2"
|
||
}
|
||
}
|
||
// 优先查询个人的数据,没有就向系统取
|
||
param := model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: querys.NeType,
|
||
StubType: stubType,
|
||
ParamName: querys.ParamName,
|
||
DeptId: deptId,
|
||
}
|
||
info := s.ptNeConfigDataService.SelectByStubType(param)
|
||
|
||
// 输出数据内容
|
||
if info.ParamJson != "" {
|
||
c.JSON(200, result.Ok(map[string]any{
|
||
"type": info.ParamType,
|
||
"data": info.ParamData,
|
||
}))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 网元参数配置修改
|
||
//
|
||
// PUT /
|
||
func (s *PtNeConfigDataController) Edit(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body struct {
|
||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||
ParamName string `json:"paramName" binding:"required"`
|
||
ParamData map[string]any `json:"paramData" binding:"required"`
|
||
Loc string `json:"loc"` // 仅array使用与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
|
||
}
|
||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
loginUser, _ := ctx.LoginUser(c)
|
||
currentUserName := loginUser.User.UserName
|
||
deptId := loginUser.User.DeptID
|
||
stubType := "2" // 存根数据类型 0系统 1班级 2个人
|
||
for _, v := range loginUser.User.Roles {
|
||
if v.RoleKey == "admin" {
|
||
stubType = "0"
|
||
}
|
||
if v.RoleKey == "teacher" {
|
||
stubType = "1"
|
||
}
|
||
if v.RoleKey == "student" {
|
||
stubType = "2"
|
||
}
|
||
}
|
||
// 优先查询个人的数据,没有就向系统取
|
||
param := model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: body.NeType,
|
||
StubType: stubType,
|
||
ParamName: body.ParamName,
|
||
DeptId: deptId,
|
||
}
|
||
info := s.ptNeConfigDataService.SelectByStubType(param)
|
||
changeLog := model.PtNeConfigDataLog{
|
||
CreateBy: currentUserName,
|
||
StubType: info.StubType,
|
||
NeType: info.NeType,
|
||
ParamName: info.ParamName,
|
||
ParamDisplay: info.ParamDisplay,
|
||
ParamType: info.ParamType,
|
||
OperaType: 2,
|
||
ParamJsonOld: info.ParamJson,
|
||
}
|
||
|
||
// 单层修改的属性
|
||
if info.ParamType == "list" && len(info.ParamData) == 1 {
|
||
for k, v := range body.ParamData {
|
||
item := info.ParamData[0]
|
||
if _, ok := item[k]; ok {
|
||
item[k] = v
|
||
}
|
||
info.ParamData[0] = item
|
||
}
|
||
}
|
||
// 多层修改的属性
|
||
if info.ParamType == "array" {
|
||
if body.Loc == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
// 检查层级
|
||
locArr := strings.Split(body.Loc, "/")
|
||
loc := parse.Number(locArr[0])
|
||
if loc < 0 || int(loc) >= len(info.ParamData) {
|
||
c.JSON(400, result.CodeMsg(400, "loc over data step"))
|
||
return
|
||
}
|
||
|
||
if len(info.ParamData) == 0 {
|
||
c.JSON(400, result.CodeMsg(400, "loc over data step"))
|
||
return
|
||
}
|
||
if len(locArr) == 1 {
|
||
body.ParamData["index"] = loc
|
||
info.ParamData[loc] = body.ParamData
|
||
}
|
||
if len(locArr) > 2 {
|
||
item := info.ParamData[loc]
|
||
if a, ok := item[locArr[1]]; ok {
|
||
arr := a.([]any)
|
||
aLoc := parse.Number(locArr[2])
|
||
if aLoc < 0 || int(aLoc) >= len(arr) {
|
||
c.JSON(400, result.CodeMsg(400, locArr[1]+" loc over data step"))
|
||
return
|
||
}
|
||
body.ParamData["index"] = aLoc
|
||
arr[aLoc] = body.ParamData
|
||
item[locArr[1]] = arr
|
||
}
|
||
info.ParamData[loc] = item
|
||
}
|
||
}
|
||
|
||
// 将json数据转字符串存储
|
||
paramDataByte, err := json.Marshal(info.ParamData)
|
||
if err != nil {
|
||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||
return
|
||
}
|
||
info.ParamJson = string(paramDataByte)
|
||
changeLog.ParamJsonNew = info.ParamJson
|
||
|
||
// 个人有数据就更新
|
||
if info.StubType == stubType {
|
||
info.UpdateBy = currentUserName
|
||
s.ptNeConfigDataService.Update(info)
|
||
} else {
|
||
s.ptNeConfigDataService.Insert(model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: info.NeType,
|
||
StubType: stubType,
|
||
ParamName: info.ParamName,
|
||
ParamDisplay: info.ParamDisplay,
|
||
ParamType: info.ParamType,
|
||
ParamJson: info.ParamJson,
|
||
DeptId: deptId,
|
||
})
|
||
}
|
||
// 保留变更日志
|
||
s.ptNeConfigDataLogService.Insert(changeLog)
|
||
|
||
c.JSON(204, nil)
|
||
}
|
||
|
||
// 网元参数配置新增(array)
|
||
//
|
||
// POST /
|
||
func (s *PtNeConfigDataController) Add(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body struct {
|
||
NeType string `json:"neType" binding:"required"` // 网元类型
|
||
ParamName string `json:"paramName" binding:"required"` // 根据配置可选值
|
||
ParamData map[string]any `json:"paramData" binding:"required"` // 数据对象
|
||
Loc string `json:"loc" binding:"required"` // 与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
|
||
}
|
||
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
loginUser, _ := ctx.LoginUser(c)
|
||
currentUserName := loginUser.User.UserName
|
||
deptId := loginUser.User.DeptID
|
||
stubType := "2" // 存根数据类型 0系统 1班级 2个人
|
||
for _, v := range loginUser.User.Roles {
|
||
if v.RoleKey == "admin" {
|
||
stubType = "0"
|
||
}
|
||
if v.RoleKey == "teacher" {
|
||
stubType = "1"
|
||
}
|
||
if v.RoleKey == "student" {
|
||
stubType = "2"
|
||
}
|
||
}
|
||
// 优先查询个人的数据,没有就向系统取
|
||
param := model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: body.NeType,
|
||
StubType: stubType,
|
||
ParamName: body.ParamName,
|
||
DeptId: deptId,
|
||
}
|
||
info := s.ptNeConfigDataService.SelectByStubType(param)
|
||
if info.ParamType != "array" || body.Loc == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
// 检查层级
|
||
locArr := strings.Split(body.Loc, "/")
|
||
loc := parse.Number(locArr[0])
|
||
if loc < 0 || int(loc) < len(info.ParamData) {
|
||
c.JSON(400, result.CodeMsg(400, "loc less data step"))
|
||
return
|
||
}
|
||
changeLog := model.PtNeConfigDataLog{
|
||
CreateBy: currentUserName,
|
||
StubType: info.StubType,
|
||
NeType: info.NeType,
|
||
ParamName: info.ParamName,
|
||
ParamDisplay: info.ParamDisplay,
|
||
ParamType: info.ParamType,
|
||
OperaType: 1,
|
||
ParamJsonOld: info.ParamJson,
|
||
}
|
||
|
||
// 要新增的属性
|
||
if len(locArr) == 1 {
|
||
body.ParamData["index"] = len(info.ParamData)
|
||
info.ParamData = append(info.ParamData, body.ParamData)
|
||
}
|
||
if len(locArr) > 2 {
|
||
item := info.ParamData[loc]
|
||
if a, ok := item[locArr[1]]; ok {
|
||
arr := a.([]any)
|
||
aLoc := parse.Number(locArr[2])
|
||
if loc < 0 || int(aLoc) < len(arr) {
|
||
c.JSON(400, result.CodeMsg(400, locArr[1]+" loc less data step"))
|
||
return
|
||
}
|
||
body.ParamData["index"] = len(arr)
|
||
arr = append(arr, body.ParamData)
|
||
item[locArr[1]] = arr
|
||
}
|
||
info.ParamData[loc] = item
|
||
}
|
||
|
||
// 将json数据转字符串存储
|
||
paramDataByte, err := json.Marshal(info.ParamData)
|
||
if err != nil {
|
||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||
return
|
||
}
|
||
info.ParamJson = string(paramDataByte)
|
||
changeLog.ParamJsonNew = info.ParamJson
|
||
|
||
// 个人有数据就更新
|
||
if info.StubType == stubType {
|
||
info.UpdateBy = currentUserName
|
||
s.ptNeConfigDataService.Update(info)
|
||
} else {
|
||
s.ptNeConfigDataService.Insert(model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: info.NeType,
|
||
StubType: stubType,
|
||
ParamName: info.ParamName,
|
||
ParamDisplay: info.ParamDisplay,
|
||
ParamType: info.ParamType,
|
||
ParamJson: info.ParamJson,
|
||
DeptId: deptId,
|
||
})
|
||
}
|
||
// 保留变更日志
|
||
s.ptNeConfigDataLogService.Insert(changeLog)
|
||
|
||
c.JSON(200, result.OkData(body.ParamData))
|
||
}
|
||
|
||
// 网元参数配置删除(array)
|
||
//
|
||
// DELETE /
|
||
func (s *PtNeConfigDataController) Remove(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var query struct {
|
||
NeType string `form:"neType" binding:"required"` // 网元类型
|
||
ParamName string `form:"paramName" binding:"required"`
|
||
Loc string `form:"loc" binding:"required"` // 与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
|
||
}
|
||
if err := c.ShouldBindQuery(&query); err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
loginUser, _ := ctx.LoginUser(c)
|
||
currentUserName := loginUser.User.UserName
|
||
deptId := loginUser.User.DeptID
|
||
stubType := "2" // 存根数据类型 0系统 1班级 2个人
|
||
for _, v := range loginUser.User.Roles {
|
||
if v.RoleKey == "admin" {
|
||
stubType = "0"
|
||
}
|
||
if v.RoleKey == "teacher" {
|
||
stubType = "1"
|
||
}
|
||
if v.RoleKey == "student" {
|
||
stubType = "2"
|
||
}
|
||
}
|
||
// 优先查询个人的数据,没有就向系统取
|
||
param := model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: query.NeType,
|
||
StubType: stubType,
|
||
ParamName: query.ParamName,
|
||
DeptId: deptId,
|
||
}
|
||
info := s.ptNeConfigDataService.SelectByStubType(param)
|
||
if info.ParamType != "array" || query.Loc == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
// 检查层级
|
||
locArr := strings.Split(query.Loc, "/")
|
||
loc := parse.Number(locArr[0])
|
||
if loc < 0 || int(loc) >= len(info.ParamData) {
|
||
c.JSON(400, result.CodeMsg(400, "loc over data step"))
|
||
return
|
||
}
|
||
// 变更日志
|
||
changeLog := model.PtNeConfigDataLog{
|
||
CreateBy: currentUserName,
|
||
StubType: info.StubType,
|
||
NeType: info.NeType,
|
||
ParamName: info.ParamName,
|
||
ParamDisplay: info.ParamDisplay,
|
||
ParamType: info.ParamType,
|
||
OperaType: 3,
|
||
ParamJsonOld: info.ParamJson,
|
||
}
|
||
|
||
// 要删除的属性
|
||
if len(locArr) == 1 {
|
||
info.ParamData = append(info.ParamData[:loc], info.ParamData[loc+1:]...)
|
||
}
|
||
if len(locArr) > 2 {
|
||
item := info.ParamData[loc]
|
||
if a, ok := item[locArr[1]]; ok {
|
||
arr := a.([]any)
|
||
aLoc := parse.Number(locArr[2])
|
||
if aLoc < 0 || int(aLoc) >= len(arr) {
|
||
c.JSON(400, result.CodeMsg(400, locArr[1]+" loc over data step"))
|
||
return
|
||
}
|
||
item[locArr[1]] = append(arr[:aLoc], arr[aLoc+1:]...)
|
||
}
|
||
info.ParamData[loc] = item
|
||
}
|
||
|
||
// 将json数据转字符串存储
|
||
paramDataByte, err := json.Marshal(info.ParamData)
|
||
if err != nil {
|
||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||
return
|
||
}
|
||
changeLog.ParamJsonNew = string(paramDataByte)
|
||
|
||
// 删除后重排index序号
|
||
if len(locArr) == 1 {
|
||
for i, v := range info.ParamData {
|
||
v["index"] = i
|
||
info.ParamData[i] = v
|
||
}
|
||
}
|
||
if len(locArr) > 2 {
|
||
item := info.ParamData[loc]
|
||
if a, ok := item[locArr[1]]; ok {
|
||
arr := a.([]any)
|
||
for i, v := range arr {
|
||
item := v.(map[string]any)
|
||
item["index"] = i
|
||
arr[i] = v
|
||
}
|
||
item[locArr[1]] = arr
|
||
}
|
||
info.ParamData[loc] = item
|
||
}
|
||
paramDataByte, _ = json.Marshal(info.ParamData)
|
||
info.ParamJson = string(paramDataByte)
|
||
|
||
// 个人有数据就更新
|
||
if info.StubType == stubType {
|
||
info.UpdateBy = currentUserName
|
||
s.ptNeConfigDataService.Update(info)
|
||
} else {
|
||
s.ptNeConfigDataService.Insert(model.PtNeConfigData{
|
||
CreateBy: currentUserName,
|
||
NeType: info.NeType,
|
||
StubType: stubType,
|
||
ParamName: info.ParamName,
|
||
ParamDisplay: info.ParamDisplay,
|
||
ParamType: info.ParamType,
|
||
ParamJson: info.ParamJson,
|
||
DeptId: deptId,
|
||
})
|
||
}
|
||
// 保留变更日志
|
||
s.ptNeConfigDataLogService.Insert(changeLog)
|
||
|
||
c.JSON(204, nil)
|
||
}
|