HistoryData.vue
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<script lang="ts" setup>
import { nextTick, onMounted, onUnmounted, Ref, ref, unref } from 'vue';
import { getDeviceHistoryInfo } from '/@/api/alarm/position';
import { Empty, Spin } from 'ant-design-vue';
import { useECharts } from '/@/hooks/web/useECharts';
import { AggregateDataEnum, selectDeviceAttrSchema } from '/@/views/device/localtion/config.data';
import { useTimePeriodForm } from '/@/views/device/localtion/cpns/TimePeriodForm';
import { defaultSchemas } from '/@/views/device/localtion/cpns/TimePeriodForm/config';
import TimePeriodForm from '/@/views/device/localtion/cpns/TimePeriodForm/TimePeriodForm.vue';
import { SchemaFiled } from '/@/views/report/config/config.data';
import { useGridLayout } from '/@/hooks/component/useGridLayout';
import { ColEx } from '/@/components/Form/src/types';
import { useHistoryData } from '../../hook/useHistoryData';
interface DeviceDetail {
tbDeviceId: string;
deviceProfileId: string;
}
const props = defineProps<{
deviceDetail: DeviceDetail;
attr?: string;
}>();
const chartRef = ref();
const loading = ref(false);
const isNull = ref(false);
const { deviceAttrs, getDeviceKeys, getSearchParams, setChartOptions, getDeviceAttribute } =
useHistoryData();
function hasDeviceAttr() {
if (!unref(deviceAttrs).length) {
return false;
} else {
return true;
}
}
const { setOptions, getInstance } = useECharts(chartRef as Ref<HTMLDivElement>);
const [register, method] = useTimePeriodForm({
schemas: [...defaultSchemas, ...selectDeviceAttrSchema],
baseColProps: useGridLayout(2, 3, 4) as unknown as ColEx,
async submitFunc() {
// 表单验证
await method.validate();
const value = method.getFieldsValue();
const searchParams = getSearchParams(value);
if (!hasDeviceAttr()) return;
// 发送请求
loading.value = true;
const res = await getDeviceHistoryInfo({
...searchParams,
entityId: props.deviceDetail.tbDeviceId,
});
// 判断数据对象是否为空
if (!Object.keys(res).length) {
isNull.value = false;
return;
} else {
isNull.value = true;
}
const selectedKeys = unref(deviceAttrs).find(
(item) => item.identifier === value[SchemaFiled.KEYS]
);
setOptions(setChartOptions(res, selectedKeys));
loading.value = false;
},
});
const getDeviceDataKey = async () => {
try {
await getDeviceAttribute(props.deviceDetail);
if (props.attr) {
method.setFieldsValue({ keys: props.attr });
}
} catch (error) {}
};
const openHistoryPanel = async () => {
await nextTick();
method.updateSchema({
field: 'keys',
componentProps: {
options: unref(deviceAttrs).map((item) => ({ label: item.name, value: item.identifier })),
},
});
method.setFieldsValue({
[SchemaFiled.START_TS]: 1 * 24 * 60 * 60 * 1000,
[SchemaFiled.LIMIT]: 7,
[SchemaFiled.AGG]: AggregateDataEnum.NONE,
});
if (!hasDeviceAttr()) return;
const keys = props.attr ? props.attr : unref(getDeviceKeys).join();
const res = await getDeviceHistoryInfo({
entityId: props.deviceDetail.tbDeviceId,
keys,
startTs: Date.now() - 1 * 24 * 60 * 60 * 1000,
endTs: Date.now(),
agg: AggregateDataEnum.NONE,
limit: 7,
});
// 判断对象是否为空
if (!Object.keys(res).length) {
isNull.value = false;
return;
} else {
isNull.value = true;
}
const selectedKeys = unref(deviceAttrs).find((item) => item.identifier === props.attr);
setOptions(setChartOptions(res, selectedKeys));
};
onMounted(async () => {
await getDeviceDataKey();
await openHistoryPanel();
});
onUnmounted(() => {
getInstance()?.clear();
});
</script>
<template>
<section class="flex flex-col p-4 h-full" style="background-color: #f0f2f5">
<section class="bg-white p-3 mb-4">
<TimePeriodForm @register="register" />
</section>
<section class="bg-white p-3">
<div v-show="isNull" ref="chartRef" :style="{ height: '400px', width: '100%' }">
<Spin :spinning="loading" :absolute="true" />
</div>
<Empty v-show="!isNull" />
</section>
</section>
</template>
<style scoped></style>