lyh
2025-06-25 e756af0f5bfd1addbd5d5c145441fb34aad91a28
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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()));
 
        }
    }
 
}