guonan
2025-05-28 86f2633b4c70675bd90e79c9bdaf8a1eb8f95938
src/views/left/KGSimOption/PredictiveSimulation.vue
@@ -2,198 +2,312 @@
  <div class="history-simulation">
    <div class="left-top">
      <span>预测模拟</span>
      <span class="clickable-text" @click="toggleDetails">{{ isCollapsed ? '展开' : '收起' }}</span>
      <span class="clickable-text" @click="toggleDetails">{{
        isCollapsed ? "展开" : "收起"
      }}</span>
    </div>
    <div class="details" :class="{ hidden: isCollapsed }">
      <div class="input-item">
        <label>预测数据:</label>
        <el-select v-model="selectedForecastData" placeholder="请选择" popper-class="mySelectStyle">
    <el-form
      :class="{ hidden: isCollapsed }"
      :model="forms"
      class="details"
      label-width="auto"
      style="max-width: 600px"
    >
      <el-form-item label="预测数据:">
        <el-select
          v-model="forms.prediction"
          placeholder="请选择"
          popper-class="mySelectStyle"
        >
          <el-option
            v-for="item in ForecastData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :value="item.name"
          ></el-option>
        </el-select>
      </div>
      <div class="input-item" style="margin-bottom: 10px;">
        <label>降雨雨量:</label>
        <el-input v-model="totalRainfall" type="number" placeholder="请输入"></el-input>
        <span>mm</span>
      </div>
      <div class="input-item" style="margin-bottom: 10px;">
        <label>降雨强度:</label>
        <el-input v-model="rainfallIntensity" type="number" placeholder="请输入"></el-input>
        <span>mm/h</span>
      </div>
      <div class="input-item">
        <label>降雨模式:</label>
        <el-select v-model="selectedRainfallPatterns" placeholder="请选择" popper-class="mySelectStyle">
      </el-form-item>
      <el-form-item label="降雨年份:" v-show="forms.prediction == '降雨场次'">
        <el-select
          v-model="forms.showRainYears"
          placeholder="请选择"
          popper-class="mySelectStyle"
          @change="changeYear"
        >
          <el-option
            v-for="item in rainYears"
            :key="item.id"
            :label="item.label"
            :value="item.label"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="降雨数据:" v-show="showSelect">
        <el-select
          @change="rainFallChange"
          v-model="forms.showRainFall"
          placeholder="请选择"
          popper-class="mySelectStyle"
        >
          <el-option
            v-for="item in rainFall"
            :key="item.rainfallId"
            :label="item.rainfallName"
            :value="item"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="方案名称:">
        <el-input
          v-model="forms.name"
          type="text"
          placeholder="请输入"
        ></el-input>
      </el-form-item>
      <el-form-item label="降雨雨量:">
        <el-input v-model="forms.rainfall" type="number" placeholder="请输入">
          <template #append>mm</template></el-input
        >
      </el-form-item>
      <el-form-item label="降雨强度:">
        <el-input v-model="forms.intensity" type="number" placeholder="请输入">
          <template #append>mm/h</template></el-input
        >
      </el-form-item>
      <el-form-item label="降雨模式:">
        <el-select
          v-model="forms.mode"
          placeholder="请选择"
          popper-class="mySelectStyle"
        >
          <el-option
            v-for="item in RainfallPatterns"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :value="item.name"
          ></el-option>
        </el-select>
      </div>
      <div class="input-item">
        <label>预计时长:</label>
        <el-input v-model="rainfallDuration" type="number" placeholder="请输入"></el-input>
        <span>h</span>
      </div>
      <div>
        <label>仿真参数:</label>
        <div style="width: 100%; height: 60px; background-color: #fff;"></div>
      </div>
    </div>
      </el-form-item>
      <el-form-item label="选择时间:">
        <el-date-picker
          v-model="forms.hours"
          type="datetimerange"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          date-format="YYYY/MM/DD ddd"
          time-format="A hh:mm:ss"
          @change="change"
        />
      </el-form-item>
      <el-form-item label="预计时长:">
        <el-input
          disabled
          v-model="forms.duration"
          type="number"
          placeholder="请输入"
          ><template #append>h</template></el-input
        >
      </el-form-item>
    </el-form>
    <div class="buttons">
      <el-button type="primary" @click="openPlan">打开方案</el-button>
      <el-button type="primary" @click="openSaveDialog">保存方案</el-button>
      <el-button type="success" @click="startPlay">开始模拟</el-button>
    </div>
    <!-- 保存方案对话框 -->
    <el-dialog
      v-model="saveDialogVisible"
      title="保存方案"
      width="50%"
      :before-close="handleClose"
      custom-class="custom-dialog"
    >
      <div class="dialog-content">
        <p><strong>所选重点沟:</strong>{{ props.selectedArea }}</p>
        <p><strong>模拟类型:</strong>预测模拟</p>
        <p><strong>预测数据:</strong>{{ selectedForecastDataName }}</p>
        <p><strong>降雨雨量:</strong>{{ totalRainfall }} mm</p>
        <p><strong>降雨强度:</strong>{{ rainfallIntensity }} mm/h</p>
        <p><strong>降雨模式:</strong>{{ selectedRainfallPatternsName }}</p>
        <p><strong>预计时长:</strong>{{ rainfallDuration }} h</p>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="saveDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmSave">确定保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue';
