Lius
2025-02-18 3423bb9ee5b25d270a00763b69ed73970d790f63
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
package org.jeecg.modules.mdc.service.impl;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.net.NetUtil;
import cn.hutool.crypto.SmUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhyd.oauth.log.Log;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.mdc.util.SqlExecutor;
import org.jeecg.modules.system.vo.SysLogTypeObjectDto;
import org.jeecg.modules.mdc.entity.LogTable;
import org.jeecg.modules.system.entity.MdcPassLog;
import org.jeecg.modules.mdc.mapper.LogTableMapper;
import org.jeecg.modules.mdc.service.ILogTableService;
import org.jeecg.modules.system.service.IMdcPassLogService;
import org.jeecg.modules.mdc.util.DateUtils;
import org.jeecg.modules.mdc.util.FileClient;
import org.jeecg.modules.system.util.FileUtils;
import org.jeecg.modules.mdc.util.SyslogClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.File;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
/**
 * @author Lius
 * @date 2024/12/18 14:29
 */
@Service
public class LogTableServiceImpl extends ServiceImpl<LogTableMapper, LogTable> implements ILogTableService {
 
    @Value("${fileService.localFilePath}")
    private String localFilePath;
 
    @Value("${fileService.servicePath}")
    private String servicePath;
 
    @Value("${fileService.host}")
    private String host;
 
    @Value("${fileService.port}")
    private String port;
 
    @Value("${fileService.username}")
    private String username;
 
    @Value("${fileService.pwd}")
    private String pwd;
 
    @Value("${fileService.addressToken}")
    private String addressToken;
 
    @Value("${fileService.addressUploadFile}")
    private String addressUploadFile;
 
    @Value("${fileService.logIp}")
    private String logIp;
 
    @Value("${fileService.logPort}")
    private String logPort;
 
    @Value("${fileService.newFilePath}")
    private String newFilePath;
 
    @Resource
    private IMdcPassLogService mdcPassLogService;
 
    private static final String TYPE = "18";
 
    @Resource
    private SqlExecutor sqlExecutor;
 
 
    /**
     * 导出同步数据表sql到xml文件
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exportDataToXml() {
        // step.1 查询最新数据 时间正序排列
        List<LogTable> logTables = this.baseMapper.selectList(new LambdaQueryWrapper<LogTable>().orderByAsc(LogTable::getCreateTime));
        if (logTables != null && !logTables.isEmpty()) {
 
            // step.2 处理数据
            List<String> sqlList = logTables.stream().map(LogTable::getSqlLog).collect(Collectors.toList());
 
            // step.3 写入文件
            // step.3.1 文件命名
            String today = DateUtils.format(DateUtils.getNow(), DateUtils.STRDATE);
            MdcPassLog mdcPassLogLast = mdcPassLogService.selectTodayLast(today);
            int sequenceNumber = 1;
            if (mdcPassLogLast != null) {
                sequenceNumber = mdcPassLogLast.getSequenceNumber() + 1;
            }
 
            // 文件路径
            String locFilePath = localFilePath + TYPE + today + String.format("%06d", sequenceNumber) + ".xml";
//            FileWriter writer = new FileWriter(locFilePath);
//            writer.writeLines(sqlList);
            String listSql = String.join("\n", sqlList);
            try {
                FileUtils.fileWriterSql(locFilePath, listSql);
            } catch (Exception e) {
                throw new JeecgBootException("数据写入文件失败!");
            }
 
            // step.4 推送网闸
            // step.4.1 测试服务器连通性
          /*  boolean ping = NetUtil.ping(host + ":" + port);
            if (!ping) {
                FileUtil.del(locFilePath);
                throw new JeecgBootException("服务器连接: " + host + ":" + port + " 异常!");
            }*/
 
            // step.4.2 获取token
            String token = "";
            try {
                token = FileClient.getToken(host, port, username, pwd, addressToken);
            } catch (Throwable e) {
                throw new JeecgBootException("获取token失败!");
            }
 
