Lius
2025-02-18 3423bb9ee5b25d270a00763b69ed73970d790f63
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java
@@ -29,10 +29,14 @@
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
@@ -77,7 +81,7 @@
    @Resource
    private IMdcPassLogService mdcPassLogService;
    private static final String TYPE = "01";
    private static final String TYPE = "18";
    @Resource
    private SqlExecutor sqlExecutor;
@@ -107,16 +111,22 @@
            // 文件路径
            String locFilePath = localFilePath + TYPE + today + String.format("%06d", sequenceNumber) + ".xml";
            FileWriter writer = new FileWriter(locFilePath);
            writer.writeLines(sqlList);
//            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);
          /*  boolean ping = NetUtil.ping(host + ":" + port);
            if (!ping) {
                FileUtil.del(locFilePath);
                throw new JeecgBootException("服务器连接: " + host + ":" + port + " 异常!");
            }
            }*/
            // step.4.2 获取token
            String token = "";
@@ -167,12 +177,19 @@
                            objectName.setDestination(host);
                            objectName.setResult("失败");
                            // 本地文件路径
                            String loFilePath = localFilePath + "\\" + file.getName();
                            String loFilePath = localFilePath + file.getName();
                            // 目标文件路径
                            String servicePathName = servicePath + "\\" + file.getName();
                            String servicePathName = servicePath + file.getName();
                            String result = FileClient.uploadFile(host, port, token, file.getName(), servicePathName, loFilePath, addressUploadFile);
                            if ("成功".equals(result)) {
                                FileUtil.del(loFilePath);
                                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 {
@@ -203,7 +220,7 @@
        File[] files = FileUtil.ls(localFilePath);
        for (File file : files) {
            if (file.isFile()) {
                String loFilePath = localFilePath + "\\" + file.getName();
                String loFilePath = localFilePath + file.getName();
                FileReader fileReader = new FileReader(loFilePath);
                String sqlList = fileReader.readString();
                try {
@@ -211,8 +228,13 @@
                } 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);
                FileUtil.move(new File(loFilePath), new File(newFilePath + file.getName()), true);
            }
        }
    }