| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.springblade.mdm.task; |
| | | |
| | | import com.alibaba.excel.util.StringUtils; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.springblade.mdm.basesetting.machine.MachineService; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.commons.service.ParamService; |
| | | import org.springblade.mdm.gkw.programnode.entity.MachineFile; |
| | | import org.springblade.mdm.gkw.programnode.service.MachineFileService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.EnableScheduling; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | | import java.nio.file.StandardCopyOption; |
| | | import java.nio.file.attribute.BasicFileAttributes; |
| | | import java.nio.file.attribute.FileTime; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * æºåºåä¼ æä»¶å¤¹recï¼ç®å½æä»¶è¶
è¿æºåºè®¾å®çæ¶é´ç§»å¨å°tempæä»¶å¤¹ |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | @EnableScheduling |
| | | public class ReceiveDirClearTask { |
| | | @Autowired |
| | | private MachineFileService machineFileService; |
| | | @Autowired |
| | | private MachineService machineService; |
| | | @Autowired |
| | | private ParamService paramService; |
| | | /** |
| | | * æä»¶é»è®¤ä¿åå°æ¶æ° |
| | | */ |
| | | private static final int DEFAULT_HOUR = 8; |
| | | // æ¯5ç§æ§è¡ä¸æ¬¡ |
| | | //@Scheduled(fixedRate = 1000000) |
| | | //@Scheduled(cron = "0 1 0 * * ?") // æ¯å¤©ä¸å0ç¹1åæ§è¡ |
| | | //@Scheduled(cron = "0 */3 * * * ?") |
| | | //@Scheduled(cron = "0 15 19 * * ?") //test |
| | | //@Scheduled(cron = "${task.cron.machine_file_scan}") |
| | | @Scheduled(cron = "${task.cron.machine_rec_clean:0 3 * * * ?}") |
| | | public void execute() { |
| | | String networkType = paramService.getParamValue(ParamService.NETWORK_TYPE,ParamService.NETWORK_TYPE_SHEMI); |
| | | |
| | | if(!ParamService.NETWORK_TYPE_SHEMI.equals(networkType)){ |
| | | //éæ¶å¯ç½ï¼ææ«æç®å½æä»¶ |
| | | moveOverTimeFiles(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * æ«ææ°æ®åºè®°å½ï¼è¶
æ¶åç§»å¨æä»¶ |
| | | */ |
| | | public void moveOverTimeFiles() { |
| | | List<Machine> machines = machineService.getEnableMachines(); |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | for (Machine machine : machines) { |
| | | int remainHours = machine.getReceiveDirExpiryHours() != null ?machine.getReceiveDirExpiryHours():DEFAULT_HOUR; |
| | | LocalDateTime earlyTime = now.minusHours(remainHours); |
| | | List<MachineFile> overTimeFiles = this.machineFileService.lambdaQuery().lt(MachineFile::getFileCreateTime,earlyTime) |
| | | .eq(MachineFile::getDirType,MachineFile.DIR_TYPE_REC) |
| | | .eq(MachineFile::getMachineCode,machine.getCode()).list(); |
| | | |
| | | for(MachineFile overTimeFile : overTimeFiles){ |
| | | try { |
| | | moveFileToTemp(overTimeFile, machine); |
| | | }catch(Exception e){ |
| | | log.error("ç§»å¨recæä»¶å¤±è´¥:"+overTimeFile.getName(),e); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * æä»¶ç§»å¨å°temp |
| | | * @param overTimeFile |
| | | */ |
| | | private void moveFileToTemp(MachineFile overTimeFile,Machine machine) throws IOException { |
| | | String srcFilepath = MachineFileService.getBasePath(machine,overTimeFile.getDirType())+ File.separator+overTimeFile.getName(); |
| | | Path source = Paths.get(srcFilepath); |
| | | if(!source.toFile().exists()){ |
| | | return; |
| | | } |
| | | String targetFilepath = MachineFileService.getBasePath(machine,MachineFile.DIR_TYPE_TEMP)+ File.separator+overTimeFile.getName(); |
| | | Path target = Paths.get(targetFilepath); |
| | | |
| | | Path targetDir = target.getParent(); |
| | | if (targetDir != null && !Files.exists(targetDir)) { |
| | | Files.createDirectories(targetDir); |
| | | } |
| | | |
| | | // ä½¿ç¨ REPLACE_EXISTING é项æ¥è¦çå·²åå¨çæä»¶ |
| | | Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * æ«æç®å½ |
| | | * @param machine æºåºä¿¡æ¯ |
| | | * @param dirType ç®å½ç±»å |
| | | * @throws IOException |
| | | */ |
| | | public void scanDir(Machine machine,String dirType) throws IOException { |
| | | String basePath = MachineFileService.getBasePath(machine,dirType); |
| | | if(basePath == null) { |
| | | log.warn("ç®å½ç±»åä¸å¹é
:{}",dirType); |
| | | return; |
| | | } |
| | | Path dirPath = Paths.get(basePath); |
| | | if(!dirPath.toFile().exists()){ |
| | | log.warn("æ«æç®å½:{} ä¸åå¨",dirPath); |
| | | return; |
| | | } |
| | | // åªå
嫿®éæä»¶ |
| | | List<Path> files = Files.list(dirPath) |
| | | .filter(Files::isRegularFile).toList(); |
| | | |
| | | byte[] buffer = new byte[2048]; |
| | | |
| | | MachineFile existFileInDb; |
| | | //boolean exists;//æä»¶æ¯å¦åå¨äºæ°æ®åºä¸ |
| | | for (Path filePath : files) { |
| | | |
| | | existFileInDb = null; |
| | | //exists = false; |
| | | |
| | | MachineFile mf = new MachineFile(); |
| | | |
| | | mf.setTenantId("000000"); |
| | | mf.setName(filePath.toFile().getName()); |
| | | mf.setDirType(dirType); |
| | | mf.setMachineCode(machine.getCode()); |
| | | |
| | | try { |
| | | BasicFileAttributes attrs = Files.readAttributes( |
| | | filePath, |
| | | BasicFileAttributes.class |
| | | ); |
| | | |
| | | FileTime creationTime = attrs.creationTime(); |
| | | Date creationDate = new Date(creationTime.toMillis()); |
| | | mf.setFileCreateTime(creationDate); |
| | | |
| | | FileTime modifyTime = attrs.lastModifiedTime(); |
| | | mf.setFileModifyTime(new Date(modifyTime.toMillis())); |
| | | mf.setFileSize(Files.size(filePath)); |
| | | try (InputStream inputStream = Files.newInputStream(filePath)) { |
| | | // 使ç¨è¾å
¥æµè¯»åæä»¶å
容 |
| | | int bytesRead = inputStream.read(buffer); |
| | | mf.setMd5(DigestUtils.md5Hex(buffer)); |
| | | } catch (IOException e) { |
| | | log.error("读åæä»¶md5失败",e); |
| | | continue;//æé误,æ è¿ |
| | | } |
| | | |
| | | //exists = machineFileService.fileExists(mf.getName(),dirType,machine.getCode()); |
| | | |
| | | existFileInDb = machineFileService.getExistsFile(mf.getName(),dirType,machine.getCode()); |
| | | System.out.println("æä»¶å建æ¶é´: " + creationDate); |
| | | } catch (IOException e) { |
| | | log.error("读åæä»¶ä¿¡æ¯å¤±è´¥",e); |
| | | } |
| | | if(existFileInDb == null) { |
| | | machineFileService.save(mf); |
| | | }else{ |
| | | log.info("æä»¶å·²å卿 è¿:{}",filePath.getFileName()); |
| | | //å·²åå¨åæ´ã |
| | | existFileInDb.setFileSize(mf.getFileSize()); |
| | | |
| | | existFileInDb.setFileCreateTime(mf.getFileCreateTime()); |
| | | existFileInDb.setFileModifyTime(mf.getFileModifyTime()); |
| | | if(!StringUtils.equals(existFileInDb.getMd5(),mf.getMd5())){ |
| | | //æä»¶å
容åçååäº,è®¾ç½®ç¶æä¸ºåå§ç¶æ |
| | | existFileInDb.setStatus(MachineFile.STATUS_NORMAL); |
| | | } |
| | | existFileInDb.setMd5(mf.getMd5()); |
| | | |
| | | |
| | | machineFileService.updateById(existFileInDb); |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * æ¸
çå·²ç»å é¤äºæä»¶çè®°å½ |
| | | * @param machine |
| | | * @param dirType |
| | | * @throws IOException |
| | | */ |
| | | void clearDeletedReccords(Machine machine,String dirType) throws IOException { |
| | | List<MachineFile> machineFiles = this.machineFileService.lambdaQuery().eq(MachineFile::getDirType,dirType) |
| | | .eq(MachineFile::getMachineCode,machine.getCode()).list(); |
| | | |
| | | String basePath = MachineFileService.getBasePath(machine,dirType); |
| | | if(basePath == null) { |
| | | log.warn("ç®å½ç±»åä¸å¹é
:{}",dirType); |
| | | return; |
| | | } |
| | | Path dirPath = Paths.get(basePath); |
| | | if(!dirPath.toFile().exists()){ |
| | | log.warn("æ«æç®å½:{} ä¸åå¨",dirPath); |
| | | return; |
| | | } |
| | | |
| | | List<Path> files = Files.list(dirPath) |
| | | .filter(Files::isRegularFile).toList(); |
| | | |
| | | for(MachineFile mf : machineFiles){ |
| | | long findCount = files.stream().filter(filePath -> filePath.toFile().getName().equals(mf.getName())).count(); |
| | | if(findCount == 0){ |
| | | //æä»¶å¤¹å
没æ¾å°ï¼éè¦å é¤è®°å½ |
| | | mf.markFileDeleted(); |
| | | this.machineFileService.updateById(mf); |
| | | } |
| | | } |
| | | } |
| | | } |