LogicServiceImpl.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package com.qmth.cet.plug.service.impl;
  2. import com.qmth.cet.plug.contant.SystemConstant;
  3. import com.qmth.cet.plug.enums.ExceptionResultEnum;
  4. import com.qmth.cet.plug.lock.LockService;
  5. import com.qmth.cet.plug.service.LogicService;
  6. import org.apache.commons.io.IOUtils;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.util.CollectionUtils;
  12. import javax.annotation.Resource;
  13. import java.io.*;
  14. import java.nio.charset.StandardCharsets;
  15. import java.util.*;
  16. import java.util.concurrent.ExecutorService;
  17. /**
  18. * @Description: logic service impl
  19. * @Param:
  20. * @return:
  21. * @Author: wangliang
  22. * @Date: 2025/6/13
  23. */
  24. @Service
  25. public class LogicServiceImpl implements LogicService {
  26. private final static Logger log = LoggerFactory.getLogger(LogicServiceImpl.class);
  27. @Resource
  28. LockService lockService;
  29. /**
  30. * 执行解析数据
  31. *
  32. * @param fileName
  33. * @param projectPath
  34. * @param rootDirName
  35. * @param executor
  36. */
  37. @Override
  38. public void execFile(String fileName, String projectPath, String rootDirName, ExecutorService executor) throws IOException, InterruptedException {
  39. File mkdir = new File(fileName);
  40. if (!mkdir.exists()) {
  41. throw ExceptionResultEnum.ERROR.exception(rootDirName + "目录不存在");
  42. }
  43. if (mkdir.isDirectory()) {
  44. File[] files = mkdir.listFiles();
  45. if (Objects.nonNull(files) && files.length > 0) {
  46. List<File> mkdirFileList = new ArrayList<>();
  47. for (int i = 0; i < files.length; i++) {
  48. mkdirFileList = SystemConstant.analyzeDir(files[i], mkdirFileList);
  49. }
  50. log.info("mkdirFileList:{}", mkdirFileList);
  51. for (File file : mkdirFileList) {
  52. String rootPath = file.getAbsolutePath();
  53. rootPath = rootDirName + rootPath.replaceAll(fileName, "");
  54. File root = new File(projectPath, rootPath);
  55. if (!root.exists()) {
  56. root.mkdirs();
  57. }
  58. log.info("root:{}", root);
  59. String lockKey = rootPath;
  60. String finalRootPath = rootPath;
  61. executor.submit(() -> {
  62. log.info("启动新线程===>id:{},name:{}", Thread.currentThread().getId(), Thread.currentThread().getName());
  63. for (; ; ) {
  64. boolean scanLock = lockService.lock(lockKey, lockKey, SystemConstant.LOCK_TIME_OUT);//30分钟
  65. if (scanLock) {
  66. try {
  67. this.scanTxtExecLogic(projectPath, finalRootPath, file);//扫描txt处理
  68. } catch (Exception e) {
  69. log.error(SystemConstant.LOG_ERROR, e);
  70. } finally {
  71. lockService.unlock(lockKey, lockKey);
  72. break;
  73. }
  74. } else {
  75. try {
  76. Thread.sleep(5000);
  77. } catch (InterruptedException e) {
  78. log.error(SystemConstant.LOG_ERROR, e);
  79. }
  80. }
  81. }
  82. });
  83. }
  84. }
  85. }
  86. }
  87. /**
  88. * 扫描文件执行逻辑
  89. *
  90. * @param projectPath
  91. * @param rootPath
  92. * @param file
  93. * @throws IOException
  94. */
  95. protected void scanTxtExecLogic(String projectPath, String rootPath, File file) throws IOException {
  96. //扫描txt处理
  97. File[] fileArray = file.listFiles();
  98. List<File> fileList = new ArrayList<>(Arrays.asList(fileArray));
  99. int min = 0, max = SystemConstant.MAX_QUERY_SIZE, size = fileList
  100. .size();
  101. if (max >= size) {
  102. max = size;
  103. }
  104. while (max <= size) {
  105. List<File> subList = fileList.subList(min, max);
  106. List<String> scanList = null;
  107. StringJoiner scanSj = new StringJoiner("\r\n");//全量扫描数据
  108. StringJoiner scanIncrementSj = new StringJoiner("\r\n");//增量扫描数据
  109. File scanTxtFile = new File(projectPath, rootPath + File.separator + "scan.txt");
  110. if (!scanTxtFile.exists()) {
  111. scanTxtFile.createNewFile();
  112. } else {
  113. ByteArrayOutputStream ou = new ByteArrayOutputStream();
  114. IOUtils.copy(new FileInputStream(scanTxtFile), ou);
  115. String string = new String(ou.toByteArray(), StandardCharsets.UTF_8);
  116. scanSj.add(string);
  117. String temp = new String(string);
  118. String[] strs = StringUtils.split(temp, "\r\n");
  119. scanList = new ArrayList<>(Arrays.asList(strs));
  120. }
  121. boolean write = false;
  122. for (int y = 0; y < subList.size(); y++) {
  123. String path = subList.get(y).getAbsolutePath();
  124. if (!path.endsWith("temp") && (path.endsWith("jpg") || path.endsWith("JPG"))) {
  125. if (!CollectionUtils.isEmpty(scanList)) {
  126. if (!scanList.contains(path)) {
  127. scanIncrementSj.add(path);
  128. scanSj.add(path);
  129. write = true;
  130. }
  131. } else {
  132. scanSj.add(path);
  133. write = true;
  134. }
  135. }
  136. }
  137. if (write) {
  138. IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(scanTxtFile));
  139. }
  140. //temp临时目录
  141. String tempDir = file.getAbsolutePath() + File.separator + "temp" + File.separator;
  142. File tempFileDir = new File(tempDir);
  143. if (!tempFileDir.exists()) {
  144. tempFileDir.mkdirs();
  145. }
  146. if (write) {
  147. //生成临时scanTxt和scanResultTxt文件
  148. String tempUUid = SystemConstant.getNanoId();
  149. File tempScanTxt = new File(tempDir, tempUUid + ".txt");
  150. tempScanTxt.createNewFile();
  151. File tempScanResultTxt = new File(tempDir, tempUUid + "-result.txt");
  152. tempScanResultTxt.createNewFile();
  153. if (scanIncrementSj.length() > 0) {
  154. IOUtils.write(scanIncrementSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(tempScanTxt));
  155. } else {
  156. IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(tempScanTxt));
  157. }
  158. }
  159. if (max == size) {
  160. break;
  161. }
  162. min = max;
  163. max += SystemConstant.MAX_QUERY_SIZE;
  164. if (max >= size) {
  165. max = size;
  166. }
  167. }
  168. }
  169. /**
  170. * 执行成功文件逻辑
  171. *
  172. * @param projectPath
  173. * @param rootPath
  174. * @param file
  175. * @throws IOException
  176. */
  177. protected void successTxtExecLogic(String projectPath, String rootPath, File file) throws IOException {
  178. File successTxtFile = new File(projectPath, rootPath + File.separator + "success.txt");
  179. if (!successTxtFile.exists()) {
  180. successTxtFile.createNewFile();
  181. }
  182. }
  183. /**
  184. * 执行错误文件逻辑
  185. *
  186. * @param projectPath
  187. * @param rootPath
  188. * @param file
  189. * @throws IOException
  190. */
  191. protected void errorTxtExecLogic(String projectPath, String rootPath, File file) throws IOException {
  192. File errorTxtFile = new File(projectPath, rootPath + File.separator + "error.txt");
  193. if (!errorTxtFile.exists()) {
  194. errorTxtFile.createNewFile();
  195. }
  196. }
  197. }