feat: 重置为示例配置 (仅学生/教师操作)功能接口

This commit is contained in:
TsMask
2024-07-03 15:39:57 +08:00
parent c411d30db2
commit 05104a514f
4 changed files with 165 additions and 93 deletions

View File

@@ -57,9 +57,9 @@ func (s *PtNeConfigData) SaveAsDefault(c *gin.Context) {
stubType := "" // 存根数据类型 0系统 1班级 2个人
loginUser, _ := ctx.LoginUser(c)
for _, v := range loginUser.User.Roles {
if v.RoleName == "admin" {
if v.RoleKey == "admin" {
stubType = "0"
} else if v.RoleName == "teacher" {
} else if v.RoleKey == "teacher" {
stubType = "1"
}
}
@@ -69,6 +69,35 @@ func (s *PtNeConfigData) SaveAsDefault(c *gin.Context) {
c.JSON(200, result.Ok(nil))
}
// 重置为示例配置 (仅学生/教师操作)
//
// POST /resetAsDefault
func (s *PtNeConfigData) ResetAsDefault(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
}
stubType := "" // 存根数据类型 0系统 1班级 2个人
loginUser, _ := ctx.LoginUser(c)
for _, v := range loginUser.User.Roles {
if v.RoleKey == "student" {
stubType = "2"
} else if v.RoleKey == "teacher" {
stubType = "1"
}
}
operaUserName := loginUser.User.NickName
s.ptNeConfigDataService.ResetAsDefaultByType(body.NeType, stubType, operaUserName)
c.JSON(200, result.Ok(nil))
}
// 网元参数配置信息
//
// GET /
@@ -101,94 +130,6 @@ func (s *PtNeConfigData) Info(c *gin.Context) {
c.JSON(200, result.Err(nil))
}
// 网元参数配置新增array
//
// 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)
if info.ParamType != "array" {
c.JSON(400, result.CodeMsg(400, "this attribute does not support adding"))
return
}
idx, idxOk := body.ParamData["index"]
if info.ParamType == "array" && !idxOk {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
changeLog := model.PtNeConfigDataLog{
CreateBy: currentUserName,
StubType: info.StubType,
NeType: info.NeType,
ParamName: info.ParamName,
ParamDisplay: info.ParamDisplay,
ParamType: info.ParamType,
OperaType: 1,
}
// 要修改的属性
if infoDataArr, ok := info.ParamData["data"]; ok {
arr := infoDataArr.([]any)
if len(arr) == 0 {
body.ParamData["index"] = 0
arr = []any{body.ParamData}
} else {
idx := parse.Number(idx)
if idx < 0 || int(idx) < len(arr) {
c.JSON(400, result.CodeMsg(400, "index less than data step"))
return
}
body.ParamData["index"] = len(arr)
arr = append(arr, body.ParamData)
}
info.ParamData["data"] = arr
}
// 将json数据转字符串存储
paramDataByte, err := json.Marshal(info.ParamData)
if err != nil {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
changeLog.ParamJsonOld = info.ParamJson
info.ParamJson = string(paramDataByte)
changeLog.ParamJsonNew = info.ParamJson
// 个人有数据就更新
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,
ParamType: info.ParamType,
ParamJson: info.ParamJson,
})
}
// 保留变更日志
s.ptNeConfigDataLogService.Insert(changeLog)
c.JSON(200, result.OkData(body.ParamData))
}
// 网元参数配置修改
//
// PUT /
@@ -284,6 +225,94 @@ func (s *PtNeConfigData) Edit(c *gin.Context) {
c.JSON(204, nil)
}
// 网元参数配置新增array
//
// 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)
if info.ParamType != "array" {
c.JSON(400, result.CodeMsg(400, "this attribute does not support adding"))
return
}
idx, idxOk := body.ParamData["index"]
if info.ParamType == "array" && !idxOk {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
changeLog := model.PtNeConfigDataLog{
CreateBy: currentUserName,
StubType: info.StubType,
NeType: info.NeType,
ParamName: info.ParamName,
ParamDisplay: info.ParamDisplay,
ParamType: info.ParamType,
OperaType: 1,
}
// 要修改的属性
if infoDataArr, ok := info.ParamData["data"]; ok {
arr := infoDataArr.([]any)
if len(arr) == 0 {
body.ParamData["index"] = 0
arr = []any{body.ParamData}
} else {
idx := parse.Number(idx)
if idx < 0 || int(idx) < len(arr) {
c.JSON(400, result.CodeMsg(400, "index less than data step"))
return
}
body.ParamData["index"] = len(arr)
arr = append(arr, body.ParamData)
}
info.ParamData["data"] = arr
}
// 将json数据转字符串存储
paramDataByte, err := json.Marshal(info.ParamData)
if err != nil {
c.JSON(400, result.CodeMsg(400, err.Error()))
return
}
changeLog.ParamJsonOld = info.ParamJson
info.ParamJson = string(paramDataByte)
changeLog.ParamJsonNew = info.ParamJson
// 个人有数据就更新
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,
ParamType: info.ParamType,
ParamJson: info.ParamJson,
})
}
// 保留变更日志
s.ptNeConfigDataLogService.Insert(changeLog)
c.JSON(200, result.OkData(body.ParamData))
}
// 网元参数配置删除array
//
// DELETE /

