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,20 +26,16 @@
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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
 * @author Lius
 * @date 2024/12/18 14:29
 */
@Service
@Slf4j
public class LogTableServiceImpl extends ServiceImpl<LogTableMapper, LogTable> implements ILogTableService {
    @Value("${fileService.localFilePath}")
@@ -77,6 +70,9 @@
    @Value("${fileService.newFilePath}")
    private String newFilePath;
    @Value("${fileService.failedLocFilePath}")
    private String failedLocFilePath;
    @Resource
    private IMdcPassLogService mdcPassLogService;
@@ -218,16 +214,30 @@
    @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();
                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);