|
|
@@ -0,0 +1,746 @@
|
|
|
1
|
+package com.ruoyi.web.calculate.service.impl;
|
|
|
2
|
+
|
|
|
3
|
+import java.io.InputStream;
|
|
|
4
|
+import java.math.BigDecimal;
|
|
|
5
|
+import java.math.RoundingMode;
|
|
|
6
|
+import java.util.ArrayList;
|
|
|
7
|
+import java.util.HashMap;
|
|
|
8
|
+import java.util.List;
|
|
|
9
|
+import java.util.Map;
|
|
|
10
|
+
|
|
|
11
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
12
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
13
|
+
|
|
|
14
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
15
|
+
|
|
|
16
|
+import org.apache.poi.ss.usermodel.Sheet;
|
|
|
17
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
18
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
19
|
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
20
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
21
|
+import org.springframework.stereotype.Service;
|
|
|
22
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
23
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
24
|
+
|
|
|
25
|
+import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
26
|
+import com.ruoyi.common.utils.poi.ExcelUtil;
|
|
|
27
|
+// import com.ruoyi.web.calculate.mapper.CmcGaussPositiveMapper;
|
|
|
28
|
+import com.ruoyi.web.calculate.domain.CmcEleDifSideLandDir;
|
|
|
29
|
+import com.ruoyi.web.calculate.service.ICmcEleDifSideLandDirService;
|
|
|
30
|
+
|
|
|
31
|
+/**
|
|
|
32
|
+ * 高差边长方向值服务实现
|
|
|
33
|
+ *
|
|
|
34
|
+ * @author R
|
|
|
35
|
+ * @date 2026-06-12
|
|
|
36
|
+ */
|
|
|
37
|
+@Service
|
|
|
38
|
+public class CmcEleDifSideLandDirServiceImpl implements ICmcEleDifSideLandDirService {
|
|
|
39
|
+ /**
|
|
|
40
|
+ * 导入Excel数据(支持多个工作表)
|
|
|
41
|
+ */
|
|
|
42
|
+ @Override
|
|
|
43
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
44
|
+ public AjaxResult importExcelData(MultipartFile file, List<String> sheetNames) {
|
|
|
45
|
+ return importExcelData(file, sheetNames, null);
|
|
|
46
|
+ }
|
|
|
47
|
+
|
|
|
48
|
+ /**
|
|
|
49
|
+ * 导入Excel数据(支持多个工作表和列映射)
|
|
|
50
|
+ */
|
|
|
51
|
+ @Override
|
|
|
52
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
53
|
+ public AjaxResult importExcelData(MultipartFile file, List<String> sheetNames, String columnMappingsJson) {
|
|
|
54
|
+ // 解析列映射配置
|
|
|
55
|
+ Map<String, Map<String, Integer>> columnMappings = null;
|
|
|
56
|
+ if (columnMappingsJson != null && !columnMappingsJson.isEmpty()) {
|
|
|
57
|
+ try {
|
|
|
58
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
59
|
+ columnMappings = mapper.readValue(columnMappingsJson,
|
|
|
60
|
+ new TypeReference<Map<String, Map<String, Integer>>>() {});
|
|
|
61
|
+ } catch (Exception e) {
|
|
|
62
|
+ // 解析失败,使用默认映射
|
|
|
63
|
+ }
|
|
|
64
|
+ }
|
|
|
65
|
+ try {
|
|
|
66
|
+ // 存储每个工作表的数据
|
|
|
67
|
+ List<Map<String, Object>> sheetDataList = new ArrayList<>();
|
|
|
68
|
+
|
|
|
69
|
+ // 合并所有工作表的数据用于计算
|
|
|
70
|
+ List<CmcEleDifSideLandDir> allDataList = new ArrayList<>();
|
|
|
71
|
+
|
|
|
72
|
+ List<String> targetSheetNames;
|
|
|
73
|
+
|
|
|
74
|
+ // 判断是否指定了工作表
|
|
|
75
|
+ if (sheetNames != null && !sheetNames.isEmpty()) {
|
|
|
76
|
+ targetSheetNames = sheetNames;
|
|
|
77
|
+ } else {
|
|
|
78
|
+ // 如果未指定,读取所有工作表
|
|
|
79
|
+ targetSheetNames = getSheetNames(file);
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ // 获取当前工作表的列映射
|
|
|
83
|
+ Map<String, Integer> currentMapping = null;
|
|
|
84
|
+
|
|
|
85
|
+ // 读取指定的多个工作表
|
|
|
86
|
+ for (String sheetName : targetSheetNames) {
|
|
|
87
|
+ // 获取当前工作表的列映射配置
|
|
|
88
|
+ if (columnMappings != null && columnMappings.containsKey(sheetName)) {
|
|
|
89
|
+ currentMapping = columnMappings.get(sheetName);
|
|
|
90
|
+ } else {
|
|
|
91
|
+ currentMapping = null;
|
|
|
92
|
+ }
|
|
|
93
|
+
|
|
|
94
|
+ List<CmcEleDifSideLandDir> sheetData = importExcelFromSheet(file, sheetName, currentMapping);
|
|
|
95
|
+ List<String> headers = readSheetHeaders(file, sheetName);
|
|
|
96
|
+ List<List<Object>> rawData = readRawSheetData(file, sheetName);
|
|
|
97
|
+ if (sheetData != null && !sheetData.isEmpty()) {
|
|
|
98
|
+ // 按工作表分组存储
|
|
|
99
|
+ Map<String, Object> sheetInfo = new HashMap<>();
|
|
|
100
|
+ sheetInfo.put("sheetName", sheetName);
|
|
|
101
|
+ sheetInfo.put("headers", headers);
|
|
|
102
|
+ sheetInfo.put("rawData", rawData);
|
|
|
103
|
+ sheetInfo.put("data", convertToMapList(sheetData));
|
|
|
104
|
+ sheetDataList.add(sheetInfo);
|
|
|
105
|
+
|
|
|
106
|
+ // 添加到合并列表用于后续计算
|
|
|
107
|
+ allDataList.addAll(sheetData);
|
|
|
108
|
+ }
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ // 检查是否有数据
|
|
|
112
|
+ if (allDataList == null || allDataList.isEmpty()) {
|
|
|
113
|
+ return AjaxResult.error("Excel文件中没有数据!");
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ // 将数据分为两组:点信息组 和 方向信息组
|
|
|
117
|
+ // 第一组:ipointNumber, pointName, cpX, cpY, cpZ(点的基本信息)
|
|
|
118
|
+ // 第二组:stationPN, directionPN(测站和方向信息)
|
|
|
119
|
+
|
|
|
120
|
+ List<Map<String, Object>> pointDataList = new ArrayList<>(); // 点信息组
|
|
|
121
|
+ List<Map<String, Object>> directionDataList = new ArrayList<>(); // 方向信息组
|
|
|
122
|
+
|
|
|
123
|
+ int pointCount = 0;
|
|
|
124
|
+ int directionCount = 0;
|
|
|
125
|
+ StringBuilder errorMsg = new StringBuilder();
|
|
|
126
|
+
|
|
|
127
|
+ for (int i = 0; i < allDataList.size(); i++) {
|
|
|
128
|
+ CmcEleDifSideLandDir item = allDataList.get(i);
|
|
|
129
|
+
|
|
|
130
|
+ try {
|
|
|
131
|
+ // 判断是点信息还是方向信息
|
|
|
132
|
+ boolean hasPointInfo = item.getPointNumber() != null && !item.getPointNumber().isEmpty();
|
|
|
133
|
+ boolean hasDirectionInfo = item.getStationPN() != null && item.getDirectionPN() != null;
|
|
|
134
|
+
|
|
|
135
|
+ // 如果有点号,作为点信息处理
|
|
|
136
|
+ if (hasPointInfo) {
|
|
|
137
|
+ Map<String, Object> pointMap = new HashMap<>();
|
|
|
138
|
+ pointMap.put("pointNumber", item.getPointNumber());
|
|
|
139
|
+ pointMap.put("pointName", item.getPointName());
|
|
|
140
|
+ pointMap.put("cpX", item.getCPX());
|
|
|
141
|
+ pointMap.put("cpY", item.getCPY());
|
|
|
142
|
+ pointMap.put("cpZ", item.getCPZ());
|
|
|
143
|
+ pointMap.put("rowNum", i + 2); // Excel中的行号
|
|
|
144
|
+ pointDataList.add(pointMap);
|
|
|
145
|
+ pointCount++;
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ // 如果有测站和方向序号,作为方向信息处理
|
|
|
149
|
+ if (hasDirectionInfo) {
|
|
|
150
|
+ Map<String, Object> directionMap = new HashMap<>();
|
|
|
151
|
+ directionMap.put("pointNumber", item.getPointNumber()); // 关联的点号(可能为空)
|
|
|
152
|
+ directionMap.put("stationPN", item.getStationPN());
|
|
|
153
|
+ directionMap.put("directionPN", item.getDirectionPN());
|
|
|
154
|
+ directionMap.put("rowNum", i + 2); // Excel中的行号
|
|
|
155
|
+ directionDataList.add(directionMap);
|
|
|
156
|
+ directionCount++;
|
|
|
157
|
+ }
|
|
|
158
|
+
|
|
|
159
|
+ } catch (Exception e) {
|
|
|
160
|
+ errorMsg.append(String.format("第%d行数据验证失败:%s;", i + 2, e.getMessage()));
|
|
|
161
|
+ }
|
|
|
162
|
+ }
|
|
|
163
|
+
|
|
|
164
|
+ // 构建返回结果
|
|
|
165
|
+ Map<String, Object> result = new HashMap<>();
|
|
|
166
|
+ result.put("total", allDataList.size());
|
|
|
167
|
+ result.put("pointCount", pointCount);
|
|
|
168
|
+ result.put("directionCount", directionCount);
|
|
|
169
|
+ result.put("pointData", pointDataList);
|
|
|
170
|
+ result.put("directionData", directionDataList);
|
|
|
171
|
+ // 添加按工作表分组的数据
|
|
|
172
|
+ result.put("sheetData", sheetDataList);
|
|
|
173
|
+
|
|
|
174
|
+ if (pointCount == 0 && directionCount == 0) {
|
|
|
175
|
+ return AjaxResult.error("没有有效数据!" + errorMsg.toString());
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+ String message = String.format("导入成功!共解析%d条数据,其中点信息%d条,方向信息%d条",
|
|
|
179
|
+ allDataList.size(), pointCount, directionCount);
|
|
|
180
|
+ if (errorMsg.length() > 0) {
|
|
|
181
|
+ message += ";部分数据验证失败:" + errorMsg.toString();
|
|
|
182
|
+ return AjaxResult.success(message, result);
|
|
|
183
|
+ }
|
|
|
184
|
+
|
|
|
185
|
+ return AjaxResult.success(message, result);
|
|
|
186
|
+
|
|
|
187
|
+ } catch (Exception e) {
|
|
|
188
|
+ e.printStackTrace();
|
|
|
189
|
+ return AjaxResult.error("导入失败:" + e.getMessage());
|
|
|
190
|
+ }
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ /**
|
|
|
194
|
+ * 将实体列表转换为Map列表
|
|
|
195
|
+ */
|
|
|
196
|
+ private List<Map<String, Object>> convertToMapList(List<CmcEleDifSideLandDir> dataList) {
|
|
|
197
|
+ List<Map<String, Object>> result = new ArrayList<>();
|
|
|
198
|
+ for (CmcEleDifSideLandDir item : dataList) {
|
|
|
199
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
200
|
+ map.put("pointNumber", item.getPointNumber());
|
|
|
201
|
+ map.put("pointName", item.getPointName());
|
|
|
202
|
+ map.put("cpX", item.getCPX());
|
|
|
203
|
+ map.put("cpY", item.getCPY());
|
|
|
204
|
+ map.put("cpZ", item.getCPZ());
|
|
|
205
|
+ map.put("stationPN", item.getStationPN());
|
|
|
206
|
+ map.put("directionPN", item.getDirectionPN());
|
|
|
207
|
+ result.add(map);
|
|
|
208
|
+ }
|
|
|
209
|
+ return result;
|
|
|
210
|
+ }
|
|
|
211
|
+
|
|
|
212
|
+ /**
|
|
|
213
|
+ * 读取工作表的表头信息
|
|
|
214
|
+ *
|
|
|
215
|
+ * @param file Excel文件
|
|
|
216
|
+ * @param sheetName 工作表名称
|
|
|
217
|
+ * @return 表头列表
|
|
|
218
|
+ */
|
|
|
219
|
+ private List<String> readSheetHeaders(MultipartFile file, String sheetName) {
|
|
|
220
|
+ List<String> headers = new ArrayList<>();
|
|
|
221
|
+
|
|
|
222
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
223
|
+ Workbook workbook = createWorkbook(inputStream, file.getOriginalFilename())) {
|
|
|
224
|
+ // 查找指定名称的工作表
|
|
|
225
|
+ Sheet sheet = workbook.getSheet(sheetName);
|
|
|
226
|
+
|
|
|
227
|
+ if (sheet == null) {
|
|
|
228
|
+ // 如果按名称找不到,尝试按索引查找
|
|
|
229
|
+ try {
|
|
|
230
|
+ int sheetIndex = Integer.parseInt(sheetName);
|
|
|
231
|
+ sheet = workbook.getSheetAt(sheetIndex);
|
|
|
232
|
+ } catch (NumberFormatException e) {
|
|
|
233
|
+ return headers;
|
|
|
234
|
+ }
|
|
|
235
|
+ }
|
|
|
236
|
+
|
|
|
237
|
+ if (sheet == null) {
|
|
|
238
|
+ return headers;
|
|
|
239
|
+ }
|
|
|
240
|
+
|
|
|
241
|
+ // 读取第一行作为表头
|
|
|
242
|
+ org.apache.poi.ss.usermodel.Row headerRow = sheet.getRow(0);
|
|
|
243
|
+ if (headerRow != null) {
|
|
|
244
|
+ int lastCellNum = headerRow.getLastCellNum();
|
|
|
245
|
+ for (int i = 0; i < lastCellNum; i++) {
|
|
|
246
|
+ headers.add(getCellStringValue(headerRow.getCell(i)));
|
|
|
247
|
+ }
|
|
|
248
|
+ }
|
|
|
249
|
+
|
|
|
250
|
+ } catch (Exception e) {
|
|
|
251
|
+ e.printStackTrace();
|
|
|
252
|
+ }
|
|
|
253
|
+
|
|
|
254
|
+ return headers;
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ /**
|
|
|
258
|
+ * 读取工作表的原始数据(从第二行开始)
|
|
|
259
|
+ *
|
|
|
260
|
+ * @param file Excel文件
|
|
|
261
|
+ * @param sheetName 工作表名称
|
|
|
262
|
+ * @return 原始数据列表
|
|
|
263
|
+ */
|
|
|
264
|
+ private List<List<Object>> readRawSheetData(MultipartFile file, String sheetName) {
|
|
|
265
|
+ List<List<Object>> rawData = new ArrayList<>();
|
|
|
266
|
+
|
|
|
267
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
268
|
+ Workbook workbook = createWorkbook(inputStream, file.getOriginalFilename())) {
|
|
|
269
|
+ // 查找指定名称的工作表
|
|
|
270
|
+ Sheet sheet = workbook.getSheet(sheetName);
|
|
|
271
|
+
|
|
|
272
|
+ if (sheet == null) {
|
|
|
273
|
+ // 如果按名称找不到,尝试按索引查找
|
|
|
274
|
+ try {
|
|
|
275
|
+ int sheetIndex = Integer.parseInt(sheetName);
|
|
|
276
|
+ sheet = workbook.getSheetAt(sheetIndex);
|
|
|
277
|
+ } catch (NumberFormatException e) {
|
|
|
278
|
+ return rawData;
|
|
|
279
|
+ }
|
|
|
280
|
+ }
|
|
|
281
|
+
|
|
|
282
|
+ if (sheet == null) {
|
|
|
283
|
+ return rawData;
|
|
|
284
|
+ }
|
|
|
285
|
+
|
|
|
286
|
+ // 获取表头列数
|
|
|
287
|
+ org.apache.poi.ss.usermodel.Row headerRow = sheet.getRow(0);
|
|
|
288
|
+ int colCount = headerRow != null ? headerRow.getLastCellNum() : 10;
|
|
|
289
|
+
|
|
|
290
|
+ // 从第二行开始读取数据
|
|
|
291
|
+ int lastRowNum = sheet.getLastRowNum();
|
|
|
292
|
+ for (int i = 1; i <= lastRowNum; i++) {
|
|
|
293
|
+ org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
|
|
|
294
|
+ if (row == null) continue;
|
|
|
295
|
+
|
|
|
296
|
+ List<Object> rowData = new ArrayList<>();
|
|
|
297
|
+ for (int j = 0; j < colCount; j++) {
|
|
|
298
|
+ rowData.add(getCellStringValue(row.getCell(j)));
|
|
|
299
|
+ }
|
|
|
300
|
+
|
|
|
301
|
+ // 跳过空行
|
|
|
302
|
+ boolean hasData = false;
|
|
|
303
|
+ for (Object obj : rowData) {
|
|
|
304
|
+ if (obj != null && !"".equals(obj.toString().trim())) {
|
|
|
305
|
+ hasData = true;
|
|
|
306
|
+ break;
|
|
|
307
|
+ }
|
|
|
308
|
+ }
|
|
|
309
|
+ if (hasData) {
|
|
|
310
|
+ rawData.add(rowData);
|
|
|
311
|
+ }
|
|
|
312
|
+ }
|
|
|
313
|
+
|
|
|
314
|
+ } catch (Exception e) {
|
|
|
315
|
+ e.printStackTrace();
|
|
|
316
|
+ }
|
|
|
317
|
+
|
|
|
318
|
+ return rawData;
|
|
|
319
|
+ }
|
|
|
320
|
+
|
|
|
321
|
+ /**
|
|
|
322
|
+ * 从指定工作表读取Excel数据
|
|
|
323
|
+ *
|
|
|
324
|
+ * @param file Excel文件
|
|
|
325
|
+ * @param sheetName 工作表名称
|
|
|
326
|
+ * @return 数据列表
|
|
|
327
|
+ */
|
|
|
328
|
+ private List<CmcEleDifSideLandDir> importExcelFromSheet(MultipartFile file, String sheetName) {
|
|
|
329
|
+ return importExcelFromSheet(file, sheetName, null);
|
|
|
330
|
+ }
|
|
|
331
|
+
|
|
|
332
|
+ /**
|
|
|
333
|
+ * 从指定工作表读取Excel数据(支持列映射)
|
|
|
334
|
+ *
|
|
|
335
|
+ * @param file Excel文件
|
|
|
336
|
+ * @param sheetName 工作表名称
|
|
|
337
|
+ * @param columnMapping 列映射配置(字段名 -> 列索引),为空则使用默认映射
|
|
|
338
|
+ * @return 数据列表
|
|
|
339
|
+ */
|
|
|
340
|
+ private List<CmcEleDifSideLandDir> importExcelFromSheet(MultipartFile file, String sheetName, Map<String, Integer> columnMapping) {
|
|
|
341
|
+ List<CmcEleDifSideLandDir> dataList = new ArrayList<>();
|
|
|
342
|
+
|
|
|
343
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
344
|
+ Workbook workbook = createWorkbook(inputStream, file.getOriginalFilename())) {
|
|
|
345
|
+ // 查找指定名称的工作表
|
|
|
346
|
+ Sheet sheet = workbook.getSheet(sheetName);
|
|
|
347
|
+
|
|
|
348
|
+ if (sheet == null) {
|
|
|
349
|
+ // 如果按名称找不到,尝试按索引查找
|
|
|
350
|
+ try {
|
|
|
351
|
+ int sheetIndex = Integer.parseInt(sheetName);
|
|
|
352
|
+ sheet = workbook.getSheetAt(sheetIndex);
|
|
|
353
|
+ } catch (NumberFormatException e) {
|
|
|
354
|
+ // 不是数字索引,返回空列表
|
|
|
355
|
+ return dataList;
|
|
|
356
|
+ }
|
|
|
357
|
+ }
|
|
|
358
|
+
|
|
|
359
|
+ if (sheet == null) {
|
|
|
360
|
+ return dataList;
|
|
|
361
|
+ }
|
|
|
362
|
+
|
|
|
363
|
+ // 直接使用POI解析Sheet数据
|
|
|
364
|
+ int lastRowNum = sheet.getLastRowNum();
|
|
|
365
|
+ // 从第二行开始读取(跳过表头)
|
|
|
366
|
+ for (int i = 1; i <= lastRowNum; i++) {
|
|
|
367
|
+ org.apache.poi.ss.usermodel.Row row = sheet.getRow(i);
|
|
|
368
|
+ if (row == null) {
|
|
|
369
|
+ continue;
|
|
|
370
|
+ }
|
|
|
371
|
+
|
|
|
372
|
+ CmcEleDifSideLandDir item = new CmcEleDifSideLandDir();
|
|
|
373
|
+
|
|
|
374
|
+ if (columnMapping != null && !columnMapping.isEmpty()) {
|
|
|
375
|
+ // 使用自定义列映射
|
|
|
376
|
+ int colIndex = columnMapping.getOrDefault("pointNumber", -1);
|
|
|
377
|
+ if (colIndex >= 0) {
|
|
|
378
|
+ item.setPointNumber(getCellStringValue(row.getCell(colIndex)));
|
|
|
379
|
+ }
|
|
|
380
|
+
|
|
|
381
|
+ colIndex = columnMapping.getOrDefault("pointName", -1);
|
|
|
382
|
+ if (colIndex >= 0) {
|
|
|
383
|
+ item.setPointName(getCellStringValue(row.getCell(colIndex)));
|
|
|
384
|
+ }
|
|
|
385
|
+
|
|
|
386
|
+ colIndex = columnMapping.getOrDefault("cpX", -1);
|
|
|
387
|
+ if (colIndex >= 0) {
|
|
|
388
|
+ item.setCPX(getCellDoubleValue(row.getCell(colIndex)));
|
|
|
389
|
+ }
|
|
|
390
|
+
|
|
|
391
|
+ colIndex = columnMapping.getOrDefault("cpY", -1);
|
|
|
392
|
+ if (colIndex >= 0) {
|
|
|
393
|
+ item.setCPY(getCellDoubleValue(row.getCell(colIndex)));
|
|
|
394
|
+ }
|
|
|
395
|
+
|
|
|
396
|
+ colIndex = columnMapping.getOrDefault("cpZ", -1);
|
|
|
397
|
+ if (colIndex >= 0) {
|
|
|
398
|
+ item.setCPZ(getCellDoubleValue(row.getCell(colIndex)));
|
|
|
399
|
+ }
|
|
|
400
|
+
|
|
|
401
|
+ colIndex = columnMapping.getOrDefault("stationPN", -1);
|
|
|
402
|
+ if (colIndex >= 0) {
|
|
|
403
|
+ item.setStationPN(getCellIntegerValue(row.getCell(colIndex)));
|
|
|
404
|
+ }
|
|
|
405
|
+
|
|
|
406
|
+ colIndex = columnMapping.getOrDefault("directionPN", -1);
|
|
|
407
|
+ if (colIndex >= 0) {
|
|
|
408
|
+ item.setDirectionPN(getCellIntegerValue(row.getCell(colIndex)));
|
|
|
409
|
+ }
|
|
|
410
|
+ } else {
|
|
|
411
|
+ // 使用默认列映射:ipointNumber, pointName, cpX, cpY, cpZ, stationPN, directionPN
|
|
|
412
|
+ item.setPointNumber(getCellStringValue(row.getCell(0)));
|
|
|
413
|
+ item.setPointName(getCellStringValue(row.getCell(1)));
|
|
|
414
|
+ item.setCPX(getCellDoubleValue(row.getCell(2)));
|
|
|
415
|
+ item.setCPY(getCellDoubleValue(row.getCell(3)));
|
|
|
416
|
+ item.setCPZ(getCellDoubleValue(row.getCell(4)));
|
|
|
417
|
+ item.setStationPN(getCellIntegerValue(row.getCell(5)));
|
|
|
418
|
+ item.setDirectionPN(getCellIntegerValue(row.getCell(6)));
|
|
|
419
|
+ }
|
|
|
420
|
+
|
|
|
421
|
+ dataList.add(item);
|
|
|
422
|
+ }
|
|
|
423
|
+
|
|
|
424
|
+ } catch (Exception e) {
|
|
|
425
|
+ e.printStackTrace();
|
|
|
426
|
+ }
|
|
|
427
|
+
|
|
|
428
|
+ return dataList;
|
|
|
429
|
+ }
|
|
|
430
|
+
|
|
|
431
|
+ /**
|
|
|
432
|
+ * 获取单元格的字符串值
|
|
|
433
|
+ */
|
|
|
434
|
+ private String getCellStringValue(org.apache.poi.ss.usermodel.Cell cell) {
|
|
|
435
|
+ if (cell == null) {
|
|
|
436
|
+ return null;
|
|
|
437
|
+ }
|
|
|
438
|
+ switch (cell.getCellType()) {
|
|
|
439
|
+ case STRING:
|
|
|
440
|
+ return cell.getStringCellValue();
|
|
|
441
|
+ case NUMERIC:
|
|
|
442
|
+ // 数字类型转字符串,避免科学计数法
|
|
|
443
|
+ double numValue = cell.getNumericCellValue();
|
|
|
444
|
+ if (numValue == Math.floor(numValue)) {
|
|
|
445
|
+ return String.valueOf((long) numValue);
|
|
|
446
|
+ }
|
|
|
447
|
+ return String.valueOf(numValue);
|
|
|
448
|
+ case BOOLEAN:
|
|
|
449
|
+ return String.valueOf(cell.getBooleanCellValue());
|
|
|
450
|
+ case FORMULA:
|
|
|
451
|
+ try {
|
|
|
452
|
+ return cell.getStringCellValue();
|
|
|
453
|
+ } catch (Exception e) {
|
|
|
454
|
+ return String.valueOf(cell.getNumericCellValue());
|
|
|
455
|
+ }
|
|
|
456
|
+ default:
|
|
|
457
|
+ return null;
|
|
|
458
|
+ }
|
|
|
459
|
+ }
|
|
|
460
|
+
|
|
|
461
|
+ /**
|
|
|
462
|
+ * 获取单元格的Double值
|
|
|
463
|
+ */
|
|
|
464
|
+ private Double getCellDoubleValue(org.apache.poi.ss.usermodel.Cell cell) {
|
|
|
465
|
+ if (cell == null) {
|
|
|
466
|
+ return null;
|
|
|
467
|
+ }
|
|
|
468
|
+ switch (cell.getCellType()) {
|
|
|
469
|
+ case NUMERIC:
|
|
|
470
|
+ return cell.getNumericCellValue();
|
|
|
471
|
+ case STRING:
|
|
|
472
|
+ try {
|
|
|
473
|
+ return Double.parseDouble(cell.getStringCellValue().trim());
|
|
|
474
|
+ } catch (NumberFormatException e) {
|
|
|
475
|
+ return null;
|
|
|
476
|
+ }
|
|
|
477
|
+ default:
|
|
|
478
|
+ return null;
|
|
|
479
|
+ }
|
|
|
480
|
+ }
|
|
|
481
|
+
|
|
|
482
|
+ /**
|
|
|
483
|
+ * 获取单元格的Integer值
|
|
|
484
|
+ */
|
|
|
485
|
+ private Integer getCellIntegerValue(org.apache.poi.ss.usermodel.Cell cell) {
|
|
|
486
|
+ if (cell == null) {
|
|
|
487
|
+ return null;
|
|
|
488
|
+ }
|
|
|
489
|
+ switch (cell.getCellType()) {
|
|
|
490
|
+ case NUMERIC:
|
|
|
491
|
+ return (int) cell.getNumericCellValue();
|
|
|
492
|
+ case STRING:
|
|
|
493
|
+ try {
|
|
|
494
|
+ return Integer.parseInt(cell.getStringCellValue().trim());
|
|
|
495
|
+ } catch (NumberFormatException e) {
|
|
|
496
|
+ return null;
|
|
|
497
|
+ }
|
|
|
498
|
+ default:
|
|
|
499
|
+ return null;
|
|
|
500
|
+ }
|
|
|
501
|
+ }
|
|
|
502
|
+
|
|
|
503
|
+ /**
|
|
|
504
|
+ * 根据文件类型创建Workbook
|
|
|
505
|
+ */
|
|
|
506
|
+ private Workbook createWorkbook(InputStream inputStream, String fileName) throws Exception {
|
|
|
507
|
+ if (fileName != null && fileName.endsWith(".xlsx")) {
|
|
|
508
|
+ return new XSSFWorkbook(inputStream);
|
|
|
509
|
+ } else {
|
|
|
510
|
+ return new HSSFWorkbook(inputStream);
|
|
|
511
|
+ }
|
|
|
512
|
+ }
|
|
|
513
|
+
|
|
|
514
|
+ /**
|
|
|
515
|
+ * 获取Excel文件的所有工作表名称
|
|
|
516
|
+ *
|
|
|
517
|
+ * @param file Excel文件
|
|
|
518
|
+ * @return 工作表名称列表
|
|
|
519
|
+ */
|
|
|
520
|
+ @Override
|
|
|
521
|
+ public List<String> getSheetNames(MultipartFile file) {
|
|
|
522
|
+ List<String> sheetNames = new ArrayList<>();
|
|
|
523
|
+
|
|
|
524
|
+ try (InputStream inputStream = file.getInputStream();
|
|
|
525
|
+ Workbook workbook = createWorkbook(inputStream, file.getOriginalFilename())) {
|
|
|
526
|
+ int numberOfSheets = workbook.getNumberOfSheets();
|
|
|
527
|
+ for (int i = 0; i < numberOfSheets; i++) {
|
|
|
528
|
+ sheetNames.add(workbook.getSheetName(i));
|
|
|
529
|
+ }
|
|
|
530
|
+ } catch (Exception e) {
|
|
|
531
|
+ e.printStackTrace();
|
|
|
532
|
+ }
|
|
|
533
|
+
|
|
|
534
|
+ return sheetNames;
|
|
|
535
|
+ }
|
|
|
536
|
+
|
|
|
537
|
+ /**
|
|
|
538
|
+ * 执行高差边长方向值计算
|
|
|
539
|
+ */
|
|
|
540
|
+ @Override
|
|
|
541
|
+ public List<CmcEleDifSideLandDir> calculateEleDifSideLandDir(List<CmcEleDifSideLandDir> dataList) {
|
|
|
542
|
+ return calculateEleDifSideLandDir(dataList, null);
|
|
|
543
|
+ }
|
|
|
544
|
+
|
|
|
545
|
+ /**
|
|
|
546
|
+ * 执行高差边长方向值计算(带列映射)
|
|
|
547
|
+ */
|
|
|
548
|
+ @Override
|
|
|
549
|
+ public List<CmcEleDifSideLandDir> calculateEleDifSideLandDir(List<CmcEleDifSideLandDir> dataList, Map<String, Map<String, Integer>> columnMappings) {
|
|
|
550
|
+ // 列映射参数在计算阶段不需要使用,因为数据已经在导入阶段按映射解析好了
|
|
|
551
|
+ // 这里保留方法签名以兼容接口
|
|
|
552
|
+ List<CmcEleDifSideLandDir> resultList = new ArrayList<>();
|
|
|
553
|
+
|
|
|
554
|
+ // 第一组:控制网点信息,key 是序号(ipointNumber作为序号)
|
|
|
555
|
+ // 假设 ipointNumber 是整数序号,用于匹配 stationPN 和 directionPN
|
|
|
556
|
+ Map<Integer, CmcEleDifSideLandDir> controlPointMap = new HashMap<>();
|
|
|
557
|
+
|
|
|
558
|
+ // 第二组:测站和方向序号对
|
|
|
559
|
+ List<Map<String, Integer>> stationDirectionList = new ArrayList<>();
|
|
|
560
|
+
|
|
|
561
|
+ for (CmcEleDifSideLandDir item : dataList) {
|
|
|
562
|
+ try {
|
|
|
563
|
+ String pointNumber = item.getPointNumber();
|
|
|
564
|
+ Integer stationPN = item.getStationPN();
|
|
|
565
|
+ Integer directionPN = item.getDirectionPN();
|
|
|
566
|
+
|
|
|
567
|
+ // 处理第一组:控制网点信息
|
|
|
568
|
+ // 检查是否有有效的坐标信息(cpX/cpY/cpZ)
|
|
|
569
|
+ boolean hasCoordinates = item.getCPX() != null || item.getCPY() != null || item.getCPZ() != null;
|
|
|
570
|
+
|
|
|
571
|
+ // 优先使用 pointNumber 作为序号,如果不是数字则尝试使用其他字段
|
|
|
572
|
+ Integer seqNumber = null;
|
|
|
573
|
+ if (pointNumber != null && !pointNumber.isEmpty()) {
|
|
|
574
|
+ try {
|
|
|
575
|
+ seqNumber = Integer.parseInt(pointNumber.trim());
|
|
|
576
|
+ } catch (NumberFormatException e) {
|
|
|
577
|
+ // 如果点号不是数字格式,尝试使用stationPN作为备选序号
|
|
|
578
|
+ if (stationPN != null) {
|
|
|
579
|
+ seqNumber = stationPN;
|
|
|
580
|
+ } else if (directionPN != null) {
|
|
|
581
|
+ seqNumber = directionPN;
|
|
|
582
|
+ }
|
|
|
583
|
+ }
|
|
|
584
|
+ } else if (stationPN != null) {
|
|
|
585
|
+ // 如果没有pointNumber,使用stationPN作为序号
|
|
|
586
|
+ seqNumber = stationPN;
|
|
|
587
|
+ } else if (directionPN != null) {
|
|
|
588
|
+ // 如果没有pointNumber和stationPN,使用directionPN作为序号
|
|
|
589
|
+ seqNumber = directionPN;
|
|
|
590
|
+ }
|
|
|
591
|
+
|
|
|
592
|
+ // 如果有序号且有坐标信息,添加到控制点映射
|
|
|
593
|
+ if (seqNumber != null && hasCoordinates) {
|
|
|
594
|
+ if (!controlPointMap.containsKey(seqNumber)) {
|
|
|
595
|
+ CmcEleDifSideLandDir pointData = new CmcEleDifSideLandDir();
|
|
|
596
|
+ pointData.setPointNumber(item.getPointNumber());
|
|
|
597
|
+ pointData.setPointName(item.getPointName());
|
|
|
598
|
+ pointData.setCPX(item.getCPX());
|
|
|
599
|
+ pointData.setCPY(item.getCPY());
|
|
|
600
|
+ pointData.setCPZ(item.getCPZ());
|
|
|
601
|
+ controlPointMap.put(seqNumber, pointData);
|
|
|
602
|
+ }
|
|
|
603
|
+ }
|
|
|
604
|
+
|
|
|
605
|
+ // 处理第二组:测站和方向序号(独立处理,不依赖pointNumber)
|
|
|
606
|
+ if (stationPN != null && directionPN != null) {
|
|
|
607
|
+ Map<String, Integer> stationDir = new HashMap<>();
|
|
|
608
|
+ stationDir.put("stationPN", stationPN);
|
|
|
609
|
+ stationDir.put("directionPN", directionPN);
|
|
|
610
|
+ stationDirectionList.add(stationDir);
|
|
|
611
|
+ }
|
|
|
612
|
+
|
|
|
613
|
+ } catch (Exception e) {
|
|
|
614
|
+ e.printStackTrace();
|
|
|
615
|
+ }
|
|
|
616
|
+ }
|
|
|
617
|
+
|
|
|
618
|
+ // 根据 stationPN 和 directionPN 匹配控制网点坐标
|
|
|
619
|
+ for (Map<String, Integer> stationDir : stationDirectionList) {
|
|
|
620
|
+ Integer stationPN = stationDir.get("stationPN");
|
|
|
621
|
+ Integer directionPN = stationDir.get("directionPN");
|
|
|
622
|
+
|
|
|
623
|
+ CmcEleDifSideLandDir result = new CmcEleDifSideLandDir();
|
|
|
624
|
+
|
|
|
625
|
+ // 设置序号(始终设置,即使匹配失败)
|
|
|
626
|
+ result.setStationPN(stationPN);
|
|
|
627
|
+ result.setDirectionPN(directionPN);
|
|
|
628
|
+
|
|
|
629
|
+ // 根据 stationPN 查找测站控制网点坐标
|
|
|
630
|
+ CmcEleDifSideLandDir stationPoint = controlPointMap.get(stationPN);
|
|
|
631
|
+ if (stationPoint != null) {
|
|
|
632
|
+ // 将测站控制网点坐标设置为测站坐标
|
|
|
633
|
+ result.setSPX(stationPoint.getCPX());
|
|
|
634
|
+ result.setSPY(stationPoint.getCPY());
|
|
|
635
|
+ result.setSPZ(stationPoint.getCPZ());
|
|
|
636
|
+ result.setStationPointName(stationPoint.getPointName());
|
|
|
637
|
+ }
|
|
|
638
|
+
|
|
|
639
|
+ // 根据 directionPN 查找方向控制网点坐标
|
|
|
640
|
+ CmcEleDifSideLandDir directionPoint = controlPointMap.get(directionPN);
|
|
|
641
|
+ if (directionPoint != null) {
|
|
|
642
|
+ // 将方向控制网点坐标设置为方向坐标
|
|
|
643
|
+ result.setDPX(directionPoint.getCPX());
|
|
|
644
|
+ result.setDPY(directionPoint.getCPY());
|
|
|
645
|
+ result.setDPZ(directionPoint.getCPZ());
|
|
|
646
|
+ result.setDirectionPointName(directionPoint.getPointName());
|
|
|
647
|
+ }
|
|
|
648
|
+
|
|
|
649
|
+ // 只要有一个匹配成功就添加到结果(允许部分匹配)
|
|
|
650
|
+ if (stationPoint != null || directionPoint != null) {
|
|
|
651
|
+ resultList.add(result);
|
|
|
652
|
+ }
|
|
|
653
|
+ }
|
|
|
654
|
+
|
|
|
655
|
+ return resultList;
|
|
|
656
|
+ }
|
|
|
657
|
+
|
|
|
658
|
+ /**
|
|
|
659
|
+ * 计算方位角
|
|
|
660
|
+ * 使用 ATAN2(方向y-测站y, 方向x-测站x) 并转换为度
|
|
|
661
|
+ * 同一测站号的所有记录使用第一个计算出的方位角值
|
|
|
662
|
+ * 同时计算角度差:原始方位角 - 复用后的方位角
|
|
|
663
|
+ *
|
|
|
664
|
+ * @param resultList 包含测站坐标和方向坐标的结果列表
|
|
|
665
|
+ * @return 计算方位角后的结果列表
|
|
|
666
|
+ */
|
|
|
667
|
+ public List<CmcEleDifSideLandDir> calculateAzimuthAngle(List<CmcEleDifSideLandDir> resultList) {
|
|
|
668
|
+ // 缓存每个测站号对应的第一个方位角值
|
|
|
669
|
+ Map<Integer, Double> stationAzimuthMap = new HashMap<>();
|
|
|
670
|
+
|
|
|
671
|
+ for (CmcEleDifSideLandDir item : resultList) {
|
|
|
672
|
+ try {
|
|
|
673
|
+ Integer stationPN = item.getStationPN();
|
|
|
674
|
+
|
|
|
675
|
+ // 如果测站号为空,跳过
|
|
|
676
|
+ if (stationPN == null) {
|
|
|
677
|
+ continue;
|
|
|
678
|
+ }
|
|
|
679
|
+
|
|
|
680
|
+ Double spX = item.getSPX();
|
|
|
681
|
+ Double spY = item.getSPY();
|
|
|
682
|
+ Double dpX = item.getDPX();
|
|
|
683
|
+ Double dpY = item.getDPY();
|
|
|
684
|
+
|
|
|
685
|
+ // 计算原始方位角
|
|
|
686
|
+ Double originalAzimuth = null;
|
|
|
687
|
+ if (spX != null && spY != null && dpX != null && dpY != null) {
|
|
|
688
|
+ // 计算坐标差
|
|
|
689
|
+ double deltaX = dpX - spX;
|
|
|
690
|
+ double deltaY = dpY - spY;
|
|
|
691
|
+
|
|
|
692
|
+ // 计算 ATAN2(Δy, Δx),结果为弧度
|
|
|
693
|
+ double azimuthRadians = Math.atan2(deltaY, deltaX);
|
|
|
694
|
+
|
|
|
695
|
+ // 转换为度
|
|
|
696
|
+ originalAzimuth = Math.toDegrees(azimuthRadians);
|
|
|
697
|
+
|
|
|
698
|
+ // 方位角标准化到 0-360 度范围
|
|
|
699
|
+ if (originalAzimuth < 0) {
|
|
|
700
|
+ originalAzimuth += 360.0;
|
|
|
701
|
+ }
|
|
|
702
|
+
|
|
|
703
|
+ // 保存原始方位角
|
|
|
704
|
+ item.setOriginalAzimuthAngle(originalAzimuth);
|
|
|
705
|
+ }
|
|
|
706
|
+
|
|
|
707
|
+ // 检查该测站号是否已经有缓存的方位角值
|
|
|
708
|
+ if (stationAzimuthMap.containsKey(stationPN)) {
|
|
|
709
|
+ // 使用缓存的第一个方位角值
|
|
|
710
|
+ double reusedAzimuth = stationAzimuthMap.get(stationPN);
|
|
|
711
|
+
|
|
|
712
|
+ // 计算角度差:原始方位角 - 复用后的方位角
|
|
|
713
|
+ double angleDiff = 0.0;
|
|
|
714
|
+ if (originalAzimuth != null) {
|
|
|
715
|
+ angleDiff = originalAzimuth - reusedAzimuth;
|
|
|
716
|
+
|
|
|
717
|
+ // 如果角度差小于0,则+360,反之保持原样
|
|
|
718
|
+ if (angleDiff < 0) {
|
|
|
719
|
+ angleDiff += 360.0;
|
|
|
720
|
+ }
|
|
|
721
|
+
|
|
|
722
|
+ item.setAngleDifference(angleDiff);
|
|
|
723
|
+ }
|
|
|
724
|
+
|
|
|
725
|
+ // 设置方位角为角度差(直接使用十进制度数)
|
|
|
726
|
+ item.setAzimuthAngle(angleDiff);
|
|
|
727
|
+ } else {
|
|
|
728
|
+ // 使用自己计算的方位角作为复用值
|
|
|
729
|
+ if (originalAzimuth != null) {
|
|
|
730
|
+ // 缓存该测站号的第一个方位角值
|
|
|
731
|
+ stationAzimuthMap.put(stationPN, originalAzimuth);
|
|
|
732
|
+
|
|
|
733
|
+ // 角度差为 0(自己与自己相同)
|
|
|
734
|
+ item.setAngleDifference(0.0);
|
|
|
735
|
+
|
|
|
736
|
+ // 第一个方向的方位角(角度差)为0
|
|
|
737
|
+ item.setAzimuthAngle(0.0);
|
|
|
738
|
+ }
|
|
|
739
|
+ }
|
|
|
740
|
+ } catch (Exception e) {
|
|
|
741
|
+ e.printStackTrace();
|
|
|
742
|
+ }
|
|
|
743
|
+ }
|
|
|
744
|
+ return resultList;
|
|
|
745
|
+ }
|
|
|
746
|
+}
|