BaseUploadFileService.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.jiayue.ipfcst.fileupload.service;
  2. import com.jiayue.ipfcst.common.core.util.DateTimeUtil;
  3. import com.jiayue.ipfcst.common.data.constant.enums.FileStatusEnum;
  4. import com.jiayue.ipfcst.common.data.constant.enums.FileTypeEnum;
  5. import com.jiayue.ipfcst.common.data.entity.UploadFileChannel;
  6. import com.jiayue.ipfcst.common.data.entity.UploadFileLog;
  7. import com.jiayue.ipfcst.common.data.entity.UploadObject;
  8. import com.jiayue.ipfcst.common.data.repository.UploadFileLogRepository;
  9. import com.jiayue.ipfcst.common.data.service.BaseService;
  10. import com.jiayue.ipfcst.fileupload.util.FileConstant;
  11. import com.jiayue.ipfcst.fileupload.util.FileUtil;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.io.FileUtils;
  14. import org.apache.commons.lang.time.DateFormatUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Propagation;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import java.io.File;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. import java.io.StringWriter;
  23. import java.nio.charset.StandardCharsets;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.UUID;
  27. import java.util.stream.Collectors;
  28. /**
  29. * 上报文件生成基础类
  30. *
  31. * @author xsl
  32. * @version 3.0
  33. */
  34. @Service
  35. @Slf4j
  36. public abstract class BaseUploadFileService extends BaseService {
  37. @Autowired
  38. UploadFileLogRepository uploadFileLogRepository;
  39. @Autowired
  40. UploadObjectService uploadObjectService;
  41. @Autowired
  42. UploadFileChannelService uploadFileChannelService;
  43. /**
  44. * 获取文件上报数值类型参数
  45. *
  46. * @param key
  47. * @param defaultValue
  48. * @return
  49. */
  50. protected int getTranSysParameter(String key, String defaultValue, String stationCode) {
  51. int returnValue;
  52. try {
  53. String value = super.getSysParameter(key, defaultValue, stationCode);
  54. returnValue = Integer.parseInt(value);
  55. } catch (RuntimeException e) {
  56. String errorInfo = "参数表" + key + "获取失败";
  57. log.error(errorInfo, e);
  58. returnValue = Integer.parseInt(defaultValue);
  59. }
  60. return returnValue;
  61. }
  62. /**
  63. * 查詢文件是否生成
  64. *
  65. * @param fileName
  66. * @return 存在则返回false,不存在返回true
  67. */
  68. protected boolean getFileName(String fileName, String fileType, String stationCode) {
  69. int cdqUpMin = getTranSysParameter("CDQ_UP_MIN", "0", stationCode);
  70. //今日凌晨
  71. long jt = DateTimeUtil.getMillisecondsSubDay() - (15 * 60 * 1000 + (long) cdqUpMin * 1000 * 60);
  72. //明日凌晨
  73. long mt = DateTimeUtil.getMillisecondsSubDay() + 1000 * 60 * 60 * 24;
  74. List<UploadFileLog> list = this.uploadFileLogRepository.findByFileNameAndFileTypeEnumAndCreateTimeBetweenAndStationCode(fileName, FileTypeEnum.valueOf(fileType), new Date(jt), new Date(mt), stationCode);
  75. return null == list || list.size() <= 0;
  76. }
  77. /**
  78. * 创建临时文件
  79. *
  80. * @param fileName 文件名
  81. * @return 临时文件
  82. */
  83. protected File createTempFile(String fileName) {
  84. // 获取临时目录
  85. String tempDir = FileUtil.getTempFilePath() + File.separator + UUID.randomUUID().toString().replace("-", "");
  86. boolean b = new File(tempDir).mkdir();
  87. if (!b) {
  88. throw new RuntimeException("创建文件" + fileName + "失败");
  89. }
  90. File file = new File(tempDir + File.separator + fileName);
  91. try {
  92. b = file.createNewFile();
  93. if (!b)
  94. throw new RuntimeException("创建文件" + fileName + "失败");
  95. } catch (IOException e) {
  96. throw new RuntimeException(e);
  97. }
  98. return file;
  99. }
  100. /**
  101. * 生成上报文件
  102. *
  103. * @param writer
  104. * @param file
  105. * @param fileType
  106. * @param uploadFileEndTime
  107. */
  108. protected void copyUploadFile(StringWriter writer, File file, String fileType, Long uploadFileEndTime, Date createTime, String stationCode) {
  109. FileOutputStream os = null;
  110. try {
  111. os = new FileOutputStream(file);
  112. // 采用UTF-8字符集
  113. os.write(writer.toString().getBytes(StandardCharsets.UTF_8));
  114. os.flush();
  115. // 将文件复制到上报路径中
  116. copyFileToUploadDir(file, fileType, uploadFileEndTime, createTime, stationCode);
  117. } catch (IOException e) {
  118. throw new RuntimeException(e);
  119. } finally {
  120. if (os != null) {
  121. try {
  122. os.close();
  123. } catch (IOException e) {
  124. log.error("文件生成关闭流失败", e);
  125. }
  126. }
  127. try {
  128. FileUtils.forceDelete(file.getParentFile());
  129. } catch (IOException e) {
  130. log.error("", e);
  131. }
  132. }
  133. }
  134. /**
  135. * 复制上报文件到上报路径
  136. *
  137. * @param file 需要上报的文件
  138. * @param fileType 文件类型
  139. * @param uploadFileEndTime 上报文件截止时间
  140. */
  141. @Transactional(propagation = Propagation.REQUIRED)
  142. public void copyFileToUploadDir(File file, String fileType, Long uploadFileEndTime, Date createTime, String stationCode) {
  143. String destFileDir = null;
  144. try {
  145. // 获取上报对象
  146. List<UploadObject> uploadObjectList = uploadObjectService.get();
  147. if (!uploadObjectList.isEmpty()) {
  148. // 遍历上报对象生成对应的文件
  149. for (UploadObject uploadObject : uploadObjectList) {
  150. if (uploadObject.getStationCode().equals(stationCode)) {
  151. List<UploadFileChannel> uploadFileChannelList = uploadFileChannelService.getByObjectId(uploadObject.getId());
  152. // 找出对象下是否有可用的通道
  153. List<UploadFileChannel> filterUploadFileChannelList = uploadFileChannelList.stream().filter(s -> "E1".equals(s.getChannelStatusEnum().toString())).collect(Collectors.toList());
  154. if (filterUploadFileChannelList.size() > 0) {
  155. String[] uploadFileType = uploadObject.getUploadFileType().split(",");
  156. for (String s : uploadFileType) {
  157. if (s.equals(fileType)) {
  158. // 上报文件目录
  159. destFileDir = FileUtil.getFileUploadPath() + File.separator + "process" + File.separator + uploadObject.getObjectNo() + File.separator + fileType;
  160. File destDir = new File(destFileDir);
  161. if (!destDir.exists()) {// 如果目录不存在则创建uploadFileEndTime目录
  162. boolean b = destDir.mkdirs();
  163. if (!b) // 如果创建失败则抛出异常
  164. throw new RuntimeException(destFileDir + " 目录创建失败");
  165. }
  166. if (!new File(destDir + File.separator + file.getName()).exists()) {
  167. FileUtils.copyFileToDirectory(file, destDir);
  168. UploadFileLog uploadFileLog = new UploadFileLog();
  169. uploadFileLog.setUploadObjectId(uploadObject.getId());
  170. uploadFileLog.setUploadObjectName(uploadObject.getUploadObjectName());
  171. uploadFileLog.setUploadObjectNo(uploadObject.getObjectNo());
  172. uploadFileLog.setFileName(file.getName());
  173. uploadFileLog.setFileTypeEnum(FileTypeEnum.valueOf(fileType));
  174. uploadFileLog.setFileStatusEnum(FileStatusEnum.E1);
  175. uploadFileLog.setUploadProtocolEnum(uploadObject.getUploadProtocolEnum());
  176. uploadFileLog.setUploadCounter(0);
  177. uploadFileLog.setUploadFileEndTime(uploadFileEndTime);
  178. uploadFileLog.setStationCode(stationCode);
  179. uploadFileLog = this.uploadFileLogRepository.save(uploadFileLog);
  180. if (createTime != null) {
  181. uploadFileLog.setCreateTime(createTime);
  182. Date date = new Date();
  183. String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
  184. uploadFileLog.setBackupA(format);
  185. uploadFileLog = this.uploadFileLogRepository.save(uploadFileLog);
  186. }
  187. String readyFileKey = uploadObject.getObjectNo() + "@" + fileType + "@" + file.getName();
  188. FileConstant.fileShouldMomentMap.put(readyFileKey, uploadFileLog.getId());
  189. FileConstant.readyUploadFileMap.put(readyFileKey, uploadFileLog);
  190. log.debug("文件生成存入缓存:" + file.getName());
  191. log.info("上报对象编号:" + uploadObject.getObjectNo() + ",生成文件" + file.getName() + "成功");
  192. } else {
  193. log.info("本地文件已经生成,上报对象编号:" + uploadObject.getObjectNo() + "," + file.getName() + "不再生成了");
  194. }
  195. break;
  196. }
  197. }
  198. }
  199. }
  200. }
  201. }
  202. } catch (Exception e) {
  203. // 文件复制失败进行告警
  204. String errorInfo = "复制文件[" + file.getName() + "]到上报目录[" + destFileDir + "]失败";
  205. log.error(errorInfo, e);
  206. }
  207. }
  208. }