From e756af0f5bfd1addbd5d5c145441fb34aad91a28 Mon Sep 17 00:00:00 2001 From: lyh <925863403@qq.com> Date: 星期三, 25 六月 2025 13:55:58 +0800 Subject: [PATCH] 同步工控网 --- lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/LogTableServiceImpl.java | 96 ++++++++++++++++++++++++++++-------------------- 1 files changed, 56 insertions(+), 40 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 8f5d25e..b3cd919 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 @@ -1,22 +1,25 @@ package org.jeecg.modules.mdc.service.impl; import cn.hutool.core.io.FileUtil; -import cn.hutool.core.io.file.FileReader; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import lombok.extern.slf4j.Slf4j; import org.jeecg.common.exception.JeecgBootException; import org.jeecg.modules.mdc.entity.LogTable; import org.jeecg.modules.mdc.mapper.LogTableMapper; import org.jeecg.modules.mdc.service.ILogTableService; -import org.jeecg.modules.mdc.util.SqlExecutor; +import org.jeecg.modules.mdc.util.DateUtils; +import org.jeecg.modules.system.entity.MdcPassLog; +import org.jeecg.modules.system.service.IMdcPassLogService; +import org.jeecg.modules.system.util.FileUtils; 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.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * @author Lius @@ -29,52 +32,65 @@ @Value("${fileService.localFilePath}") private String localFilePath; - @Value("${fileService.newFilePath}") - private String newFilePath; - - @Value("${fileService.failedLocFilePath}") - private String failedLocFilePath; - @Resource - private SqlExecutor sqlExecutor; + private IMdcPassLogService mdcPassLogService; + + private static final String TYPE = "18"; /** - * 瀵煎叆鍚屾鏁版嵁sql鎵ц鍒版暟鎹簱 + * 瀵煎嚭鍚屾鏁版嵁琛╯ql鍒皒ml鏂囦欢 */ @Override @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); - List<String> sqlList = fileReader.readLines(); - log.info("鎴愬姛璇诲彇鍒皗}鏉ql,鎵ц鎿嶄綔", sqlList.size()); - for (String sql : sqlList) { - try { - sqlExecutor.execute(sql); - } catch (Exception e) { - failedSqlList.add(sql); - } - } - if (!failedSqlList.isEmpty()) { - try { - FileUtil.appendLines(failedSqlList, failedLocFilePath, "UTF-8"); - } catch (Exception e) { - throw new JeecgBootException("鏁版嵁鍐欏叆鏂囦欢澶辫触锛�"); - } - } + public void exportDataToXml() { + // step.1 鏌ヨ鏈�鏂版暟鎹� 鏃堕棿姝e簭鎺掑垪 + List<LogTable> logTables = this.baseMapper.selectList(new LambdaQueryWrapper<LogTable>().orderByAsc(LogTable::getCreateTime)); + if (logTables != null && !logTables.isEmpty()) { - if (Integer.parseInt(file.getName().substring(file.getName().length() - 10, file.getName().length() - 4)) == 1) { - // 鍒犻櫎鍘嗗彶鏂囦欢 - FileUtil.clean(newFilePath); - } + // step.2 澶勭悊鏁版嵁 + List<String> sqlList = logTables.stream().map(LogTable::getSqlLog).collect(Collectors.toList()); - // 澶囦唤 - FileUtil.move(new File(loFilePath), new File(newFilePath + file.getName()), true); + // 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"; + String listSql = String.join("\n", sqlList); + try { + FileUtils.fileWriterSql(locFilePath, listSql); + } catch (Exception e) { + throw new JeecgBootException("鏁版嵁鍐欏叆鏂囦欢澶辫触锛�"); + } + + File[] files = FileUtil.ls(localFilePath); + for (File file : files) { + if (file.isFile()) { + //椤哄簭鍙� + 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); + // 鎻掑叆浼犺緭鏃ュ織 + MdcPassLog mdcPassLog = new MdcPassLog(); + mdcPassLog.setPassLogFileName(file.getAbsolutePath()); + mdcPassLog.setPassName(fileName); + mdcPassLog.setDayTime(dayTime); + mdcPassLog.setSequenceNumber(sequenceNum); + mdcPassLog.setSequenceOrder(sequenceOrder); + mdcPassLogService.save(mdcPassLog); + } + } + // step.5 鍒犻櫎宸茬粡鍐欏叆鏂囦欢骞跺彂閫佽嚦缃戦椄鐨勬暟鎹� + // step.5.1 鍒犻櫎鏁版嵁搴撴暟鎹� + this.baseMapper.delete(new LambdaQueryWrapper<LogTable>().le(LogTable::getCreateTime, logTables.get(logTables.size() - 1).getCreateTime())); + } } + } -- Gitblit v1.9.3