Files
ndm-web-client/src/components/device-page/device-card/history-diag-card/alarm-host-history-diag-card.vue
2025-11-26 16:28:05 +08:00

100 lines
3.5 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import type { NdmAlarmHostResultVO } from '@/apis';
import { DeviceAlarmHistoryDiagCard, DeviceStatusHistoryDiagCard } from '@/components';
import dayjs from 'dayjs';
import { NButton, NCard, NDatePicker, NFlex, NGi, NGrid, NSelect, type DatePickerProps, type SelectOption } from 'naive-ui';
import { computed, onMounted, reactive, ref, toRefs, useTemplateRef } from 'vue';
const props = defineProps<{
stationCode: string;
ndmAlarmHost: NdmAlarmHostResultVO;
}>();
const { stationCode, ndmAlarmHost } = toRefs(props);
const searchFields = reactive({
dateTimeRange: undefined as DatePickerProps['value'],
});
const onDateChange = (value: [number, number] | null) => {
if (!value) {
return;
}
const [start, end] = value;
const diffDays = dayjs(end).diff(dayjs(start), 'day');
if (diffDays > 7) {
// 如果超过7天自动调整结束时间
const adjustedEnd = dayjs(start).add(7, 'day').valueOf();
searchFields.dateTimeRange = [start, adjustedEnd];
window.$message.warning('时间范围不能超过7天已自动调整');
} else {
searchFields.dateTimeRange = value;
}
};
type DeviceStatusHistoryDiagCardInst = InstanceType<typeof DeviceStatusHistoryDiagCard> | null;
type DeviceAlarmHistoryDiagCardInst = InstanceType<typeof DeviceAlarmHistoryDiagCard> | null;
const deviceStatusHistoryDiagCardRef = useTemplateRef<DeviceStatusHistoryDiagCardInst>('deviceStatusHistoryDiagCardRef');
const deviceAlarmHistoryDiagCardRef = useTemplateRef<DeviceAlarmHistoryDiagCardInst>('deviceAlarmHistoryDiagCardRef');
function refreshData() {
deviceStatusHistoryDiagCardRef.value?.refresh();
deviceAlarmHistoryDiagCardRef.value?.refresh();
}
const loading = computed(() => {
return deviceStatusHistoryDiagCardRef.value?.isPending || deviceAlarmHistoryDiagCardRef.value?.isPending;
});
onMounted(() => {
const now = dayjs();
const todayEnd = now.endOf('date');
const weekAgo = now.subtract(1, 'week').startOf('date');
searchFields.dateTimeRange = [weekAgo.valueOf(), todayEnd.valueOf()];
refreshData();
});
const diagCards = ref<SelectOption[]>([
{ label: '设备状态', value: 'status' },
{ label: '设备告警', value: 'alarm' },
]);
const selectedCards = ref<string[]>([...diagCards.value.map((option) => `${option.value ?? ''}`)]);
</script>
<template>
<NCard size="small">
<NFlex vertical>
<NCard size="small">
<NFlex justify="space-between" :wrap="false">
<NGrid :x-gap="8" :y-gap="8">
<NGi :span="20">
<NDatePicker v-model:value="searchFields.dateTimeRange" type="datetimerange" @update:value="onDateChange" />
</NGi>
<NGi :span="20">
<NSelect v-model:value="selectedCards" multiple :options="diagCards" />
</NGi>
</NGrid>
<NButton secondary :loading="loading" @click="refreshData">刷新数据</NButton>
</NFlex>
</NCard>
<DeviceStatusHistoryDiagCard
v-if="selectedCards.includes('status')"
:ref="'deviceStatusHistoryDiagCardRef'"
:station-code="stationCode"
:ndm-device="ndmAlarmHost"
:date-time-range="searchFields.dateTimeRange"
/>
<DeviceAlarmHistoryDiagCard
v-if="selectedCards.includes('alarm')"
:ref="'deviceAlarmHistoryDiagCardRef'"
:station-code="stationCode"
:ndm-device="ndmAlarmHost"
:date-time-range="searchFields.dateTimeRange"
/>
</NFlex>
</NCard>
</template>
<style scoped lang="scss"></style>