<!-- Description: 报警管理-历史报警 Author: 李亚光 Date: 2023-06-28 --> <script lang="ts" setup name="HistoryAlarm"> import { ElLoading, ElMessage } from 'element-plus' import dayjs from 'dayjs' import { getDictByCode } from '@/api/system/dict' import { exportHistoryAlarm, getHistoryAlarmListPage } from '@/api/home/alarm/history' import { getAlarmLevelListPage, getAlarmTypeListPage } from '@/api/home/rule/alarm' import { getDeviceTypeListPage } from '@/api/home/device/type' import { uniqueMultiArray } from '@/utils/Array' import { shortcuts } from '@/utils/common' import { exportFile } from '@/utils/exportUtils' import { getDeviceListPage } from '@/api/home/device/device' import { keepSearchParams } from '@/utils/keepQuery' import { getDateDiff } from '@/utils/dayjs' import { alarmValue } from '@/views/home/alarm/current/components/dict' const $route = useRoute() const alarmCategoryList = ref<{ id: string; name: string; value: string }[]>([]) // 报警类别 const alarmTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 报警类型 const alarmLevelList = ref<{ id: string; name: string; value: string }[]>([]) // 报警等级 const deviceTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 设备类型 const realAlarmList = ref<{ id: string; name: string; value: string }[]>([ { name: '是', id: '0', value: '0', }, { name: '否', id: '1', value: '1', }, ]) // 报警状态 // 表格数据 const list = ref<any[]>([]) const total = ref(0) // 初始展示列 const columns = ref<any>([ { text: '报警类别', value: 'alarmCategory', align: 'center', width: '110' }, { text: '报警类型', value: 'alarmType', align: 'center', width: '140' }, { text: '报警原因', value: 'alarmReason', align: 'center' }, { text: '报警等级', value: 'alarmLevelName', align: 'center', width: '90' }, { text: '位置', value: 'position', align: 'center' }, { text: '设备编号', value: 'devcode', align: 'center', width: '130', isCustom: true }, { text: '管理单位', value: 'deptName', align: 'center' }, { text: '是否误报', value: 'realAlarmName', align: 'center', width: '90' }, { text: '报警时间', value: 'alarmTime', align: 'center' }, { text: '解除时间', value: 'cancelTime', align: 'center' }, { text: '解除时长', value: 'cancelDuration', align: 'center' }, ]) // 最终展示列 const columnsConfig = ref([]) // 修改列 const editColumns = (data: any) => { columnsConfig.value = data } const loadingTable = ref(true) // 查询条件 const listQuery = ref({ limit: 20, offset: 1, alarmCategory: '', alarmLevel: '', alarmTypeId: '', begTime: '', deptId: '', devCode: '', devTypeId: '', endTime: '', ledgerNumber: '', manufactureId: '', position: '', processStatus: '', realAlarm: '', }) // 开始结束时间 const datetimerange = ref() watch(() => datetimerange.value, (newVal) => { listQuery.value.begTime = '' listQuery.value.endTime = '' if (Array.isArray(newVal)) { if (newVal.length) { listQuery.value.begTime = `${newVal[0]}` listQuery.value.endTime = `${newVal[1]}` } } }) // 查询数据 const fetchData = () => { loadingTable.value = true getHistoryAlarmListPage(listQuery.value).then((res) => { console.log(res.data.rows, '查询数据') list.value = res.data.rows.map((item: any) => ({ ...item, cancelDuration: item.alarmTime && item.cancelTime ? getDateDiff(item.alarmTime, item.cancelTime) : '' , // showDeviceTips: false, // 展示设备编号提示 })) .map((item: any) => ({ ...item, alarmReason: item.alarmCategory.includes('浓度') ? `${item.alarmValue}${!item.alarmValue ? '' : item.watchObject === '2' ? 'PPM.M' : '%LEL'}` : (alarmValue[item.alarmValue] || '其他') })) total.value = res.data.total loadingTable.value = false // 查询设备类型和厂商 // list.value.forEach((item: any) => { // getDeviceListPage({ offset: 1, limit: 1, devCode: item.devcode }).then(res => { // if (res.data.rows.length) { // item.showDeviceTips = true // item.deviceTips = { // typeName: res.data.rows[0].deviceName || item.devTypeName, // manufactureName: res.data.rows[0].manufactureName // } // } // else { // item.showDeviceTips = false // item.deviceTips = {} // } // }) // }) }).catch(() => { loadingTable.value = false }) } // 重置查询条件 const reset = () => { datetimerange.value = [] listQuery.value = { limit: 20, offset: 1, alarmCategory: '', alarmLevel: '', alarmTypeId: '', begTime: '', deptId: '', devCode: '', devTypeId: '', endTime: '', ledgerNumber: '', manufactureId: '', position: '', processStatus: '', realAlarm: '', } fetchData() } // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写 const changePage = (val: { size: number; page: number }) => { if (val && val.size) { listQuery.value.limit = val.size } if (val && val.page) { listQuery.value.offset = val.page } fetchData() } // 详情 const $router = useRouter() const moreData = (row: any) => { $router.push({ name: 'AlarmHistoryDetail', query: { id: row.id, row: JSON.stringify(row), type: 'history', }, }) } // 跳转设备详情 const toDeviceDetail = (row: any) => { if (!row.devcode || !row.devTypeName) { ElMessage.warning('缺少设备关键信息') return } $router.push({ name: 'DeviceManageDetail', params: { type: 'detail', }, query: { row: JSON.stringify({ devcode: row.devcode, deviceType: row.devTypeName, deviceTypeName: row.devTypeName, devTypeName: row.devTypeName, }), }, }) } // 导出列表 const exportList = () => { if (!list.value.length) { ElMessage.warning('暂无导出数据') return } const loading = ElLoading.service({ lock: true, background: 'rgba(255, 255, 255, 0.8)', }) exportHistoryAlarm(listQuery.value).then((res) => { exportFile(res.data, '历史报警列表(数据).xlsx') loading.close() }).catch(() => { loading.close() }) } onMounted(() => { if ($route.query.row) { const data = JSON.parse($route.query.row as string) if ($route.query.type === 'localAnalyse') { // 从异常位置分析跳转过来 datetimerange.value = [data.begTime, data.endTime] listQuery.value.position = data.position } } else { datetimerange.value = [dayjs().subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss'), dayjs().format('YYYY-MM-DD HH:mm:ss')] } setTimeout(() => { fetchData() }) }) // 字典 const fetchDict = () => { // 报警类别 getDictByCode('alarmCategory').then((res) => { alarmCategoryList.value = res.data.filter((item: any) => !item.name.includes('设备')) }) // 报警类型 getAlarmTypeListPage({ offset: 1, limit: 99999 }).then((res) => { alarmTypeList.value = uniqueMultiArray(res.data.rows.filter((item: any) => item.enabled === '1').map((item: any) => ({ name: item.alarmType, value: item.id, id: item.id, })), 'name').filter((item: any) => !item.name.includes('设备')) }) // 报警等级 getAlarmLevelListPage({ offset: 1, limit: 99999 }).then((res) => { alarmLevelList.value = res.data.rows.map((item: any) => ({ name: item.alarmLevel, value: item.id, id: item.id, })) }) // 设备类型 getDeviceTypeListPage({ offset: 1, limit: 99999 }).then((res) => { deviceTypeList.value = res.data.rows.map((item: any) => ({ name: item.typeName, value: item.id, id: item.id, })) }) } fetchDict() const { proxy } = getCurrentInstance() as any onBeforeRouteLeave((to: any) => { keepSearchParams(to.path, 'HistoryAlarm') }) onActivated(() => { // 从编辑或者新增页面回来需要重新获取列表数据 // $router.options.history.state.back 上一次路由地址 if (!($router.options.history.state.back as string || '').includes('detail')) { console.log('需要重新获取列表') fetchData() } }) </script> <template> <!-- 布局 --> <app-container> <!-- 筛选条件 --> <search-area :need-clear="true" @search="fetchData" @clear="reset"> <search-item> <el-select v-model="listQuery.alarmCategory" placeholder="报警类别" clearable class="select" style="width: 192px;"> <el-option v-for="item in alarmCategoryList" :key="item.id" :value="item.value" :label="item.name" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.alarmTypeId" placeholder="报警类型" clearable class="select" style="width: 192px;"> <el-option v-for="item in alarmTypeList" :key="item.id" :value="item.value" :label="item.name" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.alarmLevel" placeholder="报警等级" clearable class="select" style="width: 192px;"> <el-option v-for="item in alarmLevelList" :key="item.id" :value="item.value" :label="item.name" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.devTypeId" placeholder="设备类型" clearable class="select" style="width: 192px;"> <el-option v-for="item in deviceTypeList" :key="item.id" :value="item.value" :label="item.name" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.realAlarm" placeholder="是否误报" clearable class="select" style="width: 192px;"> <el-option v-for="item in realAlarmList" :key="item.id" :value="item.value" :label="item.name" /> </el-select> </search-item> <search-item> <el-input v-model.trim="listQuery.position" placeholder="位置" clearable /> </search-item> <search-item> <el-input v-model.trim="listQuery.devCode" placeholder="设备编号" clearable /> </search-item> <search-item> <dept-select v-model="listQuery.deptId" placeholder="管理单位" :clearable="true" class="select" style="width: 192px;" /> </search-item> <search-item> <el-date-picker v-model="datetimerange" type="datetimerange" format="YYYY-MM-DD HH:mm:ss" :shortcuts="shortcuts" value-format="YYYY-MM-DD HH:mm:ss" range-separator="至" start-placeholder="报警开始时间" end-placeholder="报警结束时间" clearable /> </search-item> </search-area> <!-- 表头标题 --> <table-container :is-config="true" config-title="history-alarm" :columns="columns" :config-columns="columnsConfig" :edit="editColumns"> <template #btns-right> <!-- 操作 --> <div> <el-button v-if="proxy.hasPerm('/alarm/history/export')" type="primary" @click="exportList"> 导出 </el-button> </div> </template> <!-- 查询结果Table显示 --> <normal-table :data="list" :total="total" :columns="columnsConfig" :query="listQuery" :list-loading="loadingTable" @change="changePage"> <template #preColumns> <!-- <el-table-column label="选择" width="55" align="center"> <template #default="scope"> <el-radio v-model="select" :label="scope.$index + 1" class="radio" /> </template> </el-table-column> --> <el-table-column label="序号" width="55" align="center"> <template #default="scope"> {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }} </template> </el-table-column> </template> <template #isCustom="{ scope, column }"> <!-- 设备编号 --> <span v-if="column.text === '设备编号'" class="pointer link" @click="toDeviceDetail(scope.row)"> <el-tooltip v-if="scope.row.manufactureName" class="box-item" effect="dark" :content="`${scope.row?.devTypeName}(${scope.row?.manufactureName})`" placement="top"> {{ scope.row[column.value] }} </el-tooltip> <template v-else> {{ scope.row[column.value] }} </template> </span> </template> <template #columns> <el-table-column v-if="proxy.hasPerm('/alarm/history/detail')" label="操作" align="center" width="80"> <template #default="scope"> <el-button type="primary" link size="small" @click="moreData(scope.row)"> 查看 </el-button> </template> </el-table-column> </template> </normal-table> </table-container> </app-container> </template> <style lang="scss" scoped> .pointer { &:hover { cursor: pointer; } } .link { color: #0d76d4 !important; &:hover { text-decoration: underline !important; } } </style>