- Updated error response codes for various validation errors from 400 to 422 to better reflect the nature of the errors. - Changed error messages for empty parameters (e.g., userId, menuId, roleId) to use a consistent error code format. - Improved error handling in the IPerf, Ping, and WS controllers to provide more informative error messages. - Ensured that all controllers return appropriate error messages when binding JSON or query parameters fails.
279 lines
8.0 KiB
Go
279 lines
8.0 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
|
||
"be.ems/src/framework/i18n"
|
||
"be.ems/src/framework/reqctx"
|
||
"be.ems/src/framework/resp"
|
||
"be.ems/src/framework/utils/parse"
|
||
"be.ems/src/modules/network_data/model"
|
||
neDataService "be.ems/src/modules/network_data/service"
|
||
neFetchlink "be.ems/src/modules/network_element/fetch_link"
|
||
neService "be.ems/src/modules/network_element/service"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
// 实例化控制层 SMFController 结构体
|
||
var NewSMF = &SMFController{
|
||
neInfoService: neService.NewNeInfo,
|
||
cdrEventService: neDataService.NewCDREventSMF,
|
||
UDMExtendService: neDataService.NewUDMExtend,
|
||
}
|
||
|
||
// 网元SMF
|
||
//
|
||
// PATH /smf
|
||
type SMFController struct {
|
||
neInfoService *neService.NeInfo // 网元信息服务
|
||
cdrEventService *neDataService.CDREventSMF // CDR会话事件服务
|
||
UDMExtendService *neDataService.UDMExtend // UDM用户信息服务
|
||
}
|
||
|
||
// CDR会话列表
|
||
//
|
||
// GET /cdr/list
|
||
//
|
||
// @Tags network_data/smf
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param neType query string true "NE Type only SMF" Enums(SMF)
|
||
// @Param neId query string true "NE ID" default(001)
|
||
// @Param subscriberID query string false "subscriberID is IMSI"
|
||
// @Param pageNum query number true "pageNum" default(1)
|
||
// @Param pageSize query number true "pageSize" default(10)
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary CDR Session List
|
||
// @Description CDR Session List
|
||
// @Router /neData/smf/cdr/list [get]
|
||
func (s *SMFController) CDRList(c *gin.Context) {
|
||
language := reqctx.AcceptLanguage(c)
|
||
var querys model.CDREventSMFQuery
|
||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||
c.JSON(422, resp.CodeMsg(422001, errMsgs))
|
||
return
|
||
}
|
||
|
||
// 查询网元信息 rmUID
|
||
neInfo := s.neInfoService.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||
return
|
||
}
|
||
querys.RmUID = neInfo.RmUID
|
||
|
||
// 查询数据
|
||
rows, total := s.cdrEventService.FindByPage(querys)
|
||
c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
|
||
}
|
||
|
||
// CDR会话删除
|
||
//
|
||
// DELETE /cdr/:id
|
||
//
|
||
// @Tags network_data/smf
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param id path string true "list data id, multiple separated by a , sign"
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary CDR Session Delete
|
||
// @Description CDR Session Delete
|
||
// @Router /neData/smf/cdr/{id} [delete]
|
||
func (s *SMFController) CDRRemove(c *gin.Context) {
|
||
language := reqctx.AcceptLanguage(c)
|
||
id := c.Param("id")
|
||
if id == "" {
|
||
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
|
||
return
|
||
}
|
||
|
||
// 处理字符转id数组后去重
|
||
uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
|
||
// 转换成int64数组类型
|
||
ids := make([]int64, 0)
|
||
for _, v := range uniqueIDs {
|
||
ids = append(ids, parse.Number(v))
|
||
}
|
||
|
||
rows, err := s.cdrEventService.DeleteByIds(ids)
|
||
if err != nil {
|
||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
|
||
return
|
||
}
|
||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||
c.JSON(200, resp.OkMsg(msg))
|
||
}
|
||
|
||
// CDR会话列表导出
|
||
//
|
||
// POST /cdr/export
|
||
//
|
||
// @Tags network_data/smf
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param data body object true "Request Param"
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary CDR Session List Export
|
||
// @Description CDR Session List Export
|
||
// @Router /neData/smf/cdr/export [post]
|
||
func (s *SMFController) CDRExport(c *gin.Context) {
|
||
language := reqctx.AcceptLanguage(c)
|
||
// 查询结果,根据查询条件结果,单页最大值限制
|
||
var querys model.CDREventSMFQuery
|
||
if err := c.ShouldBindBodyWithJSON(&querys); err != nil {
|
||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||
c.JSON(422, resp.CodeMsg(422001, errMsgs))
|
||
return
|
||
}
|
||
// 限制导出数据集
|
||
if querys.PageSize > 10000 {
|
||
querys.PageSize = 10000
|
||
}
|
||
// 查询网元信息 rmUID
|
||
neInfo := s.neInfoService.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
|
||
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
|
||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||
return
|
||
}
|
||
querys.RmUID = neInfo.RmUID
|
||
rows, total := s.cdrEventService.FindByPage(querys)
|
||
if total == 0 {
|
||
// 导出数据记录为空
|
||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||
return
|
||
}
|
||
|
||
// 导出文件名称
|
||
fileName := fmt.Sprintf("smf_cdr_event_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||
// 导出数据表格
|
||
saveFilePath, err := s.cdrEventService.ExportXlsx(rows, fileName)
|
||
if err != nil {
|
||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
|
||
c.FileAttachment(saveFilePath, fileName)
|
||
}
|
||
|
||
// 在线订阅用户数量
|
||
//
|
||
// GET /sub/num
|
||
//
|
||
// @Tags network_data/smf
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param neId query string true "NE ID" default(001)
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary Number of online session users
|
||
// @Description Number of online session users
|
||
// @Router /neData/smf/sub/num [get]
|
||
func (s *SMFController) SubUserNum(c *gin.Context) {
|
||
language := reqctx.AcceptLanguage(c)
|
||
var query struct {
|
||
NeId string `form:"neId" binding:"required"`
|
||
}
|
||
if err := c.ShouldBindQuery(&query); err != nil {
|
||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||
c.JSON(422, resp.CodeMsg(422001, errMsgs))
|
||
return
|
||
}
|
||
|
||
// 查询网元信息
|
||
neInfo := s.neInfoService.FindByNeTypeAndNeID("SMF", query.NeId)
|
||
if neInfo.NeId != query.NeId || neInfo.IP == "" {
|
||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||
return
|
||
}
|
||
|
||
// 网元直连
|
||
num, err := neFetchlink.SMFSubNum(neInfo)
|
||
if err != nil {
|
||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
|
||
c.JSON(200, resp.OkData(num))
|
||
}
|
||
|
||
// 在线订阅用户列表信息
|
||
//
|
||
// GET /sub/list
|
||
//
|
||
// @Tags network_data/smf
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param neId query string true "NE ID" default(001)
|
||
// @Param imsi query string false "imsi"
|
||
// @Param msisdn query string false "msisdn"
|
||
// @Param upstate query string false "upstate"
|
||
// @Param pageNum query string true "pageNum" default(50)
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary Online session user list information
|
||
// @Description Online session user list information
|
||
// @Router /neData/smf/session/list [get]
|
||
func (s *SMFController) SubUserList(c *gin.Context) {
|
||
language := reqctx.AcceptLanguage(c)
|
||
var query struct {
|
||
NeId string `form:"neId" binding:"required"`
|
||
IMSI string `form:"imsi"`
|
||
MSISDN string `form:"msisdn"`
|
||
Upstate string `form:"upstate"`
|
||
PageNum string `form:"pageNum"`
|
||
}
|
||
if err := c.ShouldBindQuery(&query); err != nil {
|
||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||
c.JSON(422, resp.CodeMsg(422001, errMsgs))
|
||
return
|
||
}
|
||
|
||
// 查询网元信息
|
||
neInfo := s.neInfoService.FindByNeTypeAndNeID("SMF", query.NeId)
|
||
if neInfo.NeId != query.NeId || neInfo.IP == "" {
|
||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
|
||
return
|
||
}
|
||
|
||
// 网元直连
|
||
data, err := neFetchlink.SMFSubInfoList(neInfo, map[string]string{
|
||
"imsi": query.IMSI,
|
||
"msisdn": query.MSISDN,
|
||
"upstate": query.Upstate,
|
||
"pageNum": query.PageNum,
|
||
})
|
||
if err != nil {
|
||
c.JSON(200, resp.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
|
||
// 对数据进行处理,去掉前缀,并加入imsi拓展信息
|
||
rows := data["rows"].([]any)
|
||
if len(rows) > 0 {
|
||
arr := &rows
|
||
for i := range *arr {
|
||
item := (*arr)[i].(map[string]any)
|
||
if v, ok := item["imsi"]; ok && v != nil {
|
||
imsiStr := v.(string)
|
||
imsiStr = strings.TrimPrefix(imsiStr, "imsi-")
|
||
item["imsi"] = imsiStr
|
||
// 查UDM拓展信息
|
||
info := s.UDMExtendService.FindByIMSIAndNeID(imsiStr, "%")
|
||
item["remark"] = info.Remark
|
||
}
|
||
if v, ok := item["msisdn"]; ok && v != nil {
|
||
item["msisdn"] = strings.TrimPrefix(v.(string), "msisdn-")
|
||
}
|
||
}
|
||
}
|
||
|
||
c.JSON(200, resp.OkData(data))
|
||
}
|