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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package com.yssh.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yssh.dao.DictRecordMapper;
import com.yssh.dao.Yssh3dreliMapper;
import com.yssh.entity.DictRecord;
import com.yssh.entity.Yssh2dreli;
import com.yssh.entity.Yssh3dreli;
import com.yssh.service.Yssh3dreliService;
import com.yssh.utils.StringUtils;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.text.ParseException;
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 Yssh3dreliServiceImpl
 * @Description TODO
 * @date 2022/12/3 21:25
 * @Version 1.0
 */
@Service
public class Yssh3dreliServiceImpl extends ServiceImpl<Yssh3dreliMapper, Yssh3dreli> implements Yssh3dreliService {
    @Autowired
    private Yssh3dreliMapper mapper;
 
    @Autowired
    private DictRecordMapper dictRecordMapper;
 
 
    @Override
    public List<Yssh3dreli> getAll() {
        return mapper.getAll();
    }
 
    @Override
    public List<Yssh3dreli> query(String name) {
        return mapper.query(name);
    }
 
 
    /**
     * 创建3维热力表接口
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int create3dreliTable() {
        String tablePrefix = "yssh_3dreli_";
//        Date date = new Date();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
//        String localDday = dateFormat.format(date);
//        System.out.println("localDday = " + localDday);
//        String tableName = tablePrefix+localDday;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar calendar = Calendar.getInstance();
        //明天
        calendar.add(Calendar.DATE, +1);
        Date start = calendar.getTime();
        String tomorrow = dateFormat.format(start);
        String tableName = tablePrefix + tomorrow;
        System.out.println("tableName = " + tableName);
 
        //把新增的表增加到字典表中
        DictRecord dictRecord = new DictRecord();
        dictRecord.setTableName(tableName);
        dictRecord.setResOne("3");
        dictRecord.setResTwo(tomorrow);
        dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss"));
        dictRecordMapper.insertDictRecord(dictRecord);
        return mapper.create3dreliTable(tableName);
    }
 
 
    /**
     * 创建当天3维热力表接口
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int preRreate3dreliTable() {
        String tablePrefix = "yssh_3dreli_";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String localDday = dateFormat.format(date);
        System.out.println("localDday = " + localDday);
        String tableName = tablePrefix + localDday;
 
        //把新增的表增加到字典表中
        DictRecord dictRecord = new DictRecord();
        dictRecord.setTableName(tableName);
        dictRecord.setResOne("3");
        dictRecord.setResTwo(localDday);
        dictRecord.setCreateTime(dateFormat("yyyy-MM-dd HH:mm:ss"));
        dictRecordMapper.insertDictRecord(dictRecord);
        return mapper.create3dreliTable(tableName);
    }
 
    /**
     * 删除3维热力表接口
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int drop3dreliTable() {
        String tablePrefix = "yssh_3dreli_";
        //定义日期格式
        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 mapper.drop3dreliTable(tableName);
    }
 
 
    /**
     * 批量新增3维热力记录
     *
     * @param yssh3dreli 3维热力记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int batchInsert3dreliData(List<Yssh3dreli> yssh3dreli) {
        /*Yssh3dreli yssh3dreli1 =new Yssh3dreli();
        for (Yssh3dreli yssh3dre2: yssh3dreli) {
            System.out.println("yssh3dre2 = " + yssh3dre2.getName());
            yssh3dreli1.setName(yssh3dre2.getName());
            yssh3dreli1.setLat(yssh3dre2.getLat());
            yssh3dreli1.setLon(yssh3dre2.getLon());
            yssh3dreli1.setHeight0(yssh3dre2.getHeight0());
            yssh3dreli1.setHeight10(yssh3dre2.getHeight10());
            yssh3dreli1.setHeight20(yssh3dre2.getHeight20());
            yssh3dreli1.setHeight30(yssh3dre2.getHeight30());
            yssh3dreli1.setHeight40(yssh3dre2.getHeight40());
            yssh3dreli1.setHeight50(yssh3dre2.getHeight50());
            yssh3dreli1.setHeight60(yssh3dre2.getHeight60());
            yssh3dreli1.setHeight70(yssh3dre2.getHeight70());
            yssh3dreli1.setHeight80(yssh3dre2.getHeight80());
            yssh3dreli1.setHeight90(yssh3dre2.getHeight90());
            yssh3dreli1.setTime(yssh3dre2.getTime());
        }*/
        String tablePrefix = "yssh_3dreli_";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String localDday = dateFormat.format(date);
        String tableName = tablePrefix + localDday;
        return mapper.batchInsert3dreliData(tableName, yssh3dreli);
    }
 
    /**
     * 批量新增2维热力记录
     *
     * @param yssh2drelis 2维热力记录
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Synchronized
    @Override
    public int batchInsert2dreliData(List<Yssh2dreli> yssh2drelis) {
        String tablePrefix = "yssh_2dreli_";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String localDday = dateFormat.format(date);
        String tableName = tablePrefix + localDday;
        return mapper.batchInsert2dreliData(tableName, yssh2drelis);
    }
 
 
    /**
     * 查询接口
     */
    @Override
    public List<Yssh3dreli> query3data(Yssh3dreli yssh3dreli, String tableName) {
        List<Yssh3dreli> resultList = mapper.query3data(yssh3dreli.getTime(), tableName);
        return resultList;
    }
 
    /**
     * 查询3维热力表接口
     * 1、根据time字段传入的开始时间和结束时间
     * 2、根据时间查询记录表,获取数据表名
     * 3、查询出结果、合并结果并返回结果
     * ①time为同一天的不同时间
     * ②time为不同一天的范围时间
     */
    @Override
    public List<Yssh3dreli> thrDDataQuery(String startTime, String endOfTime) {
        String tablePrefix = "yssh_3dreli_";
        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<Yssh3dreli> resultList = new ArrayList<>();
        //根据查询时间判断查询的数据表
        //①同一个表范围查询
        if (strTime.equals(endTime)) {
            tableName = tablePrefix + strTime;
            System.out.println("tableName = " + tableName);
            resultList = mapper.thrDDataQuery1(tableName, startTime, endOfTime);
        } else {
            //②不在同一个表查询
            List<Yssh3dreli> list1 = new ArrayList<>();
            tableName = tablePrefix + strTime;
            list1 = mapper.thrDDataQuery2(tableName, startTime);
 
            List<Yssh3dreli> list2 = new ArrayList<>();
            tableName = tablePrefix + endTime;
            list2 = mapper.thrDDataQuery3(tableName, endOfTime);
            //合并结果
            resultList.addAll(list1);
            resultList.addAll(list2);
        }
        return resultList;
    }
 
 
    /**
     * 查询3维热力表接口
     * 1、根据time字段传入的开始时间和结束时间
     * 2、根据时间查询记录表,获取数据表名
     * 3、查询出结果、合并结果并返回结果
     * ①time为同一天的不同时间
     * ②time为不同一天的范围时间
     */
    @Override
    public List<Yssh3dreli> qubyt(String name, String startTime, String endOfTime) throws ParseException {
        String tablePrefix = "yssh_3dreli_";
        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<Yssh3dreli> resultList = new ArrayList<>();
        //根据查询时间判断查询的数据表
        //①同一个表范围查询
        if (strTime.equals(endTime)) {
            tableName = tablePrefix + strTime;
            System.out.println("tableName = " + tableName);
            resultList = mapper.thrDDataQuery11(name, tableName, startTime, endOfTime);
            return resultList;
        } else {
            //②不在同一个表查询 创建一个返回的list
            List<Yssh3dreli> lists = new ArrayList<>();
            //去字典记录表中查询出需要查询的表名
            List<DictRecord> resDictRecords = dictRecordMapper.selectDictRecordList3dByTime(strTime, endTime);
            for (DictRecord dicts : resDictRecords) {
                System.out.println("dicts = " + dicts.getTableName());
                lists.addAll(mapper.thrDDataQuery01(name, dicts.getTableName()));//将返回的结果添加到list中
            }
            return lists;
        }
 
    }
 
    /**
     * 将字符串格式转日期,如:yyyy-MM-dd ||自定义格式
     *
     * @param date       日期字符串
     * @param dateFormat 设置将字符串格式转日期格式,这个与date的格式必须一致
     * @param tarFormat  设置目标格式
     * @return 返回格式化的日期,默认格式:yyyy-MM-dd
     * @throws ParseException 分析时意外地出现了错误异常
     */
    public String strToDateFormat(String date, String dateFormat, String tarFormat) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(StringUtils.isBlank(dateFormat) ? "yyyyMMdd" : dateFormat);
        formatter.setLenient(false);
        Date newDate = formatter.parse(date);
        formatter = new SimpleDateFormat(StringUtils.isBlank(tarFormat) ? "yyyy-MM-dd" : tarFormat);
        return formatter.format(newDate);
    }
 
    /**
     * 获取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));
    }
 
    /**
     * JAVA计算两个日期相差多少天
     *
     * @param date1str
     * @param date2str
     * @return
     */
    public int daysBetween(String date1str, String date2str) throws ParseException {
        int count = 0;
        if ("".equals(date1str) || date1str == null || "".equals(date2str) || date2str == null) {
            return count;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Date date1 = format.parse(date1str);
        Date date2 = format.parse(date2str);
        count = Math.abs((int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24)));
        return count;
        /*System.out.println("当前时间:" + dateFormat("yyyy-MM-dd HH:mm:ss"));
            String date1 = strToDateFormat(startTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss");
            String date2 = strToDateFormat(endOfTime, "yyyyMMddHH", "yyyy-MM-dd HH:mm:ss");
            //String转Date
            Date date11 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date1);
            Date date22 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date2);
            // 获取相差的天数
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date11);
            long timeInMillis1 = calendar.getTimeInMillis();
            calendar.setTime(date22);
            long timeInMillis2 = calendar.getTimeInMillis();
            long betweenDays = ((timeInMillis2 - timeInMillis1) / (1000L * 3600L * 24L)) + 2;
            System.out.println("betweenDays = " + betweenDays);*/
    }
 
 
}