guonan
6 天以前 a57caa72a54efe9de3fe26a6c36d3e8038267377
src/components/monifangzhen/schemeCard.vue
@@ -16,10 +16,6 @@
          <span style="color: aquamarine">
            {{ item.result === "-1" ? "出错" : item.result || "创建仿真" }}
          </span>
          <!-- <span style="color: aquamarine">{{ item.result || "创建仿真" }}</span> -->
          <!-- <span style="color: aquamarine">{{
            statusText[item.status] || "未知"
          }}</span> -->
        </p>
      </div>
      <div class="cardMenu">
@@ -27,7 +23,15 @@
          <el-button size="small" @click="setSchemClick(item)"
            >方案详情</el-button
          >
          <el-button size="small" @click="startPlay(item)">进入模拟</el-button>
          <el-button
            size="small"
            v-show="item.type !== 2"
            @click="startPlay(item)"
            >进入模拟</el-button
          >
          <el-button size="small" v-show="item.type == 2" @click="rePlay(item)"
            >历史回放</el-button
          >
          <!--  :disabled="item.status !== 2" -->
        </div>
      </div>
@@ -37,10 +41,7 @@
      :selectedScheme="currentScheme"
      @back="handleBack"
    />
    <flowRateTab
    v-if="schemeInfoShow">
      123
    </flowRateTab>
    <flowRateTab v-if="schemeInfoShow"> 123 </flowRateTab>
  </div>
  <Message
    @close="close"
