HistoryTable.vue 21 KB

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