jinlin
2026-02-27 884d1457cebbda20ad2539550062c408a58709be
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.zt.life.modules.mainPart.sys.service;
 
import com.zt.common.exception.RenException;
import com.zt.common.service.BaseService;
import com.zt.life.export.service.DownloadService;
import com.zt.life.modules.mainPart.sys.dao.SysMysqlDao;
import com.zt.life.modules.mainPart.sys.model.ColumnsTemp;
import com.zt.life.modules.mainPart.sys.model.SysMysql;
import org.apache.commons.io.input.ReversedLinesFileReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
 
@Service
public class SysMysqlService extends BaseService<SysMysqlDao, SysMysql> {
    @Value("${zt.oss.log-path}")
    private String logPath;
 
    public SysMysql check(String sql) {
        try {
            SysMysql sysMysql = new SysMysql();
            List<Map<String, Object>> data = baseDao.check(sql);
            List<ColumnsTemp> tableColumns = new ArrayList<>();
 
            for (Map<String, Object> map : data) {
                ColumnsTemp obj = null;
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    obj = new ColumnsTemp();
                    obj.setProp(entry.getKey());
                    obj.setLabel(entry.getKey());
                    tableColumns.add(obj);
                }
                break;
            }
            sysMysql.setTableColumns(tableColumns);
            sysMysql.setTableData(data);
            return sysMysql;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RenException(String.valueOf(e));
        }
 
 
    }
 
    public void execute(String sql) {
        try {
            baseDao.execute(sql);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RenException(String.valueOf(e));
        }
    }
 
    public void logDownload(HttpServletRequest request, HttpServletResponse response) {
        int n = 300; // 要读取的行数
        try {
            File file = new File(logPath);
            ReversedLinesFileReader reader = new ReversedLinesFileReader(file);
 
            List<String> lines = new ArrayList<>();
            String line;
 
            while ((line = reader.readLine()) != null) {
                lines.add(line);
                if (lines.size() == n) {
                    break;
                }
            }
            reader.close();
            Collections.reverse(lines);
            String result = lines.stream().collect(Collectors.joining(", "));
            String formattedResult = result.replace(", ", "\n");
            this.writeToTxt(request, response, formattedResult, "日志文件");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void writeToTxt(HttpServletRequest request, HttpServletResponse response, String jsonString, String name) {//设置响应的字符集
        //设置响应内容的类型
        BufferedOutputStream buff = null;
        ServletOutputStream outStr = null;
        try {
            response.setContentType("text/plain;charset=UTF-8");
            String encodedFilename = DownloadService.getNameEncoder(request, name + "xml.txt");
            response.addHeader("Content-Disposition", "attachment;filename=" + encodedFilename);
            outStr = response.getOutputStream();
            buff = new BufferedOutputStream(outStr);
            buff.write(jsonString.getBytes("UTF-8"));
            buff.flush();
            buff.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                buff.close();
                outStr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}