diff --git a/src/modules/practical_training/controller/pt_param_config.go b/src/modules/practical_training/controller/pt_ne_config_data.go similarity index 87% rename from src/modules/practical_training/controller/pt_param_config.go rename to src/modules/practical_training/controller/pt_ne_config_data.go index c2197204..9393af94 100644 --- a/src/modules/practical_training/controller/pt_param_config.go +++ b/src/modules/practical_training/controller/pt_ne_config_data.go @@ -55,9 +55,10 @@ func (s *PtNeConfigData) Info(c *gin.Context) { // 输出数据内容 if info.ParamJson != "" { - paraData := info.ParamData - paraData["paramType"] = info.ParamType - c.JSON(200, result.Ok(paraData)) + c.JSON(200, result.Ok(map[string]any{ + "data": info.ParamData, + "paramType": info.ParamType, + })) return } c.JSON(200, result.Err(nil)) @@ -73,38 +74,43 @@ func (s *PtNeConfigData) Add(c *gin.Context) { 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", + StubType: "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) + paramDataByte, err := json.Marshal(info.ParamData) if err != nil { c.JSON(400, result.CodeMsg(400, err.Error())) return } - body.ParamJson = string(paramDataByte) + info.ParamJson = string(paramDataByte) - body.CreateBy = ctx.LoginUserToUserName(c) - insertId := s.ptNeConfigDataService.Insert(body) - if insertId != "" { - c.JSON(200, result.Ok(nil)) - return + // 个人没数据要新增 + 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(200, result.Err(nil)) + + c.JSON(204, nil) } // 网元参数配置修改 @@ -129,19 +135,6 @@ func (s *PtNeConfigData) Edit(c *gin.Context) { 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) diff --git a/src/modules/practical_training/controller/pt_ne_config_data_log.go b/src/modules/practical_training/controller/pt_ne_config_data_log.go new file mode 100644 index 00000000..8397f649 --- /dev/null +++ b/src/modules/practical_training/controller/pt_ne_config_data_log.go @@ -0,0 +1,82 @@ +package controller + +import ( + "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" +) + +// NewPtNeConfigDataLog 实例化控制层 PtNeConfigDataLog +var NewPtNeConfigDataLog = &PtNeConfigDataLog{ + ptNeConfigDataLogService: service.NewPtNeConfigDataLogService, +} + +// 网元参数配置数据变更日志服务 +// +// PATH /neConfigDataLog +type PtNeConfigDataLog struct { + // 实训教学_网元参数配置数据变更日志服务 + ptNeConfigDataLogService service.IPtNeConfigDataLogService +} + +// 网元参数配置数据变更日志信息 +// +// GET / +func (s *PtNeConfigDataLog) 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.PtNeConfigDataLog{ + NeType: querys.NeType, + StubType: "2", + ParamName: querys.ParamName, + } + info := s.ptNeConfigDataLogService.SelectList(param) + + // 输出数据内容 + if len(info) > 0 { + c.JSON(200, result.OkData(info[0])) + return + } + c.JSON(200, result.Err(nil)) +} + +// 网元参数配置数据变更日志删除 +// +// DELETE /:ids +func (s *PtNeConfigDataLog) 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.ptNeConfigDataLogService.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)) +} diff --git a/src/modules/practical_training/model/pt_ne_config_data.go b/src/modules/practical_training/model/pt_ne_config_data.go index 7555fa49..ce2d4c09 100644 --- a/src/modules/practical_training/model/pt_ne_config_data.go +++ b/src/modules/practical_training/model/pt_ne_config_data.go @@ -17,7 +17,7 @@ type PtNeConfigData struct { // ====== 非数据库字段属性 ====== - ParamData map[string]any `json:"paramData,omitempty" binding:"required" gorm:"-"` // 与ParamJSONStr配合转换 + ParamData []map[string]any `json:"paramData,omitempty" binding:"required" gorm:"-"` // 与ParamJson配合转换 } // TableName 表名称 diff --git a/src/modules/practical_training/model/pt_ne_config_data_log.go b/src/modules/practical_training/model/pt_ne_config_data_log.go new file mode 100644 index 00000000..003abfd1 --- /dev/null +++ b/src/modules/practical_training/model/pt_ne_config_data_log.go @@ -0,0 +1,20 @@ +package model + +// PtNeConfigDataLog 实训教学_网元参数配置数据变更日志 +type PtNeConfigDataLog struct { + ID string `json:"id" gorm:"id"` // ID + CreateBy string `json:"createBy" gorm:"create_by"` // 创建者 + CreateTime int64 `json:"createTime" gorm:"create_time"` // 创建时间 + 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数组多层 + ParamJsonOld string `json:"paramJsonOld" gorm:"param_json_old"` // 原始内容 + ParamJsonNew string `json:"paramJsonNew" gorm:"param_json_new"` // 当前内容 +} + +// TableName 表名称 +func (*PtNeConfigDataLog) TableName() string { + return "pt_ne_config_data_log" +} diff --git a/src/modules/practical_training/practical_training.go b/src/modules/practical_training/practical_training.go index a907310a..4f15448a 100644 --- a/src/modules/practical_training/practical_training.go +++ b/src/modules/practical_training/practical_training.go @@ -44,4 +44,18 @@ func Setup(router *gin.Engine) { ) } + // 网元参数配置数据变更日志 + paramConfigLogGroup := ptGroup.Group("/neConfigDataLog") + { + paramConfigLogGroup.GET("", + middleware.PreAuthorize(nil), + controller.NewPtNeConfigDataLog.Info, + ) + paramConfigLogGroup.DELETE("/:ids", + middleware.PreAuthorize(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neConfigDataLog", collectlogs.BUSINESS_TYPE_DELETE)), + controller.NewPtNeConfigDataLog.Remove, + ) + } + } diff --git a/src/modules/practical_training/repository/pt_ne_config_data_log.go b/src/modules/practical_training/repository/pt_ne_config_data_log.go new file mode 100644 index 00000000..77271333 --- /dev/null +++ b/src/modules/practical_training/repository/pt_ne_config_data_log.go @@ -0,0 +1,21 @@ +package repository + +import "be.ems/src/modules/practical_training/model" + +// IPtNeConfigDataLogRepository 数据层接口 +type IPtNeConfigDataLogRepository interface { + // SelectPage 根据条件分页查询字典类型 + SelectPage(query map[string]any) map[string]any + + // SelectList 根据实体查询 + SelectList(param model.PtNeConfigDataLog) []model.PtNeConfigDataLog + + // SelectByIds 通过ID查询 + SelectByIds(paramIds []string) []model.PtNeConfigDataLog + + // Insert 新增信息 + Insert(param model.PtNeConfigDataLog) string + + // DeleteByIds 批量删除信息 + DeleteByIds(paramIds []string) int64 +} diff --git a/src/modules/practical_training/repository/pt_ne_config_data_log.impl.go b/src/modules/practical_training/repository/pt_ne_config_data_log.impl.go new file mode 100644 index 00000000..6baa9ba6 --- /dev/null +++ b/src/modules/practical_training/repository/pt_ne_config_data_log.impl.go @@ -0,0 +1,244 @@ +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" +) + +// NewPtNeConfigDataLogRepository 实例化数据层 +var NewPtNeConfigDataLogRepository = &PtNeConfigDataLogRepository{ + selectSql: `select + id, create_by, create_time, stub_type, ne_type, param_name, param_display, param_type, param_json_old, param_json_new + from pt_ne_config_data_log`, + + resultMap: map[string]string{ + "id": "ID", + "create_by": "CreateBy", + "create_time": "CreateTime", + "stub_type": "StubType", + "ne_type": "NeType", + "param_name": "ParamName", + "param_display": "ParamDisplay", + "param_type": "ParamType", + "param_json_old": "ParamJsonOld", + "param_json_new": "ParamJsonNew", + }, +} + +// PtNeConfigDataLogRepository 数据层处理 +type PtNeConfigDataLogRepository struct { + // 查询视图对象SQL + selectSql string + // 结果字段与实体映射 + resultMap map[string]string +} + +// convertResultRows 将结果记录转实体结果组 +func (r *PtNeConfigDataLogRepository) convertResultRows(rows []map[string]any) []model.PtNeConfigDataLog { + arr := make([]model.PtNeConfigDataLog, 0) + for _, row := range rows { + item := model.PtNeConfigDataLog{} + 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 *PtNeConfigDataLogRepository) 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.PtNeConfigDataLog{}, + } + + // 查询数量 长度为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 *PtNeConfigDataLogRepository) SelectList(param model.PtNeConfigDataLog) []model.PtNeConfigDataLog { + // 查询条件拼接 + 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 create_time desc " + results, err := datasource.RawDB("", querySql, params) + if err != nil { + logger.Errorf("query err => %v", err) + } + + // 转换实体 + return r.convertResultRows(results) +} + +// SelectByIds 通过ID查询 +func (r *PtNeConfigDataLogRepository) SelectByIds(paramIds []string) []model.PtNeConfigDataLog { + 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.PtNeConfigDataLog{} + } + // 转换实体 + return r.convertResultRows(results) +} + +// Insert 新增信息 +func (r *PtNeConfigDataLogRepository) Insert(param model.PtNeConfigDataLog) string { + // 参数拼接 + params := make(map[string]any) + if param.CreateBy != "" { + params["create_by"] = param.CreateBy + params["create_time"] = time.Now().UnixMilli() + } + 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.ParamJsonOld != "" { + params["param_json_old"] = param.ParamJsonOld + } + if param.ParamJsonNew != "" { + params["param_json_new"] = param.ParamJsonNew + } + + // 构建执行语句 + keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params) + sql := "insert into pt_ne_config_data_log (" + 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 +} + +// DeleteByIds 批量删除信息 +func (r *PtNeConfigDataLogRepository) DeleteByIds(paramIds []string) int64 { + placeholder := repo.KeyPlaceholderByQuery(len(paramIds)) + sql := "delete from pt_ne_config_data_log 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 +} diff --git a/src/modules/practical_training/service/pt_ne_config_data.impl.go b/src/modules/practical_training/service/pt_ne_config_data.impl.go index 4a206404..3c5aaa4a 100644 --- a/src/modules/practical_training/service/pt_ne_config_data.impl.go +++ b/src/modules/practical_training/service/pt_ne_config_data.impl.go @@ -65,7 +65,7 @@ func (r *PtNeConfigDataService) DeleteByIds(paramIds []string) (int64, error) { // 检查是否存在 ids := r.ptNeConfigDataRepository.SelectByIds(paramIds) if len(ids) <= 0 { - return 0, fmt.Errorf("neHostCmd.noData") + return 0, fmt.Errorf("ptNeConfigData.noData") } if len(ids) == len(paramIds) { @@ -94,7 +94,11 @@ func (r *PtNeConfigDataService) SaveAsDefaultByType(neInfo neModel.NeInfo, stubT continue } // 将json数据转字符串存储 data:[{},{}] - paramDataByte, err := json.Marshal(resData) + dataArr, ok := resData["data"] + if !ok { + continue + } + paramDataByte, err := json.Marshal(dataArr) if err != nil { continue } @@ -138,7 +142,7 @@ func (r *PtNeConfigDataService) SelectByStubType(param model.PtNeConfigData) mod if len(list) > 0 { paraData = list[0] if err := json.Unmarshal([]byte(paraData.ParamJson), ¶Data.ParamData); err != nil { - paraData.ParamData = map[string]any{} + paraData.ParamData = []map[string]any{} } } return paraData diff --git a/src/modules/practical_training/service/pt_ne_config_data_log.go b/src/modules/practical_training/service/pt_ne_config_data_log.go new file mode 100644 index 00000000..e86df8c0 --- /dev/null +++ b/src/modules/practical_training/service/pt_ne_config_data_log.go @@ -0,0 +1,23 @@ +package service + +import ( + "be.ems/src/modules/practical_training/model" +) + +// IPtNeConfigDataLogService 服务层接口 +type IPtNeConfigDataLogService interface { + // SelectPage 根据条件分页查询字典类型 + SelectPage(query map[string]any) map[string]any + + // SelectList 根据实体查询 + SelectList(param model.PtNeConfigDataLog) []model.PtNeConfigDataLog + + // SelectByIds 通过ID查询 + SelectById(paramId string) model.PtNeConfigDataLog + + // Insert 新增信息 + Insert(param model.PtNeConfigDataLog) string + + // DeleteByIds 批量删除信息 + DeleteByIds(paramIds []string) (int64, error) +} diff --git a/src/modules/practical_training/service/pt_ne_config_data_log.impl.go b/src/modules/practical_training/service/pt_ne_config_data_log.impl.go new file mode 100644 index 00000000..a8e286bc --- /dev/null +++ b/src/modules/practical_training/service/pt_ne_config_data_log.impl.go @@ -0,0 +1,69 @@ +package service + +import ( + "fmt" + + neService "be.ems/src/modules/network_element/service" + "be.ems/src/modules/practical_training/model" + "be.ems/src/modules/practical_training/repository" +) + +// NewPtNeConfigDataService 实例化服务层 +var NewPtNeConfigDataLogService = &PtNeConfigDataLogService{ + ptNeConfigDataLogRepository: repository.NewPtNeConfigDataLogRepository, + neConfigService: neService.NewNeConfigImpl, + neInfoService: neService.NewNeInfoImpl, +} + +// PtNeConfigDataLogService 服务层处理 +type PtNeConfigDataLogService struct { + // 实训教学_网元参数配置数据变更日志 + ptNeConfigDataLogRepository repository.IPtNeConfigDataLogRepository + // 网元参数配置可用属性值服务 + neConfigService neService.INeConfig + // 网元信息服务 + neInfoService neService.INeInfo +} + +// SelectNeHostPage 分页查询列表数据 +func (r *PtNeConfigDataLogService) SelectPage(query map[string]any) map[string]any { + return r.ptNeConfigDataLogRepository.SelectPage(query) +} + +// SelectConfigList 查询列表 +func (r *PtNeConfigDataLogService) SelectList(param model.PtNeConfigDataLog) []model.PtNeConfigDataLog { + return r.ptNeConfigDataLogRepository.SelectList(param) +} + +// SelectByIds 通过ID查询 +func (r *PtNeConfigDataLogService) SelectById(paramId string) model.PtNeConfigDataLog { + if paramId == "" { + return model.PtNeConfigDataLog{} + } + neHosts := r.ptNeConfigDataLogRepository.SelectByIds([]string{paramId}) + if len(neHosts) > 0 { + return neHosts[0] + } + return model.PtNeConfigDataLog{} +} + +// Insert 新增信息 +func (r *PtNeConfigDataLogService) Insert(param model.PtNeConfigDataLog) string { + return r.ptNeConfigDataLogRepository.Insert(param) +} + +// DeleteByIds 批量删除信息 +func (r *PtNeConfigDataLogService) DeleteByIds(paramIds []string) (int64, error) { + // 检查是否存在 + ids := r.ptNeConfigDataLogRepository.SelectByIds(paramIds) + if len(ids) <= 0 { + return 0, fmt.Errorf("ptNeConfigDataLog.noData") + } + + if len(ids) == len(paramIds) { + rows := r.ptNeConfigDataLogRepository.DeleteByIds(paramIds) + return rows, nil + } + // 删除信息失败! + return 0, fmt.Errorf("delete fail") +}