package org.jeecg.modules.mdc.service.impl;
|
|
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.file.FileReader;
|
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.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;
|
|
/**
|
* @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;
|
|
@Value("${fileService.newFilePath}")
|
private String newFilePath;
|
|
@Value("${fileService.failedLocFilePath}")
|
private String failedLocFilePath;
|
|
@Resource
|
private SqlExecutor sqlExecutor;
|
|
/**
|
* 导入同步数据sql执行到数据库
|
*/
|
@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("数据写入文件失败!");
|
}
|
}
|
|
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);
|
}
|
}
|
}
|
}
|