package com.qmth.cet.plug.service.impl; import com.qmth.cet.plug.contant.SystemConstant; import com.qmth.cet.plug.enums.ExceptionResultEnum; import com.qmth.cet.plug.lock.LockService; import com.qmth.cet.plug.service.LogicService; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.ExecutorService; /** * @Description: logic service impl * @Param: * @return: * @Author: wangliang * @Date: 2025/6/13 */ @Service public class LogicServiceImpl implements LogicService { private final static Logger log = LoggerFactory.getLogger(LogicServiceImpl.class); @Resource LockService lockService; /** * 执行解析数据 * * @param fileName * @param projectPath * @param rootDirName * @param executor */ @Override public void execFile(String fileName, String projectPath, String rootDirName, ExecutorService executor) throws IOException, InterruptedException { File mkdir = new File(fileName); if (!mkdir.exists()) { throw ExceptionResultEnum.ERROR.exception(rootDirName + "目录不存在"); } if (mkdir.isDirectory()) { File[] files = mkdir.listFiles(); if (Objects.nonNull(files) && files.length > 0) { List mkdirFileList = new ArrayList<>(); for (int i = 0; i < files.length; i++) { mkdirFileList = SystemConstant.analyzeDir(files[i], mkdirFileList); } log.info("mkdirFileList:{}", mkdirFileList); for (File file : mkdirFileList) { String rootPath = file.getAbsolutePath(); rootPath = rootDirName + rootPath.replaceAll(fileName, ""); File root = new File(projectPath, rootPath); if (!root.exists()) { root.mkdirs(); } log.info("root:{}", root); String lockKey = rootPath; String finalRootPath = rootPath; executor.submit(() -> { log.info("启动新线程===>id:{},name:{}", Thread.currentThread().getId(), Thread.currentThread().getName()); for (; ; ) { boolean scanLock = lockService.lock(lockKey, lockKey, SystemConstant.LOCK_TIME_OUT);//30分钟 if (scanLock) { try { this.scanTxtExecLogic(projectPath, finalRootPath, file);//扫描txt处理 } catch (Exception e) { log.error(SystemConstant.LOG_ERROR, e); } finally { lockService.unlock(lockKey, lockKey); break; } } else { try { Thread.sleep(5000); } catch (InterruptedException e) { log.error(SystemConstant.LOG_ERROR, e); } } } }); } } } } /** * 扫描文件执行逻辑 * * @param projectPath * @param rootPath * @param file * @throws IOException */ protected void scanTxtExecLogic(String projectPath, String rootPath, File file) throws IOException { //扫描txt处理 File[] fileArray = file.listFiles(); List fileList = new ArrayList<>(Arrays.asList(fileArray)); int min = 0, max = SystemConstant.MAX_QUERY_SIZE, size = fileList .size(); if (max >= size) { max = size; } while (max <= size) { List subList = fileList.subList(min, max); List scanList = null; StringJoiner scanSj = new StringJoiner("\r\n");//全量扫描数据 StringJoiner scanIncrementSj = new StringJoiner("\r\n");//增量扫描数据 File scanTxtFile = new File(projectPath, rootPath + File.separator + "scan.txt"); if (!scanTxtFile.exists()) { scanTxtFile.createNewFile(); } else { ByteArrayOutputStream ou = new ByteArrayOutputStream(); IOUtils.copy(new FileInputStream(scanTxtFile), ou); String string = new String(ou.toByteArray(), StandardCharsets.UTF_8); scanSj.add(string); String temp = new String(string); String[] strs = StringUtils.split(temp, "\r\n"); scanList = new ArrayList<>(Arrays.asList(strs)); } boolean write = false; for (int y = 0; y < subList.size(); y++) { String path = subList.get(y).getAbsolutePath(); if (!path.endsWith("temp") && (path.endsWith("jpg") || path.endsWith("JPG"))) { if (!CollectionUtils.isEmpty(scanList)) { if (!scanList.contains(path)) { scanIncrementSj.add(path); scanSj.add(path); write = true; } } else { scanSj.add(path); write = true; } } } if (write) { IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(scanTxtFile)); } //temp临时目录 String tempDir = file.getAbsolutePath() + File.separator + "temp" + File.separator; File tempFileDir = new File(tempDir); if (!tempFileDir.exists()) { tempFileDir.mkdirs(); } if (write) { //生成临时scanTxt和scanResultTxt文件 String tempUUid = SystemConstant.getNanoId(); File tempScanTxt = new File(tempDir, tempUUid + ".txt"); tempScanTxt.createNewFile(); File tempScanResultTxt = new File(tempDir, tempUUid + "-result.txt"); tempScanResultTxt.createNewFile(); if (scanIncrementSj.length() > 0) { IOUtils.write(scanIncrementSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(tempScanTxt)); } else { IOUtils.write(scanSj.toString().getBytes(StandardCharsets.UTF_8), new FileOutputStream(tempScanTxt)); } } if (max == size) { break; } min = max; max += SystemConstant.MAX_QUERY_SIZE; if (max >= size) { max = size; } } } /** * 执行成功文件逻辑 * * @param projectPath * @param rootPath * @param file * @throws IOException */ protected void successTxtExecLogic(String projectPath, String rootPath, File file) throws IOException { File successTxtFile = new File(projectPath, rootPath + File.separator + "success.txt"); if (!successTxtFile.exists()) { successTxtFile.createNewFile(); } } /** * 执行错误文件逻辑 * * @param projectPath * @param rootPath * @param file * @throws IOException */ protected void errorTxtExecLogic(String projectPath, String rootPath, File file) throws IOException { File errorTxtFile = new File(projectPath, rootPath + File.separator + "error.txt"); if (!errorTxtFile.exists()) { errorTxtFile.createNewFile(); } } }