jinlin
2024-12-02 18f682f736914e427070b9bb769df538ad9f6d1c
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
package com.zt.life.modules.mainPart.sys.service;
 
import com.zt.common.exception.RenException;
import com.zt.common.service.BaseService;
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 com.zt.life.modules.mainPart.taskReliability.service.SimulatAssessService;
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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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;
 
    @Autowired
    private SimulatAssessService simulatAssessService;
 
    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");
            simulatAssessService.writeToTxt(request, response, formattedResult, "日志文件");
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}