HistoryTable.vue 11 KB

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