xing
2023-02-23 c75e6f4696634d626c5569794c6349593853223a
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
208
209
210
package com.yssh.service.impl;
 
import com.yssh.dao.DictRecordMapper;
import com.yssh.dao.Yssh2dreliMapper;
import com.yssh.entity.DictRecord;
import com.yssh.entity.Yssh2dreli;
import com.yssh.service.Yssh2dreliService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * @author lishijia
 * @ClassName Yssh2dreliServiceImpl
 * @Description TODO
 * @date 2022/12/3 21:25
 * @Version 1.0
 */
@Service
public class Yssh2dreliServiceImpl implements Yssh2dreliService {
    @Autowired
    private Yssh2dreliMapper yssh2dreliMapper;
 
    @Autowired
    private DictRecordMapper dictRecordMapper;
 
    @Override
    public List<Yssh2dreli> getAll() {
        return yssh2dreliMapper.getAll();
    }
 
    /**
     * 创建2维热力表接口
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int create2dreliTable() {
        String tablePrefix = "yssh_2dreli_";
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar1 = Calendar.getInstance();
        //明天
        calendar1.add(Calendar.DATE, +1);
        Date start = calendar1.getTime();
        String tomorrow = dateFormat.format(start);
        String tableName = tablePrefix + tomorrow;
        System.out.println("tableName = " + tableName);
 
        //把新增的表增加到字典表中
        DictRecord dictRecord = new DictRecord();
        dictRecord.setTableName(tableName);
        dictRecord.setResOne("2");
        dictRecord.setResTwo(tomorrow);
        dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss"));
        dictRecordMapper.insertDictRecord(dictRecord);
        return yssh2dreliMapper.create2dreliTable(tableName);
    }
 
    /**
     * 创建当天2维热力表接口
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int preReate2dreliTable() {
        String tablePrefix = "yssh_2dreli_";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String localDday = dateFormat.format(date);
        String tableName = tablePrefix + localDday;
        int total = yssh2dreliMapper.create2dreliTable(tableName);
        //把新增的表增加到字典表中
        DictRecord dictRecord = new DictRecord();
        dictRecord.setTableName(tableName);
        dictRecord.setResOne("2");
        dictRecord.setResTwo(localDday);
        dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss"));
        dictRecordMapper.insertDictRecord(dictRecord);
        return total;
    }
 
    /**
     * 获取LocalDateTime的指定日期格式
     *
     * @param ofPattern 设置时间格式:yyyy-MM-dd HH:mm:ss
     * @return 2023-02-09 09:51:43
     */
    public String dateFormat(String ofPattern) {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(ofPattern));
    }
 
 
    /**
     * 删除2维热力表接口
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int drop2dreliTable() {
        String tablePrefix = "yssh_2dreli_";
        //定义日期格式
        SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        //前一年
        calendar.add(Calendar.YEAR, -1);
        Date start = calendar.getTime();
        String yearBefore = format2.format(start);
        String tableName = tablePrefix + yearBefore;
        return yssh2dreliMapper.drop2dreliTable(tableName);
    }
 
 
    /**
     * 查询接口
     */
    @Override
    public List<Yssh2dreli> query2data(Yssh2dreli yssh2dreli, String tableName) {
        List<Yssh2dreli> resultList = yssh2dreliMapper.query2data(yssh2dreli.getTime(), tableName);
        return resultList;
    }
 
    /**
     * 查询2维热力表接口
     * 1、根据time字段传入的开始时间和结束时间
     * 2、根据时间查询记录表,获取数据表名
     * 3、查询出结果、合并结果并返回结果
     * ①time为同一天的不同时间
     * ②time为不同一天的范围时间
     */
    @Override
    public List<Yssh2dreli> twoDDataQuery(String startTime, String endOfTime) {
        String tablePrefix = "yssh_2dreli_";
        String tableName = "";
        System.out.println("startTime = " + startTime);
        System.out.println("endOfTime = " + endOfTime);
        String strTime = startTime.substring(0, 8);
        String endTime = endOfTime.substring(0, 8);
        List<Yssh2dreli> resultList = new ArrayList<>();
        //根据查询时间判断查询的数据表
        //①同一个表范围查询
        if (strTime.equals(endTime)) {
            tableName = tablePrefix + strTime;
            System.out.println("tableName = " + tableName);
            resultList = yssh2dreliMapper.twoDDataQuery1(tableName, startTime, endOfTime);
        } else {
            //②不在同一个表查询
            List<Yssh2dreli> list1 = new ArrayList<>();
            tableName = tablePrefix + strTime;
            list1 = yssh2dreliMapper.twoDDataQuery2(tableName, startTime);
 
            List<Yssh2dreli> list2 = new ArrayList<>();
            tableName = tablePrefix + endTime;
            list2 = yssh2dreliMapper.twoDDataQuery3(tableName, endOfTime);
            //合并结果
            resultList.addAll(list1);
            resultList.addAll(list2);
        }
        return resultList;
    }
 
 
    /**
     * 查询2维热力表接口
     * 1、根据time字段传入的开始时间和结束时间
     * 2、根据时间查询记录表,获取数据表名
     * 3、查询出结果、合并结果并返回结果
     * ①time为同一天的不同时间
     * ②time为不同一天的范围时间
     */
    @Override
    public List<Yssh2dreli> qubyt(String startTime, String endOfTime) {
        String tablePrefix = "yssh_2dreli_";
        String tableName = "";
        System.out.println("startTime = " + startTime);
        System.out.println("endOfTime = " + endOfTime);
        String strTime = startTime.substring(0, 8);
        String endTime = endOfTime.substring(0, 8);
        List<Yssh2dreli> resultList = new ArrayList<>();
        //根据查询时间判断查询的数据表
        //①同一个表范围查询
        if (strTime.equals(endTime)) {
            tableName = tablePrefix + strTime;
            System.out.println("tableName = " + tableName);
            resultList = yssh2dreliMapper.twoDDataQuery1(tableName, startTime, endOfTime);
            return resultList;
        } else {
            //②不在同一个表查询 创建一个返回的list
            List<Yssh2dreli> lists = new ArrayList<>();
            //去字典记录表中查询出需要查询的表名
            List<DictRecord> resDictRecords = dictRecordMapper.selectDictRecordList2dByTime(strTime, endTime);
            for (DictRecord dicts : resDictRecords) {
                System.out.println("dicts = " + dicts.getTableName());
                lists.addAll(yssh2dreliMapper.twoDDataQuery(dicts.getTableName()));//将返回的结果添加到list中
            }
            return lists;
        }
 
    }
 
 
}