index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import SearchComponent from '@/components/SearchComponent';
  3. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  4. import { PermissionButton } from '@/components';
  5. import {
  6. CloseCircleOutlined,
  7. DeleteOutlined,
  8. EditOutlined,
  9. LikeOutlined,
  10. PlayCircleOutlined,
  11. PlusOutlined,
  12. } from '@ant-design/icons';
  13. import { useIntl } from '@@/plugin-locale/localeExports';
  14. import { useEffect, useRef, useState } from 'react';
  15. import { Badge, Space, Tooltip } from 'antd';
  16. import ProTableCard from '@/components/ProTableCard';
  17. import Save from './Save';
  18. import Service from '@/pages/rule-engine/Alarm/Configuration/service';
  19. import AlarmConfig from '@/components/ProTableCard/CardItems/AlarmConfig';
  20. import { Store } from 'jetlinks-store';
  21. import { getMenuPathByCode, MENUS_CODE } from '@/utils/menu';
  22. import { useHistory } from 'umi';
  23. import { onlyMessage } from '@/utils/util';
  24. const service = new Service('alarm/config');
  25. const Configuration = () => {
  26. const intl = useIntl();
  27. const history = useHistory();
  28. const [visible, setVisible] = useState<boolean>(false);
  29. const actionRef = useRef<ActionType>();
  30. const { permission } = PermissionButton.usePermission('rule-engine/Alarm/Configuration');
  31. const [current, setCurrent] = useState<any>();
  32. const columns: ProColumns<ConfigurationItem>[] = [
  33. {
  34. dataIndex: 'name',
  35. title: '名称',
  36. ellipsis: true,
  37. fixed: 'left',
  38. },
  39. {
  40. title: '类型',
  41. dataIndex: 'targetType',
  42. renderText: (text: string) => {
  43. const map = {
  44. product: '产品',
  45. device: '设备',
  46. org: '部门',
  47. other: '其他',
  48. };
  49. return map[text];
  50. },
  51. valueType: 'select',
  52. valueEnum: {
  53. product: {
  54. text: '产品',
  55. status: 'product',
  56. },
  57. device: {
  58. text: '设备',
  59. status: 'device',
  60. },
  61. org: {
  62. text: '部门',
  63. status: 'org',
  64. },
  65. other: {
  66. text: '其他',
  67. status: 'other',
  68. },
  69. },
  70. },
  71. {
  72. title: '告警级别',
  73. dataIndex: 'level',
  74. render: (text: any) => (
  75. <Tooltip
  76. placement="topLeft"
  77. title={
  78. (Store.get('default-level') || []).find((item: any) => item?.level === text)?.title ||
  79. text
  80. }
  81. >
  82. <div className="ellipsis">
  83. {(Store.get('default-level') || []).find((item: any) => item?.level === text)?.title ||
  84. text}
  85. </div>
  86. </Tooltip>
  87. ),
  88. valueType: 'select',
  89. request: async () => {
  90. const res = await service.queryDefaultLevel();
  91. if (res.status === 200) {
  92. return (res?.result?.levels || []).map((item: any) => ({
  93. label: item.title,
  94. value: item.level,
  95. }));
  96. }
  97. return [];
  98. },
  99. },
  100. {
  101. title: '关联场景联动',
  102. dataIndex: 'sceneName',
  103. width: 250,
  104. render: (text: any, record: any) => (
  105. <PermissionButton
  106. type={'link'}
  107. isPermission={!!getMenuPathByCode(MENUS_CODE['rule-engine/Scene'])}
  108. style={{ padding: 0, height: 'auto' }}
  109. tooltip={{
  110. title: !!getMenuPathByCode(MENUS_CODE['rule-engine/Scene'])
  111. ? text
  112. : '没有权限,请联系管理员',
  113. }}
  114. onClick={() => {
  115. const url = getMenuPathByCode('rule-engine/Scene/Save');
  116. history.push(`${url}?id=${record.sceneId}`);
  117. }}
  118. >
  119. <span
  120. style={{
  121. width: '200px',
  122. overflow: 'hidden',
  123. textAlign: 'left',
  124. textOverflow: 'ellipsis',
  125. }}
  126. >
  127. {text}
  128. </span>
  129. </PermissionButton>
  130. ),
  131. valueType: 'select',
  132. request: async () => {
  133. const res = await service.getScene({});
  134. if (res.status === 200) {
  135. return res.result.map((item: any) => ({ label: item.name, value: item.name }));
  136. }
  137. return [];
  138. },
  139. },
  140. {
  141. title: '状态',
  142. dataIndex: 'state',
  143. valueType: 'select',
  144. renderText: (state) => (
  145. <Badge text={state?.text} status={state?.value === 'disabled' ? 'error' : 'success'} />
  146. ),
  147. valueEnum: {
  148. disabled: {
  149. text: intl.formatMessage({
  150. id: 'pages.device.product.status.disabled',
  151. defaultMessage: '禁用',
  152. }),
  153. status: 'disabled',
  154. },
  155. enabled: {
  156. text: intl.formatMessage({
  157. id: 'pages.device.product.status.enabled',
  158. defaultMessage: '正常',
  159. }),
  160. status: 'enabled',
  161. },
  162. },
  163. },
  164. {
  165. title: '说明',
  166. dataIndex: 'description',
  167. ellipsis: true,
  168. },
  169. {
  170. title: '操作',
  171. valueType: 'option',
  172. align: 'left',
  173. fixed: 'right',
  174. width: 150,
  175. render: (_, record) => [
  176. record.sceneTriggerType === 'manual' && (
  177. <PermissionButton
  178. key="trigger"
  179. type="link"
  180. style={{ padding: 0 }}
  181. isPermission={permission.tigger}
  182. tooltip={{
  183. title: record.state?.value === 'disabled' ? '未启用,不能手动触发' : '手动触发',
  184. }}
  185. disabled={record.state?.value === 'disabled'}
  186. popConfirm={{
  187. disabled: record.state?.value === 'disabled',
  188. title: '确认手动触发?',
  189. onConfirm: async () => {
  190. await service._execute(record.sceneId);
  191. onlyMessage(
  192. intl.formatMessage({
  193. id: 'pages.data.option.success',
  194. defaultMessage: '操作成功!',
  195. }),
  196. );
  197. actionRef.current?.reload();
  198. },
  199. }}
  200. >
  201. <LikeOutlined />
  202. </PermissionButton>
  203. ),
  204. <PermissionButton
  205. isPermission={permission.update}
  206. key="edit"
  207. style={{ padding: 0 }}
  208. tooltip={{
  209. title: intl.formatMessage({
  210. id: 'pages.data.option.edit',
  211. defaultMessage: '编辑',
  212. }),
  213. }}
  214. type="link"
  215. onClick={() => {
  216. setVisible(true);
  217. setCurrent(record);
  218. }}
  219. >
  220. <EditOutlined />
  221. </PermissionButton>,
  222. <PermissionButton
  223. isPermission={permission.action}
  224. key="action"
  225. style={{ padding: 0 }}
  226. popConfirm={{
  227. title: `${
  228. record.state?.value !== 'disabled'
  229. ? '禁用告警不会影响关联的场景状态,确定要禁用吗'
  230. : '确认启用'
  231. }?`,
  232. onConfirm: async () => {
  233. if (record.state?.value === 'disabled') {
  234. await service._enable(record.id);
  235. } else {
  236. await service._disable(record.id);
  237. }
  238. onlyMessage(
  239. intl.formatMessage({
  240. id: 'pages.data.option.success',
  241. defaultMessage: '操作成功!',
  242. }),
  243. );
  244. actionRef.current?.reload();
  245. },
  246. }}
  247. tooltip={{
  248. title: intl.formatMessage({
  249. id: `pages.data.option.${
  250. record.state?.value !== 'disabled' ? 'disabled' : 'enabled'
  251. }`,
  252. defaultMessage: record.state?.value !== 'disabled' ? '禁用' : '启用',
  253. }),
  254. }}
  255. type="link"
  256. >
  257. {record.state?.value === 'disabled' ? <PlayCircleOutlined /> : <CloseCircleOutlined />}
  258. </PermissionButton>,
  259. <PermissionButton
  260. type="link"
  261. isPermission={permission.delete}
  262. key="delete"
  263. disabled={record.state?.value !== 'disabled'}
  264. style={{ padding: 0 }}
  265. popConfirm={{
  266. disabled: record.state?.value !== 'disabled',
  267. title: '确认删除?',
  268. onConfirm: () => service.remove(record.id),
  269. }}
  270. tooltip={{
  271. title: record.state?.value === 'disabled' ? '删除' : '已启用,不能删除',
  272. }}
  273. >
  274. <DeleteOutlined />
  275. </PermissionButton>,
  276. ],
  277. },
  278. ];
  279. const [param, setParam] = useState({});
  280. useEffect(() => {
  281. service.queryDefaultLevel().then((resp) => {
  282. if (resp.status === 200) {
  283. Store.set('default-level', resp.result?.levels || []);
  284. }
  285. });
  286. }, []);
  287. return (
  288. <PageContainer>
  289. <SearchComponent
  290. field={columns}
  291. onSearch={(data) => {
  292. actionRef.current?.reset?.();
  293. setParam(data);
  294. }}
  295. />
  296. <ProTableCard<any>
  297. actionRef={actionRef}
  298. rowKey="id"
  299. search={false}
  300. scroll={{ x: 1366 }}
  301. params={param}
  302. columns={columns}
  303. columnEmptyText={''}
  304. request={(params) =>
  305. service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  306. }
  307. gridColumn={3}
  308. cardRender={(record) => (
  309. <AlarmConfig
  310. {...record}
  311. actions={[
  312. record.sceneTriggerType === 'manual' ? (
  313. <PermissionButton
  314. key="trigger"
  315. type="link"
  316. tooltip={{
  317. title: record.state?.value === 'disabled' ? '未启用,不能手动触发' : '',
  318. }}
  319. disabled={record.state?.value === 'disabled'}
  320. isPermission={permission.tigger}
  321. popConfirm={{
  322. title: '确认手动触发?',
  323. disabled: record.state?.value === 'disabled',
  324. onConfirm: async () => {
  325. await service._execute(record.sceneId);
  326. onlyMessage(
  327. intl.formatMessage({
  328. id: 'pages.data.option.success',
  329. defaultMessage: '操作成功!',
  330. }),
  331. );
  332. actionRef.current?.reload();
  333. },
  334. }}
  335. >
  336. <LikeOutlined />
  337. 手动触发
  338. </PermissionButton>
  339. ) : null,
  340. <PermissionButton
  341. isPermission={permission.update}
  342. key="edit"
  343. type="link"
  344. onClick={() => {
  345. setCurrent(record);
  346. setVisible(true);
  347. }}
  348. >
  349. <EditOutlined />
  350. 编辑
  351. </PermissionButton>,
  352. <PermissionButton
  353. isPermission={permission.action}
  354. style={{ padding: 0 }}
  355. popConfirm={{
  356. title: `${
  357. record.state?.value !== 'disabled'
  358. ? '禁用告警不会影响关联的场景状态,确定要禁用吗'
  359. : '确认启用'
  360. }?`,
  361. onConfirm: async () => {
  362. if (record.state?.value === 'disabled') {
  363. await service._enable(record.id);
  364. } else {
  365. await service._disable(record.id);
  366. }
  367. onlyMessage(
  368. intl.formatMessage({
  369. id: 'pages.data.option.success',
  370. defaultMessage: '操作成功!',
  371. }),
  372. );
  373. actionRef.current?.reload();
  374. },
  375. }}
  376. tooltip={{
  377. title: intl.formatMessage({
  378. id: `pages.data.option.${
  379. record.state?.value !== 'disabled' ? 'disabled' : 'enabled'
  380. }`,
  381. defaultMessage: record.state?.value !== 'disabled' ? '禁用' : '启用',
  382. }),
  383. }}
  384. key="action"
  385. type="link"
  386. >
  387. {record.state?.value !== 'disabled' ? (
  388. <CloseCircleOutlined />
  389. ) : (
  390. <PlayCircleOutlined />
  391. )}
  392. {record.state?.value !== 'disabled' ? '禁用' : '启用'}
  393. </PermissionButton>,
  394. <PermissionButton
  395. type="link"
  396. tooltip={{
  397. title: record.state?.value === 'disabled' ? '删除' : '已启用,不能删除',
  398. }}
  399. disabled={record.state?.value !== 'disabled'}
  400. popConfirm={{
  401. disabled: record.state?.value !== 'disabled',
  402. title: '确认删除?',
  403. onConfirm: async () => {
  404. await service.remove(record.id);
  405. actionRef.current?.reset?.();
  406. },
  407. }}
  408. isPermission={permission.delete}
  409. key="delete"
  410. >
  411. <DeleteOutlined />
  412. </PermissionButton>,
  413. ]}
  414. />
  415. )}
  416. headerTitle={
  417. <Space>
  418. <PermissionButton
  419. isPermission={permission.add}
  420. onClick={() => {
  421. setCurrent(undefined);
  422. setVisible(true);
  423. }}
  424. key="button"
  425. icon={<PlusOutlined />}
  426. type="primary"
  427. >
  428. {intl.formatMessage({
  429. id: 'pages.data.option.add',
  430. defaultMessage: '新增',
  431. })}
  432. </PermissionButton>
  433. </Space>
  434. }
  435. />
  436. <Save
  437. data={current}
  438. visible={visible}
  439. close={() => {
  440. setVisible(false);
  441. actionRef.current?.reset?.();
  442. }}
  443. />
  444. </PageContainer>
  445. );
  446. };
  447. export default Configuration;