yangys
2025-09-13 083df8d788c95c009a94378e620684eb5de2bd21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.springblade.mdm.task;
 
import lombok.extern.slf4j.Slf4j;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.basesetting.machine.service.MachineService;
import org.springblade.mdm.commons.service.ParamService;
import org.springblade.mdm.gkw.programnode.vo.ProgramNameVO;
import org.springblade.mdm.machinefile.entity.FileSendRecord;
import org.springblade.mdm.machinefile.entity.MachineFile;
import org.springblade.mdm.machinefile.service.FileSendRecordService;
import org.springblade.mdm.machinefile.service.MachineFileService;
import org.springblade.mdm.utils.FileContentUtil;
import org.springblade.mdm.utils.ProgramFileNameParser;
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.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
/**
 * 机床回传rec文件夹内文件异常检查
 */
@Slf4j
@Component
@EnableScheduling
public class ReceiveDirCheckTask {
    @Autowired
    private MachineFileService machineFileService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private ParamService paramService;
    @Autowired
    private FileSendRecordService fileSendRecordService;
    /**
     * 文件默认保存小时数,0不限制
     */
    private static final int DEFAULT_HOUR = 0;
    // 每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_check:0 */10 * * * ?}")
    public void execute() {
        String networkType = paramService.getParamValue(ParamService.NETWORK_TYPE,ParamService.NETWORK_TYPE_SHEMI);
 
        if(!ParamService.NETWORK_TYPE_SHEMI.equals(networkType)){
            //非涉密网,才扫描目录文件
            checkFiles();
        }
 
    }
 
    /**
     * 扫描数据库记录,超时则移动文件
     */
    public void checkFiles() {
        List<Machine> machines = machineService.getEnableMachines();
 
        for (Machine machine : machines) {
 
            List<MachineFile> recFiles = this.machineFileService.lambdaQuery()
                .eq(MachineFile::getDirType,MachineFile.DIR_TYPE_REC)
                .eq(MachineFile::getMachineCode,machine.getCode()).list();
 
            for(MachineFile machineFile : recFiles){
                try {
                    checkFile(machineFile, recFiles,machine);
                }catch(Exception e){
                    log.error("检查rec文件失败:"+machineFile.getName(),e);
                }
            }
 
        }
 
    }
 
    /**
     * 文件移动到temp
     * @param machineFile
     */
    private void checkFile(MachineFile machineFile,List<MachineFile> allFilesInDir,Machine machine) throws IOException {
        String srcFilepath = MachineFileService.getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName();
        Path source = Paths.get(srcFilepath);
        if(!source.toFile().exists()){
            return;
        }
        try(InputStream fileIns = Files.newInputStream(Paths.get(srcFilepath));) {
            if(!FileContentUtil.isTextFile(fileIns)){
                //非文本
                machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_TEXT);
            }else{
                ProgramNameVO progNameVO =  ProgramFileNameParser.parseProgramName(machineFile.getName());
                if(!progNameVO.isValidFilename()){
                    machineFile.setExceptionType(MachineFile.EXCEPTION_BAD_FILENAME);
                }else {
                    String prefix = progNameVO.logicProgramName()+"-";
                    long matchCount = allFilesInDir.stream().filter(file -> file.getName().startsWith(prefix)).count();
                    if(matchCount != progNameVO.getSegmentCount()){//文件段数缺失
                        machineFile.setExceptionType(MachineFile.EXCEPTION_LOST_FILES);
                    }else{
                        //检查是否匹配下发记录的段数
                        //正负3秒作为查询时间
                        Date beginTime = new Date(machineFile.getFileCreateTime().getTime()-3000);
                        Date endTime = new Date(machineFile.getFileCreateTime().getTime()+3000);
                        Optional<FileSendRecord> optFile = fileSendRecordService.lambdaQuery()
                            .eq(FileSendRecord::getMachineCode,machineFile.getMachineCode())
                            .likeRight(FileSendRecord::getName,prefix).between(FileSendRecord::getCreateTime,beginTime,endTime).oneOpt();
 
                        if(optFile.isPresent()){
                            //确实下发过,比对总段数是否相同
                            FileSendRecord sendFile = optFile.get();
                            ProgramNameVO sendProgNameVO =  ProgramFileNameParser.parseProgramName(sendFile.getName());
                            if(progNameVO.getSegmentCount() != sendProgNameVO.getSegmentCount()){
                                //段数不匹配
                                machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_MATCH_SEND);
                            }else{
                                machineFile.setExceptionType(MachineFile.EXCEPTION_OK);
                            }
                        }
                    }
                }
            }
 
            machineFileService.updateById(machineFile);
        }
    }
}