Files
be.ems/src/modules/network_data/service/cdr_event_smsc.go
2025-05-09 11:51:21 +08:00

138 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package service
import (
"encoding/json"
"fmt"
"strconv"
"be.ems/src/framework/i18n"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/date"
"be.ems/src/framework/utils/file"
"be.ems/src/framework/utils/parse"
"be.ems/src/modules/network_data/model"
"be.ems/src/modules/network_data/repository"
sysService "be.ems/src/modules/system/service"
)
// 实例化数据层 CDREventSMSC 结构体
var NewCDREventSMSC = &CDREventSMSC{
cdrEventRepository: repository.NewCDREventSMSC,
}
// CDREventSMSC CDR会话事件SMSC 服务层处理
type CDREventSMSC struct {
cdrEventRepository *repository.CDREventSMSC // CDR会话事件数据信息
}
// FindByPage 根据条件分页查询
func (r *CDREventSMSC) FindByPage(query model.CDREventSMSCQuery) ([]model.CDREventSMSC, int64) {
return r.cdrEventRepository.SelectByPage(query)
}
// DeleteByIds 批量删除信息
func (r *CDREventSMSC) DeleteByIds(ids []int64) (int64, error) {
// 检查是否存在
rows := r.cdrEventRepository.SelectByIds(ids)
if len(rows) <= 0 {
return 0, fmt.Errorf("not data")
}
if len(rows) == len(ids) {
rows := r.cdrEventRepository.DeleteByIds(ids)
return rows, nil
}
// 删除信息失败!
return 0, fmt.Errorf("delete fail")
}
// ExportXlsx 导出数据到 xlsx 文件
func (r CDREventSMSC) ExportXlsx(rows []model.CDREventSMSC, fileName, language string) (string, error) {
// 第一行表头标题
headerCells := map[string]string{
"A1": "ID",
"B1": "NE Name",
"C1": "Record Behavior",
"D1": "Service Type",
"E1": "Caller",
"F1": "Called",
"G1": "Result",
"H1": "Time",
}
// 读取字典数据 CDR 原因码
dictCDRCauseCode := sysService.NewSysDictData.FindByType("cdr_cause_code")
// 从第二行开始的数据
dataCells := make([]map[string]any, 0)
for i, row := range rows {
idx := strconv.Itoa(i + 2)
// 解析 JSON 字符串为 map
var cdrJSON map[string]interface{}
err := json.Unmarshal([]byte(row.CdrJson), &cdrJSON)
if err != nil {
logger.Warnf("CDRExport Error parsing JSON: %s", err.Error())
continue
}
// 记录类型
recordType := ""
if v, ok := cdrJSON["recordType"]; ok && v != nil {
recordType = v.(string)
}
// 服务类型
serviceType := ""
if v, ok := cdrJSON["serviceType"]; ok && v != nil {
serviceType = v.(string)
}
// 被叫
called := ""
if v, ok := cdrJSON["calledParty"]; ok && v != nil {
called = v.(string)
}
// 主叫
caller := ""
if v, ok := cdrJSON["callerParty"]; ok && v != nil {
caller = v.(string)
}
// 呼叫结果 0失败1成功
callResult := "Fail"
if v, ok := cdrJSON["result"]; ok && v != nil {
resultVal := parse.Number(v)
if resultVal == 1 {
callResult = "Success"
}
}
// 结果原因
if v, ok := cdrJSON["cause"]; ok && v != nil && callResult == "Fail" {
cause := fmt.Sprint(v)
for _, v := range dictCDRCauseCode {
if cause == v.DataValue {
callResult = fmt.Sprintf("%s, %s", callResult, i18n.TKey(language, v.DataLabel))
break
}
}
}
// 取时间
timeStr := ""
if v, ok := cdrJSON["updateTime"]; ok && v != nil {
if releaseTime := parse.Number(v); releaseTime > 0 {
timeStr = date.ParseDateToStr(releaseTime, date.YYYY_MM_DDTHH_MM_SSZ)
} else {
timeStr = v.(string)
}
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.ID,
"B" + idx: row.NeName,
"C" + idx: recordType,
"D" + idx: serviceType,
"E" + idx: caller,
"F" + idx: called,
"G" + idx: callResult,
"H" + idx: timeStr,
})
}
// 导出数据表格
return file.WriteSheet(headerCells, dataCells, fileName, "")
}