CarJiaoTongBuTieTests.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //package com.nokia.finance.tasks.car;
  2. //
  3. //import com.nokia.finance.tasks.common.exception.MyRuntimeException;
  4. //import com.nokia.finance.tasks.pojo.po.common.OrganizationPo;
  5. //import com.nokia.finance.tasks.service.car.CarService;
  6. //import com.nokia.finance.tasks.service.common.OrganizationService;
  7. //import lombok.extern.slf4j.Slf4j;
  8. //import org.apache.commons.csv.CSVFormat;
  9. //import org.apache.commons.csv.CSVPrinter;
  10. //import org.apache.poi.ss.usermodel.Cell;
  11. //import org.apache.poi.ss.usermodel.DateUtil;
  12. //import org.apache.poi.ss.usermodel.Row;
  13. //import org.apache.poi.ss.usermodel.Sheet;
  14. //import org.apache.poi.ss.usermodel.Workbook;
  15. //import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  16. //import org.junit.jupiter.api.Test;
  17. //import org.springframework.beans.factory.annotation.Autowired;
  18. //import org.springframework.boot.test.context.SpringBootTest;
  19. //import org.springframework.test.context.ActiveProfiles;
  20. //import org.springframework.util.CollectionUtils;
  21. //
  22. //import java.io.InputStream;
  23. //import java.io.OutputStreamWriter;
  24. //import java.nio.charset.StandardCharsets;
  25. //import java.nio.file.Files;
  26. //import java.nio.file.Path;
  27. //import java.nio.file.Paths;
  28. //import java.time.format.DateTimeFormatter;
  29. //import java.util.ArrayList;
  30. //import java.util.LinkedHashMap;
  31. //import java.util.List;
  32. //import java.util.Map;
  33. //import java.util.stream.Stream;
  34. //
  35. //@Slf4j
  36. //@SpringBootTest
  37. //@ActiveProfiles("dev")
  38. //class CarJiaoTongBuTieTests {
  39. // @Autowired
  40. // CarService carService;
  41. // @Autowired
  42. // OrganizationService organizationService;
  43. //
  44. // /**
  45. // * 执行任务
  46. // */
  47. // @Test
  48. // void runJob() {
  49. // // 数据目录
  50. // Path dir = Paths.get("test/data/jiao-tong-bu-tie");
  51. // try (Stream<Path> stream = Files.list(dir)) {
  52. // // 获取数据目录下的文件列表
  53. // List<Path> pathList = stream.filter(t -> t.toString().endsWith(".xlsx")).sorted().toList();
  54. // log.info("数据文件列表: {}", pathList);
  55. // if (CollectionUtils.isEmpty(pathList)) {
  56. // throw new MyRuntimeException("没有文件");
  57. // }
  58. // for (Path path : pathList) {
  59. // singleJob(path);
  60. // }
  61. // } catch (Exception e) {
  62. // log.error(e.toString(), e);
  63. // }
  64. // }
  65. //
  66. // /**
  67. // * 处理单个文件
  68. // *
  69. // * @param path 文件路径
  70. // */
  71. // public void singleJob(Path path) throws Exception {
  72. // List<Map<String, String>> list = readFile(path);
  73. // List<Map<String, String>> distinctList = dataProcessing(path, list);
  74. // Path csvPath = toCsv(path, distinctList);
  75. // }
  76. //
  77. // /**
  78. // * 读取文件
  79. // *
  80. // * @param path 文件路径
  81. // */
  82. // public List<Map<String, String>> readFile(Path path) throws Exception {
  83. // log.info("读取: {}", path);
  84. // List<String> headers = Stream.of("year_month","di_shi","bu_men","yuan_gong_bian_hao","xing_ming")
  85. // .toList();
  86. // try (InputStream inputStream = Files.newInputStream(path);
  87. // Workbook workbook = new XSSFWorkbook(inputStream)
  88. // ) {
  89. // List<Map<String, String>> resultList = new ArrayList<>();
  90. // // 读取第一个工作表
  91. // Sheet sheet = workbook.getSheetAt(0);
  92. // // 表头行
  93. // Row headerRow = sheet.getRow(0);
  94. // // 列数
  95. // int columnCount = headerRow.getPhysicalNumberOfCells();
  96. // log.info("columnCount: {}", columnCount);
  97. // // 最后行数
  98. // int lastRowNum = sheet.getLastRowNum();
  99. // log.info("lastRowNum: {}", lastRowNum);
  100. // if (lastRowNum == 0) {
  101. // throw new MyRuntimeException(path.getFileName() + " 数据0条");
  102. // }
  103. // // 遍历行
  104. // for (int i = 1; i <= lastRowNum; i++) {
  105. // Row row = sheet.getRow(i);
  106. // if (row == null) {
  107. // continue;
  108. // }
  109. // Map<String, String> rowMap = new LinkedHashMap<>();
  110. // // 遍历列
  111. // for (int j = 0; j < columnCount; j++) {
  112. // String header = headers.get(j);
  113. // String cellValue = "";
  114. // rowMap.put(header, cellValue);
  115. // Cell cell = row.getCell(j);
  116. // if (cell == null) {
  117. // continue;
  118. // }
  119. // switch (cell.getCellType()) {
  120. // case STRING:
  121. // // 删除字符串空白字符
  122. // cellValue = org.springframework.util.StringUtils.trimAllWhitespace(cell.getStringCellValue());
  123. // break;
  124. // case NUMERIC:
  125. // if (DateUtil.isCellDateFormatted(cell)) {
  126. // cellValue = DateUtil.getLocalDateTime(cell.getNumericCellValue())
  127. // .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  128. // break;
  129. // }
  130. // cellValue = String.valueOf(cell.getNumericCellValue());
  131. // break;
  132. // case BOOLEAN:
  133. // cellValue = String.valueOf(cell.getBooleanCellValue());
  134. // break;
  135. // default:
  136. // break;
  137. // }
  138. // rowMap.put(headers.get(j), cellValue);
  139. // }
  140. // resultList.add(rowMap);
  141. // }
  142. // return resultList;
  143. // }
  144. // }
  145. //
  146. // /**
  147. // * 数据加工
  148. // *
  149. // * @param path 文件路径
  150. // * @param list 数据
  151. // */
  152. // public List<Map<String, String>> dataProcessing(Path path, List<Map<String, String>> list) {
  153. // List<OrganizationPo> secondOrgs = organizationService.getSecondOrgs();
  154. // List<OrganizationPo> thirdOrgs = organizationService.getThirdOrgs();
  155. // Map<String, OrganizationPo> orgMap = organizationService.getOrgMap(secondOrgs, thirdOrgs);
  156. // Map<String, List<OrganizationPo>> thirdOrganizationListMap =
  157. // organizationService.getThirdOrganizationListMap(secondOrgs, thirdOrgs);
  158. // for (Map<String, String> map : list) {
  159. // String buMen = map.get("bu_men");
  160. // String firstUnit = carService.getFirstUnit(buMen);
  161. // map.put("first_unit", firstUnit);
  162. // String secondUnit = carService.getSecondUnit(buMen, firstUnit);
  163. // map.put("second_unit", secondUnit);
  164. // String thirdUnit = carService.getThirdUnit(buMen, secondUnit);
  165. // map.put("third_unit", thirdUnit);
  166. // String areaNo = carService.getAreaNo(secondOrgs, buMen);
  167. // map.put("area_no", areaNo);
  168. // String areaName = carService.getOrgName(orgMap, areaNo);
  169. // map.put("area_name", areaName);
  170. // String cityNo = carService.getCityNo(thirdOrganizationListMap, areaNo, areaName, buMen);
  171. // map.put("city_no", cityNo);
  172. // String cityName = carService.getOrgName(orgMap, cityNo);
  173. // map.put("city_name", cityName);
  174. // map.put("source", path.getFileName().toString());
  175. // }
  176. // return list;
  177. // }
  178. //
  179. // /**
  180. // * 生成csv
  181. // *
  182. // * @param path 源文件路径
  183. // * @param list 数据
  184. // */
  185. // public Path toCsv(Path path, List<Map<String, String>> list) throws Exception {
  186. // log.info("条数:{}", list.size());
  187. // Files.createDirectories(Paths.get("test/history/"));
  188. // Path csvPath = Paths.get("test/history/" + path.getFileName() + ".csv");
  189. // try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(csvPath),
  190. // StandardCharsets.UTF_8);
  191. // CSVPrinter printer = new CSVPrinter(osw, CSVFormat.DEFAULT)) {
  192. // // 添加bom头避免excel乱码
  193. // osw.write('\ufeff');
  194. // Map<String, String> header = list.get(0);
  195. // // 表头
  196. // printer.printRecord(header.keySet());
  197. // for (Map<String, String> map : list) {
  198. // printer.printRecord(map.values());
  199. // }
  200. // }
  201. // return csvPath;
  202. // }
  203. //}