View File

@@ -23,6 +23,11 @@ func Setup(router *gin.Engine) {
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigData", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewPtNeConfigData.SaveAsDefault,
)
paramConfigGroup.POST("/resetAsDefault",
middleware.PreAuthorize(map[string][]string{"hasRoles": {"student", "teacher"}}),
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigData", collectlogs.BUSINESS_TYPE_OTHER)),
controller.NewPtNeConfigData.ResetAsDefault,
)
paramConfigGroup.GET("",
middleware.PreAuthorize(nil),
controller.NewPtNeConfigData.Info,

View File

@@ -28,6 +28,9 @@ type IPtNeConfigDataService interface {
// SaveAsDefaultByType 保存为默认示例
SaveAsDefaultByType(neInfo neModel.NeInfo, stubType, operaUserName string)
// ResetAsDefaultByType 重置为默认示例
ResetAsDefaultByType(neType, stubType, operaUserName string)
// SelectByStubType 通过存根类型查询
SelectByStubType(param model.PtNeConfigData) model.PtNeConfigData
}

View File

@@ -77,7 +77,7 @@ func (r *PtNeConfigDataService) DeleteByIds(paramIds []string) (int64, error) {
}
// SaveAsDefaultByType 保存为默认示例
func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubType, operUserName string) {
func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubType, operaUserName string) {
confs := r.neConfigService.SelectList(neModel.NeConfig{NeType: neInfo.NeType})
for _, v := range confs {
// 查询是否存在记录
@@ -103,7 +103,7 @@ func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubT
// 插入
if len(hasItems) == 0 {
r.Insert(model.PtNeConfigData{
CreateBy: operUserName,
CreateBy: operaUserName,
StubType: stubType,
NeType: v.NeType,
ParamName: v.ParamName,
@@ -115,7 +115,7 @@ func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubT
// 更新
if len(hasItems) == 1 {
item := hasItems[0]
item.UpdateBy = operUserName
item.UpdateBy = operaUserName
item.ParamDisplay = v.ParamDisplay
item.ParamJson = string(paramDataByte)
r.Update(item)
@@ -123,6 +123,41 @@ func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubT
}
}
// ResetAsDefaultByType 重置为默认示例
func (r *PtNeConfigDataService) ResetAsDefaultByType(neType, stubType, operaUserName string) {
ptConfs := r.SelectList(model.PtNeConfigData{StubType: "0", NeType: neType})
for _, v := range ptConfs {
// 查询是否存在记录
hasItems := r.SelectList(model.PtNeConfigData{
NeType: v.NeType,
StubType: stubType,
ParamName: v.ParamName,
ParamType: v.ParamType,
})
// 插入
if len(hasItems) == 0 {
r.Insert(model.PtNeConfigData{
CreateBy: operaUserName,
StubType: stubType,
NeType: v.NeType,
ParamName: v.ParamName,
ParamDisplay: v.ParamDisplay,
ParamType: v.ParamType,
ParamJson: v.ParamJson,
})
}
// 更新
if len(hasItems) == 1 {
item := hasItems[0]
item.UpdateBy = operaUserName
item.ParamDisplay = v.ParamDisplay
item.ParamJson = v.ParamJson
r.Update(item)
}
}
}
// SelectByStubType 通过存根类型查询
func (r *PtNeConfigDataService) SelectByStubType(param model.PtNeConfigData) model.PtNeConfigData {
list := r.SelectList(param)