HistoryTable.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <template>
  2. <div class="history-table" v-if="loading">
  3. <BasicTable ref="historyTable" @register="registerTable" :data-source="dataSource" :scroll="tableScroll">
  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. <template v-if="column.dataIndex === 'nwartype'">
  12. <!-- 除了 101(蓝色预警)其他都是红色字体 -->
  13. <span :class="{ 'color-#ff3823': ['102', '103', '104', '201', '1001'].includes(record.nwartype) }">
  14. {{ render.renderDictText(record.nwartype, 'leveltype') || '-' }}
  15. </span>
  16. </template>
  17. <slot name="filterCell" v-bind="{ column, record }"></slot>
  18. </template>
  19. <template #form-submitBefore>
  20. <a-button type="primary" preIcon="ant-design:search-outlined" @click="getDataSource">查询</a-button>
  21. <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXlsFn"> 导出</a-button>
  22. </template>
  23. </BasicTable>
  24. <BarAndLine
  25. v-if="showCurve"
  26. :charts-columns="chartsColumns"
  27. :option="{
  28. legend: {
  29. top: '5',
  30. },
  31. grid: {
  32. top: 50,
  33. left: 200,
  34. right: 200,
  35. bottom: 50,
  36. },
  37. }"
  38. :data-source="dataSource"
  39. height="300px"
  40. x-axis-prop-type="ttime"
  41. />
  42. </div>
  43. </template>
  44. <script lang="ts" setup>
  45. //ts语法
  46. import { watchEffect, ref, watch, defineExpose, inject, nextTick, onMounted, computed } from 'vue';
  47. import { FormSchema } from '/@/components/Form/index';
  48. import { BasicTable } from '/@/components/Table';
  49. import { useListPage } from '/@/hooks/system/useListPage';
  50. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  51. import { defHttp } from '/@/utils/http/axios';
  52. import dayjs from 'dayjs';
  53. import { getAutoScrollContainer } from '/@/utils/common/compUtils';
  54. import { render } from '/@/utils/common/renderUtils';
  55. import { useMethods } from '/@/hooks/system/useMethods';
  56. import BarAndLine from '/@/components/chart/BarAndLine.vue';
  57. import { getDictItemsByCode } from '/@/utils/dict';
  58. import { get } from 'lodash-es';
  59. const globalConfig = inject('globalConfig');
  60. const props = defineProps({
  61. columnsType: {
  62. type: String,
  63. },
  64. columns: {
  65. type: Array,
  66. // required: true,
  67. default: () => [],
  68. },
  69. deviceType: {
  70. type: String,
  71. required: true,
  72. },
  73. deviceListApi: {
  74. type: Function,
  75. },
  76. deviceArr: {
  77. type: Array,
  78. // required: true,
  79. default: () => [],
  80. },
  81. designScope: {
  82. type: String,
  83. },
  84. sysId: {
  85. type: String,
  86. },
  87. deviceId: {
  88. type: String,
  89. },
  90. scroll: {
  91. type: Object,
  92. default: { y: 0 },
  93. },
  94. formSchemas: {
  95. type: Array<FormSchema>,
  96. default: () => [],
  97. },
  98. /** 仅展示已绑定设备,选择是则从系统中获取sysId下已绑定设备。仅能查询到已绑定设备的历史数据 */
  99. onlyBounedDevices: {
  100. type: Boolean,
  101. default: false,
  102. },
  103. showHistoryCurve: {
  104. type: Boolean,
  105. default: false,
  106. },
  107. });
  108. const getDeviceListApi = (params) => defHttp.post({ url: '/monitor/device', params });
  109. const historyTable = ref();
  110. const loading = ref(false);
  111. const stationType = ref('plc1');
  112. const dataSource = ref([]);
  113. const intervalMap = new Map([
  114. ['1', '1s'],
  115. ['2', '5s'],
  116. ['3', '10s'],
  117. ['4', '30s'],
  118. ['5', '1m'],
  119. ['6', '10m'],
  120. ['7', '30m'],
  121. ['8', '1h'],
  122. ['9', '1d'],
  123. ]);
  124. const getExportXlsUrl = () => {
  125. if (stationType.value !== 'redis') {
  126. return '/safety/ventanalyMonitorData/export/historydata';
  127. } else {
  128. return '/monitor/history/exportHistoryData';
  129. }
  130. };
  131. const emit = defineEmits(['change']);
  132. const historyType = ref('');
  133. const deviceKide = ref('');
  134. const columns = ref([]);
  135. let deviceOptions = ref([]);
  136. const deviceTypeStr = ref('');
  137. const deviceTypeName = ref('');
  138. const deviceType = ref('');
  139. const chartsColumns = ref([]);
  140. loading.value = true;
  141. const selectedOption = computed<Record<string, any> | undefined>(() => {
  142. let idval: string | undefined = getForm()?.getFieldsValue()?.gdeviceids;
  143. if (VENT_PARAM.historyIsMultiple && idval) {
  144. const arr = idval.split(',');
  145. idval = arr[arr.length - 1];
  146. }
  147. return deviceOptions.value.find((e: any) => {
  148. return e.value === idval;
  149. });
  150. });
  151. watch(
  152. () => {
  153. return props.columnsType;
  154. },
  155. async (newVal) => {
  156. if (!newVal) return;
  157. deviceKide.value = newVal;
  158. if (historyTable.value) {
  159. getForm().resetFields();
  160. // getForm().updateSchema();
  161. // getForm();
  162. }
  163. dataSource.value = [];
  164. // const column = getTableHeaderColumns(newVal.includes('_history') ? newVal : newVal + '_history');
  165. // if (column && column.length < 1) {
  166. // const arr = newVal.split('_');
  167. // console.log('历史记录列表表头------------>', arr[0] + '_monitor');
  168. // columns.value = getTableHeaderColumns(arr[0] + '_history');
  169. // if (columns.value.length < 1) {
  170. // if (historyType.value) {
  171. // columns.value = getTableHeaderColumns(historyType.value + '_history');
  172. // }
  173. // }
  174. // } else {
  175. // columns.value = column;
  176. // }
  177. await getDeviceList();
  178. nextTick(() => {
  179. getDataSource();
  180. });
  181. if (historyTable.value) reload();
  182. },
  183. {
  184. immediate: true,
  185. }
  186. );
  187. watch(historyType, (type) => {
  188. if (!type) return;
  189. // if (historyTable.value) getForm().resetFields()
  190. const column = getTableHeaderColumns(type.includes('_history') ? type : type + '_history');
  191. if (column && column.length < 1) {
  192. const arr = type.split('_');
  193. columns.value = getTableHeaderColumns(arr[0] + '_history');
  194. } else {
  195. columns.value = column;
  196. }
  197. if (props.showHistoryCurve) {
  198. const arr = type.split('_');
  199. // 没错,又是安全监控。安全监控的单位无法一次定好,所以根据返回的数据协定单位
  200. if (props.deviceType.startsWith('safetymonitor')) {
  201. chartsColumns.value = getTableHeaderColumns(arr[0] + '_chart').map((e) => {
  202. const unit = get(selectedOption.value, 'readData.unit', e.unit);
  203. return {
  204. ...e,
  205. unit: unit,
  206. seriesName: unit,
  207. };
  208. });
  209. } else {
  210. chartsColumns.value = getTableHeaderColumns(arr[0] + '_chart');
  211. }
  212. }
  213. setColumns(columns.value);
  214. });
  215. // 是否显示历史曲线,在devices_shows_history_curve字典里可以配置哪些设备类型需要显示曲线
  216. // 字典内的字段可以是前缀,例如fanlocal之于fanlocal_normal
  217. // 安全监控设备需要更多的配置,除去配置safetymonitor,还需要配置哪些安全监控设备需要曲线
  218. // 因此可以配置例如A1001的dataTypeName代码(可以查看真实数据参考)
  219. const historyCurveDicts = getDictItemsByCode('devices_shows_history_curve');
  220. const findDict = (str) => historyCurveDicts.some(({ value }) => str.startsWith(value));
  221. const showCurve = computed(() => {
  222. if (!props.showHistoryCurve) return false;
  223. const dt = props.deviceType; // 依赖项
  224. if (!findDict(dt)) return false;
  225. if (!dt.startsWith('safetymonitor')) return true;
  226. // 和字典的设备类型匹配后,如果是安全监控设备,需要额外的匹配安全监控类型
  227. const dtns = get(selectedOption.value, 'readData.dataTypeName', ''); // 依赖项
  228. return findDict(dtns);
  229. });
  230. const tableScroll = computed(() => {
  231. if (props.scroll.y && showCurve.value) return { y: props.scroll.y - 450 };
  232. if (props.scroll.y) return { y: props.scroll.y - 100 };
  233. return {};
  234. });
  235. // watch(stationType, (type) => {
  236. // if (type) {
  237. // nextTick(() => {
  238. // getDataSource();
  239. // });
  240. // }
  241. // });
  242. watch(
  243. () => props.deviceId,
  244. async () => {
  245. await getForm().setFieldsValue({});
  246. await getDeviceList();
  247. }
  248. );
  249. /** 获取可供查询历史数据的设备列表 */
  250. async function getDeviceList() {
  251. // if (props.deviceType.split('_')[1] && props.deviceType.split('_')[1] === 'history') return;
  252. let result;
  253. let response;
  254. if (props.onlyBounedDevices) {
  255. response = await getDeviceListApi({
  256. systemID: props.sysId,
  257. devicetype: 'sys',
  258. pageSize: 10000,
  259. }).then(({ msgTxt }) => {
  260. return { msgTxt: msgTxt.filter((e) => e.type === props.deviceType) };
  261. });
  262. } else if (props.sysId) {
  263. response = await getDeviceListApi({
  264. sysId: props.sysId,
  265. devicetype: props.deviceType.startsWith('vehicle') ? 'location_normal' : props.deviceType,
  266. pageSize: 10000,
  267. });
  268. } else if (props.deviceListApi) {
  269. response = await props.deviceListApi();
  270. } else {
  271. response = await getDeviceListApi({ devicetype: props.deviceType, pageSize: 10000 });
  272. }
  273. // 处理不同格式的数据
  274. if (response['records'] && response['records'].length > 0) {
  275. result = response['records'];
  276. } else if (response['msgTxt'] && response['msgTxt'][0] && response['msgTxt'][0]['datalist']) {
  277. result = response['msgTxt'][0]['datalist'];
  278. }
  279. if (response['msgTxt'] && response['msgTxt'][0]) {
  280. deviceTypeName.value = response['msgTxt'][0]['typeName'];
  281. deviceType.value = response['msgTxt'][0]['type'];
  282. }
  283. if (result) {
  284. deviceOptions.value = [];
  285. deviceOptions.value = result.map((item, index) => {
  286. return {
  287. label: item['strinstallpos'],
  288. value: item['id'] || item['deviceID'],
  289. strtype: item['strtype'] || item['deviceType'],
  290. strinstallpos: item['strinstallpos'],
  291. devicekind: item['devicekind'],
  292. stationtype: item['stationtype'],
  293. readData: item['readData'],
  294. };
  295. });
  296. stationType.value = deviceOptions.value[0]['stationtype'];
  297. if (props.deviceType.startsWith('vehicle')) {
  298. historyType.value = 'vehicle';
  299. } else {
  300. historyType.value = deviceOptions.value[0]['strtype'] || deviceOptions.value[0]['devicekind'];
  301. }
  302. }
  303. if (VENT_PARAM.historyIsMultiple) {
  304. await getForm().setFieldsValue({
  305. gdeviceids: [props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : ''],
  306. });
  307. await getForm().updateSchema({
  308. field: 'gdeviceids',
  309. componentProps: {
  310. mode: 'multiple',
  311. maxTagCount: 'responsive',
  312. },
  313. });
  314. } else {
  315. await getForm().setFieldsValue({
  316. gdeviceids: props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '',
  317. });
  318. await getForm().updateSchema({
  319. field: 'gdeviceids',
  320. });
  321. }
  322. }
  323. function resetFormParam() {
  324. const formData = getForm().getFieldsValue();
  325. const pagination = getPaginationRef();
  326. formData['pageNo'] = pagination['current'];
  327. formData['pageSize'] = pagination['pageSize'];
  328. formData['column'] = 'createTime';
  329. if (stationType.value !== 'redis' && deviceOptions.value[0]) {
  330. formData['strtype'] = deviceTypeStr.value
  331. ? deviceTypeStr.value
  332. : deviceOptions.value[0]['strtype']
  333. ? deviceOptions.value[0]['strtype']
  334. : props.deviceType + '*';
  335. if (props.sysId) {
  336. formData['sysId'] = props.sysId;
  337. }
  338. return formData;
  339. } else {
  340. const params = {
  341. pageNum: pagination['current'],
  342. pageSize: pagination['pageSize'],
  343. column: pagination['createTime'],
  344. startTime: formData['ttime_begin'],
  345. endTime: formData['ttime_end'],
  346. deviceId: formData['gdeviceids'],
  347. strtype: props.deviceType + '*',
  348. sysId: props.sysId,
  349. interval: intervalMap.get(formData['skip']) ? intervalMap.get(formData['skip']) : '1h',
  350. isEmployee: props.deviceType.startsWith('vehicle') ? false : true,
  351. };
  352. return params;
  353. }
  354. }
  355. async function getDataSource() {
  356. dataSource.value = [];
  357. setLoading(true);
  358. const params = await resetFormParam();
  359. if (stationType.value !== 'redis') {
  360. const result = await defHttp.get({ url: '/safety/ventanalyMonitorData/listdays', params: params });
  361. setPagination({ total: Math.abs(result['datalist']['total']) || 0 });
  362. if (result['datalist']['records'].length > 0) {
  363. dataSource.value = result['datalist']['records'].map((item: any) => {
  364. return Object.assign(item, item['readData']);
  365. });
  366. } else {
  367. dataSource.value = [];
  368. }
  369. } else {
  370. const result = await defHttp.post({ url: '/monitor/history/getHistoryData', params: params });
  371. setPagination({ total: Math.abs(result['total']) || 0 });
  372. dataSource.value = result['records'] || [];
  373. }
  374. setLoading(false);
  375. }
  376. // 列表页面公共参数、方法
  377. const { tableContext, onExportXls, onExportXlsPost } = useListPage({
  378. tableProps: {
  379. // api: list,
  380. columns: props.columnsType ? columns : (props.columns as any[]),
  381. canResize: true,
  382. showTableSetting: false,
  383. showActionColumn: false,
  384. bordered: false,
  385. size: 'small',
  386. showIndexColumn: true,
  387. tableLayout: 'auto',
  388. formConfig: {
  389. labelAlign: 'left',
  390. labelWidth: 80,
  391. showAdvancedButton: false,
  392. showSubmitButton: false,
  393. showResetButton: false,
  394. baseColProps: {
  395. xs: 24,
  396. sm: 24,
  397. md: 24,
  398. lg: 9,
  399. xl: 7,
  400. xxl: 4,
  401. },
  402. schemas:
  403. props.formSchemas.length > 0
  404. ? props.formSchemas
  405. : [
  406. {
  407. field: 'ttime_begin',
  408. label: '开始时间',
  409. component: 'DatePicker',
  410. defaultValue: dayjs().startOf('date'),
  411. required: true,
  412. componentProps: {
  413. showTime: true,
  414. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  415. getPopupContainer: getAutoScrollContainer,
  416. },
  417. colProps: {
  418. span: 4,
  419. },
  420. },
  421. {
  422. field: 'ttime_end',
  423. label: '结束时间',
  424. component: 'DatePicker',
  425. defaultValue: dayjs(),
  426. required: true,
  427. componentProps: {
  428. showTime: true,
  429. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  430. getPopupContainer: getAutoScrollContainer,
  431. },
  432. colProps: {
  433. span: 4,
  434. },
  435. },
  436. {
  437. label: computed(() => `${deviceKide.value.startsWith('location') ? '查询人员' : '查询设备'}`),
  438. field: 'gdeviceids',
  439. component: 'Select',
  440. required: true,
  441. componentProps: {
  442. showSearch: true,
  443. filterOption: (input: string, option: any) => {
  444. return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  445. },
  446. options: deviceOptions,
  447. onChange: (e, option) => {
  448. if (option && (option['strinstallpos'] || option['strtype'] || option['devicekind'])) {
  449. historyType.value = option['strtype'] || option['devicekind'];
  450. }
  451. if (option['strtype']) {
  452. deviceTypeStr.value = option['strtype'];
  453. }
  454. stationType.value = option['stationtype'];
  455. nextTick(async () => {
  456. await getDataSource();
  457. });
  458. },
  459. },
  460. colProps: {
  461. span: 5,
  462. },
  463. },
  464. {
  465. label: '间隔时间',
  466. field: 'skip',
  467. component: 'Select',
  468. defaultValue: '8',
  469. componentProps: {
  470. options: [
  471. {
  472. label: '1秒',
  473. value: '1',
  474. },
  475. {
  476. label: '5秒',
  477. value: '2',
  478. },
  479. {
  480. label: '10秒',
  481. value: '3',
  482. },
  483. {
  484. label: '30秒',
  485. value: '4',
  486. },
  487. {
  488. label: '1分钟',
  489. value: '5',
  490. },
  491. {
  492. label: '10分钟',
  493. value: '6',
  494. },
  495. {
  496. label: '30分钟',
  497. value: '7',
  498. },
  499. {
  500. label: '1小时',
  501. value: '8',
  502. },
  503. {
  504. label: '1天',
  505. value: '9',
  506. },
  507. ],
  508. },
  509. colProps: {
  510. span: 3,
  511. },
  512. },
  513. ],
  514. // fieldMapToTime: [['tickectDate', ['ttime_begin', 'ttime_end'], '']],
  515. },
  516. // fetchSetting: {
  517. // listField: 'datalist',
  518. // totalField: 'datalist.total',
  519. // },
  520. pagination: {
  521. current: 1,
  522. pageSize: 10,
  523. pageSizeOptions: ['10', '30', '50', '100'],
  524. showQuickJumper: false,
  525. },
  526. beforeFetch() {
  527. const newParams = { ...resetFormParam() };
  528. return newParams;
  529. },
  530. // afterFetch(result) {
  531. // const resultItems = result['records'];
  532. // resultItems.map((item) => {
  533. // Object.assign(item, item['readData']);
  534. // });
  535. // console.log('result---------------->', result);
  536. // return resultItems;
  537. // },
  538. },
  539. exportConfig: {
  540. name: '设备历史列表',
  541. url: getExportXlsUrl,
  542. },
  543. });
  544. //注册table数据
  545. const [registerTable, { reload, setLoading, getForm, setColumns, getPaginationRef, setPagination }] = tableContext;
  546. function onExportXlsFn() {
  547. const params = resetFormParam();
  548. // 判断时间间隔和查询时间区间,数据量下载大时进行提示
  549. if (stationType.value !== 'redis') {
  550. return onExportXls(params);
  551. } else {
  552. return onExportXlsPost(params);
  553. }
  554. }
  555. watchEffect(() => {
  556. if (historyTable.value && dataSource) {
  557. const data = dataSource.value || [];
  558. emit('change', data);
  559. }
  560. });
  561. onMounted(async () => {
  562. await getDeviceList();
  563. if (deviceOptions.value[0]) {
  564. nextTick(async () => {
  565. await getDataSource();
  566. });
  567. }
  568. watch([() => getPaginationRef()['current'], () => getPaginationRef()['pageSize']], async () => {
  569. if (deviceOptions.value[0]) {
  570. if (deviceOptions.value[0]) {
  571. await getDataSource();
  572. }
  573. }
  574. });
  575. });
  576. defineExpose({ setLoading });
  577. </script>
  578. <style scoped lang="less">
  579. @import '/@/design/theme.less';
  580. :deep(.@{ventSpace}-table-body) {
  581. height: auto !important;
  582. }
  583. :deep(.zxm-picker) {
  584. height: 30px !important;
  585. }
  586. .history-table {
  587. width: 100%;
  588. :deep(.jeecg-basic-table-form-container) {
  589. .@{ventSpace}-form {
  590. padding: 0 !important;
  591. border: none !important;
  592. margin-bottom: 0 !important;
  593. .@{ventSpace}-picker,
  594. .@{ventSpace}-select-selector {
  595. width: 100% !important;
  596. height: 100%;
  597. background: #00000017;
  598. border: 1px solid #b7b7b7;
  599. input,
  600. .@{ventSpace}-select-selection-item,
  601. .@{ventSpace}-picker-suffix {
  602. color: #fff;
  603. }
  604. .@{ventSpace}-select-selection-placeholder {
  605. color: #ffffffaa;
  606. }
  607. }
  608. }
  609. .@{ventSpace}-table-title {
  610. min-height: 0 !important;
  611. }
  612. }
  613. .pagination-box {
  614. display: flex;
  615. justify-content: flex-end;
  616. align-items: center;
  617. .page-num {
  618. border: 1px solid #0090d8;
  619. padding: 4px 8px;
  620. margin-right: 5px;
  621. color: #0090d8;
  622. }
  623. .btn {
  624. margin-right: 10px;
  625. }
  626. }
  627. }
  628. </style>