| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //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<Path> stream = Files.list(dir)) {
- // // 获取数据目录下的文件列表
- // List<Path> 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<Map<String, String>> list = readFile(path);
- // List<Map<String, String>> distinctList = dataProcessing(path, list);
- // Path csvPath = toCsv(path, distinctList);
- // }
- //
- // /**
- // * 读取文件
- // *
- // * @param path 文件路径
- // */
- // public List<Map<String, String>> readFile(Path path) throws Exception {
- // log.info("读取: {}", path);
- // List<String> 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<Map<String, String>> 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<String, String> 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<Map<String, String>> dataProcessing(Path path, List<Map<String, String>> list) {
- // List<OrganizationPo> secondOrgs = organizationService.getSecondOrgs();
- // List<OrganizationPo> thirdOrgs = organizationService.getThirdOrgs();
- // Map<String, OrganizationPo> orgMap = organizationService.getOrgMap(secondOrgs, thirdOrgs);
- // Map<String, List<OrganizationPo>> thirdOrganizationListMap =
- // organizationService.getThirdOrganizationListMap(secondOrgs, thirdOrgs);
- // for (Map<String, String> 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<Map<String, String>> 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<String, String> header = list.get(0);
- // // 表头
- // printer.printRecord(header.keySet());
- // for (Map<String, String> map : list) {
- // printer.printRecord(map.values());
- // }
- // }
- // return csvPath;
- // }
- //}
|