|
@@ -3,6 +3,7 @@ package com.nokia.finance.tasks.jobs.car.strategy;
|
|
import com.nokia.finance.tasks.common.exception.MyRuntimeException;
|
|
import com.nokia.finance.tasks.common.exception.MyRuntimeException;
|
|
import com.nokia.finance.tasks.config.JobConfig;
|
|
import com.nokia.finance.tasks.config.JobConfig;
|
|
import com.nokia.finance.tasks.dao.car.CarStrategyDao;
|
|
import com.nokia.finance.tasks.dao.car.CarStrategyDao;
|
|
|
|
+import com.nokia.finance.tasks.utils.FileUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVFormat;
|
|
import org.apache.commons.csv.CSVPrinter;
|
|
import org.apache.commons.csv.CSVPrinter;
|
|
@@ -14,11 +15,13 @@ import org.springframework.util.CollectionUtils;
|
|
import java.io.OutputStreamWriter;
|
|
import java.io.OutputStreamWriter;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
|
|
+import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 高油耗车辆策略定时任务
|
|
* 高油耗车辆策略定时任务
|
|
@@ -55,23 +58,28 @@ public class CarHighFuelConsumptionJob {
|
|
if (CollectionUtils.isEmpty(list)) {
|
|
if (CollectionUtils.isEmpty(list)) {
|
|
throw new MyRuntimeException("高油耗车辆策略为空");
|
|
throw new MyRuntimeException("高油耗车辆策略为空");
|
|
}
|
|
}
|
|
- Files.createDirectories(Paths.get(jobConfig.getCarHighFuelConsumptionOutputPath()));
|
|
|
|
|
|
+ String dir = jobConfig.getCarHighFuelConsumptionOutputPath();
|
|
|
|
+ Path dirPath = Paths.get(dir);
|
|
|
|
+ Path filePath = Paths.get(dir + "car_high_fuel_consumption_" + endYearMonth + ".csv");
|
|
|
|
+ if (!Files.exists(dirPath)) {
|
|
|
|
+ Files.createDirectories(dirPath);
|
|
|
|
+ FileUtil.setDefaultDirPermissions(dirPath);
|
|
|
|
+ }
|
|
char delimiter = 1;
|
|
char delimiter = 1;
|
|
- try (OutputStreamWriter osw = new OutputStreamWriter(
|
|
|
|
- Files.newOutputStream(Paths.get(jobConfig.getCarHighFuelConsumptionOutputPath()
|
|
|
|
- + "car_high_fuel_consumption_" + endYearMonth + ".csv")),
|
|
|
|
|
|
+ try (OutputStreamWriter osw = new OutputStreamWriter(Files.newOutputStream(filePath),
|
|
StandardCharsets.UTF_8);
|
|
StandardCharsets.UTF_8);
|
|
CSVPrinter printer = new CSVPrinter(osw,
|
|
CSVPrinter printer = new CSVPrinter(osw,
|
|
CSVFormat.DEFAULT.builder().setDelimiter(delimiter).build())) {
|
|
CSVFormat.DEFAULT.builder().setDelimiter(delimiter).build())) {
|
|
// 添加bom头避免excel乱码
|
|
// 添加bom头避免excel乱码
|
|
osw.write('\ufeff');
|
|
osw.write('\ufeff');
|
|
- LinkedHashMap<String, Object> header = list.get(0);
|
|
|
|
|
|
+ Map<String, Object> header = list.get(0);
|
|
// 表头
|
|
// 表头
|
|
printer.printRecord(header.keySet());
|
|
printer.printRecord(header.keySet());
|
|
- for (LinkedHashMap<String, Object> map : list) {
|
|
|
|
|
|
+ for (Map<String, Object> map : list) {
|
|
printer.printRecord(map.values());
|
|
printer.printRecord(map.values());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ FileUtil.setDefaultFilePermissions(filePath);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
log.error(e.getMessage(), e);
|
|
throw new MyRuntimeException("高油耗车辆策略定时任务失败");
|
|
throw new MyRuntimeException("高油耗车辆策略定时任务失败");
|