HistoryTable.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <template>
  2. <div class="history-table">
  3. <BasicTable ref="historyTable" @register="register" :data-source="data" :scroll="scroll" @change="search">
  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>
  12. <template #form-submitBefore>
  13. <a-button type="primary" preIcon="ant-design:search-outlined" @click="search">查询</a-button>
  14. <a-button type="primary" preIcon="ant-design:export-outlined" @click="exportXls"> 导出</a-button>
  15. </template>
  16. </BasicTable>
  17. </div>
  18. </template>
  19. <script lang="ts" setup>
  20. // 场景类历史数据公共组件!
  21. // 用于服务场景类历史数据业务,这类数据通常由一条数据返回多个子设备的信息,例如:{ forcFan1Temp, forcFan2Temp };
  22. // 而此组件可以将这些数据分类,例如:表格只有 温度 一列,但可以根据所选的子设备展示不同的数据;
  23. // 综上所述,此组件在基础的历史数据组件上添加了子设备下拉框(由字典驱动),用户选择子设备后表头将动态调整以适配数据展示;
  24. //
  25. // 使用方法如下:
  26. // 设有历史数据2条,[{ name, forcFan1Temp, forcFan2Temp }, { name, forcFan1Temp, forcFan2Temp }]。
  27. //
  28. // 1、配置设备字段(参考公司端综合设备管理-设备字段管理)
  29. // 以压风机为例,设压风机设备的历史数据编码为forcFan_history。
  30. // 那么字段code中需要把所有字段悉数配置,例子如下:
  31. // 显示字段 字段code
  32. // 温度 forcFanTemp
  33. // 安装位置 name
  34. //
  35. // 2、配置数据字典(参考系统管理-数据字典),为上述设备配置子设备
  36. // 同以压风机为例,设压风机子设备字典编码为forcFan_dict,且已经新增到系统中。
  37. // 则字典配置的例子如下:
  38. // 名称 数据值
  39. // 压风机1 forcFan1
  40. // 压风机2 forcFan2
  41. //
  42. // 3、运维人员应配合前端开发人员,使用指定的编码配置内容。
  43. // 同以压风机为例,需使用device-code(forcFan)、dict-code(forcFan_dict)。
  44. //
  45. // 4、其他内容说明
  46. // 同以压风机为例,当子设备没有数据时,不进行过滤,此时展示的数据是:
  47. // 温度 安装位置
  48. // 取forcFanTemp 取name
  49. // 同以压风机为例,当子设备选择压风机1时,过滤压风机1相关的表头,此时展示的数据是:
  50. // 温度 安装位置
  51. // 取forcFan1Temp 取name
  52. // 当设备字段不含数据字典关键词(forcFan)时不做处理,当设备字段包含关键词但已指定编号(即字段包含数字)时不做处理
  53. import { computed, onMounted, ref, shallowRef } from 'vue';
  54. import { BasicColumn, PaginationProps, BasicTable } from '/@/components/Table';
  55. import { getTableHeaderColumns } from '/@/hooks/web/useWebColumns';
  56. import { getDefaultSchemas } from './history.data';
  57. import { adaptFormData, getDeviceList, getExportUrl, list } from './history.api';
  58. import { useListPage } from '/@/hooks/system/useListPage';
  59. import { initDictOptions } from '/@/utils/dict';
  60. const props = withDefaults(
  61. defineProps<{
  62. /** 表格项配置,默认由deviceCode获取且联动dictCode,可以覆写,覆写后将不支持联动,参考BaiscTable */
  63. // columns?: BasicColumn[];
  64. /** 表格操作项配置,默认为空,可以覆写 */
  65. // actionColumns?: BasicColumn;
  66. /** 查询表单项配置,默认联动dictCode,可以覆写,覆写后将不支持联动,提供formProps时此项无效,参考BaiscTable */
  67. // schemas?: FormSchema[];
  68. /** 表格分页配置,可以覆写,参考BaiscTable */
  69. pagination?: PaginationProps;
  70. /** 设备编码,该编码用于请求设备信息,示例:forcFan */
  71. deviceCode: string;
  72. /** 字典编码,该编码用于从字典配置中读出设备项,示例:forcFan_dict */
  73. dictCode: string;
  74. /** 字段编码,该编码用于从设备字段配置中读取默认表头信息,示例:forcFan_history */
  75. columnsCode: string;
  76. scroll: { x: number | true; y: number };
  77. /** 表格配置,参考BaiscTable,该值会与默认的配置进行浅合并,这里提供的任何配置都是优先的 */
  78. // tableProps?: BasicTableProps;
  79. /** 查询表单配置,参考BaiscTable */
  80. // formProps?: FormProps;
  81. }>(),
  82. {
  83. deviceCode: '',
  84. dictCode: '',
  85. pagination: (): PaginationProps => ({
  86. current: 1,
  87. pageSize: 10,
  88. pageSizeOptions: ['10', '30', '50', '100'],
  89. showQuickJumper: false,
  90. }),
  91. }
  92. );
  93. // 创建表格,此表格目前不具备常用功能,需要初始化后使用(props指定表格配置时除外)
  94. let originColumns: BasicColumn[] = [];
  95. const scroll = computed(() => {
  96. return { ...props.scroll, y: props.scroll.y - 100 };
  97. });
  98. const { tableContext, onExportXls, onExportXlsPost } = useListPage({
  99. tableProps: {
  100. columns: [
  101. {
  102. align: 'center',
  103. dataIndex: 'strinstallpos',
  104. defaultHidden: false,
  105. title: '安装位置',
  106. width: 80,
  107. },
  108. ],
  109. formConfig: {
  110. labelAlign: 'left',
  111. labelWidth: 80,
  112. showAdvancedButton: false,
  113. showSubmitButton: false,
  114. showResetButton: false,
  115. actionColOptions: {
  116. xxl: 4,
  117. },
  118. },
  119. canResize: true,
  120. showTableSetting: false,
  121. showActionColumn: false,
  122. bordered: false,
  123. size: 'small',
  124. showIndexColumn: true,
  125. tableLayout: 'auto',
  126. scroll: scroll,
  127. pagination: props.pagination,
  128. },
  129. exportConfig: {
  130. name: '设备历史列表',
  131. url: () => getExportUrl(deviceInfo.value),
  132. },
  133. });
  134. const [register, { getForm, setLoading, getPaginationRef, setPagination, setColumns }] = tableContext;
  135. /**
  136. * 初始化表格,该方法将根据参数设定新的表头、表单,如果提供了自定义的表头、表单配置则不作操作。
  137. *
  138. * 之所以将设备相关的信息作参数传入,是因为这样可以确认依赖关系,即需要有设备信息之后再初始化表格。
  139. *
  140. * @param deviceCodes 获取表头所用的编码,从左到右依次尝试,直到找到第一个有表头信息的为止
  141. * @param deviceOptions 设备下拉框对应的选项
  142. */
  143. function initTable(deviceCodes: string[], deviceOptions: any[], dictOptions: any[]) {
  144. const defaultSchemas = getDefaultSchemas(dictOptions, deviceOptions, () => search());
  145. for (const code of deviceCodes) {
  146. const cols = getTableHeaderColumns(code);
  147. if (cols.length) {
  148. originColumns = cols;
  149. break;
  150. }
  151. }
  152. getForm().setProps({
  153. schemas: defaultSchemas,
  154. });
  155. }
  156. /**
  157. * 更新表头,表头默认情况下需要和子设备联动
  158. * @param prefix 子设备的值,即为表头取数据时字段的前缀
  159. */
  160. function updateColumns(prefix?: string) {
  161. if (!prefix) return setColumns(originColumns);
  162. // 如果有子设备信息,筛选符合规范的表头
  163. const cols = originColumns.map((col) => {
  164. const dataIndex = col.dataIndex as string;
  165. // 获取到子设备编码的前缀及编码,正则例子:forcFan1 => [forcFan1, forcFan, 1]
  166. const [_, pfx] = prefix.match(/([A-Za-z]+)([0-9]+)/) || [];
  167. // 同时,如若已经在前缀后配置了编号则不要更改
  168. const reg = new RegExp(`${pfx}[0-9]`);
  169. if (dataIndex.search(reg) !== -1) return col;
  170. if (dataIndex.includes(pfx)) {
  171. return {
  172. ...col,
  173. dataIndex: dataIndex.replace(pfx, prefix),
  174. };
  175. }
  176. // 默认直接放行
  177. return col;
  178. });
  179. setColumns(cols);
  180. }
  181. // 表格数据相关的字段
  182. const data = shallowRef([]);
  183. /**
  184. * 获取列表的数据
  185. *
  186. * 这些参数确认了依赖关系,即需要有表单数据、设备信息之后再尝试获取表格数据。
  187. *
  188. * @param formData 表格上方的表单数据
  189. * @param deviceCode 设备编码
  190. * @param deviceInfo 设备信息
  191. */
  192. function fetchData(formData: Record<string, unknown>, deviceCode: string, deviceInfo: any) {
  193. setLoading(true);
  194. const pagination = getPaginationRef() as PaginationProps;
  195. return list(deviceCode, deviceInfo, formData, pagination)
  196. .then(({ records, total, current }) => {
  197. setPagination({
  198. current,
  199. total,
  200. });
  201. records.forEach((item) => {
  202. Object.assign(item, item.readData);
  203. });
  204. data.value = records;
  205. })
  206. .finally(() => {
  207. setLoading(false);
  208. });
  209. }
  210. // 设备信息相关的字段
  211. const deviceInfo = ref<Record<string, unknown>>({});
  212. const deviceOptions = ref<Record<string, unknown>[]>([]);
  213. const dictOptions = ref<Record<string, unknown>[]>([]); // 子设备下拉框选项
  214. /**
  215. * 获取设备信息列表,初始化设备信息及设备可选项
  216. */
  217. async function fetchDevice() {
  218. const results = await getDeviceList({ devicetype: props.deviceCode, pageSize: 10000 });
  219. const dicts = await initDictOptions(props.dictCode);
  220. const options = results.map((item) => {
  221. return {
  222. label: item.strinstallpos,
  223. value: item.id || item.deviceID,
  224. deviceType: item.strtype || item.deviceType,
  225. devicekind: item.devicekind,
  226. stationtype: item.stationtype,
  227. };
  228. });
  229. deviceOptions.value = options;
  230. deviceInfo.value = results[0] || {};
  231. dictOptions.value = dicts;
  232. }
  233. /**
  234. * 搜索,核心方法
  235. *
  236. * 该方法获取表单、处理表单数据后尝试获取数据并设置表头
  237. */
  238. async function search() {
  239. const form = getForm();
  240. await form.validate();
  241. const formData = form.getFieldsValue();
  242. const info = deviceOptions.value.find((opt) => {
  243. return opt.value === formData.gdeviceids.split(',')[0];
  244. });
  245. if (!info) return;
  246. deviceInfo.value = info;
  247. const code = (deviceInfo.value.deviceType || props.deviceCode.concat('*')) as string;
  248. await fetchData(formData, code, deviceInfo.value);
  249. updateColumns(formData.deviceNum);
  250. }
  251. /** 导出表格内容为excel */
  252. async function exportXls() {
  253. const form = getForm();
  254. await form.validate();
  255. const formData = form.getFieldsValue();
  256. const pagination = getPaginationRef() as PaginationProps;
  257. const params = adaptFormData(props.deviceCode, deviceInfo.value, formData, pagination);
  258. if (deviceInfo.value.stationtype === 'redis') {
  259. return onExportXlsPost(params);
  260. } else {
  261. return onExportXls(params);
  262. }
  263. }
  264. onMounted(async () => {
  265. await fetchDevice();
  266. // 生成所有需要查询的表头编码,例如 forcFan_auto 对应 forcFan_auto_history、forcFan_history 这两个
  267. const codes: string[] = [];
  268. if (deviceInfo.value && deviceInfo.value.deviceType) {
  269. const arr = (deviceInfo.value.deviceType as string).split('_');
  270. while (arr.length) {
  271. codes.push(arr.join('_').concat('_history'));
  272. arr.pop();
  273. }
  274. }
  275. // 如此,例如deviceType为forcFan_auto, 则查询表头按 forcFan_auto_history、forcFan_history、columnsCode 为顺序
  276. initTable(codes.concat(props.columnsCode), deviceOptions.value, dictOptions.value);
  277. search();
  278. });
  279. </script>
  280. <style scoped lang="less">
  281. @import '/@/design/theme.less';
  282. :deep(.@{ventSpace}-table-body) {
  283. height: auto !important;
  284. }
  285. :deep(.zxm-picker) {
  286. height: 30px !important;
  287. }
  288. :deep(.zxm-table) {
  289. .zxm-table-title {
  290. display: none !important;
  291. }
  292. }
  293. .history-table {
  294. width: 100%;
  295. :deep(.jeecg-basic-table-form-container) {
  296. .@{ventSpace}-form {
  297. padding: 0 !important;
  298. border: none !important;
  299. margin-bottom: 0 !important;
  300. .@{ventSpace}-picker,
  301. .@{ventSpace}-select-selector {
  302. width: 100% !important;
  303. background: #00000017;
  304. border: 1px solid #b7b7b7;
  305. input,
  306. .@{ventSpace}-select-selection-item,
  307. .@{ventSpace}-picker-suffix {
  308. color: #fff;
  309. }
  310. .@{ventSpace}-select-selection-placeholder {
  311. color: #ffffffaa;
  312. }
  313. }
  314. }
  315. .@{ventSpace}-table-title {
  316. min-height: 0 !important;
  317. }
  318. }
  319. .pagination-box {
  320. display: flex;
  321. justify-content: flex-end;
  322. align-items: center;
  323. .page-num {
  324. border: 1px solid #0090d8;
  325. padding: 4px 8px;
  326. margin-right: 5px;
  327. color: #0090d8;
  328. }
  329. .btn {
  330. margin-right: 10px;
  331. }
  332. }
  333. }
  334. </style>