燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2023-08-10 ba0944e4d2877bf9488734945004a3776cfc3e76
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.yssh.service;
 
import com.yssh.entity.MonitorPointPosition;
import com.yssh.entity.SuYuan700;
import com.yssh.entity.xls.DayExcel;
import com.yssh.entity.xls.MonthExcel;
import com.yssh.entity.xls.WeekExcel;
import com.yssh.mapper.XlsReportMapper;
import com.yssh.utils.CalculateUtils;
import com.yssh.utils.DateUtils;
import com.yssh.utils.ExcelUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.*;
 
/**
 * Excel导出服务类
 * @author WWW
 * @author 2023-08-05
 */
@Service
public class XlsReportService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Resource
    XlsReportMapper mapper;
 
    @Resource
    CommonService commonService;
 
    /**
     * 报告路径
     */
    @Value("${report.path}")
    private String reportPath;
 
    /**
     * 预警值
     */
    @Value("${report.yjz}")
    private double yjz;
 
    /**
     * 报警值
     */
    @Value("${report.bjz}")
    private double bjz;
 
    /**
     * Excel是否已存在
     */
    private boolean xlsExists(String type, String name) {
        int rows = mapper.reportExists(type, name);
 
        return rows > 0;
    }
 
    /**
     * 获取导出路径
     */
    private String getExpPath(String type) {
        String path = reportPath + File.separator + type;
 
        File f = new File(path);
        if (!f.exists() || !f.isDirectory()) {
            f.mkdirs();
        }
 
        return path;
    }
 
    /**
     * 获取Excel模板
     */
    private String getXslTemplate(String type) throws IOException {
        ClassPathResource resource = new ClassPathResource(String.format("templates/%s.xlsx", type));
 
        return resource.exists() ? resource.getFile().getPath() : null;
    }
 
    /**
     * 创建Excel
     */
    private <T> void createExcel(String source, String target, List<T> list) {
        Map<String, List<T>> map = new HashMap<>();
        map.put("data", list);
 
        ExcelUtils.writeToTemplate(source, target, map);
    }
 
    /**
     * 创建Excel
     */
    private <T> String createExcel(String type, Date date, List<T> list) throws Exception {
        String source = getXslTemplate(type);
        String strData = DateUtils.parseDateToStr("month".equals(type) ? DateUtils.YYYYMM : DateUtils.YYYYMMDD, date);
        String target = String.format("%s\\%s.xlsx", getExpPath(type), strData);
 
        createExcel(source, target, list);
 
        return String.format("%s\\%s.xlsx", type, strData);
    }
 
    /**
     * 获取受影响因素及原因
     */
    public String getYs(List<SuYuan700> suList) {
        if (null == suList || 0 == suList.size()) return "";
 
        List<String> list = new ArrayList<>();
        for (SuYuan700 su : suList) {
            List<String> sub = new ArrayList<>();
            if (null != su.getAddr1())
                sub.add(su.getAddr1() + ",概率:" + (su.getOdds1() * 100) + "%,X:" + su.getX1() + ",Y:" + su.getY1());
            if (null != su.getAddr2())
                sub.add(su.getAddr2() + ",概率:" + (su.getOdds2() * 100) + "%,X:" + su.getX2() + ",Y:" + su.getY2());
            if (null != su.getAddr3())
                sub.add(su.getAddr3() + ",概率:" + (su.getOdds3() * 100) + "%,X:" + su.getX3() + ",Y:" + su.getY3());
 
            list.add(String.join(";", sub));
        }
 
        return String.join("\n", list);
    }
 
    /**
     * 创建日报:DayExcel day = new DayExcel("AI-" + (i < 10 ? "0" : "") + i, "lj", "ljtb", "ys", "3m/s", "东南", "°c");
     */
    public void createDayReport(Date date) {
        try {
            Date yesterday = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE);
            String yyyy = DateUtils.parseDateToStr(DateUtils.YYYY, yesterday); // 2023
            String yyyymmdd = DateUtils.parseDateToStr(DateUtils.YYYYMMDD, yesterday); // 20230724
            String yyyy_mm_dd = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, yesterday); // 2023-07-24
            Integer start = Integer.parseInt(yyyy + "010100"); // 2023010100
            Integer end = Integer.parseInt(yyyymmdd + "23"); // 2023072423
            String lastYear = DateUtils.parseDateToStr(DateUtils.YYYY, DateUtils.lastYear(yesterday)); // 2022
 
            List<DayExcel> list = new ArrayList<>();
            for (int i = 1; i < 47; i++) {
                String name = "AI-" + (i < 10 ? "0" : "") + i;
                double rjz = mapper.selectDayAvg(yyyymmdd + "%", name);
                double lj = mapper.selectAccumulate(start, end, name);
                double lastLj = mapper.selectYearAccumulate(lastYear, name);
                double ljtb = CalculateUtils.round2((lj - lastLj) / lastLj * 100);
                MonitorPointPosition point = commonService.select3dCheckPointByName(name);
                String id = point.getId().substring(0, point.getId().lastIndexOf("_") + 1) + "0";
                List<SuYuan700> suList = mapper.selectSuYuanByTime(id, yyyy_mm_dd + " 00:00:00", yyyy_mm_dd + " 23:00:00");
                String ys = getYs(suList);
 
                list.add(new DayExcel("" + rjz, "" + lj, "" + ljtb, ys, "", "", ""));
            }
 
            String filePath = createExcel("day", yesterday, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 创建周报
     */
    public void createWeekReport(Date date) {
        try {
            Date start = DateUtils.getAPeriodOfTime(date, -7, Calendar.DATE);
            Date end = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE);
 
            List<WeekExcel> list = new ArrayList<>();
            for (int i = 1; i < 47; i++) {
                WeekExcel day = new WeekExcel("AI-" + (i < 10 ? "0" : "") + i, "zhb", "ztq", "ztb", "zhb2", "lj", "ljtb", "syn", "ys", "fs", "fx", "°c");
                list.add(day);
            }
 
            String filePath = createExcel("week", end, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 创建月报
     */
    public void createMonthReport(Date date) {
        try {
            Date yesterday = DateUtils.getAPeriodOfTime(date, -1, Calendar.DATE);
            Date start = DateUtils.getMonthStart(yesterday);
            Date end = DateUtils.getMonthEnd(yesterday);
 
            List<MonthExcel> list = new ArrayList<>();
            for (int i = 1; i < 47; i++) {
                MonthExcel day = new MonthExcel("AI-" + (i < 10 ? "0" : "") + i, "yhb", "ytq", "ytb", "yhb2", "lj", "ljtb", "qyn", "ys", "fs", "fx", "°c");
                list.add(day);
            }
 
            String filePath = createExcel("month", end, list);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }
}