kaiyu
2020-11-24 1d7e1b771160dd8b7f25765e402ce25cac676641
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
package com.moral.service.impl;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.moral.common.bean.Constants;
import com.moral.common.bean.PageBean;
import com.moral.common.util.ExampleUtil;
import com.moral.common.util.ParameterUtils;
import com.moral.common.util.RedisUtils;
import com.moral.common.util.StringUtils;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.Device;
import com.moral.entity.DeviceProperty;
import com.moral.entity.MonitorPoint;
import com.moral.mapper.DeviceMapper;
import com.moral.mapper.DevicePropertyMapper;
import com.moral.mapper.DictionaryDataMapper;
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.OrganizationMapper;
import com.moral.service.DeviceService;
import com.moral.service.HistoryHourlyService;
import com.moral.service.MonitorPointService;
import com.moral.service.OrganizationService;
import com.moral.util.MyLatLng;
import com.moral.util.mapUtils;
 
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
 
@Service
public class MonitorPointServiceImpl implements MonitorPointService {
    @Resource
    private MonitorPointMapper monitorPointMapper;
    @Resource
    private DeviceMapper deviceMapper;
    @Resource
    private OrganizationMapper orgMapper;
    @Resource
    RedisUtils redisUtils;
    @Resource
    DictionaryDataMapper dictionaryDataMapper;
 
    @Resource
    private DeviceService deviceService;
 
    @Resource
    private OrganizationService organizationService;
 
    @Resource
    private HistoryHourlyService historyHourlyService;
 
    @Resource
    DevicePropertyMapper devicePropertyMapper;
 
    private static Class ENTITY_CLASS = MonitorPoint.class;
 
    @Override
    public List<MonitorPoint> getMonitorPointsByAreaName(Map<String, Object> parameters) {
        ValidateUtil.notNull(parameters.get("areaName"), "param.is.null");
        return monitorPointMapper.getMonitorPointsByAreaName(parameters);
    }
 
    @Override
    public List<MonitorPoint> queryWithStateByMap(Map<String, Object> params) {
        params.put("isDelete", Constants.IS_DELETE_FALSE);
        Object orgIdObj = params.get("orgId");
        List<MonitorPoint> monitorPointList = null;
        if (orgIdObj != null) {
            Integer orgId = Integer.parseInt(orgIdObj.toString());
            List<Integer> orgIds = orgMapper.selectLowerOrgIds(orgId);
            params.put("orgIds", orgIds);
            monitorPointList = monitorPointMapper.selectByMap(params);
//            for(MonitorPoint monitorPoint:monitorPointList){
//                Integer state = getStateFromRedis(monitorPoint.getId());
//                monitorPoint.setState(state);
//            }
        }
        return monitorPointList == null ? new ArrayList<>() : monitorPointList;
    }
 
    private Integer getStateFromRedis(Integer monitorPointId) {
        StringBuilder key = new StringBuilder();
        key.append("state_").append(monitorPointId).append("_*");
        List<Map> stateList = redisUtils.getList(key.toString(), Map.class);
        int state = -1;
        if (stateList != null) {
            for (Map deviceState : stateList) {
                int s = Integer.parseInt(deviceState.get("state").toString());
                state = s > state && s < 4 ? s : state;
            }
        }
        state = state == -1 ? 4 : state;
        return state;
    }
 
