|
@@ -30,6 +30,7 @@ import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.IntStream;
|
|
|
|
|
|
/**
|
|
|
* @Description: CsvUtils
|
|
@@ -247,6 +248,61 @@ public class CsvUtils {
|
|
|
return jsonArray;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据头名称(参数名称)获取csv文件中参数数据
|
|
|
+ *
|
|
|
+ * @param csvFilePath csv文件地址
|
|
|
+ * @param headerNames 参数名称
|
|
|
+ * @return 参数名称列表
|
|
|
+ */
|
|
|
+ public static JSONArray getCsvDataByColumn(String csvFilePath, List<String> headerNames, Integer step) {
|
|
|
+ log.info("参数列:{}", headerNames);
|
|
|
+ List<CsvRow> rows = getCsvRowList(EcoConfig.getProfile() + csvFilePath);
|
|
|
+ // 获取CSV表头,即第一行数据
|
|
|
+ List<String> headers = rows.get(0).getRawList();
|
|
|
+ log.info("getCsvDataByColumn CSV表头:{}", headers.toString());
|
|
|
+ List<Integer> indexList = IntStream.range(0, headers.size())
|
|
|
+ .filter(i -> headerNames.stream()
|
|
|
+ .anyMatch(name -> StrUtil.containsIgnoreCase(headers.get(i), name)))
|
|
|
+ .boxed()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+// headerNames.forEach(name -> {
|
|
|
+// int index = headers.indexOf(name);
|
|
|
+// if (index != -1) {
|
|
|
+// log.info("匹配到的表头:{},索引为:{}", name, index);
|
|
|
+// indexList.add(index);
|
|
|
+// }
|
|
|
+// });
|
|
|
+// for (int i = 0; i < headers.size(); i++ ) {
|
|
|
+// if (StrUtil.containsAny(headers.get(i), headerNames.toArray(new String[0]))) {
|
|
|
+// log.info("匹配到的表头:{},索引为:{}", headers.get(i), i);
|
|
|
+// indexList.add(i);
|
|
|
+// }
|
|
|
+// };
|
|
|
+
|
|
|
+ if (CollectionUtil.isEmpty(indexList)) {
|
|
|
+ throw new BusinessException("参数列匹配为空!");
|
|
|
+ }
|
|
|
+ // 去除第一行,保留后续数据
|
|
|
+ List<CsvRow> dataLines = rows.subList(1, rows.size());
|
|
|
+ JSONArray jsonArray = new JSONArray();
|
|
|
+ for (CsvRow line : dataLines) {
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ for (int i = 0; i < headers.size(); i++) {
|
|
|
+ // 参数索引位置
|
|
|
+ if (indexList.contains(i)) {
|
|
|
+ jsonObject.putOnce(headers.get(i), line.getRawList().get(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ jsonArray.add(jsonObject);
|
|
|
+ }
|
|
|
+ if (step != null) {
|
|
|
+ jsonArray = reduceFrameRate(jsonArray, 10, step);
|
|
|
+ }
|
|
|
+ log.info("处理后数据:{}", jsonArray.size());
|
|
|
+ return jsonArray;
|
|
|
+ }
|
|
|
+
|
|
|
public static JSONObject getPlaybackByHeaders(String csvFilePath, List<String> headerNames, Integer step) {
|
|
|
log.info("getPlaybackByHeaders 参数列:{}", headerNames);
|
|
|
List<CsvRow> rows = getCsvRowList(EcoConfig.getProfile() + csvFilePath);
|