1
0

index.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. import React, { useEffect, useState } from 'react';
  2. import { PageContainer } from '@ant-design/pro-components';
  3. import { Button, Card, Form, Input, message, Space, Table, Tag } from 'antd';
  4. import type { ColumnsType } from 'antd/es/table';
  5. import { queryDeviceOperations, queryDeviceOperationsData } from '@/services/afterSales/afterSales';
  6. import DeviceStatusData from './deviceStatusData';
  7. import DeviceHistory from '@/pages/AfterSales/AfterSalesManagement/deviceHistory';
  8. import { DownloadOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  9. import Overwrite from '@/pages/home/overwrite';
  10. import { queryAfterSalesExport } from '@/services/reportHome';
  11. interface DataType {
  12. home_name: string;
  13. user_name: string;
  14. device_code: string;
  15. is_online: boolean;
  16. power: number;
  17. mode: number;
  18. speed: number;
  19. set_temp: number;
  20. temperature: number;
  21. humidity: number;
  22. fan_voltage: number;
  23. record_id: string;
  24. }
  25. const BannerManagement: React.FC = () => {
  26. const [dataList, setDataList] = useState([]);
  27. const [loading, setLoading] = useState(false);
  28. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  29. const [historyVisible, setHistoryVisible] = useState(false);
  30. const [historyData, setHistoryData] = useState(null);
  31. const [statusVisible, setStatusVisible] = useState(false);
  32. const [statusData, setStatusData] = useState(null);
  33. const [form] = Form.useForm();
  34. const [searchData, setSearchData] = useState<object | null>({});
  35. const [visible, setVisible] = useState(false);
  36. const [editData, setEditData] = useState(null);
  37. const getList = () => {
  38. const params = {
  39. q: 'page',
  40. current: pagination.current,
  41. pageSize: pagination.pageSize,
  42. ...searchData,
  43. };
  44. queryDeviceOperations(params).then((res) => {
  45. if (res && res.code === 0) {
  46. setDataList(res.data.list || []);
  47. setPagination(res.data.pagination);
  48. setLoading(false);
  49. }
  50. });
  51. };
  52. // 切换分页
  53. const tableChange = (page: any) => {
  54. setLoading(true);
  55. const params = {
  56. q: 'page',
  57. current: page.current,
  58. pageSize: page.pageSize,
  59. ...searchData,
  60. };
  61. queryDeviceOperations(params).then((res) => {
  62. if (res && res.code === 0) {
  63. setDataList(res.data.list || []);
  64. setPagination(res.data.pagination);
  65. setLoading(false);
  66. }
  67. });
  68. };
  69. useEffect(() => {
  70. getList();
  71. }, []);
  72. useEffect(() => {
  73. getList();
  74. }, [searchData]);
  75. // 设备运行历史
  76. const onHistory = (record: any) => {
  77. setHistoryVisible(true);
  78. setHistoryData(record);
  79. };
  80. // 设备运行历史回调
  81. const onHistoryCallback = () => {
  82. setHistoryVisible(false);
  83. };
  84. // 24小时设备状态数据
  85. const onDeviceOperations = (record: any) => {
  86. queryDeviceOperationsData({ device_id: record.device_code }).then((res) => {
  87. if (res && res.code === 0) {
  88. const list = res.data.list || [];
  89. const result = list.reduce(
  90. (acc: Record<string, Record<string, any>>, { label, value, time }: any) => {
  91. if (!acc[time]) {
  92. // 如果不存在,则初始化一个新对象
  93. acc[time] = {};
  94. }
  95. // 将当前的 label 和 value 添加到对应的时间对象中
  96. acc[time][label] = value;
  97. return acc;
  98. },
  99. {},
  100. );
  101. const finalResult: any = Object.entries(result).map(([time, values]: any) => ({
  102. time,
  103. ...values,
  104. }));
  105. setStatusData(finalResult);
  106. setStatusVisible(true);
  107. }
  108. });
  109. };
  110. // 24小时设备状态数据回调
  111. const onStatusCallback = () => {
  112. setStatusVisible(false);
  113. };
  114. // 搜索
  115. const onFinish = () => {
  116. form.validateFields().then((data) => {
  117. setLoading(true);
  118. setSearchData(data);
  119. });
  120. };
  121. // 重置
  122. const onReset = () => {
  123. form.resetFields();
  124. setLoading(true);
  125. setSearchData(null);
  126. };
  127. // 下发
  128. const onOverwrite = (data: any) => {
  129. setVisible(true);
  130. setEditData(data);
  131. };
  132. // 下发弹框回调
  133. const overwriteCallback = () => {
  134. setVisible(false);
  135. getList();
  136. };
  137. // 导出
  138. const onExport = () => {
  139. form.validateFields().then((data) => {
  140. queryAfterSalesExport(data)
  141. .then((res: any) => {
  142. const link = document.createElement('a');
  143. link.style.display = 'none';
  144. link.href = window.URL.createObjectURL(new Blob([res]));
  145. link.setAttribute('download', '售后管理列表.xlsx');
  146. document.body.appendChild(link);
  147. link.click();
  148. document.body.removeChild(link);
  149. })
  150. .catch(() => {
  151. message.error('导出失败');
  152. });
  153. });
  154. };
  155. const columns: ColumnsType<DataType> = [
  156. {
  157. title: '序号',
  158. align: 'center',
  159. key: 'index',
  160. render: (_: any, _row: any, index: number) => index + 1,
  161. },
  162. {
  163. title: '家名称',
  164. dataIndex: 'home_name',
  165. key: 'home_name',
  166. },
  167. {
  168. title: '用户名',
  169. dataIndex: 'user_name',
  170. key: 'user_name',
  171. },
  172. {
  173. title: '手机号',
  174. dataIndex: 'phone',
  175. key: 'phone',
  176. },
  177. {
  178. title: '地区',
  179. dataIndex: 'address',
  180. key: 'address',
  181. },
  182. {
  183. title: '设备编号',
  184. dataIndex: 'device_code',
  185. key: 'device_code',
  186. },
  187. {
  188. title: '设备类型',
  189. dataIndex: 'device_type_name',
  190. key: 'device_type_name',
  191. },
  192. {
  193. title: '是否在线',
  194. dataIndex: 'is_online',
  195. key: 'is_online',
  196. render: (_, record: any) => {
  197. return (
  198. <Tag color={record.is_online ? 'green' : 'red'}>{record.is_online ? '在线' : '离线'}</Tag>
  199. );
  200. },
  201. },
  202. {
  203. title: '当前开关状态',
  204. dataIndex: 'power',
  205. key: 'power',
  206. render: (_, record: any) => {
  207. return (
  208. <Tag color={{ 0: 'red', 1: 'green' }[record.power]}>
  209. {{ 0: '关', 1: '开' }[record.power]}
  210. </Tag>
  211. );
  212. },
  213. },
  214. {
  215. title: '当前模式',
  216. dataIndex: 'mode',
  217. key: 'mode',
  218. render: (_, record: any) => {
  219. return (
  220. <span>{{ 0: '制冷', 1: '制热', 2: '除湿', 3: '送风', 4: '加湿' }[record.mode]}</span>
  221. );
  222. },
  223. },
  224. {
  225. title: '风速',
  226. dataIndex: 'speed',
  227. key: 'speed',
  228. render: (_, record: any) => {
  229. return (
  230. <span>
  231. {{ 1: '一档', 2: '二档', 3: '三档', 4: '四档', 5: '五档', 6: '六档' }[record.speed]}
  232. </span>
  233. );
  234. },
  235. },
  236. {
  237. title: '设定温度',
  238. dataIndex: 'set_temp',
  239. key: 'set_temp',
  240. render: (_, record: any) => {
  241. return `${record.set_temp}℃`;
  242. },
  243. },
  244. {
  245. title: '温度',
  246. dataIndex: 'temperature',
  247. key: 'temperature',
  248. render: (_, record: any) => {
  249. return `${record.temperature}℃`;
  250. },
  251. },
  252. {
  253. title: '湿度',
  254. dataIndex: 'humidity',
  255. key: 'humidity',
  256. },
  257. {
  258. title: '电压值',
  259. dataIndex: 'fan_voltage',
  260. key: 'fan_voltage',
  261. },
  262. {
  263. title: '操作',
  264. key: 'action',
  265. render: (_, record) => (
  266. <Space size="middle">
  267. <a
  268. onClick={() => {
  269. onHistory(record);
  270. }}
  271. >
  272. 设备运行历史
  273. </a>
  274. <a
  275. onClick={() => {
  276. onDeviceOperations(record);
  277. }}
  278. >
  279. 24小时设备状态数据
  280. </a>
  281. <a
  282. onClick={() => {
  283. onOverwrite(record);
  284. }}
  285. >
  286. 远程配置
  287. </a>
  288. </Space>
  289. ),
  290. },
  291. ];
  292. return (
  293. <PageContainer>
  294. <Card>
  295. <Form form={form} layout="inline" onFinish={onFinish} style={{ padding: '20px' }}>
  296. <Form.Item name="home_name" label="家名称">
  297. <Input placeholder="请输入家名称" />
  298. </Form.Item>
  299. <Form.Item name="user_name" label="用户名">
  300. <Input placeholder="请输入用户名" />
  301. </Form.Item>
  302. <Form.Item name="device_id" label="设备编号 ">
  303. <Input placeholder="请输入设备编号" />
  304. </Form.Item>
  305. <Form.Item style={{ marginBottom: '10px' }}>
  306. <Space>
  307. <Button type="primary" htmlType="submit">
  308. <SearchOutlined />
  309. 查询
  310. </Button>
  311. <Button htmlType="button" onClick={onReset}>
  312. <ReloadOutlined />
  313. 重置
  314. </Button>
  315. <Button type="dashed" onClick={onExport}>
  316. <DownloadOutlined />
  317. 导出
  318. </Button>
  319. </Space>
  320. </Form.Item>
  321. </Form>
  322. <Table
  323. columns={columns}
  324. dataSource={dataList}
  325. rowKey={(record) => record.record_id}
  326. pagination={pagination}
  327. loading={loading}
  328. onChange={tableChange}
  329. />
  330. {historyVisible && (
  331. <DeviceHistory
  332. visible={historyVisible}
  333. params={historyData}
  334. editCallback={onHistoryCallback}
  335. />
  336. )}
  337. {statusVisible && (
  338. <DeviceStatusData
  339. params={statusData}
  340. visible={statusVisible}
  341. editCallback={onStatusCallback}
  342. />
  343. )}
  344. {visible && (
  345. <Overwrite visible={visible} editCallback={overwriteCallback} params={editData} />
  346. )}
  347. </Card>
  348. </PageContainer>
  349. );
  350. };
  351. export default BannerManagement;