guonan
2025-04-23 11cef93fa041289db9fafd4cb654b20d53cb2c51
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<template>
  <div style="width: 100%; height: 100%">
    <div
      class="left-top"
      v-if="simStore.selectTab == '行政区划仿真'"
      style="margin-top: 0px"
    >
      行政区划仿真(30m精度)
    </div>
    <div class="left-top" v-if="simStore.selectTab == '重点区域仿真'">
      重点区域仿真(10m精度)
    </div>
    <div class="forms">
      <el-form :model="forms" label-width="auto" style="max-width: 600px">
        <el-form-item label="方案名称:">
          <el-input
            v-model="forms.name"
            style="max-width: 600px"
            placeholder="Please input"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="上传参数">
          <el-upload
            v-model:file-list="forms.fileList"
            class="upload-demo"
            :auto-upload="false"
            :multiple="false"
            :on-change="handleFileChange"
            :limit="1"
            :on-exceed="handleExceed"
            :before-upload="beforeUpload"
            accept=".xlsx,.xls,.csv"
          >
            <el-button type="primary">点击上传降雨数据</el-button>
            <template #append>mm/h</template>
          </el-upload>
        </el-form-item>
        <el-form-item
          label="行政区域:"
          v-if="simStore.selectTab == '行政区划仿真'"
        >
          <el-select
            v-model="forms.geom"
            placeholder="Select"
            style="max-width: 600px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="重点区域:"
          v-if="simStore.selectTab == '重点区域仿真'"
        >
          <el-select
            v-model="forms.geom"
            placeholder="Select"
            style="max-width: 600px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
 
        <el-form-item label="降雨量:">
          <el-input
            v-model="forms.rainfall"
            style="max-width: 600px"
            placeholder="Please input"
          >
            <template #append>mm</template>
          </el-input>
        </el-form-item>
 
        <el-form-item label="降雨时长:">
          <el-input
            v-model="forms.duration"
            style="max-width: 600px"
            placeholder="Please input"
          >
            <template #append>h</template>
          </el-input>
        </el-form-item>
 
        <el-form-item label="降雨强度:">
          <el-input
            v-model="forms.intensity"
            style="max-width: 600px"
            placeholder="Please input"
          >
            <template #append>mm/h</template>
          </el-input>
        </el-form-item>
 
        <!-- <el-form-item label="仿真参数:"></el-form-item> -->
      </el-form>
      <div style="display: flex; justify-content: flex-end">
        <el-button type="primary" @click="addSimCheme">保存方案</el-button>
        <el-button type="success" @click="startPlay">开始模拟</el-button>
      </div>
    </div>
  </div>
</template>
 
<script setup>
import { reactive, ref, watch, inject, computed, onMounted } from "vue";
import * as XLSX from "xlsx";
import Papa from "papaparse";
import { ElMessage, ElMessageBox } from "element-plus";
import { initeWaterPrimitiveView } from "@/utils/water";
import { SimAPIStore } from "@/store/simAPI";
import { getRegionData } from "@/api/trApi";
import { storeToRefs } from "pinia";
 
const simStore = SimAPIStore();
const { selectTab } = storeToRefs(simStore);
 
const options = reactive([]);
 
// 定义一个方法,用于根据 type 获取区域数据
const fetchRegionData = (type) => {
  getRegionData({ type: type }).then((res) => {
    // 使用响应式数组的方法更新内容
    options.splice(
      0,
      options.length,
      ...res.data.map((item) => ({
        value: item.geom,
        label: item.name,
      }))
    );
  });
};
 
onMounted(() => {
  fetchRegionData(1);
});
 
// 监听 selectTab 的变化
watch(selectTab, (newVal) => {
  let type;
  switch (newVal) {
    case "行政区划仿真":
      type = 1;
      break;
    case "重点区域仿真":
      type = 2;
      break;
    default:
      type = 1; // 默认值
  }
  fetchRegionData(type);
});
 
// 注入父组件提供的方法
const { startSimulate, endSimulate } = inject("simulateActions");
// 表单数据
const forms = reactive({
  name: "",
  geom: "",
  rainfall: "",
  duration: "",
  intensity: "",
  fileList: [],
  type: 3,
});
 
const addSimCheme = async () => {
  await simStore.addSimCheme(forms);
};
 
// 重置表单
const resetForm = () => {
  forms.geom = "";
  forms.eares = "孙胡沟";
  forms.rainfall = "";
  forms.duration = "";
  forms.intensity = "";
  forms.fileList = [];
};
 
