-import { ref, watch } from "vue";
+import { ref, watch, computed, onUnmounted } from "vue";
+import { getSystemEnergyCostRank } from "@/apis/headquarters";
import { useI18n } from "vue-i18n";
+import useBuildingStore from "@/stores/useBuildingStore";
+
+const store = useBuildingStore();
const { t } = useI18n();
-const props = defineProps({
- energyCostData: {
- type: Object,
- required: true,
- },
-});
-
+const energyCostData = ref({});
const energyTypeList = ref([
{
title: t("dashboard.today_energy_consumption"),
- key: "today",
+ key: "day",
},
{
title: t("dashboard.this_month_energy_consumption"),
@@ -23,17 +21,48 @@ const energyTypeList = ref([
const currentEnergyType = ref({
name: "month",
});
+let intervalId = null;
-// 取得當前能耗資料
-const getCurrentEnergyData = () => {
- if (!props.energyCostData || !props.energyCostData.rank) {
- return []; // 或者返回一些默认值
+const currentEnergyData = computed(() => {
+ if (!energyCostData.value) {
+ return [];
}
-
return currentEnergyType.value.name === "month"
- ? props.energyCostData?.rank.month || []
- : props.energyCostData?.rank.day || [];
+ ? energyCostData.value?.month || []
+ : energyCostData.value?.day || [];
+});
+
+const getEnergyRank = async () => {
+ try {
+ const res = await getSystemEnergyCostRank({
+ building_ids: store.buildings.map((building) => building.building_guid),
+ });
+ energyCostData.value = res.data;
+ } catch (error) {
+ console.error("Error fetching energy cost rank:", error);
+ }
};
+
+watch(
+ () => store.buildings,
+ (newBuilding) => {
+ if (newBuilding) {
+ getEnergyRank();
+
+ if (intervalId) {
+ clearInterval(intervalId);
+ }
+ intervalId = setInterval(() => {
+ getEnergyRank();
+ }, 60 * 60 * 1000);
+ }
+ },
+ { immediate: true }
+);
+
+onUnmounted(() => {
+ clearInterval(intervalId);
+});
@@ -62,7 +91,7 @@ const getCurrentEnergyData = () => {
-
- {{
- index + 1
- }}
- |
- {{ item.name }} |
- {{ item.value }} |
+
+
+
+ {{ index + 1 }}
+
+ |
+ {{ item.site_name }} |
+ {{ item.name }} |
+ {{ item.value }} |
diff --git a/src/views/headquarters/components/ElecTrends.vue b/src/views/headquarters/components/ElecTrends.vue
index b2f6d0c..2028179 100644
--- a/src/views/headquarters/components/ElecTrends.vue
+++ b/src/views/headquarters/components/ElecTrends.vue
@@ -1,6 +1,7 @@
@@ -207,7 +173,7 @@ watch(