import { ElMessage } from 'element-plus';
import { ref, computed, inject, reactive, watch, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { initeWaterPrimitiveView } from "@/utils/water";
import { useSimStore } from "@/store/simulation.js"; // 引入 Store
import { SimAPIStore } from "@/store/simAPI";
import { getRainfallDataYears } from "@/api/hpApi";
import { EventBus } from "@/eventBus"; // 引入事件总线
import { getSimStart } from "@/api/trApi";
const emit = defineEmits(["start", "end"]);
function endPlay() {
  emit("end");
}
onMounted(() => {});
function startPlay() {
    initeWaterPrimitiveView();
    emit("start");
}
// 获取 Store 实例
const simStore = SimAPIStore();
// 定义 Props
const props = defineProps({
  selectedArea: {
    type: String,
    required: true
  }
    type: Object,
    required: true,
  },
});
// 数据绑定
const isCollapsed = ref(false); // 控制展开/收起状态
const totalRainfall = ref(50); // 降雨雨量
const rainfallIntensity = ref(70); // 降雨强度
const rainfallDuration = ref(5); // 预计时长
const selectedForecastData = ref('4'); // 默认选中“自定义参数”
const selectedRainfallPatterns = ref('1'); // 默认选中“正态分布”
const saveDialogVisible = ref(false); // 控制保存方案对话框的显示状态
// 预测数据选项
const ForecastData = [
  { id: '1', name: '降雨场次' },
  { id: '2', name: '气象数据' },
  { id: '3', name: '逐小时预报数据' },
  { id: '4', name: '自定义参数' }
  { id: "1", name: "降雨场次" },
  { id: "2", name: "气象数据" },
  { id: "3", name: "逐小时预报数据" },
  { id: "4", name: "自定义参数" },
];
// 降雨模式选项
const RainfallPatterns = [
  { id: '1', name: '正态分布' },
  { id: '2', name: '平均分布' },
  { id: '3', name: '波动平均分布' },
  { id: '4', name: '持续上升' }
  { id: "1", name: "正态分布" },
  { id: "2", name: "平均分布" },
  { id: "3", name: "波动平均分布" },
  { id: "4", name: "持续上升" },
];
// 计算属性:获取当前选中的预测数据名称
const selectedForecastDataName = computed(() => {
  const selected = ForecastData.find(item => item.id === selectedForecastData.value);
  return selected ? selected.name : '';
const forms = reactive({
  rainfall: 50,
  name: "",
  intensity: 70,
  duration: null,
  prediction: "降雨场次",
  mode: "正态分布",
  showRainFall: "",
  hours: null,
  type: 1,
  geom: null, // 先设置为 null 或者其他默认值,
  showRainYears: "",
});
// 计算属性:获取当前选中的降雨模式名称
const selectedRainfallPatternsName = computed(() => {
  const selected = RainfallPatterns.find(item => item.id === selectedRainfallPatterns.value);
  return selected ? selected.name : '';
});
// 降雨场次选择
const rainFall = ref({});
// 降雨年份选择
const rainYears = [
  {
    label: "2024",
    value: "2024",
    id: 1,
  },
  {
    label: "2025",
    value: "2025",
    id: 2,
  },
];
const showSelect = ref(false);
const changeYear = (val) => {
  if (val) {
    showSelect.value = true;
  }
  if (val == "2024") {
    getRainfallDataYears(2024).then((res) => {
      rainFall.value = res.data.pageData;
      // console.log(rainFall.value, "2024");
    });
  } else if (val == "2025") {
    getRainfallDataYears(2025).then((res) => {
      rainFall.value = res.data.pageData;
      // console.log(rainFall.value, "2025");
    });
  }
};
// 降雨场次选择
const rainFallChange = (val) => {
  // console.log(val, "val");
  const decimalHours = (val.rainfallDuration.match(/(\d+)时(\d+)分/) || [])
    .slice(1)
    .reduce((acc, curr, i) => acc + parseInt(curr, 10) / (i === 0 ? 1 : 60), 0)
    .toFixed(1);
  // console.log(parseFloat(decimalHours)); // 输出: 2.6
  forms.duration = decimalHours;
  forms.intensity = val.rainIntensityHour;
  forms.rainfall = val.rainfallTotalValue;
};
// 切换详情显示
const toggleDetails = () => {
  isCollapsed.value = !isCollapsed.value;
};
const { calculateHoursDifference } = inject("calculateHours");
const change = (val) => {
  forms.duration = calculateHoursDifference(val);
};
const resetForm = () => {
  forms.rainfall = 50;
  forms.name = "";
  forms.intensity = 70;
  forms.duration = null;
  forms.prediction = "降雨场次";
  forms.mode = "正态分布";
  forms.showRainFall = "";
  forms.hours = null;
  forms.type = 1;
  forms.geom = null; // 先设置为 null 或者其他默认值,
};
// 打开保存方案对话框
const openSaveDialog = () => {
  if (!props.selectedArea || !selectedForecastData.value || !totalRainfall.value || !rainfallIntensity.value || !selectedRainfallPatterns.value || !rainfallDuration.value) {
    ElMessage.warning('请先填写所有必填项');
    return;
const openSaveDialog = async () => {
  // 在 setup 内部更新 geom 的值
  try {
    forms.geom = props.selectedArea;
    await simStore.addSimCheme(forms);
    resetForm();
    EventBus.emit("close-selectArea");
  } catch (err) {}
};
// 注入模拟操作方法
const { startSimulate, endSimulate } = inject("simulateActions");
// 开始模拟
async function startPlay() {
  try {
    formData.geom = props.selectedArea;
    // 保存方案
    const res = await simStore.addSimCheme(forms);
    const schemeId = res.data?.data?.id;
    if (!schemeId) {
      ElMessage.error("方案保存失败,未获取到有效 ID");
      return;
    }
    // 调用求解器
    const simStartRes = await getSimStart(schemeId);
    console.log(simStartRes, "getSimStart 返回结果");
    // 关闭选择区域窗口、初始化视图并开始模拟
    EventBus.emit("close-selectArea");
    // 暂时不在此处开始模拟,模拟都在方案列表中进行模拟
    // initeWaterPrimitiveView();
    // startSimulate();
    ElMessage.warning({
      message: "请返回方案列表开始模拟!",
      duration: 10000, // 提示框显示时长,单位为毫秒,默认是3000毫秒
    });
  } catch (error) {
    ElMessage.error("启动模拟失败,请稍后再试");
  }
  saveDialogVisible.value = true;
};
// 关闭保存方案对话框
const handleClose = () => {
  saveDialogVisible.value = false;
};
// 确认保存
const confirmSave = () => {
  console.log('保存方案成功', {
    区域: props.selectedArea,
    模拟类型: '预测模拟',
    预测数据: selectedForecastDataName.value,
    降雨雨量: `${totalRainfall.value} mm`,
    降雨强度: `${rainfallIntensity.value} mm/h`,
    降雨模式: selectedRainfallPatternsName.value,
    预计时长: `${rainfallDuration.value} h`
  });
  ElMessage.success('方案已保存');
  saveDialogVisible.value = false;
};
}
// 打开方案
const openPlan = () => {
  console.log('打开方案按钮被点击');
};
// 开始模拟
const startSimulation = () => {
  console.log('开始模拟按钮被点击');
  console.log('当前选中的区域:', props.selectedArea);
  console.log("打开方案按钮被点击");
};
</script>
<style scoped>
<style lang="less" scoped>
.history-simulation {
  margin-bottom: 20px;
}
.details {
  margin-top: 10px;
  transition: height 0.3s ease, opacity 0.3s ease;
  overflow: hidden;
  width: 100%;
  height: 100%;
  padding: 0px 10px 0px 0px;
  box-sizing: border-box;
  width: 100%;
  height: 100%;
}
.hidden {
@@ -203,8 +317,13 @@
.buttons {
  margin-top: 10px;
  margin-right: 10px;
  display: flex;
  gap: 10px;
}
.el-button {
  flex: 1;
}
.input-item {
  display: flex;
  align-items: center;
@@ -242,4 +361,18 @@
.custom-dialog {
  z-index: 3000 !important; /* 确保对话框覆盖其他元素 */
}
</style>
/deep/ .el-input-group__append,
.el-input-group__prepend {
  background-color: #084b42;
  color: #fff;
}
/deep/ .el-form-item__label {
  color: #61f7d4 !important;
}
/deep/ .el-select {
  margin-bottom: 0px !important;
}
/deep/ .el-form-item {
  margin-top: 10px;
}
</style>