//package com.nokia.finance.tasks.car; // //import com.nokia.finance.tasks.common.exception.MyRuntimeException; //import com.nokia.finance.tasks.pojo.po.common.OrganizationPo; //import com.nokia.finance.tasks.service.car.CarService; //import com.nokia.finance.tasks.service.common.OrganizationService; //import lombok.extern.slf4j.Slf4j; //import org.apache.commons.csv.CSVFormat; //import org.apache.commons.csv.CSVPrinter; //import org.apache.poi.ss.usermodel.Cell; //import org.apache.poi.ss.usermodel.DateUtil; //import org.apache.poi.ss.usermodel.Row; //import org.apache.poi.ss.usermodel.Sheet; //import org.apache.poi.ss.usermodel.Workbook; //import org.apache.poi.xssf.usermodel.XSSFWorkbook; //import org.junit.jupiter.api.Test; //import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.boot.test.context.SpringBootTest; //import org.springframework.test.context.ActiveProfiles; //import org.springframework.util.CollectionUtils; // //import java.io.InputStream; //import java.io.OutputStreamWriter; //import java.nio.charset.StandardCharsets; //import java.nio.file.Files; //import java.nio.file.Path; //import java.nio.file.Paths; //import java.time.format.DateTimeFormatter; //import java.util.ArrayList; //import java.util.LinkedHashMap; //import java.util.List; //import java.util.Map; //import java.util.stream.Stream; // //@Slf4j //@SpringBootTest //@ActiveProfiles("dev") //class CarJiaoTongBuTieTests { // @Autowired // CarService carService; // @Autowired // OrganizationService organizationService; // // /** // * 执行任务 // */ // @Test // void runJob() { // // 数据目录 // Path dir = Paths.get("test/data/jiao-tong-bu-tie"); // try (Stream stream = Files.list(dir)) { // // 获取数据目录下的文件列表 // List pathList = stream.filter(t -> t.toString().endsWith(".xlsx")).sorted().toList(); // log.info("数据文件列表: {}", pathList); // if (CollectionUtils.isEmpty(pathList)) { // throw new MyRuntimeException("没有文件"); // } // for (Path path : pathList) { // singleJob(path); // } // } catch (Exception e) { // log.error(e.toString(), e); // } // } // // /** // * 处理单个文件 // * // * @param path 文件路径 // */ // public void singleJob(Path path) throws Exception { // List> list = readFile(path); // List> distinctList = dataProcessing(path, list); // Path csvPath = toCsv(path, distinctList); // } // // /** // * 读取文件 // * // * @param path 文件路径 // */ // public List> readFile(Path path) throws Exception { // log.info("读取: {}", path); // List headers = Stream.of("year_month","di_shi","bu_men","yuan_gong_bian_hao","xing_ming") // .toList(); // try (InputStream inputStream = Files.newInputStream(path); // Workbook workbook = new XSSFWorkbook(inputStream) // ) { // List> resultList = new ArrayList<>(); // // 读取第一个工作表 // Sheet sheet = workbook.getSheetAt(0); // // 表头行 // Row headerRow = sheet.getRow(0); // // 列数 // int columnCount = headerRow.getPhysicalNumberOfCells(); // log.info("columnCount: {}", columnCount); // // 最后行数 // int lastRowNum = sheet.getLastRowNum(); // log.info("lastRowNum: {}", lastRowNum); // if (lastRowNum == 0) { // throw new MyRuntimeException(path.getFileName() + " 数据0条"); // } // // 遍历行 // for (int i = 1; i <= lastRowNum; i++) { // Row row = sheet.getRow(i); // if (row == null) { // continue; // } // Map rowMap = new LinkedHashMap<>(); // // 遍历列 // for (int j = 0; j < columnCount; j++) { // String header = headers.get(j); // String cellValue = ""; // rowMap.put(header, cellValue); // Cell cell = row.getCell(j); // if (cell == null) { // continue; // } // switch (cell.getCellType()) { // case STRING: // // 删除字符串空白字符 // cellValue = org.springframework.util.StringUtils.trimAllWhitespace(cell.getStringCellValue()); // break; // case NUMERIC: // if (DateUtil.isCellDateFormatted(cell)) { // cellValue = DateUtil.getLocalDateTime(cell.getNumericCellValue()) // .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); // break; // } // cellValue = String.valueOf(cell.getNumericCellValue()); // break; // case BOOLEAN: // cellValue = String.valueOf(cell.getBooleanCellValue()); // break; // default: // break; // } // rowMap.put(headers.get(j), cellValue); // } // resultList.add(rowMap); // } // return resultList; // } // } // // /** // * 数据加工 // * // * @param path 文件路径 // * @param list 数据 // */ // public List> dataProcessing(Path path, List> list) { // List secondOrgs = organizationService.getSecondOrgs(); // List thirdOrgs = organizationService.getThirdOrgs(); // Map orgMap = organizationService.getOrgMap(secondOrgs, thirdOrgs); // Map> thirdOrganizationListMap = // organizationService.getThirdOrganizationListMap(secondOrgs, thirdOrgs); // for (Map map : list) { // String buMen = map.get("bu_men"); // String firstUnit = carService.getFirstUnit(buMen); // map.put("first_unit", firstUnit); // String secondUnit = carService.getSecondUnit(buMen, firstUnit); // map.put("second_unit", secondUnit); // String thirdUnit = carService.getThirdUnit(buMen, secondUnit); // map.put("third_unit", thirdUnit); // String areaNo = carService.getAreaNo(secondOrgs, buMen); // map.put("area_no", areaNo); // String areaName = carService.getOrgName(orgMap, areaNo); // map.put("area_name", areaName); // String cityNo = carService.getCityNo(thirdOrganizationListMap, areaNo, areaName, buMen); // map.put("city_no", cityNo); // String cityName = carService.getOrgName(orgMap, cityNo); // map.put("city_name", cityName); // map.put("source", path.getFileName().toString()); // } // return list; // } // // /** // * 生成csv // * // * @param path 源文件路径 // * @param list 数据 // */ // public Path toCsv(Path path, List> list) throws Exception { // log.info("条数:{}", list.size()); // Files.createDirectories(Paths.get("test/history/")); // Path csvPath = Paths.get("test/history/" + path.getFileName() + ".csv"); // try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(csvPath), // StandardCharsets.UTF_8); // CSVPrinter printer = new CSVPrinter(osw, CSVFormat.DEFAULT)) { // // 添加bom头避免excel乱码 // osw.write('\ufeff'); // Map header = list.get(0); // // 表头 // printer.printRecord(header.keySet()); // for (Map map : list) { // printer.printRecord(map.values()); // } // } // return csvPath; // } //}