|
@@ -0,0 +1,90 @@
|
|
|
+package com.nokia.finance.tasks.jobs.car.chengben;
|
|
|
+
|
|
|
+import com.nokia.finance.tasks.common.exception.MyRuntimeException;
|
|
|
+import com.nokia.finance.tasks.config.JobConfig;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.nio.file.StandardCopyOption;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.CompletableFuture;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 河北成本管理系统车辆租赁费用数据入库定时任务
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CarZuLinJob {
|
|
|
+ private final JobConfig jobConfig;
|
|
|
+
|
|
|
+ public CarZuLinJob(JobConfig jobConfig) {
|
|
|
+ this.jobConfig = jobConfig;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 执行任务
|
|
|
+ */
|
|
|
+ @Scheduled(cron = "0 36 6 12 * ?")
|
|
|
+ public void runJob() {
|
|
|
+ // 数据目录
|
|
|
+ Path dir = Paths.get(jobConfig.getCarZuLinSourcePath());
|
|
|
+ try (Stream<Path> stream = Files.list(dir)) {
|
|
|
+ // 获取数据目录下的文件列表
|
|
|
+ List<Path> pathList = stream.filter(t -> !t.toString().endsWith(".MD5")).sorted().toList();
|
|
|
+ log.info("河北成本管理系统车辆租赁费用数据文件列表: {}", pathList);
|
|
|
+ if (CollectionUtils.isEmpty(pathList)) {
|
|
|
+ throw new MyRuntimeException("河北成本管理系统车辆租赁费用数据没有文件");
|
|
|
+ }
|
|
|
+ for (Path path : pathList) {
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ singleJob(path);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new MyRuntimeException(e);
|
|
|
+ }
|
|
|
+ }).get(1, TimeUnit.MINUTES);
|
|
|
+ }
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("线程中断: {}", e, e);
|
|
|
+ Thread.currentThread().interrupt();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.toString(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理单个文件
|
|
|
+ *
|
|
|
+ * @param path 文件路径
|
|
|
+ */
|
|
|
+ public void singleJob(Path path) throws IOException {
|
|
|
+ if (Files.size(path) == 0) {
|
|
|
+ move(path);
|
|
|
+ throw new MyRuntimeException(path.getFileName() + " 空文件");
|
|
|
+ }
|
|
|
+ move(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 移动源文件到历史文件夹
|
|
|
+ *
|
|
|
+ * @param path 源文件路径
|
|
|
+ */
|
|
|
+ public void move(Path path) throws IOException {
|
|
|
+ if (Files.exists(Paths.get(path + ".MD5"))) {
|
|
|
+ Files.move(Paths.get(path + ".MD5"),
|
|
|
+ Paths.get(jobConfig.getCarZuLinHistoryPath(), path.getFileName().toString() + ".MD5"),
|
|
|
+ StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ }
|
|
|
+ Files.move(path, Paths.get(jobConfig.getCarZuLinHistoryPath(), path.getFileName().toString()),
|
|
|
+ StandardCopyOption.REPLACE_EXISTING);
|
|
|
+ }
|
|
|
+}
|