HistoryTable.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <template>
  2. <div class="history-table" v-if="loading">
  3. <BasicTable ref="historyTable" @register="registerTable" :data-source="dataSource" :scroll="tableScroll">
  4. <template #form-submitBefore>
  5. <a-button type="primary" preIcon="ant-design:search-outlined" @click="getDataSource">查询</a-button>
  6. </template>
  7. </BasicTable>
  8. </div>
  9. </template>
  10. <script lang="ts" setup>
  11. //ts语法
  12. import { watchEffect, ref, watch, defineExpose, inject, nextTick, onMounted, computed } from 'vue';
  13. import { FormSchema } from '/@/components/Form/index';
  14. import { BasicTable } from '/@/components/Table';
  15. import { useListPage } from '/@/hooks/system/useListPage';
  16. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  17. import { defHttp } from '/@/utils/http/axios';
  18. import dayjs from 'dayjs';
  19. import { getAutoScrollContainer } from '/@/utils/common/compUtils';
  20. import { render } from '/@/utils/common/renderUtils';
  21. import { useMethods } from '/@/hooks/system/useMethods';
  22. import { getDeviceList, getHistoryList } from './history.api';
  23. import { getDictItemsByCode } from '/@/utils/dict';
  24. import { get } from 'lodash-es';
  25. const globalConfig = inject('globalConfig');
  26. const props = defineProps({
  27. columnsType: {
  28. type: String,
  29. },
  30. columns: {
  31. type: Array,
  32. // required: true,
  33. default: () => [],
  34. },
  35. deviceType: {
  36. type: String,
  37. required: true,
  38. },
  39. deviceListApi: {
  40. type: Function,
  41. },
  42. deviceArr: {
  43. type: Array,
  44. // required: true,
  45. default: () => [],
  46. },
  47. designScope: {
  48. type: String,
  49. },
  50. sysId: {
  51. type: String,
  52. },
  53. deviceId: {
  54. type: String,
  55. },
  56. scroll: {
  57. type: Object,
  58. default: { y: 0 },
  59. },
  60. formSchemas: {
  61. type: Array<FormSchema>,
  62. default: () => [],
  63. },
  64. /** 仅展示已绑定设备,选择是则从系统中获取sysId下已绑定设备。仅能查询到已绑定设备的历史数据 */
  65. onlyBounedDevices: {
  66. type: Boolean,
  67. default: false,
  68. },
  69. showHistoryCurve: {
  70. type: Boolean,
  71. default: false,
  72. },
  73. });
  74. const getDeviceListApi = (params) => defHttp.post({ url: '/monitor/device', params });
  75. const historyTable = ref();
  76. const loading = ref(false);
  77. const stationType = ref('plc1');
  78. const dataSource = ref([]);
  79. const emit = defineEmits(['change']);
  80. const historyType = ref('');
  81. const deviceKide = ref('');
  82. const columns = ref([]);
  83. let deviceOptions = ref([]);
  84. const deviceTypeStr = ref('');
  85. const deviceTypeName = ref('');
  86. const deviceType = ref('');
  87. loading.value = true;
  88. const selectedOption = computed<Record<string, any> | undefined>(() => {
  89. let idval: string | undefined = getForm()?.getFieldsValue()?.gdeviceids;
  90. if (VENT_PARAM.historyIsMultiple && idval) {
  91. const arr = idval.split(',');
  92. idval = arr[arr.length - 1];
  93. }
  94. return deviceOptions.value.find((e: any) => {
  95. return e.value === idval;
  96. });
  97. });
  98. watch(
  99. () => {
  100. return props.columnsType;
  101. },
  102. async (newVal) => {
  103. if (!newVal) return;
  104. deviceKide.value = newVal;
  105. if (historyTable.value) {
  106. getForm().resetFields();
  107. // getForm().updateSchema();
  108. // getForm();
  109. }
  110. dataSource.value = [];
  111. // const column = getTableHeaderColumns(newVal.includes('_history') ? newVal : newVal + '_history');
  112. // if (column && column.length < 1) {
  113. // const arr = newVal.split('_');
  114. // console.log('历史记录列表表头------------>', arr[0] + '_monitor');
  115. // columns.value = getTableHeaderColumns(arr[0] + '_history');
  116. // if (columns.value.length < 1) {
  117. // if (historyType.value) {
  118. // columns.value = getTableHeaderColumns(historyType.value + '_history');
  119. // }
  120. // }
  121. // } else {
  122. // columns.value = column;
  123. // }
  124. await getDeviceList();
  125. nextTick(() => {
  126. getDataSource();
  127. });
  128. if (historyTable.value) reload();
  129. },
  130. {
  131. immediate: true,
  132. }
  133. );
  134. watch(historyType, (type) => {
  135. if (!type) return;
  136. // if (historyTable.value) getForm().resetFields()
  137. const column = getTableHeaderColumns(type.includes('_history') ? type : type + '_history');
  138. if (column && column.length < 1) {
  139. const arr = type.split('_');
  140. columns.value = getTableHeaderColumns(arr[0] + '_history');
  141. } else {
  142. columns.value = column;
  143. }
  144. setColumns(columns.value);
  145. });
  146. // 是否显示历史曲线,在devices_shows_history_curve字典里可以配置哪些设备类型需要显示曲线
  147. // 字典内的字段可以是前缀,例如fanlocal之于fanlocal_normal
  148. // 安全监控设备需要更多的配置,除去配置safetymonitor,还需要配置哪些安全监控设备需要曲线
  149. // 因此可以配置例如A1001的dataTypeName代码(可以查看真实数据参考)
  150. function calcShowCurveValue() {
  151. const historyCurveDicts = getDictItemsByCode('devices_shows_history_curve') || [];
  152. const findDict = (str) => historyCurveDicts.some(({ value }) => str.startsWith(value));
  153. if (!props.showHistoryCurve) return false;
  154. const dt = props.deviceType; // 依赖项
  155. if (!findDict(dt)) return false;
  156. if (!dt.startsWith('safetymonitor')) return true;
  157. // 和字典的设备类型匹配后,如果是安全监控设备,需要额外的匹配安全监控类型
  158. const dtns = get(selectedOption.value, 'readData.dataTypeName', ''); // 依赖项
  159. return findDict(dtns);
  160. }
  161. const tableScroll = computed(() => {
  162. if (props.scroll.y) return { y: props.scroll.y - 100 };
  163. return {};
  164. });
  165. // watch(stationType, (type) => {
  166. // if (type) {
  167. // nextTick(() => {
  168. // getDataSource();
  169. // });
  170. // }
  171. // });
  172. watch(
  173. () => props.deviceId,
  174. async () => {
  175. await getForm().setFieldsValue({});
  176. await getDeviceList();
  177. }
  178. );
  179. /** 获取可供查询历史数据的设备列表 */
  180. async function getDeviceList() {
  181. // if (props.deviceType.split('_')[1] && props.deviceType.split('_')[1] === 'history') return;
  182. let result;
  183. let response;
  184. if (props.onlyBounedDevices) {
  185. response = await getDeviceListApi({
  186. systemID: props.sysId,
  187. devicetype: 'sys',
  188. }).then(({ msgTxt }) => {
  189. return { msgTxt: msgTxt.filter((e) => e.type === props.deviceType) };
  190. });
  191. } else if (props.sysId) {
  192. response = await getDeviceListApi({
  193. sysId: props.sysId,
  194. devicetype: props.deviceType.startsWith('vehicle') ? 'location_normal' : props.deviceType,
  195. pageSize: 10000,
  196. });
  197. } else if (props.deviceListApi) {
  198. response = await props.deviceListApi();
  199. } else {
  200. response = await getDeviceListApi({ devicetype: props.deviceType, pageSize: 10000 });
  201. }
  202. // 处理不同格式的数据
  203. if (response['records'] && response['records'].length > 0) {
  204. result = response['records'];
  205. } else if (response['msgTxt'] && response['msgTxt'][0] && response['msgTxt'][0]['datalist']) {
  206. result = response['msgTxt'][0]['datalist'];
  207. }
  208. if (response['msgTxt'] && response['msgTxt'][0]) {
  209. deviceTypeName.value = response['msgTxt'][0]['typeName'];
  210. deviceType.value = response['msgTxt'][0]['type'];
  211. }
  212. if (result) {
  213. deviceOptions.value = [];
  214. deviceOptions.value = result.map((item, index) => {
  215. return {
  216. label: item['strinstallpos'],
  217. value: item['id'] || item['deviceID'],
  218. strtype: item['strtype'] || item['deviceType'],
  219. strinstallpos: item['strinstallpos'],
  220. devicekind: item['devicekind'],
  221. stationtype: item['stationtype'],
  222. readData: item['readData'],
  223. };
  224. });
  225. stationType.value = deviceOptions.value[0]['stationtype'];
  226. if (props.deviceType.startsWith('vehicle')) {
  227. historyType.value = 'vehicle';
  228. } else {
  229. historyType.value = deviceOptions.value[0]['strtype'] || deviceOptions.value[0]['devicekind'];
  230. }
  231. }
  232. if (VENT_PARAM.historyIsMultiple) {
  233. await getForm().setFieldsValue({
  234. gdeviceids: [props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : ''],
  235. });
  236. await getForm().updateSchema({
  237. field: 'gdeviceids',
  238. componentProps: {
  239. mode: 'multiple',
  240. maxTagCount: 'responsive',
  241. },
  242. });
  243. } else {
  244. await getForm().setFieldsValue({
  245. gdeviceids: props.deviceId ? props.deviceId : deviceOptions.value[0] ? deviceOptions.value[0]['value'] : '',
  246. });
  247. await getForm().updateSchema({
  248. field: 'gdeviceids',
  249. });
  250. }
  251. }
  252. function resetFormParam() {
  253. const formData = getForm().getFieldsValue();
  254. const pagination = getPaginationRef();
  255. formData['pageNo'] = pagination['current'];
  256. formData['pageSize'] = pagination['pageSize'];
  257. formData['column'] = 'createTime';
  258. const params = {
  259. pageNo: pagination['current'],
  260. pageSize: pagination['pageSize'],
  261. column: pagination['createTime'],
  262. ttime_begin: formData['ttime_begin'],
  263. ttime_end: formData['ttime_end'],
  264. deviceId: formData['gdeviceids'],
  265. };
  266. return params;
  267. }
  268. async function getDataSource() {
  269. dataSource.value = [];
  270. setLoading(true);
  271. const params = await resetFormParam();
  272. const result = await getHistoryList(params);
  273. setPagination({ total: Math.abs(result['datalist']['total']) || 0 });
  274. if (result['datalist']['records'].length > 0) {
  275. dataSource.value = result['datalist']['records'].map((item: any) => {
  276. return Object.assign(item, item['readData']);
  277. });
  278. } else {
  279. dataSource.value = [];
  280. }
  281. setLoading(false);
  282. }
  283. // 列表页面公共参数、方法
  284. const { tableContext, onExportXls, onExportXlsPost } = useListPage({
  285. tableProps: {
  286. // api: list,
  287. columns: props.columnsType ? columns : (props.columns as any[]),
  288. canResize: true,
  289. showTableSetting: false,
  290. showActionColumn: false,
  291. bordered: false,
  292. size: 'small',
  293. showIndexColumn: true,
  294. tableLayout: 'auto',
  295. formConfig: {
  296. labelAlign: 'left',
  297. labelWidth: 80,
  298. showAdvancedButton: false,
  299. showSubmitButton: false,
  300. showResetButton: false,
  301. baseColProps: {
  302. xs: 24,
  303. sm: 24,
  304. md: 24,
  305. lg: 9,
  306. xl: 7,
  307. xxl: 4,
  308. },
  309. schemas:
  310. props.formSchemas.length > 0
  311. ? props.formSchemas
  312. : [
  313. {
  314. field: 'ttime_begin',
  315. label: '开始时间',
  316. component: 'DatePicker',
  317. defaultValue: dayjs().startOf('date'),
  318. required: true,
  319. componentProps: {
  320. showTime: true,
  321. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  322. getPopupContainer: getAutoScrollContainer,
  323. },
  324. colProps: {
  325. span: 5,
  326. },
  327. },
  328. {
  329. field: 'ttime_end',
  330. label: '结束时间',
  331. component: 'DatePicker',
  332. defaultValue: dayjs(),
  333. required: true,
  334. componentProps: {
  335. showTime: true,
  336. valueFormat: 'YYYY-MM-DD HH:mm:ss',
  337. getPopupContainer: getAutoScrollContainer,
  338. },
  339. colProps: {
  340. span: 5,
  341. },
  342. },
  343. {
  344. label: '查询设备',
  345. field: 'gdeviceids',
  346. component: 'Select',
  347. required: true,
  348. componentProps: {
  349. showSearch: true,
  350. filterOption: (input: string, option: any) => {
  351. return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  352. },
  353. options: deviceOptions,
  354. onChange: (e, option) => {
  355. if (option && (option['strinstallpos'] || option['strtype'] || option['devicekind'])) {
  356. historyType.value = option['strtype'] || option['devicekind'];
  357. }
  358. if (option['strtype']) {
  359. deviceTypeStr.value = option['strtype'];
  360. }
  361. stationType.value = option['stationtype'];
  362. nextTick(() => {
  363. getDataSource();
  364. });
  365. },
  366. },
  367. colProps: {
  368. span: 6,
  369. },
  370. },
  371. ],
  372. // fieldMapToTime: [['tickectDate', ['ttime_begin', 'ttime_end'], '']],
  373. },
  374. // fetchSetting: {
  375. // listField: 'datalist',
  376. // totalField: 'datalist.total',
  377. // },
  378. pagination: {
  379. current: 1,
  380. pageSize: 20,
  381. showQuickJumper: false,
  382. showSizeChanger: false,
  383. },
  384. beforeFetch() {
  385. const newParams = { ...resetFormParam() };
  386. return newParams;
  387. },
  388. },
  389. });
  390. //注册table数据
  391. const [registerTable, { reload, setLoading, getForm, setColumns, getPaginationRef, setPagination }] = tableContext;
  392. watchEffect(() => {
  393. if (historyTable.value && dataSource) {
  394. const data = dataSource.value || [];
  395. emit('change', data);
  396. }
  397. });
  398. onMounted(async () => {
  399. await getDeviceList();
  400. if (deviceOptions.value[0]) {
  401. nextTick(async () => {
  402. await getDataSource();
  403. });
  404. }
  405. watch([() => getPaginationRef()['current'], () => getPaginationRef()['pageSize']], async () => {
  406. if (deviceOptions.value[0]) {
  407. if (deviceOptions.value[0]) {
  408. await getDataSource();
  409. }
  410. }
  411. });
  412. });
  413. defineExpose({ setLoading });
  414. </script>
  415. <style scoped lang="less">
  416. @import '/@/design/theme.less';
  417. :deep(.@{ventSpace}-table-body) {
  418. height: auto !important;
  419. }
  420. :deep(.zxm-picker) {
  421. height: 30px !important;
  422. }
  423. .history-table {
  424. width: 100%;
  425. :deep(.jeecg-basic-table-form-container) {
  426. .@{ventSpace}-form {
  427. padding: 0 !important;
  428. border: none !important;
  429. margin-bottom: 0 !important;
  430. .@{ventSpace}-picker,
  431. .@{ventSpace}-select-selector {
  432. width: 100% !important;
  433. height: 100%;
  434. background: #00000017;
  435. border: 1px solid #b7b7b7;
  436. input,
  437. .@{ventSpace}-select-selection-item,
  438. .@{ventSpace}-picker-suffix {
  439. color: #fff;
  440. }
  441. .@{ventSpace}-select-selection-placeholder {
  442. color: #ffffffaa;
  443. }
  444. }
  445. }
  446. .@{ventSpace}-table-title {
  447. min-height: 0 !important;
  448. }
  449. }
  450. .pagination-box {
  451. display: flex;
  452. justify-content: flex-end;
  453. align-items: center;
  454. .page-num {
  455. border: 1px solid #0090d8;
  456. padding: 4px 8px;
  457. margin-right: 5px;
  458. color: #0090d8;
  459. }
  460. .btn {
  461. margin-right: 10px;
  462. }
  463. }
  464. }
  465. .history-chart {
  466. background-color: #0090d822;
  467. margin: 0 10px;
  468. }
  469. </style>