| | |
| | | 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 |
| | |
| | | @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执行到数据库 |
| | | * 导出同步数据表sql到xml文件 |
| | | */ |
| | | @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("成功读取到{}条sql,执行操作", 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 查询最新数据 时间正序排列 |
| | | 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())); |
| | | |
| | | } |
| | | } |
| | | |
| | | } |