From 0a3a835a85bd680c1c2df1b7aafe0574294a4b87 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 23 Jul 2024 11:57:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BD=91=E5=85=83=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=87=E4=BB=BD=E8=AE=B0=E5=BD=95=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ne_config_backup.go | 82 ++++++ .../network_element/model/ne_config_backup.go | 24 ++ .../repository/ne_config_backup.go | 24 ++ .../repository/ne_config_backup.impl.go | 269 ++++++++++++++++++ .../service/ne_config_backup.go | 24 ++ .../service/ne_config_backup.impl.go | 67 +++++ 6 files changed, 490 insertions(+) create mode 100644 src/modules/network_element/controller/ne_config_backup.go create mode 100644 src/modules/network_element/model/ne_config_backup.go create mode 100644 src/modules/network_element/repository/ne_config_backup.go create mode 100644 src/modules/network_element/repository/ne_config_backup.impl.go create mode 100644 src/modules/network_element/service/ne_config_backup.go create mode 100644 src/modules/network_element/service/ne_config_backup.impl.go diff --git a/src/modules/network_element/controller/ne_config_backup.go b/src/modules/network_element/controller/ne_config_backup.go new file mode 100644 index 00000000..ad12c8cb --- /dev/null +++ b/src/modules/network_element/controller/ne_config_backup.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" + neService "be.ems/src/modules/network_element/service" + "github.com/gin-gonic/gin" +) + +// NewNeConfigBackup 实例化控制层 NeConfigBackupController 结构体 +var NewNeConfigBackup = &NeConfigBackupController{ + neConfigBackupService: neService.NewNeConfigBackupImpl, +} + +// 网元配置文件备份记录 +// +// PATH /config/backup +type NeConfigBackupController struct { + // 网元配置文件备份记录服务 + neConfigBackupService neService.INeConfigBackup +} + +// 网元配置文件备份记录列表 +// +// GET /list +func (s *NeConfigBackupController) List(c *gin.Context) { + querys := ctx.QueryMap(c) + data := s.neConfigBackupService.SelectPage(querys) + + c.JSON(200, result.Ok(data)) +} + +// 网元配置文件备份记录信息 +// +// GET /?id=xx +func (s *NeConfigBackupController) Info(c *gin.Context) { + language := ctx.AcceptLanguage(c) + id, ok := c.GetQuery("id") + if !ok || id == "" { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + + item := s.neConfigBackupService.SelectById(id) + if item.ID != id { + // 没有可访问主机命令数据! + c.JSON(200, result.ErrMsg(i18n.TKey(language, "neConfigBackup.noData"))) + return + } + + c.JSON(200, result.OkData(item)) +} + +// 网元配置文件备份记录删除 +// +// DELETE /?id=xx +func (s *NeConfigBackupController) Remove(c *gin.Context) { + language := ctx.AcceptLanguage(c) + id, ok := c.GetQuery("id") + if !ok || id == "" { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + // 处理字符转id数组后去重 + ids := strings.Split(id, ",") + uniqueIDs := parse.RemoveDuplicates(ids) + if len(uniqueIDs) <= 0 { + c.JSON(200, result.Err(nil)) + return + } + rows, err := s.neConfigBackupService.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/network_element/model/ne_config_backup.go b/src/modules/network_element/model/ne_config_backup.go new file mode 100644 index 00000000..e753f652 --- /dev/null +++ b/src/modules/network_element/model/ne_config_backup.go @@ -0,0 +1,24 @@ +package model + +// NeConfigBackup 网元配置文件备份记录 ne_config_backup +type NeConfigBackup struct { + ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"` + NeType string `json:"neType" gorm:"ne_type"` // 网元类型 + NeId string `json:"neId" gorm:"ne_id"` // 网元ID + Name string `json:"name" gorm:"name"` // 命名 + Version string `json:"version" gorm:"version"` // 网元版本 + Path string `json:"path" gorm:"path"` // 压缩包位置 + Remark string `json:"remark" gorm:"remark"` // 备注 + 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"` // 更新时间 + + // ====== 非数据库字段属性 ====== + +} + +// TableName 表名称 +func (*NeConfigBackup) TableName() string { + return "ne_config_backup" +} diff --git a/src/modules/network_element/repository/ne_config_backup.go b/src/modules/network_element/repository/ne_config_backup.go new file mode 100644 index 00000000..131f363d --- /dev/null +++ b/src/modules/network_element/repository/ne_config_backup.go @@ -0,0 +1,24 @@ +package repository + +import "be.ems/src/modules/network_element/model" + +// INeConfigBackup 网元配置文件备份记录 数据层接口 +type INeConfigBackup interface { + // SelectPage 根据条件分页查询字典类型 + SelectPage(query map[string]any) map[string]any + + // SelectList 根据实体查询 + SelectList(item model.NeConfigBackup) []model.NeConfigBackup + + // SelectByIds 通过ID查询 + SelectByIds(ids []string) []model.NeConfigBackup + + // Insert 新增信息 + Insert(item model.NeConfigBackup) string + + // Update 修改信息 + Update(item model.NeConfigBackup) int64 + + // DeleteByIds 批量删除信息 + DeleteByIds(ids []string) int64 +} diff --git a/src/modules/network_element/repository/ne_config_backup.impl.go b/src/modules/network_element/repository/ne_config_backup.impl.go new file mode 100644 index 00000000..c772da54 --- /dev/null +++ b/src/modules/network_element/repository/ne_config_backup.impl.go @@ -0,0 +1,269 @@ +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/network_element/model" +) + +// 实例化数据层 NewNeConfigBackupImpl 结构体 +var NewNeConfigBackupImpl = &NeConfigBackupImpl{ + selectSql: `select + id, ne_type, ne_id, name, version, path, remark, create_by, create_time, update_by, update_time + from ne_config_backup`, + + resultMap: map[string]string{ + "id": "ID", + "ne_type": "NeType", + "ne_id": "NeId", + "name": "Name", + "version": "Version", + "path": "Path", + "remark": "Remark", + "create_by": "CreateBy", + "create_time": "CreateTime", + "update_by": "UpdateBy", + "update_time": "UpdateTime", + }, +} + +// NeConfigBackupImpl 网元配置文件备份记录 数据层处理 +type NeConfigBackupImpl struct { + // 查询视图对象SQL + selectSql string + // 结果字段与实体映射 + resultMap map[string]string +} + +// convertResultRows 将结果记录转实体结果组 +func (r *NeConfigBackupImpl) convertResultRows(rows []map[string]any) []model.NeConfigBackup { + arr := make([]model.NeConfigBackup, 0) + for _, row := range rows { + item := model.NeConfigBackup{} + 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 *NeConfigBackupImpl) 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, strings.Trim(v.(string), " ")) + } + if v, ok := query["neId"]; ok && v != "" { + conditions = append(conditions, "ne_id = ?") + params = append(params, strings.Trim(v.(string), " ")) + } + if v, ok := query["name"]; ok && v != "" { + conditions = append(conditions, "name like concat(?, '%')") + params = append(params, strings.Trim(v.(string), " ")) + } + + // 构建查询条件语句 + whereSql := "" + if len(conditions) > 0 { + whereSql += " where " + strings.Join(conditions, " and ") + } + + result := map[string]any{ + "total": 0, + "rows": []model.NeHost{}, + } + + // 查询数量 长度为0直接返回 + totalSql := "select count(1) as 'total' from ne_config_backup" + 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 *NeConfigBackupImpl) SelectList(item model.NeConfigBackup) []model.NeConfigBackup { + // 查询条件拼接 + var conditions []string + var params []any + if item.NeType != "" { + conditions = append(conditions, "ne_type = ?") + params = append(params, item.NeType) + } + if item.NeId != "" { + conditions = append(conditions, "ne_id = ?") + params = append(params, item.NeId) + } + + // 构建查询条件语句 + whereSql := "" + if len(conditions) > 0 { + whereSql += " where " + strings.Join(conditions, " and ") + } + + // 查询数据 + querySql := r.selectSql + whereSql + " order by id desc " + results, err := datasource.RawDB("", querySql, params) + if err != nil { + logger.Errorf("query err => %v", err) + } + + // 转换实体 + return r.convertResultRows(results) +} + +// SelectByIds 通过ID查询 +func (r *NeConfigBackupImpl) SelectByIds(cmdIds []string) []model.NeConfigBackup { + placeholder := repo.KeyPlaceholderByQuery(len(cmdIds)) + querySql := r.selectSql + " where id in (" + placeholder + ")" + parameters := repo.ConvertIdsSlice(cmdIds) + results, err := datasource.RawDB("", querySql, parameters) + if err != nil { + logger.Errorf("query err => %v", err) + return []model.NeConfigBackup{} + } + // 转换实体 + return r.convertResultRows(results) +} + +// Insert 新增信息 +func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string { + // 参数拼接 + params := make(map[string]any) + if item.NeType != "" { + params["ne_type"] = item.NeType + } + if item.NeId != "" { + params["ne_id"] = item.NeId + } + if item.Name != "" { + params["name"] = item.Name + } + if item.Version != "" { + params["version"] = item.Version + } + if item.Path != "" { + params["path"] = item.Path + } + if item.Remark != "" { + params["remark"] = item.Remark + } + if item.CreateBy != "" { + params["create_by"] = item.CreateBy + params["create_time"] = time.Now().UnixMilli() + } + + // 构建执行语句 + keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params) + sql := "insert into ne_config_backup (" + 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 *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 { + // 参数拼接 + params := make(map[string]any) + if item.NeType != "" { + params["ne_type"] = item.NeType + } + if item.NeId != "" { + params["ne_id"] = item.NeId + } + if item.Name != "" { + params["name"] = item.Name + } + if item.Version != "" { + params["version"] = item.Version + } + if item.Path != "" { + params["path"] = item.Path + } + params["remark"] = item.Remark + if item.UpdateBy != "" { + params["update_by"] = item.UpdateBy + params["update_time"] = time.Now().UnixMilli() + } + + // 构建执行语句 + keys, values := repo.KeyValueByUpdate(params) + sql := "update ne_config_backup set " + strings.Join(keys, ",") + " where id = ?" + + // 执行更新 + values = append(values, item.ID) + rows, err := datasource.ExecDB("", sql, values) + if err != nil { + logger.Errorf("update row : %v", err.Error()) + return 0 + } + return rows +} + +// DeleteByIds 批量删除信息 +func (r *NeConfigBackupImpl) DeleteByIds(ids []string) int64 { + placeholder := repo.KeyPlaceholderByQuery(len(ids)) + sql := "delete from ne_config_backup where id in (" + placeholder + ")" + parameters := repo.ConvertIdsSlice(ids) + results, err := datasource.ExecDB("", sql, parameters) + if err != nil { + logger.Errorf("delete err => %v", err) + return 0 + } + return results +} diff --git a/src/modules/network_element/service/ne_config_backup.go b/src/modules/network_element/service/ne_config_backup.go new file mode 100644 index 00000000..960bf569 --- /dev/null +++ b/src/modules/network_element/service/ne_config_backup.go @@ -0,0 +1,24 @@ +package service + +import "be.ems/src/modules/network_element/model" + +// INeConfigBackup 网元配置文件备份记录 服务层接口 +type INeConfigBackup interface { + // SelectNeHostPage 分页查询列表数据 + SelectPage(query map[string]any) map[string]any + + // SelectList 根据实体查询 + SelectList(item model.NeConfigBackup) []model.NeConfigBackup + + // SelectByIds 通过ID查询 + SelectById(id string) model.NeConfigBackup + + // Insert 新增信息 + Insert(item model.NeConfigBackup) string + + // Update 修改信息 + Update(item model.NeConfigBackup) int64 + + // DeleteByIds 批量删除信息 + DeleteByIds(ids []string) (int64, error) +} diff --git a/src/modules/network_element/service/ne_config_backup.impl.go b/src/modules/network_element/service/ne_config_backup.impl.go new file mode 100644 index 00000000..38f3eea2 --- /dev/null +++ b/src/modules/network_element/service/ne_config_backup.impl.go @@ -0,0 +1,67 @@ +package service + +import ( + "fmt" + + "be.ems/src/modules/network_element/model" + "be.ems/src/modules/network_element/repository" +) + +// NewNeConfigBackupImpl 网元配置文件备份记录 实例化服务层 +var NewNeConfigBackupImpl = &NeConfigBackupImpl{ + neConfigBackupRepository: repository.NewNeConfigBackupImpl, +} + +// NeConfigBackupImpl 网元配置文件备份记录 服务层处理 +type NeConfigBackupImpl struct { + // 网元配置文件备份记录 + neConfigBackupRepository repository.INeConfigBackup +} + +// SelectNeHostPage 分页查询列表数据 +func (r *NeConfigBackupImpl) SelectPage(query map[string]any) map[string]any { + return r.neConfigBackupRepository.SelectPage(query) +} + +// SelectConfigList 查询列表 +func (r *NeConfigBackupImpl) SelectList(item model.NeConfigBackup) []model.NeConfigBackup { + return r.neConfigBackupRepository.SelectList(item) +} + +// SelectByIds 通过ID查询 +func (r *NeConfigBackupImpl) SelectById(id string) model.NeConfigBackup { + if id == "" { + return model.NeConfigBackup{} + } + arr := r.neConfigBackupRepository.SelectByIds([]string{id}) + if len(arr) > 0 { + return arr[0] + } + return model.NeConfigBackup{} +} + +// Insert 新增信息 +func (r *NeConfigBackupImpl) Insert(item model.NeConfigBackup) string { + return r.neConfigBackupRepository.Insert(item) +} + +// Update 修改信息 +func (r *NeConfigBackupImpl) Update(item model.NeConfigBackup) int64 { + return r.neConfigBackupRepository.Update(item) +} + +// DeleteByIds 批量删除信息 +func (r *NeConfigBackupImpl) DeleteByIds(ids []string) (int64, error) { + // 检查是否存在 + data := r.neConfigBackupRepository.SelectByIds(ids) + if len(data) <= 0 { + return 0, fmt.Errorf("neConfigBackup.noData") + } + + if len(data) == len(ids) { + rows := r.neConfigBackupRepository.DeleteByIds(ids) + return rows, nil + } + // 删除信息失败! + return 0, fmt.Errorf("delete fail") +}