FileAnalysisService.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. package com.jiayue.ipfcst.console.service;
  2. import com.jiayue.ipfcst.common.core.util.DateTimeUtil;
  3. import com.jiayue.ipfcst.common.core.util.NumberUtils;
  4. import com.jiayue.ipfcst.common.data.constant.enums.HolidayTypeEnum;
  5. import com.jiayue.ipfcst.common.data.constant.enums.PredictionModelEnum;
  6. import com.jiayue.ipfcst.common.data.entity.*;
  7. import com.jiayue.ipfcst.common.data.repository.ElectricFieldRepository;
  8. import com.jiayue.ipfcst.common.data.repository.HolidayCalendarRepository;
  9. import com.jiayue.ipfcst.common.data.service.BaseService;
  10. import com.jiayue.ipfcst.fileupload.util.FileUtil;
  11. import lombok.AllArgsConstructor;
  12. import lombok.SneakyThrows;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.io.FileUtils;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.apache.commons.lang.time.DateFormatUtils;
  17. import org.springframework.stereotype.Service;
  18. import java.io.*;
  19. import java.math.BigDecimal;
  20. import java.math.RoundingMode;
  21. import java.nio.charset.StandardCharsets;
  22. import java.nio.file.Files;
  23. import java.nio.file.Paths;
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.Date;
  29. import java.util.List;
  30. /**
  31. * 解析电科院NWP和DQ文件
  32. *
  33. * @author cuil
  34. * @version 2.0
  35. * @since 2018/12/03 11:24
  36. */
  37. @AllArgsConstructor
  38. @Service
  39. @Slf4j
  40. public class FileAnalysisService extends BaseService {
  41. ElectricFieldService electricFieldService;
  42. ForecastPowerShortTermService forecastPowerShortTermService;
  43. HolidayCalendarRepository holidayCalendarRepository;
  44. NwpService nwpService;
  45. FileAnalysisRecordService fileAnalysisRecordService;
  46. OverHaulPlanService overHaulPlanService;
  47. SysParameterService sysParameterService;
  48. ElectricFieldRepository electricFieldRepository;
  49. HolidayCalendarService holidayCalendarService;
  50. private final String deleteFilelog = "文件删除异常";
  51. private final String format = "yyyy-MM-dd HH:mm:ss";
  52. private final String systemErrorLog = "系统错误: ";
  53. private final String errorLog = "error";
  54. private final String analysisFaild = "文件解析失败";
  55. public static void mvFile(File file, String fdPath) {
  56. try {
  57. FileUtils.copyFile(file, new File(fdPath), true);
  58. } catch (IOException e) {
  59. log.error("文件移动错误", e);
  60. }
  61. }
  62. /**
  63. * 创建备份文件夹
  64. */
  65. public static String mkDirForTime(String targetRoot, String format) {
  66. String path;
  67. File file;
  68. if (StringUtils.isNotEmpty(format)) {
  69. long current = System.currentTimeMillis();
  70. path = DateFormatUtils.format(current, format);
  71. file = new File(targetRoot + File.separator + path + File.separator);
  72. } else {
  73. file = new File(targetRoot + File.separator);
  74. }
  75. if (!file.exists() && !file.isFile()) {
  76. if (file.mkdir()) {
  77. log.info("已创建文件夹");
  78. } else {
  79. log.info("创建文件夹失败,路径:" + file.getPath());
  80. }
  81. }
  82. return file.getPath() + File.separator;
  83. }
  84. @SneakyThrows
  85. public void analysisJob() {
  86. log.info("-----------------开始执行文件解析任务----------------------");
  87. long timeD = 15 * 60 * 1000L;
  88. Long currentDate = DateTimeUtil.getMillisecondsSubDay();//今日凌晨
  89. boolean flag;
  90. List<ElectricField> electricFieldList = electricFieldService.getAll();
  91. List<String> eList = new ArrayList<>();
  92. for (ElectricField electricField : electricFieldList) {
  93. eList.add(electricField.getStationCode());
  94. }
  95. String path = FileUtil.getDownloadFilePath();
  96. //downloadFile
  97. File dirFile = new File(path);
  98. //判断该目录是否存在,不存在时创建
  99. if (!dirFile.exists()) {
  100. dirFile.mkdirs();
  101. }
  102. log.info("系统扫描路径【" + dirFile.getPath() + "】");
  103. for (String stationCode : eList) {
  104. // downLoadFile/场站编号/new
  105. String dirFiles = dirFile.getPath() + File.separator + stationCode + File.separator + "new";
  106. File filePath = new File(dirFiles);
  107. if (!filePath.exists()) {
  108. filePath.mkdirs();
  109. }
  110. Collection<File> files = FileUtils.listFiles(filePath, new String[]{"RB"}, false);
  111. String dayStr = new SimpleDateFormat("yyyyMMdd").format(new Date());//当前时间格式化为年月日
  112. if (files.isEmpty()) {
  113. for (File file : files) {
  114. flag = false;
  115. String fileName = file.getName();
  116. if (!fileName.contains(dayStr)) {
  117. try {
  118. Files.delete(Paths.get(file.getPath()));
  119. log.info(fileName + "不是当天的文件,删除!");
  120. break;
  121. }catch (IOException e2){
  122. log.info(deleteFilelog,e2);
  123. }
  124. }
  125. if (fileName.length() < 30) {
  126. //假期文件
  127. if (file.getName().startsWith("JH")) {
  128. List<HolidayCalendar> listJq = fileAnalysisJqTerm(file);
  129. if (listJq.size() > 0) {
  130. holidayCalendarRepository.deleteAll();
  131. holidayCalendarRepository.saveAll(listJq);
  132. flag = true;
  133. } else {
  134. flag = false;
  135. }
  136. }
  137. if (file.getName().startsWith("DQ")) {
  138. try {
  139. List<ForecastPowerShortTerm> listDq = fileAnalysisShortTerm(file, currentDate, stationCode);
  140. ForecastPowerShortTerm forecastPowerShortTerm;
  141. if (listDq.size() > 0) {
  142. //如果数据不全,进行补入
  143. while (listDq.get(listDq.size() - 1).getForecastTime() < DateTimeUtil.getMillisecondsSubDay() + 4 * 24 * 60 * 60 * 1000 - timeD) {
  144. forecastPowerShortTerm = new ForecastPowerShortTerm();
  145. forecastPowerShortTerm.setFpValue(listDq.get(listDq.size() - 96).getFpValue());//修正前值
  146. forecastPowerShortTerm.setGenDate(new Date()); //装机容量
  147. forecastPowerShortTerm.setForecastTime(currentDate);
  148. forecastPowerShortTerm.setPredictionModelEnum(PredictionModelEnum.E1);
  149. forecastPowerShortTerm.setStationCode(stationCode);
  150. listDq.add(forecastPowerShortTerm);
  151. }
  152. try {
  153. //短期数据修正
  154. Long startTime = listDq.get(0).getForecastTime();
  155. Long endTime = listDq.get(listDq.size() - 1).getForecastTime();//删除相同时间数据
  156. forecastPowerShortTermService.deleteBetweenAndGenTime(startTime, endTime, listDq, stationCode);
  157. flag = true;
  158. } catch (Exception e) {
  159. log.error("保存短期数据报错", e);
  160. flag = false;
  161. }
  162. } else {
  163. log.info(file.getName() + "文件数据内容为空、不能正常解析 、移除该文件、执行数据修正功能");
  164. flag = false;
  165. }
  166. } catch (Exception e) {
  167. flag = false;
  168. log.error("解析DQ文件失败", e);
  169. }
  170. }
  171. if (file.getName().startsWith("NWP")) {
  172. try {
  173. List<Nwp> listNwp = fileAnalysisNwp(file, stationCode);
  174. Nwp nwpData;
  175. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  176. if (listNwp.size() > 0) {
  177. while (listNwp.get(listNwp.size() - 1).getPreTime() < DateTimeUtil.getMillisecondsSubDay() + 4 * 24 * 60 * 60 * 1000 - timeD) {
  178. nwpData = new Nwp();
  179. long time = 0;
  180. try {
  181. time = sdf.parse(listNwp.get(listNwp.size() - 96).getPreDate()).getTime() + 24 * 60 * 60 * 1000;
  182. } catch (ParseException e) {
  183. e.printStackTrace();
  184. }
  185. nwpData.setFarmId(listNwp.get(listNwp.size() - 96).getFarmId());
  186. nwpData.setScDate(listNwp.get(listNwp.size() - 96).getScDate());
  187. nwpData.setScTime(listNwp.get(listNwp.size() - 96).getScTime());
  188. nwpData.setPreDate(sdf.format(time));
  189. nwpData.setPreTime(listNwp.get(listNwp.size() - 1).getPreTime() + timeD);
  190. nwpData.setT(listNwp.get(listNwp.size() - 96).getT());//温度
  191. nwpData.setRh(listNwp.get(listNwp.size() - 96).getRh());//湿度
  192. nwpData.setPressure(listNwp.get(listNwp.size() - 96).getPressure());//气压
  193. nwpData.setSwr(listNwp.get(listNwp.size() - 96).getSwr());//辐射
  194. nwpData.setLwr(listNwp.get(listNwp.size() - 96).getLwr());//辐射
  195. nwpData.setDiffuseRadiation(listNwp.get(listNwp.size() - 96).getDiffuseRadiation());//散接辐射
  196. nwpData.setDirectRadiation(listNwp.get(listNwp.size() - 96).getDirectRadiation());//直接辐射
  197. nwpData.setSenf(listNwp.get(listNwp.size() - 96).getSenf());//热感通量
  198. nwpData.setWs10(listNwp.get(listNwp.size() - 96).getWs10());//10 m 风速
  199. nwpData.setWs30(listNwp.get(listNwp.size() - 96).getWs30());//30 m 风速
  200. nwpData.setWs50(listNwp.get(listNwp.size() - 96).getWs50());//50 m 风速
  201. nwpData.setWs70(listNwp.get(listNwp.size() - 96).getWs70());//70 m 风速
  202. nwpData.setWs80(listNwp.get(listNwp.size() - 96).getWs80());//80 m 风速
  203. nwpData.setWs90(listNwp.get(listNwp.size() - 96).getWs90());//90 m 风速
  204. nwpData.setWs100(listNwp.get(listNwp.size() - 96).getWs100());//100 m 风速
  205. nwpData.setWs170(listNwp.get(listNwp.size() - 96).getWs170());//170 m 风速
  206. nwpData.setWd10(listNwp.get(listNwp.size() - 96).getWd10());//10 m 风向
  207. nwpData.setWd30(listNwp.get(listNwp.size() - 96).getWd30());//30 m 风向
  208. nwpData.setWd50(listNwp.get(listNwp.size() - 96).getWd50());//50 m 风向
  209. nwpData.setWd70(listNwp.get(listNwp.size() - 96).getWd70());//70 m 风向
  210. nwpData.setWd80(listNwp.get(listNwp.size() - 96).getWd80());//80 m 风向
  211. nwpData.setWd90(listNwp.get(listNwp.size() - 96).getWd90());//90 m 风向
  212. nwpData.setWd100(listNwp.get(listNwp.size() - 96).getWd100());//100 m 风向
  213. nwpData.setWd170(listNwp.get(listNwp.size() - 96).getWd170());//170 m 风向
  214. nwpData.setStationCode(stationCode);
  215. listNwp.add(nwpData);
  216. }
  217. } else {
  218. log.info(file.getName() + "文件数据内容为空、不能正常解析 、移除该文件、执行数据修正功能");
  219. }
  220. //保存NWP实时数据
  221. Long startTime = listNwp.get(0).getPreTime();
  222. Long endTime = listNwp.get(listNwp.size() - 1).getPreTime();//删除相同时间数据
  223. nwpService.deleteBetweenAndPreTime(startTime, endTime, listNwp, stationCode);
  224. flag = true;
  225. } catch (Exception e) {
  226. log.error("解析NWP文件失败", e);
  227. flag = false;
  228. }
  229. }
  230. if (flag) {
  231. //文件解析成功之后,保存记录
  232. saveFileParsingRecord(file, "1", stationCode);
  233. //移除文件备份到临时文件下
  234. moveFile(file);
  235. } else {
  236. //文件解析失败之后,保存记录
  237. saveFileParsingRecord(file, "0", stationCode);
  238. //移除文件备份到error文件下
  239. moveFileError(file);
  240. }
  241. }
  242. }
  243. }
  244. log.info("-----------------执行文件解析任务完成----------------------");
  245. }
  246. }
  247. /**
  248. * NWP解析
  249. *
  250. * @param file 文件路径
  251. * @return 样例集合
  252. */
  253. private List<Nwp> fileAnalysisNwp(File file, String stationCode) {
  254. List<Nwp> listNwp = new ArrayList<>();
  255. if (file.renameTo(file)) {
  256. InputStreamReader readNwp = null;
  257. BufferedReader bufferedReaderNwp = null;
  258. try {
  259. readNwp = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8.name());//考虑到编码格式
  260. bufferedReaderNwp = new BufferedReader(readNwp);
  261. String lineTxt;
  262. Nwp nwpData;
  263. BigDecimal nwpDirectRadiation = new BigDecimal("0.7"); //直接辐射
  264. BigDecimal nwpDiffuseRadiation = new BigDecimal("0.3"); //散接辐射
  265. while ((lineTxt = bufferedReaderNwp.readLine()) != null) {
  266. //NWP文件按照Tab方式截取
  267. String[] datas = lineTxt.split("\t");
  268. if (datas.length == 35 && datas[0].startsWith("#")) {
  269. SimpleDateFormat sdf = new SimpleDateFormat(format);
  270. nwpData = new Nwp();
  271. //将截取的文件放入nwpData中
  272. nwpData.setFarmId(datas[1]);
  273. nwpData.setScDate(datas[2]);
  274. nwpData.setScTime(datas[3]);
  275. nwpData.setPreDate(datas[4]);
  276. nwpData.setPreTime(sdf.parse(datas[5]).getTime());//采集时间 与 短期预测时间关联
  277. nwpData.setT(NumberUtils.subtract(new BigDecimal(datas[6]), new BigDecimal("273.15")));//温度
  278. nwpData.setSenf(new BigDecimal(datas[11]).setScale(2, RoundingMode.HALF_UP));//感热
  279. nwpData.setSwr(new BigDecimal(datas[9]).setScale(2, RoundingMode.HALF_UP));//短波辐射(相当于总辐射)
  280. nwpData.setLwr(new BigDecimal(datas[10]).setScale(2, RoundingMode.HALF_UP));//短波辐射(相当于总辐射)
  281. nwpData.setPressure(new BigDecimal(datas[8]).setScale(2, RoundingMode.HALF_UP));//地表气压
  282. nwpData.setRh(new BigDecimal(datas[7]).setScale(2, RoundingMode.HALF_UP));//2m相对湿度
  283. nwpData.setDiffuseRadiation(new BigDecimal(datas[9]).multiply(nwpDiffuseRadiation).setScale(2, RoundingMode.HALF_UP));//散接辐射
  284. nwpData.setDirectRadiation(new BigDecimal(datas[9]).multiply(nwpDirectRadiation).setScale(2, RoundingMode.HALF_UP));//直接辐射
  285. nwpData.setWs10(new BigDecimal(datas[19]).setScale(2, RoundingMode.HALF_UP));
  286. nwpData.setWs30(new BigDecimal(datas[20]).setScale(2, RoundingMode.HALF_UP));
  287. nwpData.setWs50(new BigDecimal(datas[21]).setScale(2, RoundingMode.HALF_UP));
  288. nwpData.setWs70(new BigDecimal(datas[22]).setScale(2, RoundingMode.HALF_UP));
  289. nwpData.setWs80(new BigDecimal(datas[23]).setScale(2, RoundingMode.HALF_UP));
  290. nwpData.setWs90(new BigDecimal(datas[24]).setScale(2, RoundingMode.HALF_UP));
  291. nwpData.setWs100(new BigDecimal(datas[25]).setScale(2, RoundingMode.HALF_UP));
  292. nwpData.setWs170(new BigDecimal(datas[26]).setScale(2, RoundingMode.HALF_UP));
  293. nwpData.setWd10(new BigDecimal(datas[27]).setScale(2, RoundingMode.HALF_UP));
  294. nwpData.setWd30(new BigDecimal(datas[28]).setScale(2, RoundingMode.HALF_UP));
  295. nwpData.setWd50(new BigDecimal(datas[29]).setScale(2, RoundingMode.HALF_UP));
  296. nwpData.setWd70(new BigDecimal(datas[30]).setScale(2, RoundingMode.HALF_UP));
  297. nwpData.setWd80(new BigDecimal(datas[31]).setScale(2, RoundingMode.HALF_UP));
  298. nwpData.setWd90(new BigDecimal(datas[32]).setScale(2, RoundingMode.HALF_UP));
  299. nwpData.setWd100(new BigDecimal(datas[33]).setScale(2, RoundingMode.HALF_UP));
  300. nwpData.setWd170(new BigDecimal(datas[34]).setScale(2, RoundingMode.HALF_UP));
  301. nwpData.setStationCode(stationCode);
  302. listNwp.add(nwpData);
  303. }
  304. }
  305. } catch (IOException | ParseException | RuntimeException e) {
  306. log.error(systemErrorLog, e);
  307. // 进行告警
  308. File destFile = new File(file.getPath().replaceFirst("new", systemErrorLog));
  309. if (destFile.exists()) {
  310. try {
  311. Files.delete(Paths.get(destFile.getPath()));
  312. }catch (IOException e2){
  313. log.info(deleteFilelog,e2);
  314. }
  315. }
  316. try {
  317. FileUtils.moveFile(file, destFile);
  318. } catch (IOException e1) {
  319. log.error(file.getName() + analysisFaild, e);
  320. }
  321. } finally {
  322. close(bufferedReaderNwp, readNwp);
  323. }
  324. }
  325. return listNwp;
  326. }
  327. /**
  328. * 假期解析
  329. *
  330. * @param file 文件路径
  331. * @return 样例集合
  332. */
  333. private List<HolidayCalendar> fileAnalysisJqTerm(File file) {
  334. SimpleDateFormat sdf = new SimpleDateFormat(format);
  335. List<HolidayCalendar> holidayCalendars = new ArrayList<>();
  336. // 当文件未被使用时,进行解析上报
  337. if (file.renameTo(file)) {
  338. InputStreamReader read = null;
  339. BufferedReader bufferedReader = null;
  340. String stringLine;
  341. HolidayCalendar holiday;
  342. try {
  343. read = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
  344. bufferedReader = new BufferedReader(read);
  345. while ((stringLine = bufferedReader.readLine()) != null) {
  346. String[] string_arr = stringLine.split("\t");
  347. if (string_arr.length == 7 && string_arr[0].startsWith("#")) {
  348. holiday = new HolidayCalendar();
  349. holiday.setId(Integer.parseInt(string_arr[1] + ""));
  350. holiday.setName(string_arr[2] + "");
  351. holiday.setStartTime(sdf.parse(string_arr[3]).getTime());
  352. holiday.setEndTime(sdf.parse(string_arr[4]).getTime());
  353. holiday.setDays(Integer.parseInt(string_arr[5] + ""));
  354. if (string_arr[6].equals("1")) {
  355. holiday.setHolidayTypeEnum(HolidayTypeEnum.valueOf(string_arr[6]));
  356. }
  357. holidayCalendars.add(holiday);
  358. }
  359. }
  360. } catch (IOException | ParseException | RuntimeException e) {
  361. log.error(systemErrorLog, e);
  362. // 进行告警
  363. File destFile = new File(file.getPath().replaceFirst("new", systemErrorLog));
  364. if (destFile.exists()) {
  365. try {
  366. Files.delete(Paths.get(destFile.getPath()));
  367. }catch (IOException e2){
  368. log.info(deleteFilelog,e2);
  369. }
  370. }
  371. try {
  372. FileUtils.moveFile(file, destFile);
  373. } catch (IOException e1) {
  374. log.error(file.getName() + analysisFaild, e);
  375. }
  376. } finally {
  377. close(bufferedReader, read);
  378. }
  379. }
  380. return holidayCalendars;
  381. }
  382. /**
  383. * 短期解析
  384. *
  385. * @param file 文件路径
  386. * @param currentDate 当前时间
  387. * @return 样例集合
  388. */
  389. private List<ForecastPowerShortTerm> fileAnalysisShortTerm(File file, Long currentDate, String stationCode) {
  390. SimpleDateFormat sdf = new SimpleDateFormat(format);
  391. List<ForecastPowerShortTerm> forecastPowerShortTerm = new ArrayList<>();
  392. // 当文件未被使用时,进行解析上报
  393. if (file.renameTo(file)) {
  394. InputStreamReader read = null;
  395. BufferedReader bufferedReader = null;
  396. String stringLine;
  397. ForecastPowerShortTerm stf;
  398. try {
  399. read = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8);
  400. bufferedReader = new BufferedReader(read);
  401. while ((stringLine = bufferedReader.readLine()) != null) {
  402. String[] string_arr = stringLine.split("\t");
  403. if (string_arr.length == 4 && string_arr[0].startsWith("#") && StringUtils.isNotEmpty(string_arr[2])) {
  404. stf = new ForecastPowerShortTerm();
  405. stf.setFpValue(new BigDecimal(string_arr[3] + ""));
  406. stf.setForecastTime(sdf.parse(string_arr[2]).getTime());
  407. stf.setGenDate(new Date(currentDate));
  408. stf.setPredictionModelEnum(PredictionModelEnum.E1);
  409. stf.setStationCode(stationCode);
  410. forecastPowerShortTerm.add(stf);
  411. }
  412. }
  413. } catch (IOException | ParseException | RuntimeException e) {
  414. log.error(systemErrorLog, e);
  415. // 进行告警
  416. File destFile = new File(file.getPath().replaceFirst("new", systemErrorLog));
  417. if (destFile.exists()) {
  418. try {
  419. Files.delete(Paths.get(destFile.getPath()));
  420. }catch (IOException e2){
  421. log.info(deleteFilelog,e2);
  422. }
  423. }
  424. try {
  425. FileUtils.moveFile(file, destFile);
  426. } catch (IOException e1) {
  427. log.error(file.getName() +analysisFaild, e);
  428. }
  429. } finally {
  430. close(bufferedReader, read);
  431. }
  432. }
  433. return forecastPowerShortTerm;
  434. }
  435. /**
  436. * 移动文件到临时目录下
  437. *
  438. * @param file 文件
  439. */
  440. private void moveFile(File file) {
  441. // 移动文件到处理目录
  442. File destFile = new File(file.getPath().replaceFirst("new", "backupsTemp"));
  443. log.info("move file :{}, dest file:{}", file, destFile);
  444. if (destFile.exists()) {
  445. try {
  446. Files.delete(Paths.get(destFile.getPath()));
  447. }catch (IOException e2){
  448. log.info(deleteFilelog,e2);
  449. }
  450. }
  451. try {
  452. FileUtils.moveFile(file, destFile);
  453. } catch (IOException e) {
  454. log.error("系统移除文件错误:", e);
  455. // 进行告警
  456. }
  457. moveFileBackups(destFile.getParent());
  458. }
  459. /**
  460. * 移动文件到处理目录
  461. *
  462. * @param filePath 文件路径
  463. */
  464. private void moveFileBackups(String filePath) {
  465. String targetRoot = filePath.replaceFirst("backupsTemp", "backups");
  466. String path = mkDirForTime(targetRoot, null);
  467. path = mkDirForTime(path, "yyyyMMdd");
  468. // 移动文件夹内容
  469. File sourceFile = new File(filePath);
  470. if (sourceFile.exists()) {
  471. try {
  472. File[] files = sourceFile.listFiles();
  473. if (files != null && files.length > 0) {
  474. for (File f : files) {
  475. if (f.renameTo(new File(path + f.getName()))) {
  476. log.info("move file :{}, dest file:{}", path, f.getName());
  477. }
  478. }
  479. }
  480. } catch (Exception e) {
  481. // 进行告警
  482. }
  483. }
  484. }
  485. /**
  486. * 移动文件到处理错误目录下
  487. *
  488. * @param file 文件
  489. */
  490. private void moveFileError(File file) {
  491. File destFile = new File(file.getPath().replaceFirst("new", systemErrorLog));
  492. if (destFile.exists()) {
  493. try {
  494. Files.delete(Paths.get(destFile.getPath()));
  495. }catch (IOException e2){
  496. log.info(deleteFilelog,e2);
  497. }
  498. }
  499. try {
  500. FileUtils.moveFile(file, destFile);
  501. } catch (IOException e) {
  502. log.error(file.getName() + analysisFaild, e);
  503. }
  504. }
  505. /**
  506. * 保存文件解析记录
  507. *
  508. * @param file 文件信息
  509. * @param fileStatus 文件解析状态 1:表示解析成功 0:解析失败
  510. */
  511. private void saveFileParsingRecord(File file, String fileStatus, String staticCode) {
  512. String fileType;
  513. if (file.getName().startsWith("JH")) {
  514. fileType = "JH";//假期文件
  515. } else if (file.getName().startsWith("DQ")) {
  516. fileType = "DQ";//短期文件
  517. } else {
  518. fileType = "NWP";//NWP文件
  519. }
  520. FileAnalysisRecord fileParsingRecord = new FileAnalysisRecord();
  521. fileParsingRecord.setFileTitle(file.getName());
  522. fileParsingRecord.setFileType(fileType);
  523. fileParsingRecord.setFileStatus(fileStatus);
  524. fileParsingRecord.setFilePath(file.getPath());
  525. fileParsingRecord.setFileDescription(DateTimeUtil.getStringDate() + " 正常解析文件");
  526. fileParsingRecord.setStationCode(staticCode);
  527. fileAnalysisRecordService.save(fileParsingRecord);
  528. }
  529. /**
  530. * 关闭文件流
  531. *
  532. * @param bufferedReader 字符数据
  533. * @param read 字节流
  534. */
  535. private void close(BufferedReader bufferedReader, InputStreamReader read) {
  536. try {
  537. if (bufferedReader != null) {
  538. bufferedReader.close();
  539. }
  540. if (read != null) {
  541. read.close();
  542. }
  543. } catch (IOException e) {
  544. log.error("关闭文件流失败:", e);
  545. }
  546. }
  547. }