// 计算属性:获取上传文件的名称列表
const uploadedFilesText = computed(() => {
  return forms.fileList.map((file) => file.name).join(", ") || "无";
});
 
// 文件变化时触发解析
const handleFileChange = (file) => {
  const reader = new FileReader();
  reader.onload = (e) => {
    const data = e.target.result;
    if (file.name.endsWith(".csv")) {
      parseCSV(data);
    } else {
      parseExcel(data);
    }
  };
  reader.readAsArrayBuffer(file.raw);
};
 
// 解析CSV文件
const parseCSV = (data) => {
  Papa.parse(new TextDecoder("utf-8").decode(data), {
    complete: (results) => {
      if (results.data.length > 0) {
        processData(results.data);
      }
    },
    header: true,
    skipEmptyLines: true,
  });
};
 
// 解析Excel文件
const parseExcel = (data) => {
  const workbook = XLSX.read(data, { type: "array" });
  const firstSheetName = workbook.SheetNames[0];
  const worksheet = workbook.Sheets[firstSheetName];
  const jsonData = XLSX.utils.sheet_to_json(worksheet);
  processData(jsonData);
};
 
// 处理数据
const processData = (data) => {
  if (data.length === 0) return;
 
  const tableColumns = Object.keys(data[0]);
 
  // 第一列:时间
  const firstTime = parseDateTime(data[0][tableColumns[0]]);
  const lastTime = parseDateTime(data[data.length - 1][tableColumns[0]]);
  const timeDuration = Math.floor((lastTime - firstTime) / 1000);
  forms.duration = (timeDuration / 3600).toFixed(2); // 更新降雨时长
 
  // 第二列:降雨强度
  const maxValue = Math.max(
    ...data.map((row) => {
      const value = parseFloat(row[tableColumns[1]]);
      return isNaN(value) ? -Infinity : value;
    })
  ).toFixed(2);
  forms.intensity = maxValue; // 更新降雨强度
 
  // 第三列:累计降雨量
  const lastValue = data[data.length - 1][tableColumns[2]];
  forms.rainfall = lastValue; // 更新降雨量
};
 
// 解析日期时间
const parseDateTime = (dateString) => {
  if (typeof dateString === "number") {
    const parsedDate = XLSX.SSF.parse_date_code(dateString);
    if (parsedDate) {
      return new Date(
        parsedDate.y,
        parsedDate.m - 1,
        parsedDate.d,
        parsedDate.H || 0,
        parsedDate.M || 0,
        parsedDate.S || 0
      ).getTime();
    }
  }
 
  const parsedDate = new Date(dateString);
  if (!isNaN(parsedDate.getTime())) {
    return parsedDate.getTime();
  }
 
  const parts = dateString.split(/[/\s:]/);
  if (parts.length >= 6) {
    const year = parseInt(parts[0], 10);
    const month = parseInt(parts[1], 10) - 1;
    const day = parseInt(parts[2], 10);
    const hour = parseInt(parts[3], 10) || 0;
    const minute = parseInt(parts[4], 10) || 0;
    const second = parseInt(parts[5], 10) || 0;
 
    const date = new Date(year, month, day, hour, minute, second);
    if (!isNaN(date.getTime())) {
      return date.getTime();
    }
  }
 
  console.warn(`无法解析日期: ${dateString}`);
  return NaN;
};
 
const handleExceed = () => {
  ElMessage.warning("每次只能上传一个文件");
};
 
const beforeUpload = (file) => {
  const isExcel = file.name.endsWith(".xlsx") || file.name.endsWith(".xls");
  const isCSV = file.name.endsWith(".csv");
  if (!isExcel && !isCSV) {
    ElMessage.error("只能上传Excel或CSV文件");
    return false;
  }
  return true;
};
 
// 开始模拟
function startPlay() {
  initeWaterPrimitiveView();
  startSimulate();
}
</script>
 
<style lang="less" scoped>
.forms {
  background: url("@/assets/img/screen/leftbg.png");
  margin-top: 10px;
  width: 100%;
  height: 100%;
  padding: 10px 10px 0px 0px;
  box-sizing: border-box;
}
/deep/ .el-input-group__append,
.el-input-group__prepend {
  background-color: #084b42;
  color: #fff;
}
/deep/ .el-form-item__label {
  color: #61f7d4 !important;
}
</style>