HistoryDataModal.vue
3.94 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
<script lang="ts" setup>
import { BasicForm, useForm } from '/@/components/Form';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { formSchema, getHistorySearchParams, SchemaFiled } from './history.config';
import { HistoryModalOkEmitParams } from './type';
import { ref } from 'vue';
import { getAllDeviceByOrg } from '/@/api/dataBoard';
import { getDeviceHistoryInfo } from '/@/api/alarm/position';
import { DataSource } from '/@/views/visual/palette/types';
import { cloneDeep } from 'lodash-es';
const emit = defineEmits(['register', 'ok']);
const [registerForm, { updateSchema, setFieldsValue, validate, getFieldsValue }] = useForm({
schemas: formSchema(),
showActionButtonGroup: false,
fieldMapToTime: [
[SchemaFiled.DATE_RANGE, [SchemaFiled.START_TS, SchemaFiled.END_TS], 'YYYY-MM-DD HH:mm:ss'],
],
});
const [registerModal, { closeModal }] = useModalInner(async (dataSource: DataSource[]) => {
try {
dataSource = cloneDeep(dataSource);
if (dataSource.length < 2) return;
dataSource = dataSource.splice(0, 2);
const deviceRecord = dataSource?.at(0) || ({} as DataSource);
if (!deviceRecord.organizationId) return;
const deviceList = await getAllDeviceByOrg(
deviceRecord.organizationId,
deviceRecord.deviceProfileId
);
const options = deviceList
.filter((item) => item.tbDeviceId === deviceRecord.deviceId)
.map((item) => ({ ...item, label: item.name, value: item.tbDeviceId }));
const attKey = dataSource.map((item) => ({
...item,
label: item.attribute,
value: item.attribute,
}));
updateSchema([
{
field: SchemaFiled.DEVICE_ID,
componentProps: {
options,
},
},
{
field: SchemaFiled.KEYS,
component: 'Select',
defaultValue: attKey.map((item) => item.value),
componentProps: {
options: attKey,
mode: 'multiple',
disabled: true,
},
},
]);
setFieldsValue({
[SchemaFiled.DEVICE_ID]: deviceRecord.deviceId,
[SchemaFiled.KEYS]: attKey.map((item) => item.value),
});
} catch (error) {
throw error;
}
});
const validEffective = (value = '') => {
return !!(value && !isNaN(value as unknown as number));
};
const loading = ref(false);
const handleOk = async () => {
try {
await validate();
let value = getFieldsValue();
value = getHistorySearchParams(value);
loading.value = true;
const res = await getDeviceHistoryInfo({
...value,
[SchemaFiled.KEYS]: value[SchemaFiled.KEYS].join(','),
});
let timespanList = Object.keys(res).reduce((prev, next) => {
const ts = res[next].map((item) => item.ts);
return [...prev, ...ts];
}, [] as number[]);
timespanList = [...new Set(timespanList)];
const track: Record<'lng' | 'lat', number>[] = [];
const keys = Object.keys(res);
for (const ts of timespanList) {
const list: { ts: number; value: number }[] = [];
for (const key of keys) {
const record = res[key].find((item) => ts === item.ts);
if (!validEffective(record?.value)) {
continue;
}
list.push(record as any);
}
if (list.length === 2 && list.every(Boolean)) {
const lng = list.at(0)?.value;
const lat = list.at(1)?.value;
if (lng && lat) track.push({ lng, lat });
}
}
emit('ok', { track, value } as HistoryModalOkEmitParams);
closeModal();
} catch (error) {
throw error;
} finally {
loading.value = false;
}
};
</script>
<template>
<BasicModal
title="历史轨迹"
@register="registerModal"
@ok="handleOk"
:ok-button-props="{ loading }"
>
<BasicForm @register="registerForm" />
</BasicModal>
</template>