Lius
2025-04-28 572466e8ebaa67f1809a97ecf912d30e8802fd98
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java
@@ -2,26 +2,23 @@
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 lombok.extern.slf4j.Slf4j;
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.SqlExecutor;
import org.jeecg.modules.mdc.util.SyslogClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.jeecg.modules.system.entity.MdcPassLog;
import org.jeecg.modules.system.service.IMdcPassLogService;
import org.jeecg.modules.system.util.FileUtils;
import org.jeecg.modules.system.vo.SysLogTypeObjectDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -29,8 +26,7 @@
import javax.annotation.Resource;
import java.io.File;
import java.net.InetAddress;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@@ -39,6 +35,7 @@
 * @date 2024/12/18 14:29
 */
@Service
@Slf4j
public class LogTableServiceImpl extends ServiceImpl<LogTableMapper, LogTable> implements ILogTableService {
    @Value("${fileService.localFilePath}")
@@ -74,10 +71,13 @@
    @Value("${fileService.newFilePath}")
    private String newFilePath;
    @Value("${fileService.failedLocFilePath}")
    private String failedLocFilePath;
    @Resource
    private IMdcPassLogService mdcPassLogService;
    private static final String TYPE = "01";
    private static final String TYPE = "18";
    @Resource
    private SqlExecutor sqlExecutor;
@@ -107,16 +107,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 +173,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 {
@@ -201,18 +214,37 @@
    @Transactional(rollbackFor = Exception.class)
    public void importXmlToData() {
        File[] files = FileUtil.ls(localFilePath);
        List<String> failedSqlList = new ArrayList<>(); // 用于存储执行失败的 SQL
        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 {
                    sqlExecutor.execute(sqlList);
                } catch (Exception e) {
                    throw new JeecgBootException("执行sql失败!");
                List<String> sqlList = fileReader.readLines();
                log.info("成功读取到{}条sql,执行操作", sqlList.size());
                for (String sql : sqlList) {
                    try {
                        sqlExecutor.execute(sql);
                    } catch (Exception e) {
                        failedSqlList.add(sql);
//                        throw new JeecgBootException("执行sql失败!");
                    }
                }
                if (!failedSqlList.isEmpty()) {
                    try {
                        FileUtil.appendLines(failedSqlList, failedLocFilePath, "UTF-8");
                    } catch (Exception e) {
                        throw new JeecgBootException("数据写入文件失败!");
                    }
                }
//                String sqlList = fileReader.readString();
                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);
            }
        }
    }