index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { Badge, Button, Modal } from 'antd';
  2. import type { DeviceInstance } from '@/pages/device/Instance/typings';
  3. import SearchComponent from '@/components/SearchComponent';
  4. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  5. import ProTable from '@jetlinks/pro-table';
  6. import { useRef, useState } from 'react';
  7. import { service } from '@/pages/device/Instance';
  8. import { useIntl } from 'umi';
  9. import moment from 'moment';
  10. interface Props {
  11. data: Partial<DeviceInstance>;
  12. onCancel: () => void;
  13. onOk: (parentId: string) => void;
  14. }
  15. const statusMap = new Map();
  16. statusMap.set('online', 'success');
  17. statusMap.set('offline', 'error');
  18. statusMap.set('notActive', 'warning');
  19. const BindParentDevice = (props: Props) => {
  20. const intl = useIntl();
  21. const actionRef = useRef<ActionType>();
  22. const [searchParams, setSearchParams] = useState<any>({});
  23. const [bindKeys, setBindKeys] = useState<any[]>([]);
  24. const columns: ProColumns<DeviceInstance>[] = [
  25. {
  26. title: 'ID',
  27. dataIndex: 'id',
  28. ellipsis: true,
  29. },
  30. {
  31. title: '设备名称',
  32. ellipsis: true,
  33. dataIndex: 'name',
  34. },
  35. {
  36. title: '所属产品',
  37. ellipsis: true,
  38. dataIndex: 'productName',
  39. },
  40. {
  41. title: '注册时间',
  42. dataIndex: 'registryTime',
  43. ellipsis: true,
  44. width: '200px',
  45. valueType: 'dateTime',
  46. render: (text: any) => (!!text ? moment(text).format('YYYY-MM-DD HH:mm:ss') : '/'),
  47. sorter: true,
  48. },
  49. {
  50. title: '状态',
  51. dataIndex: 'state',
  52. ellipsis: true,
  53. width: 100,
  54. renderText: (record) =>
  55. record ? <Badge status={statusMap.get(record.value)} text={record.text} /> : '',
  56. valueType: 'select',
  57. valueEnum: {
  58. notActive: {
  59. text: intl.formatMessage({
  60. id: 'pages.device.instance.status.notActive',
  61. defaultMessage: '未启用',
  62. }),
  63. status: 'notActive',
  64. },
  65. offline: {
  66. text: intl.formatMessage({
  67. id: 'pages.device.instance.status.offLine',
  68. defaultMessage: '离线',
  69. }),
  70. status: 'offline',
  71. },
  72. online: {
  73. text: intl.formatMessage({
  74. id: 'pages.device.instance.status.onLine',
  75. defaultMessage: '在线',
  76. }),
  77. status: 'online',
  78. },
  79. },
  80. },
  81. ];
  82. const submitBtn = async () => {
  83. if (props.data?.id) {
  84. const resp = await service.bindDevice(bindKeys[0], [props.data?.id]);
  85. if (resp.status === 200) {
  86. props.onOk(bindKeys[0]);
  87. setBindKeys([]);
  88. actionRef.current?.reset?.();
  89. }
  90. }
  91. };
  92. return (
  93. <Modal
  94. maskClosable={false}
  95. title="绑定父设备"
  96. visible
  97. width={1000}
  98. onOk={() => {
  99. submitBtn();
  100. }}
  101. onCancel={() => {
  102. props.onCancel();
  103. setBindKeys([]);
  104. }}
  105. footer={[
  106. <Button
  107. key="back"
  108. onClick={() => {
  109. props.onCancel();
  110. setBindKeys([]);
  111. actionRef.current?.reset?.();
  112. }}
  113. >
  114. 取消
  115. </Button>,
  116. <Button
  117. disabled={!(bindKeys.length > 0)}
  118. key="submit"
  119. type="primary"
  120. onClick={() => {
  121. submitBtn();
  122. }}
  123. >
  124. 确定
  125. </Button>,
  126. ]}
  127. >
  128. <SearchComponent<DeviceInstance>
  129. field={[...columns]}
  130. target="parents-device-bind"
  131. enableSave={false}
  132. model={'simple'}
  133. defaultParam={[
  134. {
  135. terms: [
  136. {
  137. column: 'productId$product-info',
  138. value: [
  139. {
  140. column: 'deviceType',
  141. termType: 'eq',
  142. value: 'gateway',
  143. },
  144. ],
  145. },
  146. ],
  147. },
  148. {
  149. terms: [{ column: 'id$not', value: props.data.id!, type: 'and' }],
  150. },
  151. ]}
  152. onSearch={(param) => {
  153. actionRef.current?.reset?.();
  154. setSearchParams(param);
  155. }}
  156. />
  157. <ProTable<DeviceInstance>
  158. search={false}
  159. columns={columns}
  160. size="small"
  161. rowSelection={{
  162. type: 'radio',
  163. selectedRowKeys: bindKeys,
  164. onChange: (selectedRowKeys, selectedRows) => {
  165. setBindKeys(selectedRows.map((item) => item.id));
  166. },
  167. }}
  168. tableAlertRender={false}
  169. actionRef={actionRef}
  170. params={searchParams}
  171. rowKey="id"
  172. toolBarRender={false}
  173. pagination={{
  174. pageSize: 10,
  175. }}
  176. request={(params) => service.query({ ...params })}
  177. />
  178. </Modal>
  179. );
  180. };
  181. export default BindParentDevice;