feat: 新增网元信息接口
This commit is contained in:
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"ems.agt/src/framework/i18n"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"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"
|
||||
)
|
||||
|
||||
// 实例化控制层 NeInfoController 结构体
|
||||
@@ -30,10 +32,10 @@ type NeInfoController struct {
|
||||
var neStateCacheMap sync.Map
|
||||
var mutex sync.Mutex
|
||||
|
||||
// 网元状态
|
||||
// 网元信息状态
|
||||
//
|
||||
// GET /state
|
||||
func (s *NeInfoController) NeState(c *gin.Context) {
|
||||
func (s *NeInfoController) State(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
@@ -82,32 +84,23 @@ func (s *NeInfoController) NeState(c *gin.Context) {
|
||||
c.JSON(200, result.OkData(resData))
|
||||
}
|
||||
|
||||
// 网元neType和neID查询
|
||||
//
|
||||
// GET /info
|
||||
func (s *NeInfoController) NeTypeAndID(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(neInfo))
|
||||
}
|
||||
|
||||
// 网元列表
|
||||
// 网元信息列表
|
||||
//
|
||||
// GET /list
|
||||
func (s *NeInfoController) NeList(c *gin.Context) {
|
||||
func (s *NeInfoController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
bandStatus := false
|
||||
if v, ok := querys["bandStatus"]; ok && v != nil {
|
||||
bandStatus = parse.Boolean(v)
|
||||
}
|
||||
data := s.neInfoService.SelectPage(querys, bandStatus)
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
// 网元信息列表全部无分页
|
||||
//
|
||||
// GET /listAll
|
||||
func (s *NeInfoController) ListAll(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType"`
|
||||
@@ -128,10 +121,145 @@ func (s *NeInfoController) NeList(c *gin.Context) {
|
||||
ne.NeId = querys.NeId
|
||||
}
|
||||
bandStatus := parse.Boolean(querys.BandStatus)
|
||||
neList := s.neInfoService.SelectNeList(ne, bandStatus)
|
||||
neList := s.neInfoService.SelectList(ne, bandStatus)
|
||||
if len(neList) == 0 {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(neList))
|
||||
}
|
||||
|
||||
// 网元信息
|
||||
//
|
||||
// GET /:infoId
|
||||
func (s *NeInfoController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
infoId := c.Param("infoId")
|
||||
if infoId == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neHost := s.neInfoService.SelectById(infoId)
|
||||
if neHost.ID != infoId {
|
||||
// 没有可访问网元信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neInfo.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, result.OkData(neHost))
|
||||
}
|
||||
|
||||
// 网元neType和neID查询
|
||||
//
|
||||
// GET /
|
||||
func (s *NeInfoController) NeTypeAndID(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
NeType string `form:"neType" binding:"required"`
|
||||
NeID string `form:"neId" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.OkData(neInfo))
|
||||
}
|
||||
|
||||
// 网元信息新增
|
||||
//
|
||||
// POST /
|
||||
func (s *NeInfoController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeInfo
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID != "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueInfo := s.neInfoService.CheckUniqueNeTypeAndNeId(body.NeType, body.NeId, "")
|
||||
if !uniqueInfo {
|
||||
// 网元信息操作【%s】失败,同类型下标识已存在
|
||||
msg := i18n.TTemplate(language, "neInfo.errKeyExists", map[string]any{"key": body.NeId})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
insertId := s.neInfoService.Insert(body)
|
||||
if insertId != "" {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元信息修改
|
||||
//
|
||||
// PUT /
|
||||
func (s *NeInfoController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.NeInfo
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.ID == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查属性值唯一
|
||||
uniqueHostCmd := s.neInfoService.CheckUniqueNeTypeAndNeId(body.NeType, body.NeId, body.ID)
|
||||
if !uniqueHostCmd {
|
||||
// 网元信息操作【%s】失败,同类型下标识已存在
|
||||
msg := i18n.TTemplate(language, "neInfo.errKeyExists", map[string]any{"key": body.NeId})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否存在
|
||||
neInfo := s.neInfoService.SelectById(body.ID)
|
||||
if neInfo.ID != body.ID {
|
||||
// 没有可访问网元信息数据!
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "neInfo.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
rows := s.neInfoService.Update(body)
|
||||
if rows > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
}
|
||||
|
||||
// 网元信息删除
|
||||
//
|
||||
// DELETE /:infoIds
|
||||
func (s *NeInfoController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
infoIds := c.Param("infoIds")
|
||||
if infoIds == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
ids := strings.Split(infoIds, ",")
|
||||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||||
if len(uniqueIDs) <= 0 {
|
||||
c.JSON(200, result.Err(nil))
|
||||
return
|
||||
}
|
||||
rows, err := s.neInfoService.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))
|
||||
}
|
||||
|
||||
@@ -2,14 +2,14 @@ package model
|
||||
|
||||
// NeInfo 网元信息对象 ne_info
|
||||
type NeInfo struct {
|
||||
ID int64 `json:"id"`
|
||||
NeType string `json:"neType"`
|
||||
NeId string `json:"neId"`
|
||||
ID string `json:"id"`
|
||||
NeType string `json:"neType" binding:"required"`
|
||||
NeId string `json:"neId" binding:"required"`
|
||||
RmUID string `json:"rmUid"`
|
||||
NeName string `json:"neName"`
|
||||
IP string `json:"ip"`
|
||||
Port int64 `json:"port"`
|
||||
PvFlag string `json:"pvFlag"` // enum('PNF','VNF')
|
||||
IP string `json:"ip" binding:"required"`
|
||||
Port int64 `json:"port" binding:"required,number,max=65535,min=1"`
|
||||
PvFlag string `json:"pvFlag" binding:"oneof=PNF VNF"` // enum('PNF','VNF')
|
||||
Province string `json:"province"`
|
||||
VendorName string `json:"vendorName"`
|
||||
Dn string `json:"dn"`
|
||||
|
||||
@@ -20,23 +20,7 @@ func Setup(router *gin.Engine) {
|
||||
|
||||
neGroup := router.Group("/ne")
|
||||
|
||||
// 网元信息
|
||||
{
|
||||
neGroup.GET("/info",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.NeTypeAndID,
|
||||
)
|
||||
neGroup.GET("/state",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.NeState,
|
||||
)
|
||||
neGroup.GET("/list",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.NeList,
|
||||
)
|
||||
}
|
||||
|
||||
// 网元处理
|
||||
// 网元操作处理
|
||||
neActionGroup := neGroup.Group("/action")
|
||||
{
|
||||
neActionGroup.GET("/files",
|
||||
@@ -54,6 +38,46 @@ func Setup(router *gin.Engine) {
|
||||
)
|
||||
}
|
||||
|
||||
// 网元信息
|
||||
neInfoGroup := neGroup.Group("/info")
|
||||
{
|
||||
neInfoGroup.GET("/listAll",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.ListAll,
|
||||
)
|
||||
neInfoGroup.GET("/list",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.List,
|
||||
)
|
||||
neInfoGroup.GET("/:infoId",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.Info,
|
||||
)
|
||||
neInfoGroup.POST("",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_INSERT)),
|
||||
controller.NewNeInfo.Add,
|
||||
)
|
||||
neInfoGroup.PUT("",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_UPDATE)),
|
||||
controller.NewNeInfo.Edit,
|
||||
)
|
||||
neInfoGroup.DELETE("/:infoIds",
|
||||
middleware.PreAuthorize(nil),
|
||||
collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_DELETE)),
|
||||
controller.NewNeInfo.Remove,
|
||||
)
|
||||
neInfoGroup.GET("/state",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.State,
|
||||
)
|
||||
neInfoGroup.GET("/byTypeAndID",
|
||||
middleware.PreAuthorize(nil),
|
||||
controller.NewNeInfo.NeTypeAndID,
|
||||
)
|
||||
}
|
||||
|
||||
// 网元主机
|
||||
neHostGroup := neGroup.Group("/host")
|
||||
{
|
||||
|
||||
@@ -9,6 +9,24 @@ type INeInfo interface {
|
||||
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
||||
SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo
|
||||
|
||||
// SelectNeList 查询网元列表
|
||||
SelectNeList(ne model.NeInfo) []model.NeInfo
|
||||
// SelectPage 根据条件分页查询
|
||||
SelectPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectList 查询列表
|
||||
SelectList(neInfo model.NeInfo) []model.NeInfo
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectByIds(infoIds []string) []model.NeInfo
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neInfo model.NeInfo) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neInfo model.NeInfo) int64
|
||||
|
||||
// DeleteByIds 批量删除网元信息
|
||||
DeleteByIds(infoIds []string) int64
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string
|
||||
}
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"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"
|
||||
)
|
||||
@@ -114,18 +117,80 @@ func (r *NeInfoImpl) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeIn
|
||||
return model.NeInfo{}
|
||||
}
|
||||
|
||||
// SelectNeList 查询网元列表
|
||||
func (r *NeInfoImpl) SelectNeList(ne model.NeInfo) []model.NeInfo {
|
||||
// SelectPage 根据条件分页查询
|
||||
func (r *NeInfoImpl) SelectPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if ne.NeType != "" {
|
||||
if v, ok := query["neType"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, ne.NeType)
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if ne.NeId != "" {
|
||||
if v, ok := query["neId"]; ok && v != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, ne.NeId)
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
}
|
||||
if v, ok := query["rmUid"]; ok && v != "" {
|
||||
conditions = append(conditions, "rmUid 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.NeInfo{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from ne_info"
|
||||
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 + " order by ne_type asc, ne_id desc " + 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 *NeInfoImpl) SelectList(neInfo model.NeInfo) []model.NeInfo {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neInfo.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neInfo.NeType)
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neInfo.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
@@ -144,3 +209,178 @@ func (r *NeInfoImpl) SelectNeList(ne model.NeInfo) []model.NeInfo {
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeInfoImpl) SelectByIds(infoIds []string) []model.NeInfo {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(infoIds))
|
||||
querySql := r.selectSql + " where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(infoIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.NeInfo{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
func (r *NeInfoImpl) CheckUniqueNeTypeAndNeId(neInfo model.NeInfo) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if neInfo.NeType != "" {
|
||||
conditions = append(conditions, "ne_type = ?")
|
||||
params = append(params, neInfo.NeType)
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
conditions = append(conditions, "ne_id = ?")
|
||||
params = append(params, neInfo.NeId)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select id as 'str' from ne_info " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprint(results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeInfoImpl) Insert(neInfo model.NeInfo) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neInfo.NeType != "" {
|
||||
params["ne_type"] = neInfo.NeType
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
params["ne_id"] = neInfo.NeId
|
||||
}
|
||||
if neInfo.RmUID != "" {
|
||||
params["rm_uid"] = neInfo.RmUID
|
||||
}
|
||||
if neInfo.NeName != "" {
|
||||
params["ne_name"] = neInfo.NeName
|
||||
}
|
||||
if neInfo.IP != "" {
|
||||
params["ip"] = neInfo.IP
|
||||
}
|
||||
if neInfo.Port > 0 {
|
||||
params["port"] = neInfo.Port
|
||||
}
|
||||
if neInfo.PvFlag != "" {
|
||||
params["pv_flag"] = neInfo.PvFlag
|
||||
}
|
||||
if neInfo.Province != "" {
|
||||
params["province"] = neInfo.Province
|
||||
}
|
||||
if neInfo.VendorName != "" {
|
||||
params["vendor_name"] = neInfo.VendorName
|
||||
}
|
||||
if neInfo.Dn != "" {
|
||||
params["dn"] = neInfo.VendorName
|
||||
}
|
||||
if neInfo.NeAddress != "" {
|
||||
params["ne_address"] = neInfo.NeAddress
|
||||
}
|
||||
params["status"] = neInfo.Status
|
||||
params["update_time"] = time.Now()
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into ne_info (" + 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 *NeInfoImpl) Update(neInfo model.NeInfo) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if neInfo.NeType != "" {
|
||||
params["ne_type"] = neInfo.NeType
|
||||
}
|
||||
if neInfo.NeId != "" {
|
||||
params["ne_id"] = neInfo.NeId
|
||||
}
|
||||
if neInfo.RmUID != "" {
|
||||
params["rm_uid"] = neInfo.RmUID
|
||||
}
|
||||
if neInfo.NeName != "" {
|
||||
params["ne_name"] = neInfo.NeName
|
||||
}
|
||||
if neInfo.IP != "" {
|
||||
params["ip"] = neInfo.IP
|
||||
}
|
||||
if neInfo.Port > 0 {
|
||||
params["port"] = neInfo.Port
|
||||
}
|
||||
if neInfo.PvFlag != "" {
|
||||
params["pv_flag"] = neInfo.PvFlag
|
||||
}
|
||||
params["province"] = neInfo.Province
|
||||
params["vendor_name"] = neInfo.VendorName
|
||||
params["dn"] = neInfo.VendorName
|
||||
params["ne_address"] = neInfo.NeAddress
|
||||
params["status"] = neInfo.Status
|
||||
params["update_time"] = time.Now()
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update ne_info set " + strings.Join(keys, ",") + " where id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, neInfo.ID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除网元信息
|
||||
func (r *NeInfoImpl) DeleteByIds(infoIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(infoIds))
|
||||
sql := "delete from ne_info where id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(infoIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -13,6 +13,28 @@ type INeInfo interface {
|
||||
// ClearNeCacheByNeType 清除网元类型缓存
|
||||
ClearNeCacheByNeType(neType string) bool
|
||||
|
||||
// SelectNeList 查询网元列表
|
||||
SelectNeList(ne model.NeInfo, bandStatus bool) []model.NeInfo
|
||||
// SelectPage 根据条件分页查询
|
||||
//
|
||||
// bandStatus 带状态信息
|
||||
SelectPage(query map[string]any, bandStatus bool) map[string]any
|
||||
|
||||
// SelectList 查询列表
|
||||
//
|
||||
// bandStatus 带状态信息
|
||||
SelectList(ne model.NeInfo, bandStatus bool) []model.NeInfo
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
SelectById(infoId string) model.NeInfo
|
||||
|
||||
// Insert 新增信息
|
||||
Insert(neInfo model.NeInfo) string
|
||||
|
||||
// Update 修改信息
|
||||
Update(neInfo model.NeInfo) int64
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
DeleteByIds(infoIds []string) (int64, error)
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
CheckUniqueNeTypeAndNeId(neType, neId, infoId string) bool
|
||||
}
|
||||
|
||||
@@ -69,9 +69,38 @@ func (r *NeInfoImpl) ClearNeCacheByNeType(neType string) bool {
|
||||
return delOk
|
||||
}
|
||||
|
||||
// SelectNeList 查询网元列表
|
||||
func (r *NeInfoImpl) SelectNeList(ne model.NeInfo, bandStatus bool) []model.NeInfo {
|
||||
list := r.neInfoRepository.SelectNeList(ne)
|
||||
// SelectPage 根据条件分页查询
|
||||
//
|
||||
// bandStatus 带状态信息
|
||||
func (r *NeInfoImpl) SelectPage(query map[string]any, bandStatus bool) map[string]any {
|
||||
data := r.neInfoRepository.SelectPage(query)
|
||||
|
||||
// 网元直连读取网元服务状态
|
||||
if bandStatus {
|
||||
rows := data["rows"].([]model.NeInfo)
|
||||
arr := &rows
|
||||
for i := range *arr {
|
||||
v := (*arr)[i]
|
||||
result, err := NeState(v)
|
||||
if err != nil {
|
||||
(*arr)[i].ServerState = map[string]any{
|
||||
"online": false,
|
||||
}
|
||||
continue
|
||||
}
|
||||
result["online"] = true
|
||||
(*arr)[i].ServerState = result
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
// SelectList 查询列表
|
||||
//
|
||||
// bandStatus 带状态信息
|
||||
func (r *NeInfoImpl) SelectList(ne model.NeInfo, bandStatus bool) []model.NeInfo {
|
||||
list := r.neInfoRepository.SelectList(ne)
|
||||
|
||||
// 网元直连读取网元服务状态
|
||||
if bandStatus {
|
||||
@@ -92,3 +121,53 @@ func (r *NeInfoImpl) SelectNeList(ne model.NeInfo, bandStatus bool) []model.NeIn
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
// SelectByIds 通过ID查询
|
||||
func (r *NeInfoImpl) SelectById(infoId string) model.NeInfo {
|
||||
if infoId == "" {
|
||||
return model.NeInfo{}
|
||||
}
|
||||
neInfos := r.neInfoRepository.SelectByIds([]string{infoId})
|
||||
if len(neInfos) > 0 {
|
||||
return neInfos[0]
|
||||
}
|
||||
return model.NeInfo{}
|
||||
}
|
||||
|
||||
// Insert 新增信息
|
||||
func (r *NeInfoImpl) Insert(neInfo model.NeInfo) string {
|
||||
return r.neInfoRepository.Insert(neInfo)
|
||||
}
|
||||
|
||||
// Update 修改信息
|
||||
func (r *NeInfoImpl) Update(neInfo model.NeInfo) int64 {
|
||||
return r.neInfoRepository.Update(neInfo)
|
||||
}
|
||||
|
||||
// DeleteByIds 批量删除信息
|
||||
func (r *NeInfoImpl) DeleteByIds(infoIds []string) (int64, error) {
|
||||
// 检查是否存在
|
||||
ids := r.neInfoRepository.SelectByIds(infoIds)
|
||||
if len(ids) <= 0 {
|
||||
return 0, fmt.Errorf("neHostCmd.noData")
|
||||
}
|
||||
|
||||
if len(ids) == len(infoIds) {
|
||||
rows := r.neInfoRepository.DeleteByIds(infoIds)
|
||||
return rows, nil
|
||||
}
|
||||
// 删除信息失败!
|
||||
return 0, fmt.Errorf("delete fail")
|
||||
}
|
||||
|
||||
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
||||
func (r *NeInfoImpl) CheckUniqueNeTypeAndNeId(neType, neId, infoId string) bool {
|
||||
uniqueId := r.neInfoRepository.CheckUniqueNeTypeAndNeId(model.NeInfo{
|
||||
NeType: neType,
|
||||
NeId: neId,
|
||||
})
|
||||
if uniqueId == infoId {
|
||||
return true
|
||||
}
|
||||
return uniqueId == ""
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user