HistoryTable.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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="stationType !== 'redis' ? 'ttime' : 'time'"
  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. setColumns(columns.value);
  201. });
  202. const showCurve = ref(false);
  203. // 是否显示历史曲线,在devices_shows_history_curve字典里可以配置哪些设备类型需要显示曲线
  204. // 字典内的字段可以是前缀,例如fanlocal之于fanlocal_normal
  205. // 安全监控设备需要更多的配置,除去配置safetymonitor,还需要配置哪些安全监控设备需要曲线
  206. // 因此可以配置例如A1001的dataTypeName代码(可以查看真实数据参考)
  207. function calcShowCurveValue() {
  208. const historyCurveDicts = getDictItemsByCode('devices_shows_history_curve') || [];
  209. const findDict = (str) => historyCurveDicts.some(({ value }) => str.startsWith(value));
  210. if (!props.showHistoryCurve) return false;
  211. const dt = props.deviceType; // 依赖项
  212. if (!findDict(dt)) return false;
  213. if (!dt.startsWith('safetymonitor')) return true;
  214. // 和字典的设备类型匹配后,如果是安全监控设备,需要额外的匹配安全监控类型
  215. const dtns = get(selectedOption.value, 'readData.dataTypeName', ''); // 依赖项
  216. return findDict(dtns);
  217. }
  218. function initHistoryCurveColumns() {
  219. if (!props.showHistoryCurve) return;
  220. const arr = historyType.value.split('_');
  221. // 没错,又是安全监控。安全监控的单位无法一次定好,所以根据返回的数据协定单位
  222. if (props.deviceType.startsWith('safetymonitor')) {
  223. chartsColumns.value = getTableHeaderColumns(arr[0] + '_chart').map((e) => {
  224. const unit = get(selectedOption.value, 'readData.unit', e.unit);
  225. return {
  226. ...e,
  227. unit: unit,
  228. seriesName: unit,
  229. };
  230. });
  231. } else {
  232. chartsColumns.value = getTableHeaderColumns(arr[0] + '_chart');
  233. }
  234. }
  235. const tableScroll = computed(() => {
  236. if (props.scroll.y && showCurve.value) return { y: props.scroll.y - 450 };
  237. if (props.scroll.y) return { y: props.scroll.y - 100 };
  238. return {};
  239. });
  240. // watch(stationType, (type) => {
  241. // if (type) {
  242. // nextTick(() => {
  243. // getDataSource();
  244. // });
  245. // }
  246. // });
  247. watch(
  248. () => props.deviceId,
  249. async () => {
  250. await getForm().setFieldsValue({});
  251. await getDeviceList();
  252. }
  253. );
  254. /** 获取可供查询历史数据的设备列表 */
  255. async function getDeviceList() {
  256. // if (props.deviceType.split('_')[1] && props.deviceType.split('_')[1] === 'history') return;
  257. let result;
  258. let response;
  259. if (props.onlyBounedDevices) {
  260. response = await getDeviceListApi({
  261. systemID: props.sysId,
  262. devicetype: 'sys',
  263. }).then(({ msgTxt }) => {
  264. return { msgTxt: msgTxt.filter((e) => e.type === props.deviceType) };
  265. });
  266. } else if (props.sysId) {
  267. response = await getDeviceListApi({
  268. sysId: props.sysId,
  269. devicetype: props.deviceType.startsWith('vehicle') ? 'location_normal' : props.deviceType,
  270. pageSize: 10000,
  271. });
  272. } else if (props.deviceListApi) {
  273. response = await props.deviceListApi();
  274. } else {
  275. response = await getDeviceListApi({ devicetype: props.deviceType, pageSize: 10000 });
  276. }
  277. // 处理不同格式的数据
  278. if (response['records'] && response['records'].length > 0) {
  279. result = response['records'];
  280. } else if (response['msgTxt'] && response['msgTxt'][0] && response['msgTxt'][0]['datalist']) {
  281. result = response['msgTxt'][0]['datalist'];
  282. }
  283. if (response['msgTxt'] && response['msgTxt'][0]) {
  284. deviceTypeName.value = response['msgTxt'][0]['typeName'];
  285. deviceType.value = response['msgTxt'][0]['type'];
  286. }
  287. if (result) {
  288. deviceOptions.value = [];
  289. deviceOptions.value = result.map((item, index) => {
  290. return {
  291. label: item['strinstallpos'],
  292. value: item['id'] || item['deviceID'],
  293. strtype: item['strtype'] || item['deviceType'],
  294. strinstallpos: item['strinstallpos'],
  295. devicekind: item['devicekind'],
  296. stationtype: item['stationtype'],
  297. readData: item['readData'],
  298. };
  299. });
  300. stationType.value = deviceOptions.value[0]['stationtype'];
  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. /** 此处使用nextTick是由于可能表单暂未更新,而下面的方法依赖表单项 */
  307. nextTick(() => {
  308. showCurve.value = calcShowCurveValue();
  309. initHistoryCurveColumns();
  310. });
  311. }
  312. if (VENT_PARAM.historyIsMultiple) {
  313. await getForm().setFieldsValue({
  314. gdeviceids: [props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : ''],
  315. });
  316. await getForm().updateSchema({
  317. field: 'gdeviceids',
  318. componentProps: {
  319. mode: 'multiple',
  320. maxTagCount: 'responsive',
  321. },
  322. });
  323. } else {
  324. await getForm().setFieldsValue({
  325. gdeviceids: props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '',
  326. });
  327. await getForm().updateSchema({
  328. field: 'gdeviceids',
  329. });
  330. }
  331. }
  332. function resetFormParam() {
  333. const formData = getForm().getFieldsValue();
  334. const pagination = getPaginationRef();
  335. formData['pageNo'] = pagination['current'];
  336. formData['pageSize'] = pagination['pageSize'];
  337. formData['column'] = 'createTime';
  338. if (stationType.value !== 'redis' && deviceOptions.value[0]) {
  339. formData['strtype'] = deviceTypeStr.value
  340. ? deviceTypeStr.value
  341. : deviceOptions.value[0]['strtype']
  342. ? deviceOptions.value[0]['strtype']
  343. : props.deviceType + '*';
  344. if (props.sysId) {
  345. formData['sysId'] = props.sysId;
  346. }
  347. return formData;
  348. } else {
  349. const params = {
  350. pageNum: pagination['current'],
  351. pageSize: pagination['pageSize'],
  352. column: pagination['createTime'],
  353. startTime: formData['ttime_begin'],
  354. endTime: formData['ttime_end'],
  355. deviceId: formData['gdeviceids'],
  356. strtype: props.deviceType + '*',
  357. sysId: props.sysId,
  358. interval: intervalMap.get(formData['skip']) ? intervalMap.get(formData['skip']) : '1h',
  359. isEmployee: props.deviceType.startsWith('vehicle') ? false : true,
  360. };
  361. return params;
  362. }
  363. }
  364. async function getDataSource() {
  365. dataSource.value = [];
  366. setLoading(true);
  367. const params = await resetFormParam();
  368. if (stationType.value !== 'redis') {
  369. const result = await defHttp.get({ url: '/safety/ventanalyMonitorData/listdays', params: params });
  370. setPagination({ total: Math.abs(result['datalist']['total']) || 0 });
  371. if (result['datalist']['records'].length > 0) {
  372. dataSource.value = result['datalist']['records'].map((item: any) => {
  373. return Object.assign(item, item['readData']);
  374. });
  375. } else {
  376. dataSource.value = [];
  377. }
  378. } else {
  379. const result = await defHttp.post({ url: '/monitor/history/getHistoryData', params: params });
  380. setPagination({ total: Math.abs(result['total']) || 0 });
  381. dataSource.value = result['records'] || [];
  382. }
  383. setLoading(false);
  384. }
  385. // 列表页面公共参数、方法
  386. const { tableContext, onExportXls, onExportXlsPost } = useListPage({
  387. tableProps: {
  388. // api: list,
  389. columns: props.columnsType ? columns : (props.columns as any[]),
  390. canResize: true,
  391. showTableSetting: false,
  392. showActionColumn: false,
  393. bordered: false,
  394. size: 'small',
  395. showIndexColumn: true,
  396. tableLayout: 'auto',
  397. formConfig: {
  398. labelAlign: 'left',
  399. labelWidth: 80,
  400. showAdvancedButton: false,
  401. showSubmitButton: false,
  402. showResetButton: false,
  403. baseColProps: {
  404. xs: 24,
  405. sm: 24,
  406. md: 24,
  407. lg: 9,
  408. xl: 7,
  409. xxl: 4,
  410. },
  411. schemas:
  412. props.formSchemas.length > 0
  413. ? props.formSchemas
  414. : [
  415. {
  416. field: 'ttime_begin',
  417. label: '开始时间',
  418. component: 'DatePicker',
  419. defaultValue: dayjs().startOf('date'),
  420. required: true,
  421. componentProps: {
  422. showTime: true,
  423. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  424. getPopupContainer: getAutoScrollContainer,
  425. },
  426. colProps: {
  427. span: 4,
  428. },
  429. },
  430. {
  431. field: 'ttime_end',
  432. label: '结束时间',
  433. component: 'DatePicker',
  434. defaultValue: dayjs(),
  435. required: true,
  436. componentProps: {
  437. showTime: true,
  438. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  439. getPopupContainer: getAutoScrollContainer,
  440. },
  441. colProps: {
  442. span: 4,
  443. },
  444. },
  445. {
  446. label: computed(() => `${deviceKide.value.startsWith('location') ? '查询人员' : '查询设备'}`),
  447. field: 'gdeviceids',
  448. component: 'Select',
  449. required: true,
  450. componentProps: {
  451. showSearch: true,
  452. filterOption: (input: string, option: any) => {
  453. return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  454. },
  455. options: deviceOptions,
  456. onChange: (e, option) => {
  457. if (option && (option['strinstallpos'] || option['strtype'] || option['devicekind'])) {
  458. historyType.value = option['strtype'] || option['devicekind'];
  459. }
  460. if (option['strtype']) {
  461. deviceTypeStr.value = option['strtype'];
  462. }
  463. stationType.value = option['stationtype'];
  464. nextTick(() => {
  465. showCurve.value = calcShowCurveValue();
  466. initHistoryCurveColumns();
  467. getDataSource();
  468. });
  469. },
  470. },
  471. colProps: {
  472. span: 5,
  473. },
  474. },
  475. {
  476. label: '间隔时间',
  477. field: 'skip',
  478. component: 'Select',
  479. defaultValue: '8',
  480. componentProps: {
  481. options: [
  482. {
  483. label: '1秒',
  484. value: '1',
  485. },
  486. {
  487. label: '5秒',
  488. value: '2',
  489. },
  490. {
  491. label: '10秒',
  492. value: '3',
  493. },
  494. {
  495. label: '30秒',
  496. value: '4',
  497. },
  498. {
  499. label: '1分钟',
  500. value: '5',
  501. },
  502. {
  503. label: '10分钟',
  504. value: '6',
  505. },
  506. {
  507. label: '30分钟',
  508. value: '7',
  509. },
  510. {
  511. label: '1小时',
  512. value: '8',
  513. },
  514. {
  515. label: '1天',
  516. value: '9',
  517. },
  518. ],
  519. },
  520. colProps: {
  521. span: 3,
  522. },
  523. },
  524. ],
  525. // fieldMapToTime: [['tickectDate', ['ttime_begin', 'ttime_end'], '']],
  526. },
  527. // fetchSetting: {
  528. // listField: 'datalist',
  529. // totalField: 'datalist.total',
  530. // },
  531. pagination: {
  532. current: 1,
  533. pageSize: 10,
  534. pageSizeOptions: ['10', '30', '50', '100'],
  535. showQuickJumper: false,
  536. },
  537. beforeFetch() {
  538. const newParams = { ...resetFormParam() };
  539. return newParams;
  540. },
  541. // afterFetch(result) {
  542. // const resultItems = result['records'];
  543. // resultItems.map((item) => {
  544. // Object.assign(item, item['readData']);
  545. // });
  546. // console.log('result---------------->', result);
  547. // return resultItems;
  548. // },
  549. },
  550. exportConfig: {
  551. name: '设备历史列表',
  552. url: getExportXlsUrl,
  553. },
  554. });
  555. //注册table数据
  556. const [registerTable, { reload, setLoading, getForm, setColumns, getPaginationRef, setPagination }] = tableContext;
  557. function onExportXlsFn() {
  558. const params = resetFormParam();
  559. // 判断时间间隔和查询时间区间,数据量下载大时进行提示
  560. if (stationType.value !== 'redis') {
  561. return onExportXls(params);
  562. } else {
  563. return onExportXlsPost(params);
  564. }
  565. }
  566. watchEffect(() => {
  567. if (historyTable.value && dataSource) {
  568. const data = dataSource.value || [];
  569. emit('change', data);
  570. }
  571. });
  572. onMounted(async () => {
  573. await getDeviceList();
  574. if (deviceOptions.value[0]) {
  575. nextTick(async () => {
  576. await getDataSource();
  577. });
  578. }
  579. watch([() => getPaginationRef()['current'], () => getPaginationRef()['pageSize']], async () => {
  580. if (deviceOptions.value[0]) {
  581. if (deviceOptions.value[0]) {
  582. await getDataSource();
  583. }
  584. }
  585. });
  586. });
  587. defineExpose({ setLoading });
  588. </script>
  589. <style scoped lang="less">
  590. @import '/@/design/theme.less';
  591. :deep(.@{ventSpace}-table-body) {
  592. height: auto !important;
  593. }
  594. :deep(.zxm-picker) {
  595. height: 30px !important;
  596. }
  597. .history-table {
  598. width: 100%;
  599. :deep(.jeecg-basic-table-form-container) {
  600. .@{ventSpace}-form {
  601. padding: 0 !important;
  602. border: none !important;
  603. margin-bottom: 0 !important;
  604. .@{ventSpace}-picker,
  605. .@{ventSpace}-select-selector {
  606. width: 100% !important;
  607. height: 100%;
  608. background: #00000017;
  609. border: 1px solid #b7b7b7;
  610. input,
  611. .@{ventSpace}-select-selection-item,
  612. .@{ventSpace}-picker-suffix {
  613. color: #fff;
  614. }
  615. .@{ventSpace}-select-selection-placeholder {
  616. color: #ffffffaa;
  617. }
  618. }
  619. }
  620. .@{ventSpace}-table-title {
  621. min-height: 0 !important;
  622. }
  623. }
  624. .pagination-box {
  625. display: flex;
  626. justify-content: flex-end;
  627. align-items: center;
  628. .page-num {
  629. border: 1px solid #0090d8;
  630. padding: 4px 8px;
  631. margin-right: 5px;
  632. color: #0090d8;
  633. }
  634. .btn {
  635. margin-right: 10px;
  636. }
  637. }
  638. }
  639. .history-chart {
  640. background-color: #0090d822;
  641. margin: 0 10px;
  642. }
  643. </style>