            // step 4.3 调用文件上传接口
            if (StringUtils.isNotBlank(token)) {
                try {
                    File[] files = FileUtil.ls(localFilePath);
                    for (File file : files) {
                        if (file.isFile()) {
                            SysLogTypeObjectDto objectName = new SysLogTypeObjectDto();
 
                            objectName.setDateTime(DateUtils.format(DateUtils.getNow(), DateUtils.STR_DATE_TIME_SMALL));
                            objectName.setFileName(file.getName());
                            objectName.setFileSize(FileUtils.changeFileFormatKb(String.valueOf(new File(localFilePath).length())));
                            objectName.setSourceAddress(localFilePath);
                            String sm3 = SmUtil.sm3(file);
                            objectName.setAbstract1(sm3);
                            //顺序号
                            String fileName = file.getName();
                            String sequenceOrder = fileName.substring(fileName.length() - 10, fileName.length() - 4);
                            String dayTime = fileName.substring(fileName.length() - 18, fileName.length() - 10);
                            int sequenceNum = Integer.parseInt(sequenceOrder);
                            objectName.setFileNum(Integer.toString(sequenceNum));
                            // 插入传输日志
                            MdcPassLog mdcPassLog = new MdcPassLog();
                            mdcPassLog.setPassLogFileName(file.getAbsolutePath());
                            mdcPassLog.setPassName(fileName);
                            mdcPassLog.setDayTime(dayTime);
                            mdcPassLog.setSequenceNumber(sequenceNum);
                            mdcPassLog.setSequenceOrder(sequenceOrder);
                            mdcPassLogService.save(mdcPassLog);
 
                            String ip = InetAddress.getLocalHost().getHostAddress();
                            if (StringUtils.isBlank(ip)) {
                                objectName.setSourceAddress("10.118.10.62");
                                objectName.setAddress("10.118.10.62");
                            } else {
                                objectName.setSourceAddress(ip);
                                objectName.setAddress(ip);
                            }
 
                            objectName.setDestination(host);
                            objectName.setResult("失败");
                            // 本地文件路径
                            String loFilePath = localFilePath + file.getName();
                            // 目标文件路径
                            String servicePathName = servicePath + file.getName();
                            String result = FileClient.uploadFile(host, port, token, file.getName(), servicePathName, loFilePath, addressUploadFile);
                            if ("成功".equals(result)) {
                                if (sequenceNumber == 1) {
                                    // 删除昨日备份文件夹中历史文件
                                    FileUtil.clean(newFilePath);
                                }
                                // 成功之后将文件备份走
                                FileUtil.move(new File(locFilePath), new File(newFilePath + fileName), true);
//                                FileUtil.copy(locFilePath, newFilePath + fileName, true);
//                                FileUtil.del(loFilePath);
                                objectName.setResult("成功");
                                SyslogClient.sendClient(logIp, Integer.valueOf(logPort), objectName.toString());
                            } else {
                                objectName.setResult(result);
                                objectName.setTypes("error");
                                SyslogClient.sendClient(logIp, Integer.valueOf(logPort), objectName.toString());
                            }
                        }
                    }
                } catch (Throwable e) {
                    throw new JeecgBootException("调用文件上传接口失败!");
                }
            }
 
            // step.5 删除已经写入文件并发送至网闸的数据
            // step.5.1 删除数据库数据
            this.baseMapper.delete(new LambdaQueryWrapper<LogTable>().le(LogTable::getCreateTime, logTables.get(logTables.size() - 1).getCreateTime()));
 
        }
    }
 
    /**
     * 导入同步数据sql执行到数据库
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importXmlToData() {
        File[] files = FileUtil.ls(localFilePath);
        for (File file : files) {
            if (file.isFile()) {
                String loFilePath = localFilePath + file.getName();
                FileReader fileReader = new FileReader(loFilePath);
                String sqlList = fileReader.readString();
                try {
                    sqlExecutor.execute(sqlList);
                } catch (Exception e) {
                    throw new JeecgBootException("执行sql失败!");
                }
                if (Integer.parseInt(file.getName().substring(file.getName().length() - 10, file.getName().length() - 4)) == 1) {
                    // 删除历史文件
                    FileUtil.clean(newFilePath);
                }
 
                // 备份
                FileUtil.move(new File(loFilePath), new File(newFilePath + file.getName()), true);
            }
        }
    }
}