Lius
2025-06-24 92b8a49f972e6a6b887745f4d2cc2c0cac704a96
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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);
            }
        }
    }
}