feat: 新增网元主机命令接口
This commit is contained in:
152
src/modules/network_element/controller/ne_host_cmd.go
Normal file
152
src/modules/network_element/controller/ne_host_cmd.go
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"ems.agt/src/framework/i18n"
|
||||||
|
"ems.agt/src/framework/utils/ctx"
|
||||||
|
"ems.agt/src/framework/utils/parse"
|
||||||
|
"ems.agt/src/framework/vo/result"
|
||||||
|
"ems.agt/src/modules/network_element/model"
|
||||||
|
neService "ems.agt/src/modules/network_element/service"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/gin-gonic/gin/binding"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 实例化控制层 NeHostCmdController 结构体
|
||||||
|
var NewNeHostCmd = &NeHostCmdController{
|
||||||
|
neHostCmdService: neService.NewNeHostCmdImpl,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机命令请求
|
||||||
|
//
|
||||||
|
// PATH /hostCmd
|
||||||
|
type NeHostCmdController struct {
|
||||||
|
// 网元主机命令服务
|
||||||
|
neHostCmdService neService.INeHostCmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机命令列表
|
||||||
|
//
|
||||||
|
// GET /list
|
||||||
|
func (s *NeHostCmdController) List(c *gin.Context) {
|
||||||
|
querys := ctx.QueryMap(c)
|
||||||
|
data := s.neHostCmdService.SelectPage(querys)
|
||||||
|
|
||||||
|
c.JSON(200, result.Ok(data))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机命令信息
|
||||||
|
//
|
||||||
|
// GET /:cmdId
|
||||||
|
func (s *NeHostCmdController) Info(c *gin.Context) {
|
||||||
|
language := ctx.AcceptLanguage(c)
|
||||||
|
cmdId := c.Param("cmdId")
|
||||||
|
if cmdId == "" {
|
||||||
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
neHost := s.neHostCmdService.SelectById(cmdId)
|
||||||
|
if neHost.CmdID != cmdId {
|
||||||
|
// 没有可访问主机命令数据!
|
||||||
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, result.OkData(neHost))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机命令新增
|
||||||
|
//
|
||||||
|
// POST /
|
||||||
|
func (s *NeHostCmdController) Add(c *gin.Context) {
|
||||||
|
language := ctx.AcceptLanguage(c)
|
||||||
|
var body model.NeHostCmd
|
||||||
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||||
|
if err != nil || body.CmdID != "" {
|
||||||
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查属性值唯一
|
||||||
|
uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, "")
|
||||||
|
if !uniqueHostCmd {
|
||||||
|
// 主机命令操作【%s】失败,同组内名称已存在
|
||||||
|
msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
|
||||||
|
c.JSON(200, result.ErrMsg(msg))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body.CreateBy = ctx.LoginUserToUserName(c)
|
||||||
|
insertId := s.neHostCmdService.Insert(body)
|
||||||
|
if insertId != "" {
|
||||||
|
c.JSON(200, result.Ok(nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, result.Err(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机命令修改
|
||||||
|
//
|
||||||
|
// PUT /
|
||||||
|
func (s *NeHostCmdController) Edit(c *gin.Context) {
|
||||||
|
language := ctx.AcceptLanguage(c)
|
||||||
|
var body model.NeHostCmd
|
||||||
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||||
|
if err != nil || body.CmdID == "" {
|
||||||
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查属性值唯一
|
||||||
|
uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, body.CmdID)
|
||||||
|
if !uniqueHostCmd {
|
||||||
|
// 主机命令操作【%s】失败,同组内名称已存在
|
||||||
|
msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
|
||||||
|
c.JSON(200, result.ErrMsg(msg))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否存在
|
||||||
|
neHost := s.neHostCmdService.SelectById(body.CmdID)
|
||||||
|
if neHost.CmdID != body.CmdID {
|
||||||
|
// 没有可访问主机命令数据!
|
||||||
|
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||||||
|
rows := s.neHostCmdService.Update(body)
|
||||||
|
if rows > 0 {
|
||||||
|
c.JSON(200, result.Ok(nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(200, result.Err(nil))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 网元主机命令删除
|
||||||
|
//
|
||||||
|
// DELETE /:cmdIds
|
||||||
|
func (s *NeHostCmdController) Remove(c *gin.Context) {
|
||||||
|
language := ctx.AcceptLanguage(c)
|
||||||
|
cmdIds := c.Param("cmdIds")
|
||||||
|
if cmdIds == "" {
|
||||||
|
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 处理字符转id数组后去重
|
||||||
|
ids := strings.Split(cmdIds, ",")
|
||||||
|
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||||
|
if len(uniqueIDs) <= 0 {
|
||||||
|
c.JSON(200, result.Err(nil))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
rows, err := s.neHostCmdService.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))
|
||||||
|
}
|
||||||
20
src/modules/network_element/model/ne_host_cmd.go
Normal file
20
src/modules/network_element/model/ne_host_cmd.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
// NeHostCmd 网元主机命令表 ne_host_cmd
|
||||||
|
type NeHostCmd struct {
|
||||||
|
CmdID string `json:"cmdId" gorm:"column:cmd_id"` // 命令主键
|
||||||
|
CmdType string `json:"cmdType" gorm:"column:cmd_type"` // 命令类型
|
||||||
|
GroupID string `json:"groupId" gorm:"column:group_id"` // 分组(0默认)
|
||||||
|
Title string `json:"title" gorm:"column:title" binding:"required"` // 标题名称
|
||||||
|
Command string `json:"command" gorm:"column:command" binding:"required"` // 命令字符串
|
||||||
|
Remark string `json:"remark" gorm:"column:remark"` // 备注
|
||||||
|
CreateBy string `json:"createBy" gorm:"column:create_by"` // 创建者
|
||||||
|
CreateTime int64 `json:"createTime" gorm:"column:create_time"` // 创建时间
|
||||||
|
UpdateBy string `json:"updateBy" gorm:"column:update_by"` // 更新者
|
||||||
|
UpdateTime int64 `json:"updateTime" gorm:"column:update_time"` // 更新时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// TableName 表名称
|
||||||
|
func (*NeHostCmd) TableName() string {
|
||||||
|
return "ne_host_cmd"
|
||||||
|
}
|
||||||
@@ -92,6 +92,34 @@ func Setup(router *gin.Engine) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 网元主机命令
|
||||||
|
neHostCmdGroup := neGroup.Group("/hostCmd")
|
||||||
|
{
|
||||||
|
neHostCmdGroup.GET("/list",
|
||||||
|
middleware.PreAuthorize(nil),
|
||||||
|
controller.NewNeHostCmd.List,
|
||||||
|
)
|
||||||
|
neHostCmdGroup.GET("/:cmdId",
|
||||||
|
middleware.PreAuthorize(nil),
|
||||||
|
controller.NewNeHostCmd.Info,
|
||||||
|
)
|
||||||
|
neHostCmdGroup.POST("",
|
||||||
|
middleware.PreAuthorize(nil),
|
||||||
|
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHostCmd", collectlogs.BUSINESS_TYPE_INSERT)),
|
||||||
|
controller.NewNeHostCmd.Add,
|
||||||
|
)
|
||||||
|
neHostCmdGroup.PUT("",
|
||||||
|
middleware.PreAuthorize(nil),
|
||||||
|
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHostCmd", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||||
|
controller.NewNeHostCmd.Edit,
|
||||||
|
)
|
||||||
|
neHostCmdGroup.DELETE("/:cmdIds",
|
||||||
|
middleware.PreAuthorize(nil),
|
||||||
|
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neHostCmd", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||||
|
controller.NewNeHostCmd.Remove,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// UDM鉴权用户信息
|
// UDM鉴权用户信息
|
||||||
udmAuthGroup := neGroup.Group("/udm/auth")
|
udmAuthGroup := neGroup.Group("/udm/auth")
|
||||||
{
|
{
|
||||||
|
|||||||
27
src/modules/network_element/repository/ne_host_cmd.go
Normal file
27
src/modules/network_element/repository/ne_host_cmd.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import "ems.agt/src/modules/network_element/model"
|
||||||
|
|
||||||
|
// INeHostCmd 网元主机命令 数据层接口
|
||||||
|
type INeHostCmd interface {
|
||||||
|
// SelectPage 根据条件分页查询字典类型
|
||||||
|
SelectPage(query map[string]any) map[string]any
|
||||||
|
|
||||||
|
// SelectList 根据实体查询
|
||||||
|
SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd
|
||||||
|
|
||||||
|
// SelectByIds 通过ID查询
|
||||||
|
SelectByIds(cmdIds []string) []model.NeHostCmd
|
||||||
|
|
||||||
|
// Insert 新增信息
|
||||||
|
Insert(neHostCmd model.NeHostCmd) string
|
||||||
|
|
||||||
|
// Update 修改信息
|
||||||
|
Update(neHostCmd model.NeHostCmd) int64
|
||||||
|
|
||||||
|
// DeleteByIds 批量删除信息
|
||||||
|
DeleteByIds(cmdIds []string) int64
|
||||||
|
|
||||||
|
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||||
|
CheckUniqueGroupTitle(neHostCmd model.NeHostCmd) string
|
||||||
|
}
|
||||||
309
src/modules/network_element/repository/ne_host_cmd.impl.go
Normal file
309
src/modules/network_element/repository/ne_host_cmd.impl.go
Normal file
@@ -0,0 +1,309 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"ems.agt/src/framework/datasource"
|
||||||
|
"ems.agt/src/framework/logger"
|
||||||
|
"ems.agt/src/framework/utils/parse"
|
||||||
|
"ems.agt/src/framework/utils/repo"
|
||||||
|
"ems.agt/src/modules/network_element/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 实例化数据层 NewNeHostCmd 结构体
|
||||||
|
var NewNeHostCmdImpl = &NeHostCmd{
|
||||||
|
selectSql: `select
|
||||||
|
cmd_id, cmd_type, group_id, title, command, remark, create_by, create_time, update_by, update_time
|
||||||
|
from ne_host_cmd`,
|
||||||
|
|
||||||
|
resultMap: map[string]string{
|
||||||
|
"cmd_id": "CmdID",
|
||||||
|
"cmd_type": "CmdType",
|
||||||
|
"group_id": "GroupID",
|
||||||
|
"title": "Title",
|
||||||
|
"command": "Command",
|
||||||
|
"remark": "Remark",
|
||||||
|
"create_by": "CreateBy",
|
||||||
|
"create_time": "CreateTime",
|
||||||
|
"update_by": "UpdateBy",
|
||||||
|
"update_time": "UpdateTime",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// NeHostCmd 网元主机连接 数据层处理
|
||||||
|
type NeHostCmd struct {
|
||||||
|
// 查询视图对象SQL
|
||||||
|
selectSql string
|
||||||
|
// 结果字段与实体映射
|
||||||
|
resultMap map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
// convertResultRows 将结果记录转实体结果组
|
||||||
|
func (r *NeHostCmd) convertResultRows(rows []map[string]any) []model.NeHostCmd {
|
||||||
|
arr := make([]model.NeHostCmd, 0)
|
||||||
|
for _, row := range rows {
|
||||||
|
item := model.NeHostCmd{}
|
||||||
|
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 *NeHostCmd) SelectPage(query map[string]any) map[string]any {
|
||||||
|
// 查询条件拼接
|
||||||
|
var conditions []string
|
||||||
|
var params []any
|
||||||
|
if v, ok := query["cmdType"]; ok && v != "" {
|
||||||
|
conditions = append(conditions, "cmd_type = ?")
|
||||||
|
params = append(params, strings.Trim(v.(string), " "))
|
||||||
|
}
|
||||||
|
if v, ok := query["groupId"]; ok && v != "" {
|
||||||
|
conditions = append(conditions, "group_id = ?")
|
||||||
|
params = append(params, strings.Trim(v.(string), " "))
|
||||||
|
}
|
||||||
|
if v, ok := query["title"]; ok && v != "" {
|
||||||
|
conditions = append(conditions, "title 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_host_cmd"
|
||||||
|
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 *NeHostCmd) SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd {
|
||||||
|
// 查询条件拼接
|
||||||
|
var conditions []string
|
||||||
|
var params []any
|
||||||
|
if neHostCmd.CmdType != "" {
|
||||||
|
conditions = append(conditions, "cmd_type = ?")
|
||||||
|
params = append(params, neHostCmd.CmdType)
|
||||||
|
}
|
||||||
|
if neHostCmd.GroupID != "" {
|
||||||
|
conditions = append(conditions, "group_id = ?")
|
||||||
|
params = append(params, neHostCmd.GroupID)
|
||||||
|
}
|
||||||
|
if neHostCmd.Title != "" {
|
||||||
|
conditions = append(conditions, "title like concat(?, '%')")
|
||||||
|
params = append(params, neHostCmd.Title)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询条件语句
|
||||||
|
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 *NeHostCmd) SelectByIds(cmdIds []string) []model.NeHostCmd {
|
||||||
|
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||||
|
querySql := r.selectSql + " where cmd_id in (" + placeholder + ")"
|
||||||
|
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||||
|
results, err := datasource.RawDB("", querySql, parameters)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("query err => %v", err)
|
||||||
|
return []model.NeHostCmd{}
|
||||||
|
}
|
||||||
|
// 转换实体
|
||||||
|
return r.convertResultRows(results)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||||
|
func (r *NeHostCmd) CheckUniqueGroupTitle(neHostCmd model.NeHostCmd) string {
|
||||||
|
// 查询条件拼接
|
||||||
|
var conditions []string
|
||||||
|
var params []any
|
||||||
|
if neHostCmd.CmdType != "" {
|
||||||
|
conditions = append(conditions, "cmd_type = ?")
|
||||||
|
params = append(params, neHostCmd.CmdType)
|
||||||
|
}
|
||||||
|
if neHostCmd.GroupID != "" {
|
||||||
|
conditions = append(conditions, "group_id = ?")
|
||||||
|
params = append(params, neHostCmd.GroupID)
|
||||||
|
}
|
||||||
|
if neHostCmd.Title != "" {
|
||||||
|
conditions = append(conditions, "title = ?")
|
||||||
|
params = append(params, neHostCmd.Title)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建查询条件语句
|
||||||
|
whereSql := ""
|
||||||
|
if len(conditions) > 0 {
|
||||||
|
whereSql += " where " + strings.Join(conditions, " and ")
|
||||||
|
} else {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询数据
|
||||||
|
querySql := "select host_id as 'str' from ne_host " + whereSql + " limit 1"
|
||||||
|
results, err := datasource.RawDB("", querySql, params)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("query err %v", err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if len(results) > 0 {
|
||||||
|
v, ok := results[0]["str"].(string)
|
||||||
|
if ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert 新增信息
|
||||||
|
func (r *NeHostCmd) Insert(neHostCmd model.NeHostCmd) string {
|
||||||
|
// 参数拼接
|
||||||
|
params := make(map[string]any)
|
||||||
|
if neHostCmd.CmdType != "" {
|
||||||
|
params["cmd_type"] = neHostCmd.CmdType
|
||||||
|
}
|
||||||
|
if neHostCmd.GroupID != "" {
|
||||||
|
params["group_id"] = neHostCmd.GroupID
|
||||||
|
}
|
||||||
|
if neHostCmd.Title != "" {
|
||||||
|
params["title"] = neHostCmd.Title
|
||||||
|
}
|
||||||
|
if neHostCmd.Command != "" {
|
||||||
|
params["command"] = neHostCmd.Command
|
||||||
|
}
|
||||||
|
if neHostCmd.Remark != "" {
|
||||||
|
params["remark"] = neHostCmd.Remark
|
||||||
|
}
|
||||||
|
if neHostCmd.CreateBy != "" {
|
||||||
|
params["create_by"] = neHostCmd.CreateBy
|
||||||
|
params["create_time"] = time.Now().UnixMilli()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建执行语句
|
||||||
|
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||||
|
sql := "insert into ne_host_cmd (" + 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 *NeHostCmd) Update(neHostCmd model.NeHostCmd) int64 {
|
||||||
|
// 参数拼接
|
||||||
|
params := make(map[string]any)
|
||||||
|
if neHostCmd.CmdType != "" {
|
||||||
|
params["cmd_type"] = neHostCmd.CmdType
|
||||||
|
}
|
||||||
|
if neHostCmd.GroupID != "" {
|
||||||
|
params["group_id"] = neHostCmd.GroupID
|
||||||
|
}
|
||||||
|
if neHostCmd.Title != "" {
|
||||||
|
params["title"] = neHostCmd.Title
|
||||||
|
}
|
||||||
|
if neHostCmd.Command != "" {
|
||||||
|
params["command"] = neHostCmd.Command
|
||||||
|
}
|
||||||
|
params["remark"] = neHostCmd.Remark
|
||||||
|
if neHostCmd.UpdateBy != "" {
|
||||||
|
params["update_by"] = neHostCmd.UpdateBy
|
||||||
|
params["update_time"] = time.Now().UnixMilli()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 构建执行语句
|
||||||
|
keys, values := repo.KeyValueByUpdate(params)
|
||||||
|
sql := "update ne_host_cmd set " + strings.Join(keys, ",") + " where cmd_id = ?"
|
||||||
|
|
||||||
|
// 执行更新
|
||||||
|
values = append(values, neHostCmd.CmdID)
|
||||||
|
rows, err := datasource.ExecDB("", sql, values)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("update row : %v", err.Error())
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return rows
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteByIds 批量删除信息
|
||||||
|
func (r *NeHostCmd) DeleteByIds(cmdIds []string) int64 {
|
||||||
|
placeholder := repo.KeyPlaceholderByQuery(len(cmdIds))
|
||||||
|
sql := "delete from ne_host_cmd where cmd_id in (" + placeholder + ")"
|
||||||
|
parameters := repo.ConvertIdsSlice(cmdIds)
|
||||||
|
results, err := datasource.ExecDB("", sql, parameters)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorf("delete err => %v", err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
27
src/modules/network_element/service/ne_host_cmd.go
Normal file
27
src/modules/network_element/service/ne_host_cmd.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import "ems.agt/src/modules/network_element/model"
|
||||||
|
|
||||||
|
// INeHostCmd 网元主机命令 服务层接口
|
||||||
|
type INeHostCmd interface {
|
||||||
|
// SelectPage 根据条件分页查询字典类型
|
||||||
|
SelectPage(query map[string]any) map[string]any
|
||||||
|
|
||||||
|
// SelectList 根据实体查询
|
||||||
|
SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd
|
||||||
|
|
||||||
|
// SelectByIds 通过ID查询
|
||||||
|
SelectById(cmdId string) model.NeHostCmd
|
||||||
|
|
||||||
|
// Insert 新增信息
|
||||||
|
Insert(neHostCmd model.NeHostCmd) string
|
||||||
|
|
||||||
|
// Update 修改信息
|
||||||
|
Update(neHostCmd model.NeHostCmd) int64
|
||||||
|
|
||||||
|
// DeleteByIds 批量删除信息
|
||||||
|
DeleteByIds(cmdIds []string) (int64, error)
|
||||||
|
|
||||||
|
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||||
|
CheckUniqueGroupTitle(groupId, title, cmdType, cmdId string) bool
|
||||||
|
}
|
||||||
80
src/modules/network_element/service/ne_host_cmd.impl.go
Normal file
80
src/modules/network_element/service/ne_host_cmd.impl.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"ems.agt/src/modules/network_element/model"
|
||||||
|
"ems.agt/src/modules/network_element/repository"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 实例化服务层 NeHostCmdImpl 结构体
|
||||||
|
var NewNeHostCmdImpl = &NeHostCmdImpl{
|
||||||
|
neHostCmdRepository: repository.NewNeHostCmdImpl,
|
||||||
|
}
|
||||||
|
|
||||||
|
// NeHostCmdImpl 网元主机命令 服务层处理
|
||||||
|
type NeHostCmdImpl struct {
|
||||||
|
// 网元主机命令表
|
||||||
|
neHostCmdRepository repository.INeHostCmd
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectNeHostPage 分页查询列表数据
|
||||||
|
func (r *NeHostCmdImpl) SelectPage(query map[string]any) map[string]any {
|
||||||
|
return r.neHostCmdRepository.SelectPage(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectConfigList 查询列表
|
||||||
|
func (r *NeHostCmdImpl) SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd {
|
||||||
|
return r.neHostCmdRepository.SelectList(neHostCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SelectByIds 通过ID查询
|
||||||
|
func (r *NeHostCmdImpl) SelectById(cmdId string) model.NeHostCmd {
|
||||||
|
if cmdId == "" {
|
||||||
|
return model.NeHostCmd{}
|
||||||
|
}
|
||||||
|
neHosts := r.neHostCmdRepository.SelectByIds([]string{cmdId})
|
||||||
|
if len(neHosts) > 0 {
|
||||||
|
return neHosts[0]
|
||||||
|
}
|
||||||
|
return model.NeHostCmd{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Insert 新增信息
|
||||||
|
func (r *NeHostCmdImpl) Insert(neHostCmd model.NeHostCmd) string {
|
||||||
|
return r.neHostCmdRepository.Insert(neHostCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update 修改信息
|
||||||
|
func (r *NeHostCmdImpl) Update(neHostCmd model.NeHostCmd) int64 {
|
||||||
|
return r.neHostCmdRepository.Update(neHostCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteByIds 批量删除信息
|
||||||
|
func (r *NeHostCmdImpl) DeleteByIds(cmdIds []string) (int64, error) {
|
||||||
|
// 检查是否存在
|
||||||
|
ids := r.neHostCmdRepository.SelectByIds(cmdIds)
|
||||||
|
if len(ids) <= 0 {
|
||||||
|
return 0, fmt.Errorf("neHostCmd.noData")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(ids) == len(cmdIds) {
|
||||||
|
rows := r.neHostCmdRepository.DeleteByIds(cmdIds)
|
||||||
|
return rows, nil
|
||||||
|
}
|
||||||
|
// 删除信息失败!
|
||||||
|
return 0, fmt.Errorf("delete fail")
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckUniqueGroupTitle 校验同类型组内是否唯一
|
||||||
|
func (r *NeHostCmdImpl) CheckUniqueGroupTitle(groupId, title, cmdType, cmdId string) bool {
|
||||||
|
uniqueId := r.neHostCmdRepository.CheckUniqueGroupTitle(model.NeHostCmd{
|
||||||
|
CmdType: cmdType,
|
||||||
|
GroupID: groupId,
|
||||||
|
Title: title,
|
||||||
|
})
|
||||||
|
if uniqueId == cmdId {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return uniqueId == ""
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user