import React, { useState, useEffect } from 'react'; import { Card, Table, Tag, Space, Button, Drawer, Descriptions, Form, Select, Pagination, message, DatePicker, Input } from 'antd'; import { EyeOutlined } from '@ant-design/icons'; import dayjs from 'dayjs'; // 接口 import { getAlarmList, getAlarmDetail } from '../../api/alarmApi.js'; import { alarmTypeMap } from '@/constants.js'; import { useTranslation } from 'react-i18next'; import { t } from 'i18next'; const { Option } = Select; const { RangePicker } = DatePicker; const alarmLevelConfig = { 1: { description: t('constants.critical'), color: '#FF4D4F' }, 2: { description: t('constants.important'), color: '#FA8C16' }, 3: { description: t('constants.general'), color: '#FADB14' }, 4: { description: t('constants.info'), color: '#1890FF' } }; const handleStatusMap = { 1: t('constants.pending'), 2: t('alerts.processing'), 3: t('constants.closed'), 4: t('constants.ignored') }; // 自动生成颜色/文字映射 const levelColor = Object.keys(alarmLevelConfig).reduce((map, key) => { map[key] = alarmLevelConfig[key].color; return map; }, {}); const levelText = Object.keys(alarmLevelConfig).reduce((map, key) => { map[key] = alarmLevelConfig[key].description; return map; }, {}); const AlarmHistory = () => { const { t } = useTranslation(); const [form] = Form.useForm(); // 抽屉 const [drawerVisible, setDrawerVisible] = useState(false); const [currentAlarm, setCurrentAlarm] = useState(null); const [detailLoading, setDetailLoading] = useState(false); // 列表 const [loading, setLoading] = useState(false); const [alarmList, setAlarmList] = useState([]); const [total, setTotal] = useState(0); const [pageNum, setPageNum] = useState(1); const [pageSize, setPageSize] = useState(10); // 查询条件 const [queryParams, setQueryParams] = useState({ deviceId: '', alarmLevel: '', handleStatus: '', startTime: '', endTime: '', }); // 获取列表 const fetchList = async () => { setLoading(true); try { const res = await getAlarmList({ pageNum, pageSize, ...queryParams }); if (res.code === 0) { setAlarmList(res.rows || []); setTotal(res.total || 0); } } catch (err) { message.error(t('common.loadFailed')); } finally { setLoading(false); } }; // 查看详情(打开抽屉) const showDetail = async (id) => { setDetailLoading(true); try { const res = await getAlarmDetail(id); if (res.code === 200) { setCurrentAlarm(res.data); setDrawerVisible(true); } else { message.error(t('common.loadDetailFailed')); } } catch (err) { message.error(t('common.requestError')); } finally { setDetailLoading(false); } }; // 搜索 const handleSearch = (values) => { let startTime = ''; let endTime = ''; if (values.timeRange && values.timeRange.length === 2) { startTime = values.timeRange[0].format('YYYY-MM-DD HH:mm:ss'); endTime = values.timeRange[1].format('YYYY-MM-DD HH:mm:ss'); } setQueryParams({ deviceId: values.deviceId || '', alarmLevel: values.alarmLevel || '', alarmType: values.alarmType || '', handleStatus: values.handleStatus || '', startTime, endTime, }); setPageNum(1); }; useEffect(() => { fetchList(); }, [pageNum, pageSize, queryParams]); // 表格列 const columns = [ { title: t('alerts.alarmNo'), dataIndex: 'alarmNo', width: 180 }, { title: t('alerts.alertTime'), dataIndex: 'alarmTime', width: 190, render: (t) => t ? dayjs(t).format('YYYY-MM-DD HH:mm:ss') : '-' }, { title: t('common.deviceId'), dataIndex: 'deviceId', width: 160 }, { title: t('devices.deviceName'), dataIndex: 'deviceName', ellipsis: true }, { title: t('alerts.alertType'), dataIndex: 'alarmType', width: 130, render: (t) => alarmTypeMap[t] || '-' }, { title: t('alerts.alertLevel'), width: 100, render: (r) => ( {levelText[r.alarmLevel]} ) }, { title: t('alerts.alarmTitle'), dataIndex: 'alarmTitle', ellipsis: true, width: 180 }, { title: t('alerts.alarmContent'), dataIndex: 'alarmContent', ellipsis: true }, { title: t('alerts.handleStatus'), width: 110, render: (r) => { const s = r.handleStatus; let color = 'default'; if (s === 1) color = 'orange'; if (s === 2) color = 'blue'; if (s === 3) color = 'green'; if (s === 4) color = 'default'; return {handleStatusMap[s] || '-'}; } }, { title: t('roles.actions'), width: 120, fixed: 'right', render: (_, r) => ( ) } ]; return (
{/* 设备ID */} {/* 时间范围 */}
{ setPageNum(p); setPageSize(s); }} showSizeChanger showQuickJumper showTotal={(total) => `${t('common.total')} ${total} ${t('common.items')}`} />
{/* 右侧详情抽屉 */} setDrawerVisible(false)} width={650} destroyOnClose > {currentAlarm?.alarmNo || '-'} {currentAlarm?.alarmTitle || '-'} {currentAlarm?.alarmContent || '-'} {currentAlarm?.deviceId || '-'} {currentAlarm?.deviceName || '-'} {currentAlarm?.alarmTime ? dayjs(currentAlarm.alarmTime).format('YYYY-MM-DD HH:mm:ss') : '-'} {alarmTypeMap[currentAlarm?.alarmType] || '-'} {levelText[currentAlarm?.alarmLevel]} {(() => { const s = currentAlarm?.handleStatus; let color = 'default'; if (s === 1) color = 'orange'; if (s === 2) color = 'blue'; if (s === 3) color = 'green'; if (s === 4) color = 'default'; return {handleStatusMap[s] || '-'}; })()} {currentAlarm?.handleUserName || '-'} {currentAlarm?.handleTime ? dayjs(currentAlarm.handleTime).format('YYYY-MM-DD HH:mm:ss') : '-'} {currentAlarm?.handleRemark || '-'} {currentAlarm?.rootCause || '-'} {currentAlarm?.handleSuggestion || '-'} {/* 通知订阅模块 */} {currentAlarm && ( {t('alerts.sms')}: {currentAlarm.notifySms === 1 ? t('common.enabled') : t('constants.closed')} {t('alerts.dingTalk')}: {currentAlarm.notifyTelegram === 1 ? t('common.enabled') : t('constants.closed')} {t('alerts.email')}: {currentAlarm.notifyEmail === 1 ? t('common.enabled') : t('constants.closed')} )} ); }; export default AlarmHistory;