338 lines
11 KiB
TypeScript
338 lines
11 KiB
TypeScript
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) => (
|
||
<Tag color={levelColor[r.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
|
||
{levelText[r.alarmLevel]}
|
||
</Tag>
|
||
)
|
||
},
|
||
{ 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 <Tag color={color}>{handleStatusMap[s] || '-'}</Tag>;
|
||
}
|
||
},
|
||
{
|
||
title: t('roles.actions'),
|
||
width: 120,
|
||
fixed: 'right',
|
||
render: (_, r) => (
|
||
<Button type="link" icon={<EyeOutlined />} onClick={() => showDetail(r.id)}>
|
||
{t('common.viewDetail')}
|
||
</Button>
|
||
)
|
||
}
|
||
];
|
||
|
||
return (
|
||
<div style={{ padding: 24, background: '#f5f7fa', minHeight: '100vh' }}>
|
||
<Card title={t('alerts.alarmHistory')} style={{ borderRadius: 8 }}>
|
||
<Form
|
||
form={form}
|
||
layout="inline"
|
||
onFinish={handleSearch}
|
||
style={{ marginBottom: 16, flexWrap: 'wrap', gap: 16 }}
|
||
>
|
||
{/* 设备ID */}
|
||
<Form.Item name="deviceId" label={t('common.deviceId')}>
|
||
<Input placeholder={t('common.pleaseInputDeviceId')} style={{ width: 160 }} allowClear />
|
||
</Form.Item>
|
||
|
||
<Form.Item name="alarmLevel" label={t('alerts.alertLevel')}>
|
||
<Select placeholder={t('common.all')} style={{ width: 130 }} allowClear>
|
||
<Option value={1}>{t('constants.critical')}</Option>
|
||
<Option value={2}>{t('constants.important')}</Option>
|
||
<Option value={3}>{t('constants.general')}</Option>
|
||
<Option value={4}>{t('constants.info')}</Option>
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
<Form.Item name="alarmType" label={t('alerts.alertType')}>
|
||
<Select placeholder={t('common.all')} style={{ width: 150 }} allowClear>
|
||
<Option value="UAV_FAULT">{t('constants.uavFault')}</Option>
|
||
<Option value="MOWER_FAULT">{t('constants.mowerFault')}</Option>
|
||
<Option value="OTHER_DEVICE_FAULT">{t('constants.otherDeviceFault')}</Option>
|
||
<Option value="SERVER_FAILURE">{t('constants.serverFailure')}</Option>
|
||
<Option value="SYSTEM_ERROR">{t('constants.systemError')}</Option>
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
<Form.Item name="handleStatus" label={t('alerts.handleStatus')}>
|
||
<Select placeholder={t('common.all')} style={{ width: 130 }} allowClear>
|
||
<Option value={1}>{t('constants.pending')}</Option>
|
||
<Option value={2}>{t('constants.closed')}</Option>
|
||
</Select>
|
||
</Form.Item>
|
||
|
||
{/* 时间范围 */}
|
||
<Form.Item name="timeRange" label={t('common.timeRange')}>
|
||
<RangePicker
|
||
showTime
|
||
format="YYYY-MM-DD HH:mm:ss"
|
||
style={{ width: 350 }}
|
||
/>
|
||
</Form.Item>
|
||
|
||
<Form.Item>
|
||
<Space>
|
||
<Button type="primary" htmlType="submit">{t('common.search2')}</Button>
|
||
<Button htmlType="reset">{t('common.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) => `${t('common.total')} ${total} ${t('common.items')}`}
|
||
/>
|
||
</div>
|
||
</Card>
|
||
|
||
{/* 右侧详情抽屉 */}
|
||
<Drawer
|
||
title={t('alerts.alertDetail')}
|
||
open={drawerVisible}
|
||
onClose={() => setDrawerVisible(false)}
|
||
width={650}
|
||
destroyOnClose
|
||
>
|
||
<Descriptions column={1} bordered size="small" loading={detailLoading}>
|
||
<Descriptions.Item label={t('alerts.alarmNo')}>{currentAlarm?.alarmNo || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.alarmTitle')}>{currentAlarm?.alarmTitle || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.alarmContent')}>{currentAlarm?.alarmContent || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('common.deviceId')}>{currentAlarm?.deviceId || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('devices.deviceName')}>{currentAlarm?.deviceName || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.alertTime')}>
|
||
{currentAlarm?.alarmTime ? dayjs(currentAlarm.alarmTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.alertType')}>{alarmTypeMap[currentAlarm?.alarmType] || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.alertLevel')}>
|
||
<Tag color={levelColor[currentAlarm?.alarmLevel]} style={{ border: 'none', color: '#fff' }}>
|
||
{levelText[currentAlarm?.alarmLevel]}
|
||
</Tag>
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.handleStatus')}>
|
||
{(() => {
|
||
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={t('alerts.handler')}>{currentAlarm?.handleUserName || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.handleTime')}>
|
||
{currentAlarm?.handleTime ? dayjs(currentAlarm.handleTime).format('YYYY-MM-DD HH:mm:ss') : '-'}
|
||
</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.handleRemark')}>{currentAlarm?.handleRemark || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.rootCause')}>{currentAlarm?.rootCause || '-'}</Descriptions.Item>
|
||
<Descriptions.Item label={t('alerts.recommendedAction')}>
|
||
{currentAlarm?.handleSuggestion || '-'}
|
||
</Descriptions.Item>
|
||
</Descriptions>
|
||
|
||
{/* 通知订阅模块 */}
|
||
{currentAlarm && (
|
||
<Card size="small" title={t('alerts.notifySubscription')} style={{ marginTop: 16 }}>
|
||
<Space style={{ width: '100%' }}>
|
||
<Space>
|
||
<span>{t('alerts.sms')}:</span>
|
||
<Tag color={currentAlarm.notifySms === 1 ? 'green' : 'default'}>
|
||
{currentAlarm.notifySms === 1 ? t('common.enabled') : t('constants.closed')}
|
||
</Tag>
|
||
</Space>
|
||
<Space>
|
||
<span>{t('alerts.dingTalk')}:</span>
|
||
<Tag color={currentAlarm.notifyTelegram === 1 ? 'green' : 'default'}>
|
||
{currentAlarm.notifyTelegram === 1 ? t('common.enabled') : t('constants.closed')}
|
||
</Tag>
|
||
</Space>
|
||
<Space>
|
||
<span>{t('alerts.email')}:</span>
|
||
<Tag color={currentAlarm.notifyEmail === 1 ? 'green' : 'default'}>
|
||
{currentAlarm.notifyEmail === 1 ? t('common.enabled') : t('constants.closed')}
|
||
</Tag>
|
||
</Space>
|
||
</Space>
|
||
</Card>
|
||
)}
|
||
</Drawer>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default AlarmHistory;
|