feat: 实训教学模块
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"be.ems/src/modules/monitor"
|
||||
networkdata "be.ems/src/modules/network_data"
|
||||
networkelement "be.ems/src/modules/network_element"
|
||||
practicalTraining "be.ems/src/modules/practical_training"
|
||||
"be.ems/src/modules/system"
|
||||
"be.ems/src/modules/trace"
|
||||
"be.ems/src/modules/ws"
|
||||
@@ -136,6 +137,8 @@ func initModulesRoute(app *gin.Engine) {
|
||||
networkelement.Setup(app)
|
||||
// 网元数据模块
|
||||
networkdata.Setup(app)
|
||||
// 实训教学模块
|
||||
practicalTraining.Setup(app)
|
||||
// 跟踪模块
|
||||
trace.Setup(app)
|
||||
// 图表模块
|
||||
|
||||
232
src/modules/practical_training/controller/pt_param_config.go
Normal file
232
src/modules/practical_training/controller/pt_param_config.go
Normal file
@@ -0,0 +1,232 @@
|
||||
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 != "" {
|
||||
paraData := info.ParamData
|
||||
paraData["paramType"] = info.ParamType
|
||||
c.JSON(200, result.Ok(paraData))
|
||||
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,
|
||||
ParamType: "2",
|
||||
ParamName: body.ParamName,
|
||||
}
|
||||
info := s.ptNeConfigDataService.SelectByStubType(param)
|
||||
|
||||
// 要修改的属性
|
||||
for k, v := range body.ParamData {
|
||||
if _, ok := info.ParamData[k]; ok {
|
||||
info.ParamData[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
// 将json数据转字符串存储
|
||||
paramDataByte, err := json.Marshal(body.ParamData)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, err.Error()))
|
||||
return
|
||||
}
|
||||
body.ParamJson = string(paramDataByte)
|
||||
|
||||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||
insertId := s.ptNeConfigDataService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(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)
|
||||
|
||||
// 要修改的属性
|
||||
if infoDataArr, ok := info.ParamData["data"]; ok {
|
||||
arr := infoDataArr.([]any)
|
||||
if len(arr) == 1 {
|
||||
for k, v := range body.ParamData {
|
||||
item := arr[0].(map[string]any)
|
||||
if _, ok := item[k]; ok {
|
||||
item[k] = v
|
||||
arr[0] = item
|
||||
}
|
||||
}
|
||||
}
|
||||
info.ParamData["data"] = arr
|
||||
}
|
||||
|
||||
// 将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))
|
||||
}
|
||||
26
src/modules/practical_training/model/pt_ne_config_data.go
Normal file
26
src/modules/practical_training/model/pt_ne_config_data.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
// PtNeConfigData 实训教学_网元参数配置数据信息
|
||||
type PtNeConfigData struct {
|
||||
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"` // ID
|
||||
CreateBy string `json:"createBy" gorm:"create_by"` // 创建者
|
||||
CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间
|
||||
UpdateBy string `json:"updateBy" gorm:"update_by"` // 更新者
|
||||
UpdateTime int64 `json:"updateTime" gorm:"update_time"` // 更新时间
|
||||
Remark string `json:"remark" gorm:"remark"` // 备注
|
||||
StubType string `json:"stubType" gorm:"stub_type"` // 存根数据类型 0系统 1班级 2个人
|
||||
NeType string `json:"neType" gorm:"ne_type"` // 网元类型
|
||||
ParamName string `json:"paramName" gorm:"param_name"` // 参数名
|
||||
ParamDisplay string `json:"paramDisplay" gorm:"param_display"` // 参数显示名
|
||||
ParamType string `json:"paramType" gorm:"param_type"` // 参数类型 list列表单层 array数组多层
|
||||
ParamJson string `json:"paramJson" gorm:"param_json"` // 参数数据
|
||||
|
||||
// ====== 非数据库字段属性 ======
|
||||
|
||||
ParamData map[string]any `json:"paramData,omitempty" binding:"required" gorm:"-"` // 与ParamJSONStr配合转换
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
func (*PtNeConfigData) TableName() string {
|
||||
return "pt_ne_config_data"
|
||||
}
|
||||
47
src/modules/practical_training/practical_training.go
Normal file
47
src/modules/practical_training/practical_training.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package practical_training
|
||||
|
||||
import (
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/middleware"
|
||||
"be.ems/src/framework/middleware/collectlogs"
|
||||
"be.ems/src/modules/practical_training/controller"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 模块路由注册
|
||||
func Setup(router *gin.Engine) {
|
||||
logger.Infof("开始加载 ====> practical_training 模块路由")
|
||||
|
||||
ptGroup := router.Group("/pt")
|
||||
|
||||
// 网元参数配置
|
||||
paramConfigGroup := ptGroup.Group("/neConfigData")
|
||||
{
|
||||
paramConfigGroup.GET("",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewPtNeConfigData.Info,
|
||||
)
|
||||
paramConfigGroup.POST("",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigData", collectlogs.BUSINESS_TYPE_INSERT)),
|
||||
controller.NewPtNeConfigData.Add,
|
||||
)
|
||||
paramConfigGroup.PUT("",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigData", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewPtNeConfigData.Edit,
|
||||
)
|
||||
paramConfigGroup.DELETE("/:ids",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigData", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewPtNeConfigData.Remove,
|
||||
)
|
||||
paramConfigGroup.POST("/saveAsDefault",
|
||||
middleware.PreAuthorize(map[string][]string{"hasRoles": {"admin", "teacher"}}),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigData", collectlogs.BUSINESS_TYPE_OTHER)),
|
||||
controller.NewPtNeConfigData.SaveAsDefault,
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package repository
|
||||
|
||||
import "be.ems/src/modules/practical_training/model"
|
||||
|
||||
// IPtNeConfigDataRepository 数据层接口
|
||||
type IPtNeConfigDataRepository interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(param model.PtNeConfigData) []model.PtNeConfigData
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(paramIds []string) []model.PtNeConfigData
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(param model.PtNeConfigData) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(param model.PtNeConfigData) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(paramIds []string) int64
|
||||
}
|
||||
@@ -0,0 +1,286 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/practical_training/model"
|
||||
)
|
||||
|
||||
// NewPtNeConfigDataRepository 实例化数据层
|
||||
var NewPtNeConfigDataRepository = &PtNeConfigDataRepository{
|
||||
selectSql: `select
|
||||
id, create_by, create_time, update_by, update_time, remark, stub_type, ne_type, param_name, param_display, param_type, param_json
|
||||
from pt_ne_config_data`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"id": "ID",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
"stub_type": "StubType",
|
||||
"ne_type": "NeType",
|
||||
"param_name": "ParamName",
|
||||
"param_display": "ParamDisplay",
|
||||
"param_type": "ParamType",
|
||||
"param_json": "ParamJson",
|
||||
},
|
||||
}
|
||||
|
||||
// PtNeConfigDataRepository 数据层处理
|
||||
type PtNeConfigDataRepository struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *PtNeConfigDataRepository) convertResultRows(rows []map[string]any) []model.PtNeConfigData {
|
||||
arr := make([]model.PtNeConfigData, 0)
|
||||
for _, row := range rows {
|
||||
item := model.PtNeConfigData{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&item, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, item)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
func (r *PtNeConfigDataRepository) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["paramName"]; ok && v != "" {
|
||||
conditions = append(conditions, "param_name = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["paramType"]; ok && v != "" {
|
||||
conditions = append(conditions, "param_type = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.PtNeConfigData{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from pt_ne_config_data"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *PtNeConfigDataRepository) SelectList(param model.PtNeConfigData) []model.PtNeConfigData {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if param.CreateBy != "" {
|
||||
conditions = append(conditions, "create_by = ?")
|
||||
params = append(params, param.CreateBy)
|
||||
}
|
||||
if param.StubType != "" {
|
||||
conditions = append(conditions, "stub_type = ?")
|
||||
params = append(params, param.StubType)
|
||||
}
|
||||
if param.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, param.NeType)
|
||||
}
|
||||
if param.ParamName != "" {
|
||||
conditions = append(conditions, "param_name = ?")
|
||||
params = append(params, param.ParamName)
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
conditions = append(conditions, "param_type = ?")
|
||||
params = append(params, param.ParamType)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by update_time asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *PtNeConfigDataRepository) SelectByIds(paramIds []string) []model.PtNeConfigData {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(paramIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(paramIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.PtNeConfigData{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *PtNeConfigDataRepository) Insert(param model.PtNeConfigData) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if param.CreateBy != "" {
|
||||
params["create_by"] = param.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
params["remark"] = param.Remark
|
||||
if param.StubType != "" {
|
||||
params["stub_type"] = param.StubType
|
||||
}
|
||||
if param.NeType != "" {
|
||||
params["ne_type"] = param.NeType
|
||||
}
|
||||
if param.ParamName != "" {
|
||||
params["param_name"] = param.ParamName
|
||||
}
|
||||
if param.ParamDisplay != "" {
|
||||
params["param_display"] = param.ParamDisplay
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
params["param_type"] = param.ParamType
|
||||
}
|
||||
if param.ParamJson != "" {
|
||||
params["param_json"] = param.ParamJson
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into pt_ne_config_data (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *PtNeConfigDataRepository) Update(param model.PtNeConfigData) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if param.UpdateBy != "" {
|
||||
params["update_by"] = param.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
params["remark"] = param.Remark
|
||||
if param.StubType != "" {
|
||||
params["stub_type"] = param.StubType
|
||||
}
|
||||
if param.NeType != "" {
|
||||
params["ne_type"] = param.NeType
|
||||
}
|
||||
if param.ParamName != "" {
|
||||
params["param_name"] = param.ParamName
|
||||
}
|
||||
if param.ParamDisplay != "" {
|
||||
params["param_display"] = param.ParamDisplay
|
||||
}
|
||||
if param.ParamType != "" {
|
||||
params["param_type"] = param.ParamType
|
||||
}
|
||||
if param.ParamJson != "" {
|
||||
params["param_json"] = param.ParamJson
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update pt_ne_config_data set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, param.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *PtNeConfigDataRepository) DeleteByIds(paramIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(paramIds))
|
||||
sql := "delete from pt_ne_config_data where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(paramIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
33
src/modules/practical_training/service/pt_ne_config_data.go
Normal file
33
src/modules/practical_training/service/pt_ne_config_data.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
neModel "be.ems/src/modules/network_element/model"
|
||||
"be.ems/src/modules/practical_training/model"
|
||||
)
|
||||
|
||||
// IPtNeConfigDataService 服务层接口
|
||||
type IPtNeConfigDataService interface {
|
||||
// SelectPage 根据条件分页查询字典类型
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 根据实体查询
|
||||
SelectList(param model.PtNeConfigData) []model.PtNeConfigData
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectById(paramId string) model.PtNeConfigData
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(param model.PtNeConfigData) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(param model.PtNeConfigData) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(paramIds []string) (int64, error)
|
||||
|
||||
// SaveAsDefaultByType 保存为默认示例
|
||||
SaveAsDefaultByType(neInfo neModel.NeInfo, stubType, operaUserName string)
|
||||
|
||||
// SelectByStubType 通过存根类型查询
|
||||
SelectByStubType(param model.PtNeConfigData) model.PtNeConfigData
|
||||
}
|
||||
145
src/modules/practical_training/service/pt_ne_config_data.impl.go
Normal file
145
src/modules/practical_training/service/pt_ne_config_data.impl.go
Normal file
@@ -0,0 +1,145 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
neFetchlink "be.ems/src/modules/network_element/fetch_link"
|
||||
neModel "be.ems/src/modules/network_element/model"
|
||||
neService "be.ems/src/modules/network_element/service"
|
||||
"be.ems/src/modules/practical_training/model"
|
||||
"be.ems/src/modules/practical_training/repository"
|
||||
)
|
||||
|
||||
// NewPtNeConfigDataService 实例化服务层
|
||||
var NewPtNeConfigDataService = &PtNeConfigDataService{
|
||||
ptNeConfigDataRepository: repository.NewPtNeConfigDataRepository,
|
||||
neConfigService: neService.NewNeConfigImpl,
|
||||
neInfoService: neService.NewNeInfoImpl,
|
||||
}
|
||||
|
||||
// PtNeConfigDataService 服务层处理
|
||||
type PtNeConfigDataService struct {
|
||||
// 实训教学_网元参数配置表
|
||||
ptNeConfigDataRepository repository.IPtNeConfigDataRepository
|
||||
// 网元参数配置可用属性值服务
|
||||
neConfigService neService.INeConfig
|
||||
// 网元信息服务
|
||||
neInfoService neService.INeInfo
|
||||
}
|
||||
|
||||
// SelectNeHostPage 分页查询列表数据
|
||||
func (r *PtNeConfigDataService) SelectPage(query map[string]any) map[string]any {
|
||||
return r.ptNeConfigDataRepository.SelectPage(query)
|
||||
}
|
||||
|
||||
// SelectConfigList 查询列表
|
||||
func (r *PtNeConfigDataService) SelectList(param model.PtNeConfigData) []model.PtNeConfigData {
|
||||
return r.ptNeConfigDataRepository.SelectList(param)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *PtNeConfigDataService) SelectById(paramId string) model.PtNeConfigData {
|
||||
if paramId == "" {
|
||||
return model.PtNeConfigData{}
|
||||
}
|
||||
neHosts := r.ptNeConfigDataRepository.SelectByIds([]string{paramId})
|
||||
if len(neHosts) > 0 {
|
||||
return neHosts[0]
|
||||
}
|
||||
return model.PtNeConfigData{}
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *PtNeConfigDataService) Insert(param model.PtNeConfigData) string {
|
||||
return r.ptNeConfigDataRepository.Insert(param)
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *PtNeConfigDataService) Update(param model.PtNeConfigData) int64 {
|
||||
return r.ptNeConfigDataRepository.Update(param)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *PtNeConfigDataService) DeleteByIds(paramIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
ids := r.ptNeConfigDataRepository.SelectByIds(paramIds)
|
||||
if len(ids) <= 0 {
|
||||
return 0, fmt.Errorf("neHostCmd.noData")
|
||||
}
|
||||
|
||||
if len(ids) == len(paramIds) {
|
||||
rows := r.ptNeConfigDataRepository.DeleteByIds(paramIds)
|
||||
return rows, nil
|
||||
}
|
||||
// 删除信息失败!
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
|
||||
// SaveAsDefaultByType 保存为默认示例
|
||||
func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubType, operUserName string) {
|
||||
confs := r.neConfigService.SelectList(neModel.NeConfig{NeType: neInfo.NeType})
|
||||
|
||||
for _, v := range confs {
|
||||
// 查询是否存在记录
|
||||
hasItems := r.SelectList(model.PtNeConfigData{
|
||||
NeType: v.NeType,
|
||||
StubType: stubType,
|
||||
ParamName: v.ParamName,
|
||||
ParamType: v.ParamType,
|
||||
})
|
||||
// 网元直连 获取网元配置信息
|
||||
resData, err := neFetchlink.NeConfigInfo(neInfo, v.ParamName)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// 将json数据转字符串存储 data:[{},{}]
|
||||
paramDataByte, err := json.Marshal(resData)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// 插入
|
||||
if len(hasItems) == 0 {
|
||||
r.Insert(model.PtNeConfigData{
|
||||
CreateBy: operUserName,
|
||||
StubType: stubType,
|
||||
NeType: v.NeType,
|
||||
ParamName: v.ParamName,
|
||||
ParamDisplay: v.ParamDisplay,
|
||||
ParamType: v.ParamType,
|
||||
ParamJson: string(paramDataByte),
|
||||
})
|
||||
}
|
||||
// 更新
|
||||
if len(hasItems) == 1 {
|
||||
item := hasItems[0]
|
||||
item.UpdateBy = operUserName
|
||||
item.ParamDisplay = v.ParamDisplay
|
||||
item.ParamJson = string(paramDataByte)
|
||||
r.Update(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SelectByStubType 通过存根类型查询
|
||||
func (r *PtNeConfigDataService) SelectByStubType(param model.PtNeConfigData) model.PtNeConfigData {
|
||||
list := r.SelectList(param)
|
||||
if len(list) == 0 && param.StubType != "1" {
|
||||
param.CreateBy = ""
|
||||
param.StubType = "1"
|
||||
list = r.SelectList(param)
|
||||
}
|
||||
if len(list) == 0 && param.StubType != "0" {
|
||||
param.CreateBy = ""
|
||||
param.StubType = "0"
|
||||
list = r.SelectList(param)
|
||||
}
|
||||
var paraData model.PtNeConfigData
|
||||
if len(list) > 0 {
|
||||
paraData = list[0]
|
||||
if err := json.Unmarshal([]byte(paraData.ParamJson), ¶Data.ParamData); err != nil {
|
||||
paraData.ParamData = map[string]any{}
|
||||
}
|
||||
}
|
||||
return paraData
|
||||
}
|
||||
Reference in New Issue
Block a user