HistoryTable.vue 21 KB

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