index.tsx 14 KB

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