zhangwencui
17 小时以前 897524d4918c407dbaa32fc98e5c5867c70e4064
src/views/energyManagement/energyConsumptionStatistical/index.vue
@@ -657,10 +657,29 @@
  // 统计维度切换
  const handleTypeChange = () => {
    // 重置时间范围
    searchForm.dateRange = [];
    searchForm.monthRange = [];
    searchForm.year = new Date().getFullYear();
    // 重置时间范围并设置默认值
    const end = new Date();
    const start = new Date();
    if (statisticsType.value === "day") {
      // 默认最近7天
      start.setDate(start.getDate() - 6);
      searchForm.dateRange = [
        start.toISOString().split("T")[0],
        end.toISOString().split("T")[0],
      ];
    } else if (statisticsType.value === "month") {
      // 默认最近3个月
      start.setMonth(start.getMonth() - 2);
      searchForm.monthRange = [
        start.toISOString().slice(0, 7),
        end.toISOString().slice(0, 7),
      ];
    } else if (statisticsType.value === "year") {
      // 默认当前年份
      searchForm.year = new Date().getFullYear();
    }
    page.current = 1;
    handleQuery();
  };
@@ -670,6 +689,12 @@
    tableLoading.value = true;
    const params = {
      type: "",
      state:
        statisticsType.value === "day"
          ? "日"
          : statisticsType.value === "month"
          ? "月"
          : "年",
    };
    // 构造请求参数
@@ -689,14 +714,20 @@
    } else if (statisticsType.value === "month") {
      if (searchForm.monthRange && searchForm.monthRange.length === 2) {
        params.startDate = searchForm.monthRange[0] + "-01";
        params.endDate = searchForm.monthRange[1] + "-01";
        // 计算月数
        const start = new Date(searchForm.monthRange[0] + "-01");
        const end = new Date(searchForm.monthRange[1] + "-01");
        params.days =
          (end.getFullYear() - start.getFullYear()) * 12 +
          (end.getMonth() - start.getMonth()) +
          1;
        const [endYearStr, endMonthStr] = String(searchForm.monthRange[1]).split(
          "-"
        );
        const endYear = Number(endYearStr);
        const endMonth = Number(endMonthStr);
        const lastDay = new Date(endYear, endMonth, 0).getDate();
        params.endDate = `${endYearStr}-${endMonthStr}-${String(lastDay).padStart(
          2,
          "0"
        )}`;
        // 计算天数
        const start = new Date(params.startDate);
        const end = new Date(params.endDate);
        params.days = Math.ceil((end - start) / (1000 * 60 * 60 * 24)) + 1;
      }
    } else if (statisticsType.value === "year") {
      params.startDate = searchForm.year + "-01-01";