jinpengyong
2023-10-31 69790994b403a61e92a20cef7451b7f087b50ad2
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.config.mybatis.MybatisPlusConfig;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.HistoryFiveMinutelyMapper;
import com.moral.api.mapper.HistoryHourlyMapper;
import com.moral.api.mapper.OrganizationUnitAlarmMapper;
import com.moral.api.mapper.UnitConversionMapper;
import com.moral.api.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.SensorService;
import com.moral.api.service.UnitConversionService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.constant.SeparateTableType;
import com.moral.util.DateUtils;
 
import com.moral.util.MybatisPLUSUtils;
import org.apache.kafka.streams.state.internals.metrics.Sensors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 设备表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-06-28
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Autowired
    OrganizationUnitAlarmMapper organizationUnitAlarmMapper;
 
    @Autowired
    SensorService sensorService;
 
    @Autowired
    UnitConversionService unitConversionService;
 
    @Autowired
    HistoryHourlyMapper historyHourlyMapper;
 
    @Autowired
    HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
 
    @Override
    public List<Device> getDevicesByMonitorPointId(Integer monitorPointId) {
        QueryWrapper<Device> wrapper = new QueryWrapper();
        wrapper.eq("monitor_point_id", monitorPointId);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        wrapper.orderByAsc("dev_num");
        return deviceMapper.selectList(wrapper);
    }
 
    @Override
    public List<Device> getDevicesByMonitorPointId(List<Integer> monitorPointId) {
        QueryWrapper<Device> wrapper = new QueryWrapper();
        wrapper.in("monitor_point_id", monitorPointId);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        wrapper.orderByAsc("dev_num");
        return deviceMapper.selectList(wrapper);
    }
 
    @Override
    public Map<String, Object> getSensorsByMac(Map<String, Object> params) {
        List<String> sensorCodes = Arrays.asList(Constants.SENSOR_CODE_PM25
                , Constants.SENSOR_CODE_PM10
                , Constants.SENSOR_CODE_SO2
                , Constants.SENSOR_CODE_NO2
                , Constants.SENSOR_CODE_CO
                , Constants.SENSOR_CODE_O3
                , Constants.SENSOR_CODE_TEMP
                , "a01002"
                , Constants.SENSOR_CODE_WIND_SPEED
                , Constants.SENSOR_CODE_WIND_DIR
                , "a01006"
                , "a00e12"
        );
 
        //设备mac
        List<String> macs = (List<String>) params.remove("macs");
        List<Map<String, Object>> elementLists = new ArrayList<>();
 
        for (String mac : macs) {
            //从redis中获取设备因子信息
            Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO, mac);
            List<Sensor> sensors = device.getVersion().getSensors();
            Map<String, Object> map = new HashMap<>();
            for (Sensor sensor : sensors) {
                String sensorCode = sensor.getCode();
                String name = sensor.getName();
                map.put(sensorCode, name);
            }
            elementLists.add(map);
        }
 
        Map<String, Object> map = elementLists.stream()
                .filter(elementList -> elementList.size() != 0)
                .reduce((a, b) -> {
                    a.keySet().retainAll(b.keySet());
                    return a;
                }).orElse(new HashMap<>());
        Map<String, Object> result = new LinkedHashMap<>();
        sensorCodes.forEach(sensorCode -> {
            Object o = map.remove(sensorCode);
            if (o != null) {
                result.put(sensorCode, o);
            }
        });
        result.putAll(map);
        return result;
    }
 
    @Override
    public List<Map<String, Object>> getTrendChartData(Map<String, Object> params) {
        Object type = params.get("type");
        //设备mac
        List<String> macs = (List<String>) params.remove("macs");
 
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "name").in("mac", macs);
        List<Device> devices = deviceMapper.selectList(queryWrapper);
 
        //所选时间
        List<String> times = (List<String>) params.remove("times");
        //因子code
        String sensorCode = params.get("sensorCode").toString();
        String end;
        String timeUnits;
        String dateFormat;
        //返回结果集,time=data
        List<Map<String, Object>> result = new ArrayList<>();
 
        for (String start : times) {
            if ("hour".equals(type)) {
                end = DateUtils.getDateAddDay(start, 1);
                String yearAndMonth = DateUtils.dateToDateString(DateUtils.getDate(start, DateUtils.yyyy_MM_dd_EN), DateUtils.yyyyMM_EN);
                timeUnits = "hourly_" + yearAndMonth;
                dateFormat = "%Y-%m-%d %H";
            } else if ("day".equals(type)) {
                end = DateUtils.getDateAddMonth(start, 1);
                timeUnits = "daily";
                dateFormat = "%Y-%m-%d";
            } else {
                end = DateUtils.getDateAddYear(start, 1);
                timeUnits = "monthly";
                dateFormat = "%Y-%m";
            }
            params.put("timeUnits", timeUnits);
            params.put("start", start);
            params.put("end", end);
            params.put("macs", macs);
            params.put("dateFormat", dateFormat);
            //获取多设备指定因子数据
            List<Map<String, Object>> list = deviceMapper.getTrendChartData(params);
 
            for (String s : DateUtils.getTimeLag(start)) {
                Map<String, Object> resultMap = new HashMap<>();
                resultMap.put("time", s);
                List<Map<String, Object>> deviceData = new ArrayList<>();
                for (Device device : devices) {
                    Map<String, Object> valueMap = new HashMap<>();
                    valueMap.put("name", device.getName());
                    valueMap.put("sensorValue", "0.0");
                    for (Map<String, Object> map : list) {
                        Object time = map.get("time");
                        Object sensorValue = map.get(sensorCode);
                        Object mac = map.get("mac");
                        if (s.equals(time) && device.getMac().equals(mac)) {
                            valueMap.put("sensorValue", sensorValue);
                        }
                    }
                    deviceData.add(valueMap);
                }
                resultMap.put("deviceData", deviceData);
                result.add(resultMap);
            }
        }
        return result;
    }
 
    @Override
    public List<Map<String, Object>> getTrendChartDataV2(Map<String, Object> params) {
        Object type = params.get("type");
        //设备mac
        List<String> macs = (List<String>) params.remove("macs");
 
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "name").in("mac", macs);
        queryWrapper.orderByAsc("dev_num","name","id");
        List<Device> devices = deviceMapper.selectList(queryWrapper);
        Map<String,Object> devicesInfo = new HashMap<>();
        for (Device device:devices) {
            devicesInfo.put(device.getMac(),device.getName());
        }
 
        //所选时间
        List<String> times = (List<String>) params.remove("times");
        String startTime = times.get(0);
        String endTime = times.get(1);
        //因子code
        String sensorCode = params.get("sensorCode").toString();
        String end;
        String timeUnits;
        String dateFormat;
        //返回结果集,time=data
        List<Map<String, Object>> result = new ArrayList<>();
        List<Map<String, Object>> list = new ArrayList<>();
        if ("hour".equals(type)) {
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_EN);
            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
            for (String mac:macs) {
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("startDate",startDate);
                mapParams.put("endDate",endDate);
                mapParams.put("mac",mac);
                List<HistoryHourly> historyHourlies = multiTableQuery(mapParams, tableNames);
                historyHourlies = historyHourlies.stream().distinct().collect(Collectors.toList());
                List<HistoryHourly> distinctHistoryHourlies = new ArrayList<>();
                Map<String, Object> disMap = new HashMap<>();
                for (HistoryHourly historyHourly:historyHourlies) {
                    Date time = historyHourly.getTime();
                    String timeStr = DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
                    if (!disMap.containsKey(timeStr)){
                        distinctHistoryHourlies.add(historyHourly);
                        disMap.put(timeStr,true);
                    }
                }
                for (HistoryHourly historyHourly:distinctHistoryHourlies) {
                    Map<String,Object> historyHourlyMap = new HashMap<>();
                    historyHourlyMap.put("mac",mac);
                    JSONObject  value= JSONObject.parseObject(historyHourly.getValue());
                    Double sensorValue = Objects.nonNull(value)&&Objects.nonNull(value.get(sensorCode))?Double.parseDouble(value.get(sensorCode).toString()):0d;
                    historyHourlyMap.put(sensorCode,sensorValue);
                    Date time = historyHourly.getTime();
                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_EN);
                    historyHourlyMap.put("time",timeStr);
                    list.add(historyHourlyMap);
                }
            }
            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_EN);
            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN),DateUtils.yyyy_MM_dd_HH_EN)<=0){
                for (String mac:macs) {
                    boolean flag = true;
                    for (Map map:list) {
                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_dd_HH_EN)) && map.get("mac").toString().equals(mac.toString())){
                            flag = false;
                        }
                    }
                    if (flag){
                        Map<String,Object> historyHourlyMap = new HashMap<>();
                        historyHourlyMap.put("mac",mac);
                        historyHourlyMap.put(sensorCode,"0.0");
                        historyHourlyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_HH_EN));
                        list.add(historyHourlyMap);
                    }
                }
                middleDate = DateUtils.addHours(middleDate,1);
            }
        } else if ("day".equals(type)) {
            end = DateUtils.getDateAddDay(endTime,1);
            timeUnits = "daily";
            dateFormat = "%Y-%m-%d";
            params.put("timeUnits", timeUnits);
            params.put("start", startTime);
            params.put("end", end);
            params.put("macs", macs);
            params.put("dateFormat", dateFormat);
            //获取多设备指定因子数据
            list = deviceMapper.getTrendChartData(params);
            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_EN);
            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN),DateUtils.yyyy_MM_dd_EN)<=0){
                for (String mac:macs) {
                    boolean flag = true;
                    for (Map map:list) {
                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_dd_EN)) && map.get("mac").toString().equals(mac.toString())){
                            flag = false;
                        }
                    }
                    if (flag){
                        Map<String,Object> historyDailyMap = new HashMap<>();
                        historyDailyMap.put("mac",mac);
                        historyDailyMap.put(sensorCode,"0.0");
                        historyDailyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_dd_EN));
                        list.add(historyDailyMap);
                    }
                }
                middleDate = DateUtils.addDays(middleDate,1);
            }
        } else if ("month".equals(type)){
            end = DateUtils.getDateAddMonth(endTime, 1);
            timeUnits = "monthly";
            dateFormat = "%Y-%m";
            params.put("timeUnits", timeUnits);
            params.put("start", startTime);
            params.put("end", end);
            params.put("macs", macs);
            params.put("dateFormat", dateFormat);
            //获取多设备指定因子数据
            list = deviceMapper.getTrendChartData(params);
            Date middleDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_EN);
            while (DateUtils.compareDateStr(endTime,DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN),DateUtils.yyyy_MM_EN)<=0){
                for (String mac:macs) {
                    boolean flag = true;
                    for (Map map:list) {
                        if (map.get("time").equals(DateUtils.dateToDateString(middleDate, DateUtils.yyyy_MM_EN)) && map.get("mac").toString().equals(mac.toString())){
                            flag = false;
                        }
                    }
                    if (flag){
                        Map<String,Object> historyMonthlyMap = new HashMap<>();
                        historyMonthlyMap.put("mac",mac);
                        historyMonthlyMap.put(sensorCode,"0.0");
                        historyMonthlyMap.put("time",DateUtils.dateToDateString(middleDate,DateUtils.yyyy_MM_EN));
                        list.add(historyMonthlyMap);
                    }
                }
                middleDate = DateUtils.addMonths(middleDate,1);
            }
        }else {
            Date startDate = DateUtils.getDate(startTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            Date endDate = DateUtils.getDate(endTime,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
            List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
            for (String mac:macs) {
                Map<String, Object> mapParams = new HashMap<>();
                mapParams.put("startDate",startDate);
                mapParams.put("endDate",endDate);
                mapParams.put("mac",mac);
                List<HistoryFiveMinutely> HistoryFiveMinutelys = FiveMinuteTableQuery(mapParams, tableNames);
                HistoryFiveMinutelys = HistoryFiveMinutelys.stream().distinct().collect(Collectors.toList());
                List<HistoryFiveMinutely> distinctHistoryHourlies = new ArrayList<>();
                Map<String, Object> disMap = new HashMap<>();
                for (HistoryFiveMinutely historyFiveMinutely:HistoryFiveMinutelys) {
                    Date time = historyFiveMinutely.getTime();
                    String timeStr = DateUtils.dateToDateString(time,DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
                    if (!disMap.containsKey(timeStr)){
                        distinctHistoryHourlies.add(historyFiveMinutely);
                        disMap.put(timeStr,true);
                    }
                }
                for (HistoryFiveMinutely historyFiveMinutely:distinctHistoryHourlies) {
                    Map<String,Object> historyHourlyMap = new HashMap<>();
                    historyHourlyMap.put("mac",mac);
                    JSONObject value = JSONObject.parseObject(historyFiveMinutely.getValue());
                    if (value.get(sensorCode)==null){
                        historyHourlyMap.put(sensorCode,0.0);
                    }else {
                        Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
                        historyHourlyMap.put(sensorCode,sensorValue);
                    }
//                    Double sensorValue = Double.parseDouble(value.get(sensorCode).toString());
//                    historyHourlyMap.put(sensorCode,sensorValue);
                    Date time = historyFiveMinutely.getTime();
                    String timeStr = DateUtils.dateToDateString(time, DateUtils.yyyy_MM_dd_HH_mm_ss_EN);
                    historyHourlyMap.put("time",timeStr);
                    list.add(historyHourlyMap);
                }
            }
        }
        for (Map map:list) {
            String time = map.get("time").toString();
            Map<String,Object> deviceMap = new HashMap<>();
            deviceMap.put("name",devicesInfo.get(map.get("mac")));
            deviceMap.put("sensorValue",map.get(sensorCode));
            if (result.size()>0){
                boolean flag = true;
                for (Map resultMap:result) {
                    if (resultMap.get("time").toString().equals(time)){
                        List<Map<String,Object>> deviceData = new ArrayList<>();
                        deviceData = (List<Map<String,Object>>)resultMap.get("deviceData");
                        deviceData.add(deviceMap);
                        Collections.sort(deviceData, (map1,map2) -> {
                            String name1 = map1.get("name").toString();//name1是从你list里面拿出来的一个
                            String name2 = map2.get("name").toString(); //name1是从你list里面拿出来的第二个name
                            return name1.compareTo(name2);
                        });
                        resultMap.put("deviceData",deviceData);
                        //result.add(resultMap);
                        flag = false;
                        break;
                    }
                }
                if (flag){
                    List<Map<String,Object>> deviceData = new ArrayList<>();
                    deviceData.add(deviceMap);
                    Collections.sort(deviceData, (map1,map2) -> {
                        String name1 = map1.get("name").toString();//name1是从你list里面拿出来的一个
                        String name2 = map2.get("name").toString(); //name1是从你list里面拿出来的第二个name
                        return name1.compareTo(name2);
                    });
                    Map<String,Object> resultMap = new HashMap<>();
                    resultMap.put("deviceData",deviceData);
                    resultMap.put("time",time);
                    result.add(resultMap);
                }
            }else {
                List<Map<String,Object>> deviceData = new ArrayList<>();
                deviceData.add(deviceMap);
                Collections.sort(deviceData, (map1,map2) -> {
                    String name1 = map1.get("name").toString();//name1是从你list里面拿出来的一个
                    String name2 = map2.get("name").toString(); //name1是从你list里面拿出来的第二个name
                    return name1.compareTo(name2);
                });
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("deviceData",deviceData);
                resultMap.put("time",time);
                result.add(resultMap);
            }
        }
        Collections.sort(result, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String id1 = (String) o1.get("time");
                String id2 = (String) o2.get("time");
                return id1.compareTo(id2);
            }
 
        });
        return result;
    }
 
    @Override
    public Device getDeviceByMac(String mac) {
        Map<String, Object> deviceMap = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac);
        Device device = JSON.parseObject(JSON.toJSONString(deviceMap), Device.class);
        //从map获取organizationId和monitorPointId以及versionId
        Map<String, Object> organizationMap = (Map<String, Object>) deviceMap.get("organization");
        Map<String, Object> monitorPointMap = (Map<String, Object>) deviceMap.get("monitorPoint");
        Map<String, Object> versionMap = (Map<String, Object>) deviceMap.get("version");
        device.setDeviceVersionId((Integer) versionMap.get("id"));
        device.setOrganizationId((Integer) organizationMap.get("id"));
        device.setMonitorPointId((Integer) monitorPointMap.get("id"));
        //如果缓存为空则查询数据库
        if (ObjectUtils.isEmpty(device)) {
            return getDeviceByMacFromDB(mac);
        }
        return device;
    }
 
    @Override
    public List<Map<String, Object>> getDevicesByOrganizationId(Integer orgId) {
        //从数据库获取mac
        List macs = getMacsByOrganizationId(orgId);
        //从redis获取设备详细信息
        List<Map<String, Object>> result = new ArrayList<>();
        for (Object mac : macs) {
            Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac.toString());
            result.add(map);
        }
        return result;
    }
 
    @Override
    public List getMacsByOrganizationId(Integer organizationId) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac")
                .eq("organization_id", organizationId)
                .eq("is_delete", Constants.NOT_DELETE);
        return deviceMapper.selectObjs(queryWrapper);
    }
 
    @Override
    public List getMacsByOrgIdAndRegionCode(Integer organizationId, Integer regionCode) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac")
                .eq("organization_id", organizationId)
                .eq("is_delete", Constants.NOT_DELETE)
                .eq("town_code", regionCode);
        return deviceMapper.selectObjs(queryWrapper);
    }
 
    @Override
    public Device getDeviceUnitAlramInforByMac(String mac) {
        Device device = (Device) redisTemplate.opsForHash().get(RedisConstants.DEVICE_INFO,mac);
        if(device==null)
            return getDeviceUnitAlramInforByMacFromDb(mac);
        return device;
    }
 
    /**
     * 模糊查询
     *
     * @param mac
     * @return
     */
    @Override
    public List<Device> getFuzzySearch(String mac) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        if (ObjectUtils.isEmpty(mac)){
            return null;
        }
        wrapper.eq(Device::getIsDelete,Constants.NOT_DELETE);
        wrapper.like(Device::getMac,mac);
        List<Device> devices = deviceMapper.selectList(wrapper);
        return devices;
    }
 
    /**
     * 根据站点查询设备
     *
     * @param id
     * @return
     */
    @Override
    public List<Device> getOrganizationDevice(Integer id) {
        LambdaQueryWrapper<Device> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Device::getMonitorPointId,id);
        wrapper.eq(Device::getIsDelete,Constants.NOT_DELETE);
        wrapper.orderByAsc(Device::getCreateTime);
        List<Device> devices = deviceMapper.selectList(wrapper);
        return devices;
    }
 
    private Device getDeviceUnitAlramInforByMacFromDb(String mac){
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("mac",mac);
        wrapper.eq("is_delete",Constants.NOT_DELETE);
        Device device = deviceMapper.selectOne(wrapper);
        if(device==null)
            return null;
        QueryWrapper<OrganizationUnitAlarm> unitAlarmQueryWrapper = new QueryWrapper<>();
        unitAlarmQueryWrapper.eq("organization_id",device.getOrganizationId());
        unitAlarmQueryWrapper.eq("version_id",device.getDeviceVersionId());
        unitAlarmQueryWrapper.eq("is_delete",Constants.NOT_DELETE);
        List<OrganizationUnitAlarm> organizationUnitAlarms = organizationUnitAlarmMapper.selectList(unitAlarmQueryWrapper);
        Version version = new Version();
        version.setId(device.getDeviceVersionId());
        List<Sensor> sensors = new ArrayList<>();
        for (OrganizationUnitAlarm organizationUnitAlarm : organizationUnitAlarms) {
            Sensor sensor = sensorService.getSensorByCode(organizationUnitAlarm.getSensorCode());
            sensor.setUnit(organizationUnitAlarm.getUnitKey());
            sensor.setShowUnit(organizationUnitAlarm.getShowUnitKey());
            sensor.setShowUnitKey(organizationUnitAlarm.getShowUnitKey());
            sensor.setUnitKey(organizationUnitAlarm.getUnitKey());
            sensor.setAlarmLevel(organizationUnitAlarm.getAlarmLevel());
            String formula = unitConversionService.getFormula(Integer.valueOf(organizationUnitAlarm.getUnitKey()), Integer.valueOf(organizationUnitAlarm.getShowUnitKey()),sensor.getCode());
            sensor.setFormula(formula);
            sensors.add(sensor);
        }
        version.setSensors(sensors);
        device.setVersion(version);
        redisTemplate.opsForHash().put(RedisConstants.DEVICE_INFO,mac,device);
        return device;
    }
 
    private Device getDeviceByMacFromDB(String mac) {
        QueryWrapper<Device> wrapper = new QueryWrapper<>();
        wrapper.eq("mac", mac);
        wrapper.eq("is_delete", Constants.NOT_DELETE);
        return deviceMapper.selectOne(wrapper);
    }
 
    /**
     * @Description: 多表查询,传入表名集合,以及条件wrapper,返回数据
     * @Param: [wrapper, tableNames]
     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
     * @Author: 陈凯裕
     * @Date: 2021/9/23
     */
    private List<HistoryHourly> multiTableQuery(Map<String, Object> params, List<String> tableNames) {
        List<HistoryHourly> result = new ArrayList<>();
        for (String tableName : tableNames) {
            params.put("table",tableName);
            List<HistoryHourly> datas = historyHourlyMapper.listResult(params);
            result.addAll(datas);
        }
        return result;
    }
 
    private List<HistoryFiveMinutely> FiveMinuteTableQuery(Map<String, Object> params, List<String> tableNames) {
        List<HistoryFiveMinutely> result = new ArrayList<>();
        for (String tableName : tableNames) {
            params.put("table",tableName);
            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.listResult(params);
            result.addAll(datas);
        }
        return result;
    }
 
 
}