feat: 新增告警导出接口
This commit is contained in:
@@ -2,11 +2,16 @@ package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"be.ems/src/framework/constants"
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/date"
|
||||
"be.ems/src/framework/utils/file"
|
||||
"be.ems/src/modules/network_data/model"
|
||||
"be.ems/src/modules/network_data/repository"
|
||||
sysService "be.ems/src/modules/system/service"
|
||||
)
|
||||
|
||||
// 实例化数据层 Alarm 结构体
|
||||
@@ -114,3 +119,83 @@ func (r Alarm) AlarmAckByIds(ids []int64, ackUser string, ackState bool) (int64,
|
||||
// 清除失败!
|
||||
return 0, fmt.Errorf("ack fail")
|
||||
}
|
||||
|
||||
// ExportXlsx 导出数据到 xlsx 文件
|
||||
func (r Alarm) ExportXlsx(rows []model.Alarm, fileName, language, alarmStatus string) (string, error) {
|
||||
// 第一行表头标题
|
||||
headerCells := map[string]string{
|
||||
"A1": i18n.TKey(language, "alarm.export.alarmType"),
|
||||
"B1": i18n.TKey(language, "alarm.export.origSeverity"),
|
||||
"C1": i18n.TKey(language, "alarm.export.alarmTitle"),
|
||||
"D1": i18n.TKey(language, "alarm.export.eventTime"),
|
||||
"E1": i18n.TKey(language, "alarm.export.alarmId"),
|
||||
"F1": i18n.TKey(language, "alarm.export.alarmCode"),
|
||||
"G1": i18n.TKey(language, "ne.common.neType"),
|
||||
"H1": i18n.TKey(language, "ne.common.neName"),
|
||||
"I1": i18n.TKey(language, "ne.common.neId"),
|
||||
}
|
||||
if alarmStatus == "0" {
|
||||
headerCells["J1"] = i18n.TKey(language, "alarm.export.clearUser")
|
||||
headerCells["K1"] = i18n.TKey(language, "alarm.export.clearTime")
|
||||
headerCells["L1"] = i18n.TKey(language, "alarm.export.clearType")
|
||||
}
|
||||
// 读取字典数据 告警原始严重程度
|
||||
dictActiveAlarmType := sysService.NewSysDictData.FindByType("active_alarm_type")
|
||||
// 读取字典数据 告警类型
|
||||
dictActiveClearType := sysService.NewSysDictData.FindByType("active_clear_type")
|
||||
// 读取字典数据 告警确认类型
|
||||
dictActiveAlarmSeverity := sysService.NewSysDictData.FindByType("active_alarm_severity")
|
||||
// 从第二行开始的数据
|
||||
dataCells := make([]map[string]any, 0)
|
||||
for i, row := range rows {
|
||||
idx := strconv.Itoa(i + 2)
|
||||
|
||||
// 原始严重程度
|
||||
origSeverity := "-"
|
||||
for _, v := range dictActiveAlarmSeverity {
|
||||
if row.OrigSeverity == v.DataValue {
|
||||
origSeverity = i18n.TKey(language, v.DataLabel)
|
||||
break
|
||||
}
|
||||
}
|
||||
// 活动告警类型
|
||||
alarmType := "-"
|
||||
for _, v := range dictActiveAlarmType {
|
||||
if row.AlarmType == v.DataValue {
|
||||
alarmType = i18n.TKey(language, v.DataLabel)
|
||||
break
|
||||
}
|
||||
}
|
||||
// 告警清除类型
|
||||
clearType := "-"
|
||||
for _, v := range dictActiveClearType {
|
||||
if fmt.Sprint(row.ClearType) == v.DataValue {
|
||||
clearType = i18n.TKey(language, v.DataLabel)
|
||||
break
|
||||
}
|
||||
}
|
||||
eventTimeStr := date.ParseDateToStr(row.EventTime, date.YYYY_MM_DDTHH_MM_SSZ)
|
||||
|
||||
cells := map[string]any{
|
||||
"A" + idx: alarmType,
|
||||
"B" + idx: origSeverity,
|
||||
"C" + idx: row.AlarmTitle,
|
||||
"D" + idx: eventTimeStr,
|
||||
"E" + idx: row.AlarmId,
|
||||
"F" + idx: row.AlarmCode,
|
||||
"G" + idx: row.NeType,
|
||||
"H" + idx: row.NeName,
|
||||
"I" + idx: row.NeId,
|
||||
}
|
||||
if alarmStatus == "0" {
|
||||
clearTimeStr := date.ParseDateToStr(row.ClearTime, date.YYYY_MM_DDTHH_MM_SSZ)
|
||||
cells["J"+idx] = row.ClearUser
|
||||
cells["K"+idx] = clearType
|
||||
cells["L"+idx] = clearTimeStr
|
||||
}
|
||||
dataCells = append(dataCells, cells)
|
||||
}
|
||||
|
||||
// 导出数据表格
|
||||
return file.WriteSheet(headerCells, dataCells, fileName, "")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user