lyh
13 小时以前 78aeb8a8c97a884a640d46755e4be706bde48b7d
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.lxzn.base.service.impl;
 
import com.lxzn.base.service.IReceiveFileService;
import com.lxzn.base.service.ISingleDictionaryService;
import com.lxzn.framework.domain.base.SingleDictionary;
import com.lxzn.framework.domain.base.ext.NcTxtFilePathInfo;
import com.lxzn.framework.utils.date.DateUtil;
import com.lxzn.framework.utils.file.FileUtil;
import com.lxzn.nc.service.IDocInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import java.io.*;
import java.util.Scanner;
 
/**
 * @author clown
 * * @date 2023/12/15
 */
@Service
@PropertySource("classpath:/scheduled.properties")
public class ReceiveFileServiceImpl implements IReceiveFileService {
 
    @Autowired
    private IDocInfoService docInfoService;
    @Autowired
    private ISingleDictionaryService dictionaryService;
    @Value("${serviceIntranet.filePath}")
    private String filePath;
 
    @Value("${serviceIntranet.newFilePath}")
    private String newFilePath;
    @Value("${fileNCPath}")
    private String fileNCPath;
 
    @Override
    @Scheduled(cron = "${ncFileRec}")
    public void analysisAndCopyFileRec() {
        if (StringUtils.isBlank(filePath)) {
            return;
        }
        File f3 = new File(filePath);
        File[] files = f3.listFiles();
        for (File fi : files){
            InputStreamReader input =null;
            BufferedReader bufferedReader =null;
            String loFilePath = filePath + fi.getName();
            System.out.println(loFilePath);
            try {
                if (FileUtil.getFileSuffix(fi.getName()).equals("nc")) {
                    String line;
                    int count = 0;
                    NcTxtFilePathInfo result = new NcTxtFilePathInfo();
                    FileInputStream fis = null;
                    try {
                        fis = new FileInputStream(loFilePath);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }
                    Scanner sc = new Scanner(fis, "UTF-8");
                    while (count < 8 &&  sc.hasNext()) {
                        line = sc.nextLine();
                        if (count ==0) {
                            result.setFileTxtName(line.trim());
                        } else  if (count ==1) {
                            result.setFileNcName(line.trim());
                        }else  if (count ==2) {
                            result.setOrigFileName(line.trim());
                        }else  if (count ==3) {
                            result.setOrigFileSuffix(line.trim());
                        }else  if (count ==4) {
                            result.setFilePath(line.trim());
                        }else  if (count ==5) {
                            result.setEquipmentId(line.trim());
                        }else  if (count ==6) {
                            result.setFileAddOrDelete(Integer.valueOf(line.trim()));
                        }else  if (count == 7) {
                            result.setFileSize(line.trim());
                        }
                        count++;
                    }
                    if (sc != null) {
                        sc.close();
                    }
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (input != null) {
                        input.close();
                    }
                    if (result == null) {
                        continue;
                    }
                    if (result.getFileAddOrDelete() != null ) {
                        if ( result.getFileAddOrDelete()==1) {
                            String path = "";
                            SingleDictionary singleDictionary=dictionaryService.findListFromTypeCode("virtual_equipment_id").get(0);
                            String docEquipmentId = null;
                            if (singleDictionary != null) {
                                docEquipmentId= String.valueOf(singleDictionary.getDicValue());
                            }
                            if (result.getEquipmentId().equals(docEquipmentId)) {
                                //虚拟设备处理
                                path = filePath + result.getOrigFileName();
                                if (StringUtils.isNotBlank(result.getOrigFileName()) &&
                                        result.getOrigFileName().equals("null")) {
                                    //文件待处理
                                    //备份数据
                                    FileUtil.deleteFileNewRec(loFilePath);
                                    continue;
                                }
                            }else {
                                path = filePath + result.getFileNcName()+ ".NC";
                                if (StringUtils.isNotBlank(result.getFileNcName()) &&
                                        result.getFileNcName().equals("null")) {
                                    //文件待处理
                                    //备份数据
                                    FileUtil.deleteFileNewRec(loFilePath);
                                    continue;
                                }
                            }
                            File ncFile = new File(path);
                            if (ncFile == null) {
                                continue;
                            } else {
                                //已经存在文件需要备份
                                String size = String.valueOf(ncFile.length());
                                if (!size.equals(result.getFileSize())) {
                                    continue;
                                }
 
                                boolean bool = docInfoService.addDocInfoRecService(result.getEquipmentId(),
                                        ncFile,result.getOrigFileSuffix(),
                                        FileUtil.getFilenameNonSuffix(result.getOrigFileName())
                                        ,result.getFilePath());
                                if (bool) {
                                    FileUtil.deleteFileNewRec(path);
                                    FileUtil.deleteFileNewRec(loFilePath);
                                }
                            }
                        } else {
                            String path;
                            /*if(StringUtils.isBlank(result.getOrigFileSuffix())) {
                                path = fileNCPath + "/" + result.getFilePath() + "/send/" +result.getOrigFileName();
                            } else {
                                path = fileNCPath + "/" + result.getFilePath() + "/send/" +result.getOrigFileName()+ "." + result.getOrigFileSuffix();
                            }*/
 
                            /*File ncFile = new File(path);*/
                            /*if (ncFile == null) {
                                return;
                            } else {
                                //待优化
                                boolean deleteFile = FileUtil.copyFileNcToBak(result.getFilePath(),
                                        result.getOrigFileName(), result.getOrigFileSuffix());
                                FileUtil.deleteFile(path);
                                FileUtil.deleteFile(loFilePath);
                            }*/
                        }
 
                    }
                }
            } catch (Exception e1) {
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (input != null) {
                        input.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                e1.printStackTrace();
            } finally {
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                    if (input != null) {
                        input.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}