admin
2024-04-09 3d28cc569ff9bdf20ac56598ae4de8e2809d6f35
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.ruoyi.interchange.service.impl;
 
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.config.ActiveManager;
import com.ruoyi.interchange.domain.TbDataRules;
import com.ruoyi.interchange.domain.TbDataSendRecords;
import com.ruoyi.interchange.service.ITbDataSendRecordsService;
import com.ruoyi.interchange.service.PlatformService;
import com.ruoyi.utils.DESUtil;
import com.ruoyi.utils.FileUtils;
import com.ruoyi.utils.FtpUtils;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.jms.Destination;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
 
@Service
public class PlatformServiceImpl implements PlatformService {
    @Autowired
    public ITbDataSendRecordsService tbDataSendRecordsService;
    @Autowired
    private ActiveManager activeManager;
    /**
     * 将文件夹下的文件校验,发送
     *
     * @param rule 校验规则对象
     * @param list 接收文件列表
     */
    public void checkFileName(TbDataRules rule, List<File> list){
        try {
            Date date=new Date();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String path=rule.getReceiveUrl();
            FtpUtils ftpUtil=new FtpUtils();
            String ftpPath= RuoYiConfig.getFtpPath()+"/"+path;        //接收文件路径
            String sendPath= RuoYiConfig.getFtpPath()+"/send/"+path; //待发送路径
            String bakPath=RuoYiConfig.getFtpPath()+"/bak/"+path;    //备份路径
            TbDataSendRecords records=new TbDataSendRecords();      //接收文件记录
            if(StringUtils.isNotEmpty(rule.getCoalMineCode())){
                records.setCoalMineId(Long.parseLong(rule.getCoalMineCode()));
            }
            UUID uuid=UUID.randomUUID();
            records.setId(uuid.toString());
            records.setSystemId(rule.getSystemId());
            records.setRuleId(rule.getId());
            records.setDetectionTime(new Date());
            String dataHeader=rule.getCheckHead()+"0;"+sdf.format(date);
            records.setDataHeader(dataHeader);
            if(list==null||list.size()==0){
                //生成校验记录未接收到文件,已经生成空白文件
                String fileName= FileUtils.createFtpFile(path,rule.getCheckTitle(),rule.getCheckHead());
                records.setFileName(fileName);
                records.setDataNumber(0l);
                records.setSendFileUrl(sendPath);
                records.setDetectionResult("0");
                records.setCheckResult("1");
                fileName=fileName.substring(0,fileName.length()-4)+".tmp";
                String fileRoot=sendPath+"/"+fileName;
                boolean storeFlag=ftpUtil.uploadFile(path,"",fileName,fileRoot);
                if(storeFlag){
                    records.setSendResult("0");
                }else{
                    records.setSendResult("1");
                }
                tbDataSendRecordsService.recordSaveMongo(records);
                Boolean flag= FileUtils.deleteFile(fileRoot);
                System.out.println("是否成功删除=====================================================================》"+flag);
            }else{
                boolean noHave = true;
                for(File file:list){
                    if(file.getName().contains(rule.getCheckTitle())){
                        //生成校验记录已接收到文件,文件标题校验通过
                        records.setFileName(file.getName());
                        records.setSendFileUrl(sendPath);
                        records.setDetectionResult("0");
                        records.setCheckResult("0");
                        tbDataSendRecordsService.recordSaveMongo(records);
                        //文件名符合规范
                        //文件发送到send文件夹下
                        FileUtils.copyFile(new File(ftpPath+"/"+file.getName()),new File(sendPath+"/"+file.getName()));
                        //文件发送bak文件夹下
                        FileUtils.copyFile(new File(ftpPath+"/"+file.getName()),new File(bakPath+"/"+file.getName()));
                        //删除201目录下的文件
                        file.delete();
                        //校验与加密队列
                        Destination destination = new ActiveMQQueue("encryption");//P2P模式单个消费者
                        rule.setSendPath(sendPath+"/"+file.getName());
                        activeManager.send(destination,JSON.toJSONString(rule));
                        noHave = false;
                    }
                }
                if (noHave){
                    String fileName=FileUtils.createFtpFile(path,rule.getCheckTitle(),rule.getCheckHead());
                    records.setFileName(fileName);
                    records.setDataNumber(0l);
                    records.setSendFileUrl(sendPath);
                    records.setDetectionResult("0");
                    records.setCheckResult("1");
                    fileName=fileName.substring(0,fileName.length()-4)+".tmp";
                    String fileRoot=sendPath+"/"+fileName;
                    boolean storeFlag=ftpUtil.uploadFile(path,"",fileName,fileRoot);
                    if(storeFlag){
                        records.setSendResult("0");
                    }else{
                        records.setSendResult("1");
                    }
                    tbDataSendRecordsService.recordSaveMongo(records);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 将发送失败的文件重新发送
     *
     */
    public void waitFileSend(String path){
        try {
            String filePath=RuoYiConfig.getFtpPath()+"/wait"+"/"+path;        //接收文件路径
            FtpUtils ftpUtils=new FtpUtils();
            List<File>list= FileUtils.getAllFile(filePath);
            if(list!=null&&list.size()>0){
                for(File file:list){
                    Boolean flag=ftpUtils.uploadFile(path,"",file.getName(),file.getPath());
                    if(flag){
                        Files.delete(Paths.get(file.getPath()));
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
 
 
    public void  fileEncrypt(String path,String tmpPath) throws Exception {
        FileOutputStream fot=null;
        OutputStreamWriter ops=null;
        File tmpFile=new File(tmpPath);
        tmpFile.createNewFile();
        try {
            //文件加密
            List<String>list=FileUtils.readFile(path);
            byte []fileByte=new byte[list.size()];
            fot=new FileOutputStream(tmpFile);
            ops=new OutputStreamWriter(fot);
            for(int i=0;i<list.size();i++){
                fileByte=list.get(i).getBytes(StandardCharsets.UTF_8);
                byte[] byte2= DESUtil.encrypt(fileByte,RuoYiConfig.getPass());
                String line=DESUtil.byteToHex(byte2);
                ops.write(line);
            }
            //FileUtils.moveFile(path,tmpPath);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ops!=null){
                ops.flush();
                ops.close();
            }
            if(fot!=null){
                fot.flush();
                fot.close();
            }
        }
    }
    public  void  fileEncrypt2(String path,String tmpPath) throws Exception {
        FileOutputStream fot=null;
        OutputStreamWriter ops=null;
        File file=new File(path);
        try {
            //文件加密
            List<String>list=FileUtils.readFile(path);
            byte []fileByte=new byte[list.size()];
            fot=new FileOutputStream(file);
            ops=new OutputStreamWriter(fot);
            for(int i=0;i<list.size();i++){
                fileByte=list.get(i).getBytes(StandardCharsets.UTF_8);
                byte[] byte2= DESUtil.encrypt(fileByte,"xxxltk00");
                String line=DESUtil.byteToHex(byte2);
                ops.write(line);
            }
            ops.flush();
            fot.flush();
            ops.close();
            fot.close();
            FileUtils.moveFile(path,tmpPath);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ops!=null){
                ops.close();
            }
            if(fot!=null){
                fot.close();
            }
        }
    }
    public void  fileEncrypt3(String path,String tmpPath) throws Exception {
        FileOutputStream fot=null;
        OutputStreamWriter ops=null;
        File file=new File(path);
        try {
            //文件加密
            List<String>list=FileUtils.readFile(path);
            byte []fileByte=new byte[list.size()];
            fot=new FileOutputStream(file);
            ops=new OutputStreamWriter(fot);
            for(int i=0;i<list.size();i++){
                fileByte=list.get(i).getBytes(StandardCharsets.UTF_8);
                byte[] byte2= DESUtil.encrypt(fileByte,"xxxltk00");
                String line=DESUtil.byteToHex(byte2);
                ops.write(line);
            }
            FileUtils.moveFile(path,tmpPath);
            ops.flush();
            fot.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(ops!=null){
                ops.close();
            }
            if(fot!=null){
                fot.close();
            }
        }
    }
 
}