abnormal-alarm.tsx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. "use client"
  2. import {useEffect, useState} from "react"
  3. import {
  4. Alert,
  5. Button,
  6. Card,
  7. Col,
  8. Descriptions,
  9. Form,
  10. Input,
  11. Modal,
  12. Row,
  13. Select,
  14. Space,
  15. Table,
  16. Tag,
  17. Timeline,
  18. } from "antd"
  19. import {BellOutlined, CheckCircleOutlined, EditOutlined, EyeOutlined} from "@ant-design/icons"
  20. import {Line} from "@ant-design/plots"
  21. import globalMessage from "@/app/_modules/globalMessage";
  22. const { Option } = Select
  23. const { TextArea } = Input
  24. // 模拟报警数据
  25. const mockAlarmData = [
  26. {
  27. id: "A001",
  28. title: "解放路段压力异常报警",
  29. type: "压力异常",
  30. level: "高",
  31. location: "解放路调压站",
  32. deviceId: "D001",
  33. deviceName: "压力监测器001",
  34. value: 0.52,
  35. threshold: 0.45,
  36. unit: "MPa",
  37. status: "未处理",
  38. createTime: "2024-01-15 14:30:25",
  39. updateTime: "2024-01-15 14:30:25",
  40. handler: null,
  41. handleTime: null,
  42. description: "管网压力超出安全阈值,可能存在安全隐患",
  43. solution: null,
  44. },
  45. {
  46. id: "A002",
  47. title: "人民路段流量异常报警",
  48. type: "流量异常",
  49. level: "中",
  50. location: "人民路段",
  51. deviceId: "D002",
  52. deviceName: "流量监测器002",
  53. value: 4200,
  54. threshold: 3500,
  55. unit: "m³/h",
  56. status: "处理中",
  57. createTime: "2024-01-15 13:45:10",
  58. updateTime: "2024-01-15 14:15:30",
  59. handler: "张三",
  60. handleTime: "2024-01-15 14:15:30",
  61. description: "流量超出正常范围,需要检查管网状态",
  62. solution: "正在检查管网连接状态,预计30分钟内完成",
  63. },
  64. {
  65. id: "A003",
  66. title: "建设路段泄漏报警",
  67. type: "泄漏检测",
  68. level: "高",
  69. location: "建设路北段",
  70. deviceId: "D004",
  71. deviceName: "泄漏检测器004",
  72. value: 0.08,
  73. threshold: 0.05,
  74. unit: "ppm",
  75. status: "已处理",
  76. createTime: "2024-01-15 12:20:15",
  77. updateTime: "2024-01-15 13:45:20",
  78. handler: "李四",
  79. handleTime: "2024-01-15 12:25:15",
  80. description: "检测到燃气泄漏,浓度超标",
  81. solution: "已完成管线检修,泄漏点已修复,监测数据恢复正常",
  82. },
  83. ]
  84. // 模拟报警统计数据
  85. const mockAlarmStats = [
  86. { time: "00:00", count: 2 },
  87. { time: "04:00", count: 1 },
  88. { time: "08:00", count: 3 },
  89. { time: "12:00", count: 5 },
  90. { time: "16:00", count: 2 },
  91. { time: "20:00", count: 1 },
  92. ]
  93. export default function AbnormalAlarm() {
  94. const [isModalVisible, setIsModalVisible] = useState(false)
  95. const [isDetailModalVisible, setIsDetailModalVisible] = useState(false)
  96. const [selectedAlarm, setSelectedAlarm] = useState(null)
  97. const [form] = Form.useForm()
  98. const [alarmData, setAlarmData] = useState(mockAlarmData)
  99. // 模拟实时报警更新
  100. useEffect(() => {
  101. const interval = setInterval(() => {
  102. // 模拟新报警
  103. if (Math.random() < 0.1) {
  104. const newAlarm = {
  105. id: `A${Date.now()}`,
  106. title: "新报警事件",
  107. type: "压力异常",
  108. level: "中",
  109. location: "测试路段",
  110. deviceId: "D999",
  111. deviceName: "测试设备",
  112. value: 0.48,
  113. threshold: 0.45,
  114. unit: "MPa",
  115. status: "未处理",
  116. createTime: new Date().toLocaleString(),
  117. updateTime: new Date().toLocaleString(),
  118. handler: null,
  119. handleTime: null,
  120. description: "模拟报警事件",
  121. solution: null,
  122. }
  123. setAlarmData((prev) => [newAlarm, ...prev])
  124. globalMessage.warning("收到新报警!")
  125. }
  126. }, 30000) // 30秒检查一次
  127. return () => clearInterval(interval)
  128. }, [])
  129. const handleView = (record: any) => {
  130. setSelectedAlarm(record)
  131. setIsDetailModalVisible(true)
  132. }
  133. const handleProcess = (record: any) => {
  134. setSelectedAlarm(record)
  135. form.setFieldsValue({
  136. handler: "当前用户",
  137. solution: "",
  138. })
  139. setIsModalVisible(true)
  140. }
  141. const handleModalOk = () => {
  142. form.validateFields().then((values) => {
  143. const updatedData = alarmData.map((item) =>
  144. item.id === selectedAlarm?.id
  145. ? {
  146. ...item,
  147. status: "处理中",
  148. handler: values.handler,
  149. handleTime: new Date().toLocaleString(),
  150. updateTime: new Date().toLocaleString(),
  151. solution: values.solution,
  152. }
  153. : item,
  154. )
  155. setAlarmData(updatedData)
  156. globalMessage.success("报警处理成功")
  157. setIsModalVisible(false)
  158. form.resetFields()
  159. })
  160. }
  161. const handleComplete = (id: string) => {
  162. const updatedData = alarmData.map((item) =>
  163. item.id === id
  164. ? {
  165. ...item,
  166. status: "已处理",
  167. updateTime: new Date().toLocaleString(),
  168. }
  169. : item,
  170. )
  171. setAlarmData(updatedData)
  172. globalMessage.success("报警已标记为完成")
  173. }
  174. const getStatusColor = (status: string) => {
  175. switch (status) {
  176. case "未处理":
  177. return "red"
  178. case "处理中":
  179. return "orange"
  180. case "已处理":
  181. return "green"
  182. default:
  183. return "default"
  184. }
  185. }
  186. const getLevelColor = (level: string) => {
  187. switch (level) {
  188. case "高":
  189. return "red"
  190. case "中":
  191. return "orange"
  192. case "低":
  193. return "blue"
  194. default:
  195. return "default"
  196. }
  197. }
  198. const columns = [
  199. { title: "报警编号", dataIndex: "id", key: "id", width: 100 },
  200. { title: "报警标题", dataIndex: "title", key: "title", width: 200 },
  201. { title: "报警类型", dataIndex: "type", key: "type", width: 120 },
  202. {
  203. title: "报警等级",
  204. dataIndex: "level",
  205. key: "level",
  206. width: 100,
  207. render: (level: string) => <Tag color={getLevelColor(level)}>{level}</Tag>,
  208. },
  209. { title: "位置", dataIndex: "location", key: "location", width: 150 },
  210. { title: "设备名称", dataIndex: "deviceName", key: "deviceName", width: 150 },
  211. {
  212. title: "异常值",
  213. key: "value",
  214. width: 120,
  215. render: (_, record: any) => `${record.value} ${record.unit}`,
  216. },
  217. {
  218. title: "阈值",
  219. key: "threshold",
  220. width: 120,
  221. render: (_, record: any) => `${record.threshold} ${record.unit}`,
  222. },
  223. {
  224. title: "状态",
  225. dataIndex: "status",
  226. key: "status",
  227. width: 100,
  228. render: (status: string) => <Tag color={getStatusColor(status)}>{status}</Tag>,
  229. },
  230. { title: "创建时间", dataIndex: "createTime", key: "createTime", width: 180 },
  231. { title: "处理人", dataIndex: "handler", key: "handler", width: 100 },
  232. {
  233. title: "操作",
  234. key: "action",
  235. width: 200,
  236. fixed: "right",
  237. render: (_, record) => (
  238. <Space>
  239. <Button type="link" icon={<EyeOutlined />} onClick={() => handleView(record)}>
  240. 查看
  241. </Button>
  242. {record.status === "未处理" && (
  243. <Button type="link" icon={<EditOutlined />} onClick={() => handleProcess(record)}>
  244. 处理
  245. </Button>
  246. )}
  247. {record.status === "处理中" && (
  248. <Button type="link" onClick={() => handleComplete(record.id)}>
  249. 完成
  250. </Button>
  251. )}
  252. </Space>
  253. ),
  254. },
  255. ]
  256. const lineConfig = {
  257. data: mockAlarmStats,
  258. xField: "time",
  259. yField: "count",
  260. color: "#f5222d",
  261. point: {
  262. size: 5,
  263. shape: "diamond",
  264. },
  265. area: {
  266. style: {
  267. fill: "l(270) 0:#ffffff 0.5:#ffccc7 1:#f5222d",
  268. fillOpacity: 0.3,
  269. },
  270. },
  271. }
  272. const unhandledCount = alarmData.filter((item) => item.status === "未处理").length
  273. const processingCount = alarmData.filter((item) => item.status === "处理中").length
  274. const handledCount = alarmData.filter((item) => item.status === "已处理").length
  275. return (
  276. <div className="p-6">
  277. <Card title="异常报警管理">
  278. {/* 报警概览 */}
  279. <Row gutter={16} className="mb-6">
  280. <Col span={6}>
  281. <Card size="small">
  282. <div className="text-center">
  283. <div className="text-2xl font-bold text-red-600">{unhandledCount}</div>
  284. <div className="text-gray-600">未处理报警</div>
  285. </div>
  286. </Card>
  287. </Col>
  288. <Col span={6}>
  289. <Card size="small">
  290. <div className="text-center">
  291. <div className="text-2xl font-bold text-orange-600">{processingCount}</div>
  292. <div className="text-gray-600">处理中</div>
  293. </div>
  294. </Card>
  295. </Col>
  296. <Col span={6}>
  297. <Card size="small">
  298. <div className="text-center">
  299. <div className="text-2xl font-bold text-green-600">{handledCount}</div>
  300. <div className="text-gray-600">已处理</div>
  301. </div>
  302. </Card>
  303. </Col>
  304. <Col span={6}>
  305. <Card size="small">
  306. <div className="text-center">
  307. <div className="text-2xl font-bold text-blue-600">{alarmData.length}</div>
  308. <div className="text-gray-600">报警总数</div>
  309. </div>
  310. </Card>
  311. </Col>
  312. </Row>
  313. {/* 紧急报警提醒 */}
  314. {unhandledCount > 0 && (
  315. <Alert
  316. message={`当前有 ${unhandledCount} 个未处理报警`}
  317. description="请及时处理高等级报警,确保系统安全运行"
  318. type="error"
  319. icon={<BellOutlined />}
  320. showIcon
  321. className="mb-6"
  322. />
  323. )}
  324. {/* 报警趋势图 */}
  325. <Row gutter={16} className="mb-6">
  326. <Col span={24}>
  327. <Card title="24小时报警趋势" size="small">
  328. <Line {...lineConfig} height={200} />
  329. </Card>
  330. </Col>
  331. </Row>
  332. {/* 筛选控件 */}
  333. <div className="mb-4">
  334. <Space>
  335. <Select placeholder="报警类型" style={{ width: 120 }}>
  336. <Option value="pressure">压力异常</Option>
  337. <Option value="flow">流量异常</Option>
  338. <Option value="temperature">温度异常</Option>
  339. <Option value="leak">泄漏检测</Option>
  340. </Select>
  341. <Select placeholder="报警等级" style={{ width: 120 }}>
  342. <Option value="high">高</Option>
  343. <Option value="medium">中</Option>
  344. <Option value="low">低</Option>
  345. </Select>
  346. <Select placeholder="处理状态" style={{ width: 120 }}>
  347. <Option value="unhandled">未处理</Option>
  348. <Option value="processing">处理中</Option>
  349. <Option value="handled">已处理</Option>
  350. </Select>
  351. </Space>
  352. </div>
  353. {/* 报警列表 */}
  354. <Table
  355. columns={columns}
  356. dataSource={alarmData}
  357. rowKey="id"
  358. pagination={{ pageSize: 10 }}
  359. scroll={{ x: 1600 }}
  360. rowClassName={(record) => (record.status === "未处理" && record.level === "高" ? "bg-red-50" : "")}
  361. />
  362. {/* 处理模态框 */}
  363. <Modal
  364. title="处理报警"
  365. open={isModalVisible}
  366. onOk={handleModalOk}
  367. onCancel={() => setIsModalVisible(false)}
  368. width={600}
  369. >
  370. <Form form={form} layout="vertical">
  371. <Form.Item name="handler" label="处理人" rules={[{ required: true }]}>
  372. <Input />
  373. </Form.Item>
  374. <Form.Item name="solution" label="处理方案" rules={[{ required: true }]}>
  375. <TextArea rows={4} placeholder="请输入处理方案..." />
  376. </Form.Item>
  377. </Form>
  378. </Modal>
  379. {/* 详情查看模态框 */}
  380. <Modal
  381. title="报警详情"
  382. open={isDetailModalVisible}
  383. onCancel={() => setIsDetailModalVisible(false)}
  384. footer={[
  385. <Button key="close" onClick={() => setIsDetailModalVisible(false)}>
  386. 关闭
  387. </Button>,
  388. ]}
  389. width={800}
  390. >
  391. {selectedAlarm && (
  392. <div>
  393. <Descriptions column={2} bordered>
  394. <Descriptions.Item label="报警编号">{selectedAlarm.id}</Descriptions.Item>
  395. <Descriptions.Item label="报警标题">{selectedAlarm.title}</Descriptions.Item>
  396. <Descriptions.Item label="报警类型">{selectedAlarm.type}</Descriptions.Item>
  397. <Descriptions.Item label="报警等级">
  398. <Tag color={getLevelColor(selectedAlarm.level)}>{selectedAlarm.level}</Tag>
  399. </Descriptions.Item>
  400. <Descriptions.Item label="位置">{selectedAlarm.location}</Descriptions.Item>
  401. <Descriptions.Item label="设备名称">{selectedAlarm.deviceName}</Descriptions.Item>
  402. <Descriptions.Item label="异常值">
  403. {selectedAlarm.value} {selectedAlarm.unit}
  404. </Descriptions.Item>
  405. <Descriptions.Item label="阈值">
  406. {selectedAlarm.threshold} {selectedAlarm.unit}
  407. </Descriptions.Item>
  408. <Descriptions.Item label="状态">
  409. <Tag color={getStatusColor(selectedAlarm.status)}>{selectedAlarm.status}</Tag>
  410. </Descriptions.Item>
  411. <Descriptions.Item label="创建时间">{selectedAlarm.createTime}</Descriptions.Item>
  412. <Descriptions.Item label="处理人">{selectedAlarm.handler || "未分配"}</Descriptions.Item>
  413. <Descriptions.Item label="处理时间">{selectedAlarm.handleTime || "未处理"}</Descriptions.Item>
  414. </Descriptions>
  415. <div className="mt-4">
  416. <h4 className="font-medium mb-2">报警描述</h4>
  417. <div className="p-3 bg-gray-50 rounded">{selectedAlarm.description}</div>
  418. </div>
  419. {selectedAlarm.solution && (
  420. <div className="mt-4">
  421. <h4 className="font-medium mb-2">处理方案</h4>
  422. <div className="p-3 bg-blue-50 rounded">{selectedAlarm.solution}</div>
  423. </div>
  424. )}
  425. <div className="mt-4">
  426. <h4 className="font-medium mb-2">处理时间线</h4>
  427. <Timeline>
  428. <Timeline.Item color="red" dot={<BellOutlined />}>
  429. <div>报警触发</div>
  430. <div className="text-gray-500 text-sm">{selectedAlarm.createTime}</div>
  431. </Timeline.Item>
  432. {selectedAlarm.handleTime && (
  433. <Timeline.Item color="orange" dot={<EditOutlined />}>
  434. <div>开始处理 - {selectedAlarm.handler}</div>
  435. <div className="text-gray-500 text-sm">{selectedAlarm.handleTime}</div>
  436. </Timeline.Item>
  437. )}
  438. {selectedAlarm.status === "已处理" && (
  439. <Timeline.Item color="green" dot={<CheckCircleOutlined />}>
  440. <div>处理完成</div>
  441. <div className="text-gray-500 text-sm">{selectedAlarm.updateTime}</div>
  442. </Timeline.Item>
  443. )}
  444. </Timeline>
  445. </div>
  446. </div>
  447. )}
  448. </Modal>
  449. </Card>
  450. </div>
  451. )
  452. }