FileAnalysisService.java 23 KB

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