From 572466e8ebaa67f1809a97ecf912d30e8802fd98 Mon Sep 17 00:00:00 2001 From: Lius <Lius2225@163.com> Date: 星期一, 28 四月 2025 14:18:39 +0800 Subject: [PATCH] update --- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java | 84 +++++++++++++++++++++++++++++------------- 1 files changed, 58 insertions(+), 26 deletions(-) diff --git a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java b/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java index c9c68b1..884c72c 100644 --- a/lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java +++ b/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("鎴愬姛璇诲彇鍒皗}鏉ql,鎵ц鎿嶄綔", 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); } } } -- Gitblit v1.9.3