@@ -52,7 +53,15 @@
<script setup>
import { EventBus } from "@/eventBus"; // 引入事件总线
import { onMounted, ref, watch, defineEmits, onUnmounted } from "vue";
import {
  nextTick,
  onMounted,
  ref,
  watch,
  defineEmits,
  onUnmounted,
  inject,
} from "vue";
import dayjs from "dayjs";
import { initeWaterPrimitiveView } from "@/utils/water";
import Message from "@/components/tools/Message.vue";
@@ -63,12 +72,16 @@
import { ElMessage, ElMessageBox } from "element-plus";
const emit = defineEmits(["start", "end", "reset", "closeBtn"]);
import {
  getRegionData,
  getSimData,
  deleteSimData,
  getSimStart,
  getSimDataById,
  getSimresult,
} from "@/api/trApi.js";
import { getAeraTownCode, getDeviceNWJ } from "@/api/hpApi";
import { createPoint, removeEntities, clearAllPoints } from "@/utils/map";
import { deviceDictList, getDictName } from "@/constant/dict.js";
const simStore = useSimStore();
const simAPIStore = SimAPIStore();
@@ -80,14 +93,6 @@
function selectScheme(id) {
  selectedId.value = id;
}
const statusText = {
  0: "创建仿真",
  1: "预处理",
  2: "分析中",
  10: "完成",
  20: "出错",
};
function formatTime(time) {
  return dayjs(time).format("YYYY-MM-DD HH:mm:ss");
@@ -108,61 +113,226 @@
  messageShow.value = false;
}
function startPlay(item) {
  // 分析中
  if (item.status == 2) {
// 实时模拟五分钟请求一次的定时器
const realTimeSimInterval = ref(null);
const { startSimulate, endSimulate } = inject("simulateActions");
const BJCode = ref([
  { label: "密云区", value: "110118000000" },
  { label: "房山区", value: "110111000000" },
  { label: "门头沟区", value: "110109000000" },
  { label: "延庆区", value: "110119000000" },
  { label: "怀柔区", value: "110116000000" },
  { label: "昌平区", value: "110114000000" },
  { label: "平谷区", value: "110117000000" },
  { label: "海淀区", value: "110108000000" },
  { label: "石景山区", value: "110107000000" },
  { label: "丰台区", value: "110106000000" },
]);
async function startPlay(item) {
  simStore.openDia = false;
  clearAllPoints();
  const areaName = item.areaName;
  let districtCode;
  // 1. 判断是否包含 “区”
  if (!areaName.includes("区")) {
    console.log(
      `方案中模拟【${areaName}】不包含“区”,使用默认编码:怀柔区(110116000000)`
    );
    districtCode = "110116000000"; // 手动指定为怀柔区编码
  } else {
    // 2. 在 BJCode 中查找匹配的区域 value
    const matchedArea = BJCode.value.find((area) => area.label === areaName);
    if (!matchedArea) {
      console.warn(`未找到 ${areaName} 对应的区域编码`);
      return;
    }
    districtCode = matchedArea.value;
  }
  // 1. 获取乡镇区域编码
  const areaRes = await getAeraTownCode(districtCode);
  const districtCodes = areaRes.data.map((item) => item.districtCode);
  // 2. 泥位计类型ID
  const ids = "1437295811";
  // 3. 并行请求所有设备数据
  const requests = districtCodes.map((code) =>
    getDeviceNWJ(ids, code)
      .then((res) => res.data?.pageData || []) // 安全提取 pageData
      .catch((err) => {
        console.error(`请求失败 (code: ${code})`, err);
        return []; // 出错时也返回空数组,避免 Promise.all 中断
      })
  );
  // 4. 等待所有请求完成
  const allPageDataArrays = await Promise.all(requests);
  // 5. 合并二维数组为一维数组
  const mergedPageData = allPageDataArrays.flat();
  // 6. 如果不是“区”,则过滤出 deviceName 包含 "孙胡沟" 的设备
  const filteredPageData = areaName.includes("区")
    ? mergedPageData
    : mergedPageData.filter((device) => device.deviceName.includes("孙胡沟"));
  // 孙胡沟设备经纬度映射
  const deviceMapping = {
    怀柔区琉璃庙镇孙胡沟椴树底下东沟泥位计5007: {
      lon: 116.598891,
      lat: 40.554979,
    },
    怀柔区琉璃庙镇孙胡沟村上台子河东南沟泥位计5006: {
      lon: 116.593215,
      lat: 40.554399,
    },
  };
  let displayedData = filteredPageData;
  if (!areaName.includes("区")) {
    // 添加 lon 和 lan 字段
    displayedData = filteredPageData.map((device) => {
      const mapping = deviceMapping[device.deviceName];
      return {
        ...device,
        ...(mapping && { lon: mapping.lon, lat: mapping.lat }), // 如果 mapping 存在,才添加
      };
    });
    // 添加额外的两个点位
    const extraPoint1 = {
      deviceName: "弯沟1",
      longitude: 116.597836,
      latitude: 40.564962,
      // height: 530.14,
      type: "泥位计",
      lon: 116.597836,
      lat: 40.564962,
      dictDeviceType: "1437295811",
      deviceId: "custom_extraPoint1", // 手动加一个唯一 ID
    };
    const extraPoint2 = {
      deviceName: "弯沟2",
      longitude: 116.591571,
      latitude: 40.573093,
      // height: 483.89,
      type: "泥位计",
      lon: 116.591571,
      lat: 40.573093,
      dictDeviceType: "1437295811",
      deviceId: "custom_extraPoint2", // 手动加一个唯一 ID
    };
    displayedData = [...displayedData, extraPoint1, extraPoint2];
    // displayedData = [...displayedData, extraPoint1];
  }
  console.log(
    displayedData,
    areaName.includes("区")
      ? "全部泥位计设备列表"
      : "孙胡沟泥位计 + 额外点位列表"
  );
  // 7. 创建点
  displayedData.forEach((item) => {
    // 根据需求可增删
    item.type = getDictName(deviceDictList, item.dictDeviceType);
    item.name = item.deviceName;
    item.id = item.deviceId;
    item.className = "device";
    item.showLabel = true;
    createPoint(item);
  });
  if (item.status === 2) {
    ElMessage.warning("当前方案正在分析中,无法进入模拟!");
    return;
  }
  // 出错
  if (item.status == 20) {
  if (item.status === 20) {
    ElMessage.error("当前方案分析出错,请重新新建方案!");
    return;
  }
  // 调用求解器并拿到最新生成的serviceName
  // 新创建的方案没有状态以及serviceName则执行调用求解器
  if (!item.status && !item.serviceName) {
    getSimStart(item.id).then((res) => {
      getSimDataById(item.id).then((res) => {
        item.serviceName = res.data[0].serviceName;
        simStore.setSelectedScheme(item);
        console.log(item, "无服务名称");
        ElMessage.warning("当前方案正在分析中,请稍后再模拟");
        getScheme();
      });
    });
  } else {
  // 如果是已完成的方案(status == 10)
  if (item.status === 10) {
    const flyHeight = item.areaType === 1 ? 100000 : 50000;
    simStore.setSelectedScheme(item);
    console.log("有服务名称");
  }
  const flyHeight = ref(100000);
  const shouldShowFill = false;
  // 求解器求解完成之后才可以显示时间轴
  if (item.status == 10) {
    // 只有行政区划执行
    if (item.areaType == 1) {
      flyHeight.value = 100000;
    if (item.areaType === 1) {
      EventBus.emit("select-geom", {
        geom: item.geom,
        flyHeight: flyHeight.value,
        shouldShowFill: shouldShowFill,
        flyHeight,
        shouldShowFill: false,
      });
    } else {
      // 孙胡沟区域跳转视角
      initeWaterPrimitiveView();
    }
    currentScheme.value = item;
    schemeInfoShow.value = true;
    emit("closeBtn", false);
    emit("start");
    startSimulate();
    return;
  }
  // 新建方案,没有 status 和 serviceName 且 type != 2
  if (!item.status && !item.serviceName && item.type !== 2) {
    try {
      await getSimStart(item.id);
      const res = await getSimDataById(item.id);
      item.serviceName = res.data[0]?.serviceName || null;
      simStore.setSelectedScheme(item);
      ElMessage.warning("当前方案正在分析中,请稍后再模拟");
      getScheme();
    } catch (e) {
      console.error("获取模拟数据失败:", e);
    }
    return;
  }
  // 默认情况:有服务名称
  simStore.setSelectedScheme(item);
}
function endPlay() {
  emit("end");
// 实时模拟历史回放
function rePlay(item) {
  // 当前选中的方案
  simStore.setSelectedScheme(item);
  // 拿id去请求results接口,如果长度不为0,则可以进行历史回放
  getSimresult(item.id)
    .then((res) => {
      if (res.code == 500) {
        // 如果长度为0,提示用户并且不进行后续操作
        ElMessage.warning("提示:没有可回放的数据!");
        return; // 阻止后续操作
      } else {
        simStore.rePlayList = res.data;
        console.log(simStore.rePlayList, "实时模拟历史回放列表");
      }
      // 使用 nextTick 确保 DOM 更新后再执行后续操作
      nextTick(() => {
        initeWaterPrimitiveView();
        startSimulate();
      });
    })
    .catch((error) => {
      console.log("请求失败:", error);
      // 错误处理
    });
}
function handleBack(value) {
@@ -191,7 +361,9 @@
      (item) =>
        item.result == "创建仿真" ||
        item.result == "完成" ||
        item.result == "-1"
        item.result == "-1" ||
        item.result == "已停止" ||
        item.result == "运行中"
    );
    simAPIStore.shouldPoll = !shouldStop; // 修改 Pinia 状态
    // 3. 如果需要停止
@@ -212,7 +384,6 @@
watch(
  () => simAPIStore.shouldPoll,
  (isStarted) => {
    console.log(isStarted, "定时器");
    if (isStarted) {
      getScheme(); // 首次立即获取一次
      intervalId = setInterval(getScheme, 60 * 1000); // 每隔一分钟执行