HistoryTable.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <template>
  2. <div class="history-table" v-if="loading">
  3. <BasicTable ref="historyTable" @register="registerTable" :data-source="dataSource">
  4. <template #bodyCell="{ column, record }">
  5. <a-tag v-if="column.dataIndex === 'warnFlag'" :color="record.warnFlag == '0' ? 'green' : 'red'">{{
  6. record.warnFlag == '0' ? '正常' : '报警'
  7. }}</a-tag>
  8. <a-tag v-if="column.dataIndex === 'netStatus'" :color="record.netStatus == '0' ? '#f00' : 'green'">{{
  9. record.netStatus == '0' ? '断开' : '连接'
  10. }}</a-tag>
  11. <slot name="filterCell" v-bind="{ column, record }"></slot>
  12. </template>
  13. <template #form-submitBefore>
  14. <a-button type="primary" preIcon="ant-design:search-outlined" @click="getDataSource">查询</a-button>
  15. <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXlsFn"> 导出</a-button>
  16. </template>
  17. </BasicTable>
  18. </div>
  19. </template>
  20. <script lang="ts" setup>
  21. //ts语法
  22. import { watchEffect, ref, watch, defineExpose, inject, nextTick, onMounted, computed } from 'vue';
  23. import { FormSchema } from '/@/components/Form/index';
  24. import { BasicTable } from '/@/components/Table';
  25. import { useListPage } from '/@/hooks/system/useListPage';
  26. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  27. import { defHttp } from '/@/utils/http/axios';
  28. import dayjs from 'dayjs';
  29. import { getAutoScrollContainer } from '/@/utils/common/compUtils';
  30. const globalConfig = inject('globalConfig');
  31. const props = defineProps({
  32. columnsType: {
  33. type: String,
  34. },
  35. columns: {
  36. type: Array,
  37. // required: true,
  38. default: () => [],
  39. },
  40. deviceType: {
  41. type: String,
  42. required: true,
  43. },
  44. deviceListApi: {
  45. type: Function,
  46. },
  47. deviceArr: {
  48. type: Array,
  49. // required: true,
  50. default: () => [],
  51. },
  52. designScope: {
  53. type: String,
  54. },
  55. sysId: {
  56. type: String,
  57. },
  58. deviceId: {
  59. type: String,
  60. },
  61. scroll: {
  62. type: Object,
  63. default: { y: 0 },
  64. },
  65. formSchemas: {
  66. type: Array<FormSchema>,
  67. default: () => [],
  68. },
  69. });
  70. const getDeviceListApi = (params) => defHttp.post({ url: '/monitor/device', params });
  71. const historyTable = ref();
  72. const loading = ref(false);
  73. const stationType = ref('plc1');
  74. const dataSource = ref([]);
  75. const intervalMap = new Map([
  76. ['1', '1s'],
  77. ['2', '5s'],
  78. ['3', '10s'],
  79. ['4', '30s'],
  80. ['5', '1m'],
  81. ['6', '10m'],
  82. ['7', '30m'],
  83. ['8', '1h'],
  84. ['9', '1d'],
  85. ]);
  86. const getExportXlsUrl = () => {
  87. if (stationType.value !== 'redis') {
  88. return '/safety/ventanalyMonitorData/export/historydata';
  89. } else {
  90. return '/monitor/history/exportHistoryData';
  91. }
  92. };
  93. const emit = defineEmits(['change']);
  94. const historyType = ref('');
  95. const deviceKide = ref('');
  96. const columns = ref([]);
  97. const tableScroll = props.scroll.y ? ref({ y: props.scroll.y - 100 }) : ref({});
  98. let deviceOptions = ref([]);
  99. const deviceTypeStr = ref('');
  100. loading.value = true;
  101. watch(
  102. () => {
  103. return props.columnsType;
  104. },
  105. async (newVal) => {
  106. debugger;
  107. if (!newVal) return;
  108. deviceKide.value = newVal;
  109. if (historyTable.value) {
  110. getForm().resetFields();
  111. // getForm().updateSchema();
  112. // getForm();
  113. }
  114. dataSource.value = [];
  115. // const column = getTableHeaderColumns(newVal.includes('_history') ? newVal : newVal + '_history');
  116. // if (column && column.length < 1) {
  117. // const arr = newVal.split('_');
  118. // console.log('历史记录列表表头------------>', arr[0] + '_monitor');
  119. // columns.value = getTableHeaderColumns(arr[0] + '_history');
  120. // if (columns.value.length < 1) {
  121. // if (historyType.value) {
  122. // columns.value = getTableHeaderColumns(historyType.value + '_history');
  123. // }
  124. // }
  125. // } else {
  126. // columns.value = column;
  127. // }
  128. await getDeviceList();
  129. nextTick(() => {
  130. getDataSource();
  131. });
  132. if (historyTable.value) reload();
  133. },
  134. {
  135. immediate: true,
  136. }
  137. );
  138. watch(historyType, (type) => {
  139. if (!type) return;
  140. // if (historyTable.value) getForm().resetFields()
  141. const column = getTableHeaderColumns(type.includes('_history') ? type : type + '_history');
  142. if (column && column.length < 1) {
  143. const arr = type.split('_');
  144. columns.value = getTableHeaderColumns(arr[0] + '_history');
  145. } else {
  146. columns.value = column;
  147. }
  148. setColumns(columns.value);
  149. });
  150. watch(
  151. () => props.scroll.y,
  152. (newVal) => {
  153. if (newVal) {
  154. tableScroll.value = { y: newVal - 100 };
  155. } else {
  156. tableScroll.value = {};
  157. }
  158. }
  159. );
  160. // watch(stationType, (type) => {
  161. // if (type) {
  162. // nextTick(() => {
  163. // getDataSource();
  164. // });
  165. // }
  166. // });
  167. watch(
  168. () => props.deviceId,
  169. async () => {
  170. await getForm().setFieldsValue({});
  171. await getDeviceList();
  172. }
  173. );
  174. async function getDeviceList() {
  175. // if (props.deviceType.split('_')[1] && props.deviceType.split('_')[1] === 'history') return;
  176. let result;
  177. if (!props.sysId) {
  178. if (props.deviceListApi) {
  179. const res = await props.deviceListApi();
  180. if (props.deviceType.startsWith('modelsensor')) {
  181. if (res['msgTxt'] && res['msgTxt'][0] && res['msgTxt'][0]['datalist']) {
  182. result = res['msgTxt'][0]['datalist'];
  183. }
  184. } else {
  185. if (res['records'] && res['records'].length > 0) result = res['records'];
  186. }
  187. } else {
  188. const res = await getDeviceListApi({ devicetype: props.deviceType, pageSize: 10000 });
  189. if (res['records'] && res['records'].length > 0) {
  190. result = res['records'];
  191. } else if (res['msgTxt'] && res['msgTxt'][0] && res['msgTxt'][0]['datalist']) {
  192. result = res['msgTxt'][0]['datalist'];
  193. }
  194. }
  195. } else {
  196. const res = await getDeviceListApi({
  197. sysId: props.sysId,
  198. devicetype: props.deviceType.startsWith('vehicle') ? 'location_normal' : props.deviceType,
  199. pageSize: 10000,
  200. });
  201. if (res['records'] && res['records'].length > 0) {
  202. result = res['records'];
  203. } else if (res['msgTxt'] && res['msgTxt'][0] && res['msgTxt'][0]['datalist']) {
  204. result = res['msgTxt'][0]['datalist'];
  205. }
  206. }
  207. if (result) {
  208. deviceOptions.value = [];
  209. deviceOptions.value = result.map((item, index) => {
  210. return {
  211. label: item['strinstallpos'],
  212. value: item['id'] || item['deviceID'],
  213. strtype: item['strtype'] || item['deviceType'],
  214. strinstallpos: item['strinstallpos'],
  215. devicekind: item['devicekind'],
  216. stationtype: item['stationtype'],
  217. };
  218. });
  219. stationType.value = deviceOptions.value[0]['stationtype'];
  220. historyType.value = deviceOptions.value[0]['strtype'] || deviceOptions.value[0]['devicekind'];
  221. }
  222. await getForm().setFieldsValue({ gdeviceid: props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '' });
  223. }
  224. function resetFormParam() {
  225. const stationTypeStr = stationType.value;
  226. const formData = getForm().getFieldsValue();
  227. const pagination = getPaginationRef();
  228. formData['pageNo'] = pagination['current'];
  229. formData['pageSize'] = pagination['pageSize'];
  230. formData['column'] = 'createTime';
  231. if (stationTypeStr !== 'redis') {
  232. formData['strtype'] = deviceTypeStr.value
  233. ? deviceTypeStr.value
  234. : deviceOptions.value[0]['strtype']
  235. ? deviceOptions.value[0]['strtype']
  236. : props.deviceType + '*';
  237. if (props.sysId) {
  238. formData['sysId'] = props.sysId;
  239. }
  240. return formData;
  241. } else {
  242. const params = {
  243. pageNum: pagination['current'],
  244. pageSize: pagination['pageSize'],
  245. column: pagination['createTime'],
  246. startTime: formData['ttime_begin'],
  247. endTime: formData['ttime_end'],
  248. deviceId: formData['gdeviceid'],
  249. strtype: props.deviceType + '*',
  250. sysId: props.sysId,
  251. interval: intervalMap.get(formData['skip']) ? intervalMap.get(formData['skip']) : '1h',
  252. isEmployee: props.deviceType.startsWith('vehicle') ? false : true,
  253. };
  254. return params;
  255. }
  256. }
  257. async function getDataSource() {
  258. const stationTypeStr = stationType.value;
  259. dataSource.value = [];
  260. setLoading(true);
  261. const params = await resetFormParam();
  262. if (stationTypeStr !== 'redis') {
  263. const result = await defHttp.get({ url: '/safety/ventanalyMonitorData/listdays', params: params });
  264. setPagination({ total: Math.abs(result['datalist']['total']) || 0 });
  265. if (result['datalist']['records'].length > 0) {
  266. dataSource.value = result['datalist']['records'].map((item: any) => {
  267. return Object.assign(item, item['readData']);
  268. });
  269. } else {
  270. dataSource.value = [];
  271. }
  272. } else {
  273. const result = await defHttp.post({ url: '/monitor/history/getHistoryData', params: params });
  274. setPagination({ total: Math.abs(result['total']) || 0 });
  275. dataSource.value = result['records'] || [];
  276. }
  277. setLoading(false);
  278. }
  279. // 列表页面公共参数、方法
  280. const { tableContext, onExportXls, onExportXlsPost } = useListPage({
  281. tableProps: {
  282. // api: list,
  283. columns: props.columnsType ? columns : (props.columns as any[]),
  284. canResize: true,
  285. showTableSetting: false,
  286. showActionColumn: false,
  287. bordered: false,
  288. size: 'small',
  289. scroll: tableScroll,
  290. showIndexColumn: true,
  291. tableLayout: 'auto',
  292. formConfig: {
  293. labelAlign: 'left',
  294. showAdvancedButton: false,
  295. showSubmitButton: false,
  296. showResetButton: false,
  297. baseColProps: {
  298. xs: 24,
  299. sm: 24,
  300. md: 24,
  301. lg: 9,
  302. xl: 7,
  303. xxl: 4,
  304. },
  305. schemas:
  306. props.formSchemas.length > 0
  307. ? props.formSchemas
  308. : [
  309. {
  310. field: 'ttime_begin',
  311. label: '开始时间',
  312. component: 'DatePicker',
  313. defaultValue: dayjs().startOf('date'),
  314. required: true,
  315. componentProps: {
  316. showTime: true,
  317. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  318. getPopupContainer: getAutoScrollContainer,
  319. },
  320. colProps: {
  321. span: 4,
  322. },
  323. },
  324. {
  325. field: 'ttime_end',
  326. label: '结束时间',
  327. component: 'DatePicker',
  328. defaultValue: dayjs(),
  329. required: true,
  330. componentProps: {
  331. showTime: true,
  332. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  333. getPopupContainer: getAutoScrollContainer,
  334. },
  335. colProps: {
  336. span: 4,
  337. },
  338. },
  339. {
  340. label: computed(() => `${deviceKide.value.startsWith('location') ? '查询人员' : '查询设备'}`),
  341. field: 'gdeviceid',
  342. component: 'Select',
  343. defaultValue: props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '',
  344. required: true,
  345. componentProps: {
  346. showSearch: true,
  347. filterOption: (input: string, option: any) => {
  348. return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  349. },
  350. options: deviceOptions,
  351. onChange: (e, option) => {
  352. if (option && (option['strinstallpos'] || option['strtype'] || option['devicekind']))
  353. historyType.value = option['strtype'] || option['devicekind'];
  354. if (option['strtype']) deviceTypeStr.value = option['strtype'];
  355. stationType.value = option['stationtype'];
  356. nextTick(async () => {
  357. await getDataSource();
  358. });
  359. },
  360. },
  361. colProps: {
  362. span: 4,
  363. },
  364. },
  365. {
  366. label: '间隔时间',
  367. field: 'skip',
  368. component: 'Select',
  369. defaultValue: '8',
  370. componentProps: {
  371. options: [
  372. {
  373. label: '1秒',
  374. value: '1',
  375. },
  376. {
  377. label: '5秒',
  378. value: '2',
  379. },
  380. {
  381. label: '10秒',
  382. value: '3',
  383. },
  384. {
  385. label: '30秒',
  386. value: '4',
  387. },
  388. {
  389. label: '1分钟',
  390. value: '5',
  391. },
  392. {
  393. label: '10分钟',
  394. value: '6',
  395. },
  396. {
  397. label: '30分钟',
  398. value: '7',
  399. },
  400. {
  401. label: '1小时',
  402. value: '8',
  403. },
  404. {
  405. label: '1天',
  406. value: '9',
  407. },
  408. ],
  409. },
  410. colProps: {
  411. span: 4,
  412. },
  413. },
  414. ],
  415. // fieldMapToTime: [['tickectDate', ['ttime_begin', 'ttime_end'], '']],
  416. },
  417. // fetchSetting: {
  418. // listField: 'datalist',
  419. // totalField: 'datalist.total',
  420. // },
  421. pagination: {
  422. current: 1,
  423. pageSize: 10,
  424. pageSizeOptions: ['10', '30', '50', '100'],
  425. showQuickJumper: false,
  426. },
  427. beforeFetch() {
  428. const newParams = { ...resetFormParam() };
  429. debugger;
  430. return newParams;
  431. },
  432. // afterFetch(result) {
  433. // const resultItems = result['records'];
  434. // resultItems.map((item) => {
  435. // Object.assign(item, item['readData']);
  436. // });
  437. // console.log('result---------------->', result);
  438. // return resultItems;
  439. // },
  440. },
  441. exportConfig: {
  442. name: '设备历史列表',
  443. url: getExportXlsUrl,
  444. },
  445. });
  446. //注册table数据
  447. const [registerTable, { reload, setLoading, getForm, setColumns, getPaginationRef, setPagination }] = tableContext;
  448. function onExportXlsFn() {
  449. const params = resetFormParam();
  450. // 判断时间间隔和查询时间区间,数据量下载大时进行提示
  451. if (stationType.value !== 'redis') {
  452. return onExportXls(params);
  453. } else {
  454. return onExportXlsPost(params);
  455. }
  456. }
  457. watchEffect(() => {
  458. if (historyTable.value && dataSource) {
  459. const data = dataSource.value || [];
  460. emit('change', data);
  461. }
  462. });
  463. onMounted(async () => {
  464. await getDeviceList();
  465. if (deviceOptions.value[0]) {
  466. stationType.value = deviceOptions.value[0]['stationtype'];
  467. historyType.value = deviceOptions.value[0]['strtype'] || deviceOptions.value[0]['devicekind'];
  468. nextTick(async () => {
  469. await getDataSource();
  470. });
  471. }
  472. watch([() => getPaginationRef()['current'], () => getPaginationRef()['pageSize']], async () => {
  473. if (deviceOptions.value[0]) {
  474. if (deviceOptions.value[0]) {
  475. await getDataSource();
  476. }
  477. }
  478. });
  479. });
  480. defineExpose({ setLoading });
  481. </script>
  482. <style scoped lang="less">
  483. @import '/@/design/theme.less';
  484. :deep(.@{ventSpace}-table-body) {
  485. height: auto !important;
  486. }
  487. :deep(.zxm-picker) {
  488. height: 30px !important;
  489. }
  490. .history-table {
  491. width: 100%;
  492. :deep(.jeecg-basic-table-form-container) {
  493. .@{ventSpace}-form {
  494. padding: 0 !important;
  495. border: none !important;
  496. margin-bottom: 0 !important;
  497. .@{ventSpace}-picker,
  498. .@{ventSpace}-select-selector {
  499. width: 100% !important;
  500. height: 100%;
  501. background: #00000017;
  502. border: 1px solid #b7b7b7;
  503. input,
  504. .@{ventSpace}-select-selection-item,
  505. .@{ventSpace}-picker-suffix {
  506. color: #fff;
  507. }
  508. .@{ventSpace}-select-selection-placeholder {
  509. color: #ffffffaa;
  510. }
  511. }
  512. }
  513. .@{ventSpace}-table-title {
  514. min-height: 0 !important;
  515. }
  516. }
  517. .pagination-box {
  518. display: flex;
  519. justify-content: flex-end;
  520. align-items: center;
  521. .page-num {
  522. border: 1px solid #0090d8;
  523. padding: 4px 8px;
  524. margin-right: 5px;
  525. color: #0090d8;
  526. }
  527. .btn {
  528. margin-right: 10px;
  529. }
  530. }
  531. }
  532. </style>