    @Override
    public PageBean queryByPageBean(PageBean pageBean) {
        Example example = ExampleUtil.generateExample(ENTITY_CLASS, pageBean);
        List<Example.Criteria> criteriaList = example.getOredCriteria();
        PageHelper.startPage(pageBean.getPageIndex(), pageBean.getPageSize());
        List<MonitorPoint> monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example);
        return new PageBean(monitorPointList);
    }
 
    @Override
    public MonitorPoint queryWithRelationById(Integer id) {
        Example example = new Example(ENTITY_CLASS);
        example.or().andEqualTo("id", id);
        List<MonitorPoint> monitorPointList = monitorPointMapper.selectWithAreaNameByExample(example);
        return monitorPointList != null && monitorPointList.size() > 0 ? monitorPointList.get(0) : null;
    }
 
    @Override
    public void addOrModify(MonitorPoint monitorPoint) {
        try {
            if (monitorPoint.getId() == null) {
                monitorPoint.setIsDelete(Constants.IS_DELETE_FALSE);
                monitorPointMapper.insertSelective(monitorPoint);
            } else {
                MonitorPoint queryMonitorPoint = new MonitorPoint();
                queryMonitorPoint.setId(monitorPoint.getId());
                queryMonitorPoint.setOrganizationId(monitorPoint.getOrganizationId());
                // num = 1,说明未改变,此查询在更新之前
                Integer num = monitorPointMapper.selectCount(queryMonitorPoint);
                boolean needRefreshCach = (num != 1);
                monitorPointMapper.updateByPrimaryKeySelective(monitorPoint);
                if (needRefreshCach) {
                    // 刷新当前监控点下设备 在redis里设备信息
                    refreshDevicesInRedis(monitorPoint.getId());
                }
            }
        } catch (Exception ex) {
            throw ex;
        }
    }
 
    /*
      刷新当前监控点下设备 在redis里设备信息
     */
    private void refreshDevicesInRedis(int monitorPointId) {
        Device queryDevice = new Device();
        queryDevice.setMonitorPointId(monitorPointId);
        List<Device> deviceList = deviceMapper.select(queryDevice);
        if (!CollectionUtils.isEmpty(deviceList)) {
            List<Integer> orgIds = monitorPointMapper.selectOrganizationIds(monitorPointId);
            if (!CollectionUtils.isEmpty(orgIds)) {
                deviceList.stream().forEach(dev -> {
                    if (!StringUtils.isNullOrEmpty(dev.getMac())) {
                        String key = "device_" + dev.getMac();
//                        // 简化的设备信息 用以缓存redis
//                        Device simpleDevice = new Device();
//                        simpleDevice.setId(dev.getId());
//                        simpleDevice.setDeviceVersion(dev.getDeviceVersion());
//                        simpleDevice.setMac(dev.getMac());
//                        simpleDevice.setMonitorPointId(dev.getMonitorPointId());
                        // 设置新组织关系,防止读写分离时数据库同步延迟
                        dev.setOrganizationIds(orgIds);
                        redisUtils.set(key, dev);
                    }
                });
            }
        }
 
    }
 
    @Override
    public void deleteByIds(Integer... ids) {
        MonitorPoint monitorPoint = new MonitorPoint();
        monitorPoint.setIsDelete(Constants.IS_DELETE_TRUE);
        if (ids != null && ids.length > 0) {
            if (ids.length == 1) {
                monitorPoint.setId(ids[0]);
                monitorPointMapper.updateByPrimaryKeySelective(monitorPoint);
            } else {
                Example example = new Example(ENTITY_CLASS);
                example.or().andIn("id", Arrays.asList(ids));
                monitorPointMapper.updateByExampleSelective(monitorPoint, example);
            }
 
        }
    }
 
    @Override
    public List<MonitorPoint> getMonitorPointsByName(String name) {
        Example example = new Example(MonitorPoint.class);
        Criteria criteria = example.createCriteria();
 
        criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE).andLike("name", "%" + name + "%");
        example.or().andEqualTo("isDelete", Constants.IS_DELETE_FALSE)
                .andCondition("getPY(" + getReplaceStr("name") + ") like ", "%" + name + "%");
 
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectByExample(example);
        return monitorPoints;
    }
 
    private String getReplaceStr(String name) {
        List<String[]> list = new ArrayList<String[]>();
        list.add(new String[]{"(", ""});
        list.add(new String[]{")", ""});
        for (String[] string : list) {
            name = replace(name, string[0], string[1]);
        }
        return name;
    }
 
    private String replace(String name, String fromStr, String toStr) {
        return "REPLACE (" + name + ",'" + fromStr + "','" + toStr + "')";
    }
 
    /**
     * @param idList
     * @return {id:,state:}
     */
    @Override
    public List<Map<String, String>> queryMonitroPointsState(List<Integer> idList) {
        List<Map<String, String>> list = idList.stream().map(id -> {
            Integer state = getStateFromRedis(id);
            Map<String, String> stateMap = new HashMap<>();
            stateMap.put("id", id.toString());
            stateMap.put("state", state.toString());
            return stateMap;
        }).collect(Collectors.toList());
        return list;
    }
 
    /**
     * 获取所属组织的监控点总数
     *
     * @param orgId
     * @return
     */
    @Override
    public Integer countOfSubOrgs(@NotNull Integer orgId) {
        Example example = new Example(ENTITY_CLASS);
        //过滤超级管理员账号
        if (!dictionaryDataMapper.isSupperOrgId(orgId)) {
            List<Integer> orgIds = orgMapper.selectLowerOrgIds(orgId);
            example.or().andIn("organizationId", orgIds);
        }
        return monitorPointMapper.selectCountByExample(example);
    }
 
    @Override
    public List<MonitorPoint> getMonitorPointsByOrganizationId(Integer orgId) {
        Example example = new Example(MonitorPoint.class);
        Criteria criteria = example.createCriteria();
 
        criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        if (Constants.isNotSpecialOrgId(orgId)) {
            //criteria.andEqualTo("organizationId", orgId);
            Set<Integer> organizationIds = organizationService.getChildOrganizationIds(orgId);
            criteria.andIn("organizationId", organizationIds);
 
        }
        example.orderBy("name").asc();
        return monitorPointMapper.selectByExample(example);
    }
 
    @Override
    public List<MonitorPoint> getMonitorPointsByRegion(Map<String, Object> parameters) {
        Example example = new Example(MonitorPoint.class);
        Criteria criteria = example.createCriteria();
 
        criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        criteria.andEqualTo(parameters.get("name").toString(), parameters.get("value"));
        return monitorPointMapper.selectByExample(example);
    }
 
    @Override
    public List<Integer> queryVersionsById(Integer id) {
        return monitorPointMapper.selectVersionsById(id);
    }
 
    @Override
    public MonitorPoint queryMonitorPointById(Integer mpointId) {
        return this.monitorPointMapper.selectByPrimaryKey(mpointId);
    }
 
    @Override
    public List<MonitorPoint> getMonitorPointsAndDevicesByRegion(Map<String, Object> parameters) {
        //校验参数
        Object organizationId = parameters.remove("organizationId");
        //判断参数是否为null,如果为null则抛出自定义异常,msgKey为异常信息
        ValidateUtil.notNull(organizationId, "param.is.null");
        ValidateUtil.notNull(parameters.get("regionCode"), "param.is.null");
 
        //组装查询条件
        ParameterUtils.getRegionType4RegionCode(parameters);
 
        //查询对应code的站点数据
        Example example = new Example(MonitorPoint.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDelete", Constants.IS_DELETE_FALSE);
 
        //判断是否为本公司开发人员,如果不是则添加orgid限制用户查看权限
        if (Constants.isNotSpecialOrgId(Integer.valueOf(organizationId.toString()))) {
            Set<Integer> organizationIds = organizationService.getChildOrganizationIds(Integer.valueOf(organizationId.toString()));
            criteria.andIn("organizationId", organizationIds);
        }
        criteria.andEqualTo(parameters.get("regionType") + "Code", parameters.remove("regionCode"));
        List<MonitorPoint> monitorPoints = monitorPointMapper.selectByExample(example);
 
        //查询所有设备信息
        Example deviceExample = new Example(Device.class);
        Criteria deviceCriteria = deviceExample.createCriteria();
        deviceCriteria.orEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        List<Device> devicesInfo = deviceMapper.selectByExample(deviceExample);
 
        //获取所有站点信息
        Example monitorExample = new Example(MonitorPoint.class);
        Criteria monitorCriteria = monitorExample.createCriteria();
        monitorCriteria.orEqualTo("isDelete", Constants.IS_DELETE_FALSE);
        List<MonitorPoint> monitorPointInfo = monitorPointMapper.selectByExample(monitorExample);
 
        //获取所有设备附加属性
        List<DeviceProperty> devicePropertyList = devicePropertyMapper.selectAll();
 
        //附加属性全部添加到设备实体中
        for (Device d : devicesInfo) {
            for (DeviceProperty dp : devicePropertyList) {
                if (dp.getId().equals(d.getId())) {
                    d.setDeviceProperty(dp);
                }
            }
        }
 
        //把对应的设备信息添加到站点中
        Map<Integer, List<Device>> monitorDeviceMap = new HashMap();
        for (MonitorPoint m : monitorPointInfo) {
            List<Device> monitorDevices = new ArrayList<>();
            for (Device d : devicesInfo) {
                if (m.getId().equals(d.getMonitorPointId())) {
                    monitorDevices.add(d);
                }
            }
            monitorDeviceMap.put(m.getId(), monitorDevices);
        }
 
        //对应地区Code的迭代器,向站点中添加设备信息
        Iterator<MonitorPoint> iterator = monitorPoints.iterator();
        while (iterator.hasNext()) {
            MonitorPoint monitorPoint = iterator.next();
            for (Map.Entry<Integer, List<Device>> entry : monitorDeviceMap.entrySet()) {
                if (monitorPoint.getId().equals(entry.getKey())) {
                    if (!CollectionUtils.isEmpty(entry.getValue())) {
                        monitorPoint.setDevices(entry.getValue());
                    } else {
                        iterator.remove();
                    }
                }
            }
        }
        return monitorPoints;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public Collection<Object> getDevicesStateByRegion(Map<String, Object> parameters) {
        //校验参数
        Object organizationId = parameters.remove("organizationId");
        ValidateUtil.notNull(organizationId, "param.is.null");
        ValidateUtil.notNull(parameters.get("regionCode"), "param.is.null");
 
        //组装查询条件
        ParameterUtils.getRegionType4RegionCode(parameters);
 
        if (Constants.isNotSpecialOrgId(Integer.valueOf(organizationId.toString()))) {
            Set<Integer> organizationIds = organizationService.getChildOrganizationIds(Integer.valueOf(organizationId.toString()));
            parameters.put("orgIds", organizationIds);
        }
        List<Map<String, Object>> monitorPoints = deviceMapper.getDevicesStateByRegion(parameters);
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> device;
        List<Map<String, Object>> devices;
        for (Map<String, Object> map : monitorPoints) {
            String id = map.get("id").toString();
 
            device = new HashMap<String, Object>();
            device.put("id", map.remove("deviceId"));
            device.put("name", map.remove("deviceName"));
            device.put("state", map.remove("state"));
            device.put("mac", map.remove("mac"));
 
            if (result.containsKey(id)) {
                Map<String, Object> monitorPoint = (Map<String, Object>) result.get(id);
                devices = (List<Map<String, Object>>) monitorPoint.get("devices");
            } else {
                devices = new ArrayList<Map<String, Object>>();
                result.put(id, map);
            }
            devices.add(device);
            map.put("devices", devices);
            result.put(id, map);
        }
 
        return result.values();
    }
 
    @Override
    public void isCompensateCalculation(Map<String, Object> parameters) {
        MonitorPoint monitorPoint = monitorPointMapper.selectByPrimaryKey(Integer.valueOf(parameters.get("monitorPointId").toString()));
        if (Integer.valueOf(320581).equals(monitorPoint.getAreaCode())) {
            parameters.put("compensate", true);
        }
 
    }
 
    @Override
    public Map<String, Object> selectAllById(String id) {
        int id2 = Integer.parseInt(id);
        Map<String, Object> map = monitorPointMapper.selectAllById(id2);
        return map;
    }
 
    @Override
    public List<Device> getDeviceList(int id) {
        return monitorPointMapper.getDeviceList(id);
    }
 
    @Override
    public JSONObject getMonitorPointById(int id, String Time, int i, String sensor) {
        JSONObject params = new JSONObject();
        MonitorPoint monitorPoint = monitorPointMapper.getMonitorPointById(id);
        if (i == 1) {
            Double longitude = monitorPoint.getLongitude();
            Double latitude = monitorPoint.getLatitude();
 
            Double workshop = 0.0;
 
            Double flue = 0.0;
            //烟道设备
            List<Device> device1 = deviceService.getDeviceById1(id);
 
            //厂界设备
            List<Device> device2 = deviceService.getDeviceById2(id);
 
            //车间设备
            List<Device> device3 = deviceService.getDeviceById3(id);
 
 
            String purificationRate = null;//净化率
 
            if (device3.size() == 0) {
                purificationRate = "公司无车间设备,无法计算净化率";
                params.put("device2", device2);
                params.put("device1", device1);
            } else {
                params.put("device1", device1);
                params.put("device2", device2);
                params.put("device3", device3);
                for (Device device33 : device3) {
                    String mac3 = device33.getMac();
                    String tvoc = historyHourlyService.getTVOCByMac(mac3, Time, sensor);
                    if (tvoc != null) {
                        workshop = Double.parseDouble(tvoc);
                    } else {
                        workshop = 0.0;
                    }
                }
                if (device1.size() == 0) {
                    purificationRate = "公司无烟道设备,无法计算净化率";
                } else {
                    for (Device device11 : device1) {
                        String mac1 = device11.getMac();
                        flue = Double.parseDouble(historyHourlyService.getTVOCByMac(mac1, Time, sensor));
                        if (workshop == 0.0) {
                            purificationRate = "车间设备无数据浓度,无法计算浓度值";
                        } else {
                            if (flue == 0.0) {
                                purificationRate = "烟道设备无数据浓度,无法计算净化率";
                            } else {
                                BigDecimal b = new BigDecimal(((workshop - flue) / workshop) * 100);
                                double purity2 = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                                purificationRate = (purity2) + "%";
                            }
                        }
                    }
                }
            }
            params.put("latitude", latitude);
            params.put("longitude", longitude);
            params.put("purificationRate", purificationRate);
        }
        if (i == 2) {
            Double longitude = monitorPoint.getLongitude();
            Double latitude = monitorPoint.getLatitude();
            //烟道设备
            List<Device> device1 = deviceService.getDeviceById1(id);
            //厂界设备
            List<Device> device2 = deviceService.getDeviceById2(id);
            //车间设备
            List<Device> device3 = deviceService.getDeviceById3(id);
            String collectionRate = null;//收集率
            Double pressure = 0.0;//平均压强
            List<Double> list1 = new ArrayList();
            if (device3.size() == 0) {
                collectionRate = "该公司车间无设备,无法计算收集率";
                params.put("device1", device1);
                params.put("device2", device2);
            } else {
                params.put("device3", device3);
                if (device2.size() == 0) {
                    collectionRate = "该公司厂界无设备,无法计算收集率";
                    params.put("device1", device1);
                } else {
                    params.put("device2", device2);
                    params.put("device1", device1);
                    Double sum = 0.0;
                    for (Device device22 : device2) {
                        String mac2 = device22.getMac();
                        String pressure2 = historyHourlyService.getPressureByMac(mac2, Time);
                        if (pressure2 != null) {
                            pressure = Double.parseDouble(pressure2);
                            list1.add(pressure);
                        } else {
                            collectionRate = "该公司厂界设备无数据浓度,无法计算收集率";
                        }
 
                    }
                    for (Double d : list1) {
                        sum = sum + d;
                    }
                    if (list1.size() != 0) {
                        pressure = sum / list1.size();
                    } else {
                        collectionRate = "该公司厂界设备无数据浓度,无法计算收集率";
                    }
                    Double pressure33 = 0.0;
                    for (Device device33 : device3) {
                        String mac3 = device33.getMac();
                        String pressure3 = historyHourlyService.getPressureByMac(mac3, Time);
                        if (pressure3 != null) {
                            pressure33 = Double.parseDouble(pressure3);
                            BigDecimal b = new BigDecimal((pressure - pressure33 + 3.5) * 20);//临时加2.5,有具体设备就不需要加
                            double collection = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                            collectionRate = collection + "%";
                        }
                    }
                }
            }
            params.put("latitude", latitude);
            params.put("longitude", longitude);
            params.put("collectionRate", collectionRate);
        }
 
        if (i == 3) {
            Double longitudeCompany = monitorPoint.getLongitude();
            Double latitudeCompany = monitorPoint.getLatitude();
            Double longitude = 0.0;//每台设备的经纬度
            Double latitude = 0.0;//每台设备的经纬度
            List<Device> deviceList = deviceService.getDeviceById2(id);
            //List<Device> deviceList = monitorPointService.getDeviceList(id);
            List<Map> list = new ArrayList<Map>();//存放historyHourly的json数据
            String smac = "";//,每台设备的Mac号
            List<Map> windList = new ArrayList<Map>();//存放每台设备的风向和经纬度
            Map<String, Object> jsonMap = new HashMap<String, Object>();//每台设备的数据
            Map<String, Object> deviceMap = null;
            if (deviceList.size() > 1) {
                for (Device device : deviceList) {
                    deviceMap = new HashMap<String, Object>();
                    smac = device.getMac();
                    latitude = device.getLatitude();
                    longitude = device.getLongitude();
 
                    jsonMap = historyHourlyService.getDataByMac(smac, Time);
                    deviceMap.put("longitude", longitude);
                    deviceMap.put("latitude", latitude);
                    if (jsonMap != null) {
                        deviceMap.put("jsonMap", jsonMap);
                    } else {
                        deviceMap.put("jsonMap", "该设备该时间点无数据");
                    }
 
                    list.add(deviceMap);
                }
                Map<String, Object> mapData = new HashMap<String, Object>();
                Map<String, Object> mapDevice;
                JSONArray windDir;
                for (Map<String, Object> objectMap : list) {
                    mapDevice = new HashMap<String, Object>();
                    Map<String, Object> map = null;
                    if (!(objectMap.get("jsonMap") instanceof String)) {
                        mapData = (Map<String, Object>) objectMap.get("jsonMap");
                        map = (Map) JSON.parseObject((String) mapData.get("json"));
                        windDir = (JSONArray) JSONArray.toJSON(map.get("e23"));
                        JSONArray concentration = (JSONArray) JSONArray.toJSON(map.get(sensor));
 
                        BigDecimal bigDecimal1 = new BigDecimal(0);
                        BigDecimal bigDecimal = new BigDecimal(0);
                        if (concentration != null) {
                            if (concentration.get(0) instanceof Integer) {
                                bigDecimal1 = new BigDecimal(concentration.get(0).toString());
                            } else {
                                bigDecimal1 = (BigDecimal) concentration.get(0);
                            }
                            if (windDir != null) {
                                if (windDir.get(0) instanceof Integer) {
                                    bigDecimal = new BigDecimal(windDir.get(0).toString());
                                } else {
                                    bigDecimal = (BigDecimal) windDir.get(0);
                                }
                                mapDevice.put("e23", bigDecimal.doubleValue());
                                mapDevice.put("sensor", bigDecimal1.doubleValue());
                                mapDevice.put("longitude", objectMap.get("longitude"));
                                mapDevice.put("latitude", objectMap.get("latitude"));
                            } else {
                                mapDevice.put("noSensor", "无风向数据");
                                mapDevice.put("longitude", objectMap.get("longitude"));
                                mapDevice.put("latitude", objectMap.get("latitude"));
                            }
                        }
                    } else {
                        mapDevice.put("info", objectMap.get("jsonMap"));
                        mapDevice.put("longitude", objectMap.get("longitude"));
                        mapDevice.put("latitude", objectMap.get("latitude"));
                    }
                    windList.add(mapDevice);
                }
                // if (windDir!=null){}
                Double differenceNum = 0.0;
                Map<String, Object> indexMap;
                List<Map> mapList = new ArrayList<>();
                for (int j = 0; j < windList.size(); j++) {
                    for (int k = 0; k < windList.size(); k++) {
                        if (k != j) {
                            indexMap = new HashMap<String, Object>();
                            if (windList.get(k).containsKey("e23") && windList.get(j).containsKey("e23")) {
                                Double e23Numk = (Double) windList.get(k).get("e23");
                                Double e23Numj = (Double) windList.get(j).get("e23");
                                Double diff = e23Numk - e23Numj;
                                BigDecimal b = new BigDecimal(Math.abs(diff));
 
                                differenceNum = b.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
                                if (differenceNum > 180) {
                                    differenceNum = 360 - differenceNum;
                                }
                                indexMap.put("k", k);
                                indexMap.put("j", j);
                                indexMap.put("diff", differenceNum);
                            } else {
                                indexMap.put("k", k);
                                indexMap.put("j", j);
                                indexMap.put("info", "设备无数据");
                            }
 
//                     indexMap.put("longitude",windList.get(k).get("longitude"));
//                     indexMap.put("latitude",windList.get(k).get("latitude"));
                            mapList.add(indexMap);
                        } else {
                            continue;
                        }
 
                    }
                }
                Double min = 0.0;
                int indexMin = 0;
                if (mapList.size() > 0) {
                    if (mapList.get(0).containsKey("diff")) {
                        min = (Double) mapList.get(0).get("diff");
                        for (int q = 1; q < mapList.size(); q++) {
                            if (mapList.get(q).containsKey("diff")) {
                                if (min > (Double) mapList.get(q).get("diff")) {
                                    min = (Double) mapList.get(q).get("diff");
                                    indexMin = q;
                                }
                            } else {
                                indexMin = q;
                            }
                        }
                    }
                }
 
                Map twoDeviceMap = mapList.get(indexMin);
                List<Map> list1 = new ArrayList<Map>();           //存放风向夹角最小的两台设备的经纬度
                list1.add(windList.get((Integer) twoDeviceMap.get("k")));
                list1.add(windList.get((Integer) twoDeviceMap.get("j")));
 
                Double wind = 0.0;
                Double sum = 0.0;
                if (list1.get(0).containsKey("e23")) {
                    if (Math.abs((Double) list1.get(0).get("e23") - (Double) list1.get(1).get("e23")) > 180) {
                        sum = (Double) list1.get(0).get("e23") + (Double) list1.get(1).get("e23");
                        wind = sum / 2 + 180;
                        if (wind > 360) {
                            wind = wind - 360;
                        }
                    } else {
                        for (Map map : list1) {
                            sum += (Double) map.get("e23");
                        }
                        wind = sum / 2;
                    }
                } else {
                    wind = 0.0;
                }
                Map<String, Object> longAndLatiMap;
                List<Map> longAndLatiList = new ArrayList<>();//经纬度夹角集合,存放的是windList中两台设备的下标和两台设备夹角与风向角的差值
                List<Map> preAngleDeviceList = new ArrayList<Map>(); //角度减去风向最小的两台设备
                if (wind != 0.0) {
                    for (int f = 0; f < windList.size(); f++) {
                        for (int h = 0; h < windList.size(); h++) {
                            if (f != h) {
                                longAndLatiMap = new HashMap<String, Object>();
                                longAndLatiMap.put("h", h);
                                longAndLatiMap.put("f", f);
                                Double angle = mapUtils.getAngle(
                                        new MyLatLng((Double) windList.get(h).get("longitude"),
                                                (Double) windList.get(h).get("latitude")),
                                        new MyLatLng((Double) windList.get(f).get("longitude"),
                                                (Double) windList.get(f).get("latitude")));
                                Double angleDiff = Math.abs(angle - wind);
                                longAndLatiMap.put("angle", angleDiff);
                                longAndLatiList.add(longAndLatiMap);
                            }
                        }
                    }
                    Double minAngle = (Double) longAndLatiList.get(0).get("angle");
                    int indexAngle = 0;
                    for (int j = 0; j < longAndLatiList.size(); j++) {
                        if (minAngle > (Double) longAndLatiList.get(j).get("angle")) {
                            minAngle = (Double) longAndLatiList.get(j).get("angle");
                            indexAngle = j;
                        }
                    }
                    //windList.get((Integer) longAndLatiList.get(indexAngle).get("f"))获得风向和经纬度
                    preAngleDeviceList.add(windList.get((Integer) longAndLatiList.get(indexAngle).get("h")));
                    preAngleDeviceList.add(windList.get((Integer) longAndLatiList.get(indexAngle).get("f")));
 
                    Double length = mapUtils.getDistance((Double) preAngleDeviceList.get(0).get("longitude"), (Double) preAngleDeviceList.get(0).get("latitude"),
                            (Double) preAngleDeviceList.get(1).get("longitude"), (Double) preAngleDeviceList.get(1).get("latitude"));
                    Double subLength = length / 5;
                    Double subLength1 = length / 6.1;
                    Double angle = mapUtils.getAngle(
                            new MyLatLng((Double) preAngleDeviceList.get(0).get("longitude"),
                                    (Double) preAngleDeviceList.get(0).get("latitude")),
                            new MyLatLng((Double) preAngleDeviceList.get(1).get("longitude"),
                                    (Double) preAngleDeviceList.get(1).get("latitude")));
 
                    params.put("preAngleDeviceList", preAngleDeviceList);
                    params.put("angle", angle);
                    List locationList = new ArrayList();
                    String[] firstLocation = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, (Double) preAngleDeviceList.get(0).get("longitude"),
                            (Double) preAngleDeviceList.get(0).get("latitude"), subLength);
                    String[] secondLoction = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(firstLocation[0]),
                            Double.parseDouble(firstLocation[1]), subLength);
                    String[] thirdLocation = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(secondLoction[0]),
                            Double.parseDouble(secondLoction[1]), subLength);
                    String[] fourthLoction = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(thirdLocation[0]),
                            Double.parseDouble(thirdLocation[1]), subLength);
                    String[] fivethLoction = mapUtils.calLocationByDistanceAndLocationAndDirection(angle, Double.parseDouble(fourthLoction[0]),
                            Double.parseDouble(fourthLoction[1]), subLength1);
                    locationList.add(firstLocation);
                    locationList.add(secondLoction);
                    locationList.add(thirdLocation);
                    locationList.add(fourthLoction);
                    locationList.add(fivethLoction);
                    params.put("locationList", locationList);
 
                    String preAngleDeviceString = JSON.toJSON(preAngleDeviceList).toString();
                    params.put("preAngleDeviceString", preAngleDeviceString);//两台添加箭头的设备
                } else {
                    params.put("preAngleDeviceString", 0);
                }
                params.put("wind", wind);
                //String preAngleDeviceString=preAngleDeviceList.
                params.put("deviceList", deviceList);
            } else {
                if (deviceList.size() != 0) {
                    params.put("deviceList", deviceList.get(0));
                } else {
                    params.put("deviceList", "");
                }
                params.put("preAngleDeviceString", 1);
                params.put("locationList", "");
            }
 
            params.put("latitudeCompany", latitudeCompany);
            params.put("longitudeCompany", longitudeCompany);
        }
        return params;
    }
 
    @Override
    public JSONObject getMacList(int monitPointId) {
        JSONObject params = new JSONObject();
        MonitorPoint monitorPoint = monitorPointMapper.getMonitorPointById(monitPointId);
 
        //获取厂界所有设备
        List<Device> deviceList = deviceService.getDeviceById2(monitPointId);
 
        params.put("longitudeCompany",monitorPoint.getLongitude());
        params.put("latitudeCompany",monitorPoint.getLatitude());
        params.put("latitudeCompany",monitorPoint.getLatitude());
        params.put("monitPointId",monitPointId);
        params.put("deviceList",deviceList);
        return params;
    }
 
    @Override
    public List<MonitorPoint> getMonitorPointListByAccountId(int id) {
        return monitorPointMapper.getMonitorPointListByAccountId(id);
    }
 
    @Override
    public MonitorPoint byIdGetMonitorPoint(int id) {
        return monitorPointMapper.byIdGetMonitorPoint(id);
    }
 
    @Override
    public String getOrgIdByMac(String mac) {
        return monitorPointMapper.getOrgIdByMac(mac);
    }
 
 
}