Files
web-Iot/src/components/alerts/AlarmHistory.tsx
2026-07-20 13:56:10 +08:00

337 lines
9.9 KiB
TypeScript
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.

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';
const { Option } = Select;
const { RangePicker } = DatePicker;
const alarmLevelConfig = {
1: { description: '严重', color: '#FF4D4F' },
2: { description: '重要', color: '#FA8C16' },
3: { description: '一般', color: '#FADB14' },
4: { description: '提示', color: '#1890FF' }
};
const handleStatusMap = {
1: '待处理',
2: '处理中',
3: '已关闭',
4: '已忽略'
};
// 自动生成颜色/文字映射
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 [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('加载失败');
} 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('获取详情失败');
}
} catch (err) {
message.error('请求异常');
} 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: '告警编号', dataIndex: 'alarmNo', width: 180 },
{
title: '告警时间',
dataIndex: 'alarmTime',
width: 190,
render: (t) => t ? dayjs(t).format('YYYY-MM-DD HH:mm:ss') : '-'
},
{ title: '设备ID', dataIndex: 'deviceId', width: 160 },
{ title: '设备名称', dataIndex: 'deviceName', ellipsis: true },
{
title: '告警类型',
dataIndex: 'alarmType',
width: 130,
render: (t) => alarmTypeMap[t] || '-'
},
{
title: '告警级别',
width: 100,
render: (r) => (
<Tag color={levelColor[r.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
{levelText[r.alarmLevel]}
</Tag>
)
},
{ title: '告警标题', dataIndex: 'alarmTitle', ellipsis: true, width: 180 },
{ title: '告警内容', dataIndex: 'alarmContent', ellipsis: true },
{
title: '处理状态',
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 <Tag color={color}>{handleStatusMap[s] || '-'}</Tag>;
}
},
{
title: '操作',
width: 120,
fixed: 'right',
render: (_, r) => (
<Button type="link" icon={<EyeOutlined />} onClick={() => showDetail(r.id)}>
查看详情
</Button>
)
}
];
return (
<div style={{ padding: 24, background: '#f5f7fa', minHeight: '100vh' }}>
<Card title="告警历史" style={{ borderRadius: 8 }}>
<Form
form={form}
layout="inline"
onFinish={handleSearch}
style={{ marginBottom: 16, flexWrap: 'wrap', gap: 16 }}
>
{/* 设备ID */}
<Form.Item name="deviceId" label="设备ID">
<Input placeholder="请输入设备ID" style={{ width: 160 }} allowClear />
</Form.Item>
<Form.Item name="alarmLevel" label="告警级别">
<Select placeholder="全部" style={{ width: 130 }} allowClear>
<Option value={1}>严重</Option>
<Option value={2}>重要</Option>
<Option value={3}>一般</Option>
<Option value={4}>提示</Option>
</Select>
</Form.Item>
<Form.Item name="alarmType" label="告警类型">
<Select placeholder="全部" style={{ width: 150 }} allowClear>
<Option value="UAV_FAULT">无人机故障</Option>
<Option value="MOWER_FAULT">割草机故障</Option>
<Option value="OTHER_DEVICE_FAULT">其他设备故障</Option>
<Option value="SERVER_FAILURE">服务器故障</Option>
<Option value="SYSTEM_ERROR">系统错误</Option>
</Select>
</Form.Item>
<Form.Item name="handleStatus" label="处理状态">
<Select placeholder="全部" style={{ width: 130 }} allowClear>
<Option value={1}>待处理</Option>
<Option value={2}>处理中</Option>
<Option value={3}>已关闭</Option>
<Option value={4}>已忽略</Option>
</Select>
</Form.Item>
{/* 时间范围 */}
<Form.Item name="timeRange" label="时间范围">
<RangePicker
showTime
format="YYYY-MM-DD HH:mm:ss"
style={{ width: 350 }}
/>
</Form.Item>
<Form.Item>
<Space>
<Button type="primary" htmlType="submit">查询</Button>
<Button htmlType="reset">重置</Button>
</Space>
</Form.Item>
</Form>
<Table
rowKey="id"
columns={columns}
dataSource={alarmList}
loading={loading}
pagination={false}
scroll={{ x: 'max-content' }}
/>
<div style={{ textAlign: 'center', marginTop: 16 }}>
<Pagination
current={pageNum}
pageSize={pageSize}
total={total}
onChange={(p, s) => { setPageNum(p); setPageSize(s); }}
showSizeChanger
showQuickJumper
showTotal={(total) => `共 ${total} 条`}
/>
</div>
</Card>
{/* 右侧详情抽屉 */}
<Drawer
title="告警详情"
open={drawerVisible}
onClose={() => setDrawerVisible(false)}
width={650}
destroyOnClose
>
<Descriptions column={1} bordered size="small" loading={detailLoading}>
<Descriptions.Item label="告警编号">{currentAlarm?.alarmNo || '-'}</Descriptions.Item>
<Descriptions.Item label="告警标题">{currentAlarm?.alarmTitle || '-'}</Descriptions.Item>
<Descriptions.Item label="告警内容">{currentAlarm?.alarmContent || '-'}</Descriptions.Item>
<Descriptions.Item label="设备ID">{currentAlarm?.deviceId || '-'}</Descriptions.Item>
<Descriptions.Item label="设备名称">{currentAlarm?.deviceName || '-'}</Descriptions.Item>
<Descriptions.Item label="告警时间">
{currentAlarm?.alarmTime ? dayjs(currentAlarm.alarmTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
</Descriptions.Item>
<Descriptions.Item label="告警类型">{alarmTypeMap[currentAlarm?.alarmType] || '-'}</Descriptions.Item>
<Descriptions.Item label="告警级别">
<Tag color={levelColor[currentAlarm?.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
{levelText[currentAlarm?.alarmLevel]}
</Tag>
</Descriptions.Item>
<Descriptions.Item label="处理状态">
{(() => {
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 <Tag color={color}>{handleStatusMap[s] || '-'}</Tag>;
})()}
</Descriptions.Item>
<Descriptions.Item label="处理人">{currentAlarm?.handleUserName || '-'}</Descriptions.Item>
<Descriptions.Item label="处理时间">
{currentAlarm?.handleTime ? dayjs(currentAlarm.handleTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
</Descriptions.Item>
<Descriptions.Item label="处理备注">{currentAlarm?.handleRemark || '-'}</Descriptions.Item>
<Descriptions.Item label="根因分析">{currentAlarm?.rootCause || '-'}</Descriptions.Item>
<Descriptions.Item label="推荐处理措施">
{currentAlarm?.handleSuggestion || '-'}
</Descriptions.Item>
</Descriptions>
{/* 通知订阅模块 */}
{currentAlarm && (
<Card size="small" title="通知订阅" style={{ marginTop: 16 }}>
<Space style={{ width: '100%' }}>
<Space>
<span>短信:</span>
<Tag color={currentAlarm.notifySms === 1 ? 'green' : 'default'}>
{currentAlarm.notifySms === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
<Space>
<span>钉钉:</span>
<Tag color={currentAlarm.notifyTelegram === 1 ? 'green' : 'default'}>
{currentAlarm.notifyTelegram === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
<Space>
<span>邮件:</span>
<Tag color={currentAlarm.notifyEmail === 1 ? 'green' : 'default'}>
{currentAlarm.notifyEmail === 1 ? '已开启' : '已关闭'}
</Tag>
</Space>
</Space>
</Card>
)}
</Drawer>
</div>
);
};
export default AlarmHistory;