package org.jeecg.modules.mdc.service.impl;
|
|
import cn.hutool.core.io.FileUtil;
|
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.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.List;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author Lius
|
* @date 2024/12/18 14:29
|
*/
|
@Service
|
@Slf4j
|
public class LogTableServiceImpl extends ServiceImpl<LogTableMapper, LogTable> implements ILogTableService {
|
|
@Value("${fileService.localFilePath}")
|
private String localFilePath;
|
|
@Resource
|
private IMdcPassLogService mdcPassLogService;
|
|
private static final String TYPE = "18";
|
|
/**
|
* 导出同步数据表sql到xml文件
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void exportDataToXml() {
|
// step.1 查询最新数据 时间正序排列
|
List<LogTable> logTables = this.baseMapper.selectList(new LambdaQueryWrapper<LogTable>().orderByAsc(LogTable::getCreateTime));
|
if (logTables != null && !logTables.isEmpty()) {
|
|
// step.2 处理数据
|
List<String> sqlList = logTables.stream().map(LogTable::getSqlLog).collect(Collectors.toList());
|
|
// 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()));
|
|
}
|
}
|
|
}
|