cjl
2023-12-14 6992aaf0587c09f7c511c1afd12e1519d91363d3
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.moral.api.config.Interceptor.UserHelper;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.mapper.HistoryHourlyMapper;
import com.moral.api.pojo.dto.Wind.WindData;
import com.moral.api.pojo.dto.historyFiveMinutely.DeviceAndFiveMinuteDataDTO;
import com.moral.api.pojo.enums.SysDictTypeEnum;
import com.moral.api.pojo.ext.wind.WindListExt;
import com.moral.api.pojo.form.device.MonitorPointQueryForm;
import com.moral.api.pojo.form.historyFiveMinutely.QueryDeviceAndFiveMinuteDataForm;
import com.moral.api.pojo.vo.historyFiveMinutely.QueryFiveDataByMacVO;
import com.moral.api.pojo.vo.user.QxUser;
import com.moral.api.service.DeviceService;
import com.moral.api.service.HistoryFiveMinutelyService;
import com.moral.api.service.MonitorPointService;
import com.moral.api.service.SysDictTypeService;
import com.moral.api.utils.UnitConvertUtils;
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 com.moral.util.RegionCodeUtils;
import io.lettuce.core.GeoCoordinates;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
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.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 5分钟数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-07-15
 */
@Service
@Slf4j
public class HistoryFiveMinutelyServiceImpl extends ServiceImpl<HistoryFiveMinutelyMapper, HistoryFiveMinutely> implements HistoryFiveMinutelyService {
 
    @Autowired
    MonitorPointService monitorPointService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    HistoryFiveMinutelyMapper historyFiveMinutelyMapper;
 
    @Autowired
    HistoryHourlyMapper historyHourlyMapper;
    @Autowired
    DeviceService deviceService;
    @Autowired
    DeviceMapper deviceMapper;
    @Autowired
    private SysDictTypeService sysDictTypeService;
 
    @Override
    public List<Map<String, Object>> queryDeviceAndFiveMinuteData(QueryDeviceAndFiveMinuteDataForm form) {
        //取参
        String times = DateUtils.dateToDateString(new Date(),DateUtils.yyyyMM_EN);
        Integer organizationId = form.getOrganizationId();
        Integer regionCode = form.getRegionCode();
        String sensorCode = form.getSensorCode();
        String type = "$."+ sensorCode;
        String region = null;
        String endHourlyTime = null;
        if (regionCode != null && organizationId!=24) {
            region = RegionCodeUtils.regionCodeConvertToName(regionCode);
        }
        if (form.getChooseTime().equals("true")) {
            endHourlyTime = form.getTime();
            Date time = DateUtils.convertDate(endHourlyTime);
            times = DateUtils.dateToDateString(time,DateUtils.yyyyMM_EN);
        }
        List<Map<String, Object>> listAll = deviceMapper.deviceList(organizationId,regionCode,region,type,sensorCode,times,endHourlyTime);
        //查询组织在对应地区下的站点以及设备
        /*List<MonitorPoint> monitorPoints = monitorPointService.queryByOrgIdAndRegionCode(new MonitorPointQueryForm(organizationId, regionCode));
        List<Device> devices = new ArrayList<>();
        for (MonitorPoint monitorPoint : monitorPoints) {
            List<Device> monitorPointDevices = monitorPoint.getDevices();
            if (!ObjectUtils.isEmpty(monitorPointDevices)) {
                //过滤掉线设备
                for (Device device : monitorPointDevices) {
                    if (!device.getState().equals(Constants.DEVICE_STATE_OFFLINE))
                        devices.add(device);
                }
            }
 
        }*/
        //查询所有设备对应的数据
        /*List<Device> devices = new ArrayList<>();
        List<DeviceAndFiveMinuteDataDTO> dtos = new ArrayList<>();
        if (form.getChooseTime().equals("true")) {
            String time = form.getTime();
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            for (Device device : devices) {
                DeviceAndFiveMinuteDataDTO dto = new DeviceAndFiveMinuteDataDTO();
                String mac = device.getMac();
                String dbDataStr = historyHourlyMapper.queryLastHourlyMac(mac, time, timeUnits);
//                String dbDataStr = historyFiveMinutelyMapper.queryLastFiveMinutely(mac, time, timeUnits);
                HashMap sensorValues = JSON.parseObject(dbDataStr, HashMap.class);
                Map<String, Object> sensorValue = new HashMap<>();
                if (sensorValues != null && sensorValues.get(sensorCode) != null)
                    sensorValue.put(sensorCode, sensorValues.get(sensorCode));
                else
                    sensorValue.put(sensorCode, null);
                dto.setDevice(device);
                dto.setSensorValue(sensorValue);
                dtos.add(dto);
            }
            return dtos;
        }
        //List<Map<String, Object>> sensorValuess = (List<Map<String, Object>>) redisTemplate.opsForValue().multiGet(Arrays.asList("data_five_minutes:p5dnd7a0245400","data_five_minutes:p5dnd7a0245412"));
        for (Device device : devices) {
            DeviceAndFiveMinuteDataDTO dto = new DeviceAndFiveMinuteDataDTO();
            String mac = device.getMac();
            Map<String, Object> sensorValues = queryLastDataByMac(mac);
 
            String dbDataStr = JSON.toJSONString(sensorValues);
            sensorValues = JSON.parseObject(dbDataStr, HashMap.class);
            Map<String, Object> sensorValue = new HashMap<>();
            if (sensorValues != null && sensorValues.get(sensorCode) != null)
                sensorValue.put(sensorCode, sensorValues.get(sensorCode));
            else
                sensorValue.put(sensorCode, null);
            dto.setDevice(device);
            dto.setSensorValue(sensorValue);
            dtos.add(dto);
        }*/
        return listAll;
    }
 
    @Override
    public Map<String, Object> queryLastDataByMac(String mac) {
        //从缓存中获取
        Map<String, Object> sensorValues = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DATA_FIVE_MINUTES, mac);
        //如果没有数据从数据库查询
        if (sensorValues != null)
            return sensorValues;
        Map<String, Object> mapParams = new HashMap<>();
        mapParams.put("mac",mac);
        //获取当月的表名
        Date date = new Date();
        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(date, date, SeparateTableType.MONTH);
        List<HistoryFiveMinutely> datas = multiTableQuery(mapParams, tableNames);
        if (ObjectUtils.isEmpty(datas))
            return null;
        HistoryFiveMinutely historyFiveMinutely = datas.get(0);
        sensorValues = JSON.parseObject(historyFiveMinutely.getValue(), Map.class);
        sensorValues.put("dataTime", DateUtils.dateToDateString(historyFiveMinutely.getTime(), "yyyy-MM-dd HH:mm:ss"));
        return sensorValues;
    }
 
 
    @Override
    public List<Object> getAreaWindData(Map<String, Object> params) {
//        String timeUnits = DateUtils.dateToDateString(new Date(), DateUtils.yyyyMM_EN);
//        params.put("timeUnits", timeUnits);
//        //风场数据
//        List<WindData> windData = historyFiveMinutelyMapper.getAreaWindData(params);
//        return getWindData(windData);
        String chooseTime = params.get("chooseTime").toString();
        List<WindData> windData = null;
        if (chooseTime.equals("true")) {
            String time = params.get("time").toString();
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            params.put("timeUnits", timeUnits);
            windData = historyHourlyMapper.getArea(params);
        } else {
            String timeUnits = DateUtils.dateToDateString(new Date(), DateUtils.yyyyMM_EN);
            params.put("timeUnits", timeUnits);
            //风场数据
            windData = historyFiveMinutelyMapper.getAreaWindData(params);
        }
        return getWindData(windData,params);
    }
    @Override
    public List<Object> getAreaWindDataCenter(Map<String, Object> params) {
//        String timeUnits = DateUtils.dateToDateString(new Date(), DateUtils.yyyyMM_EN);
//        params.put("timeUnits", timeUnits);
//        //风场数据
//        List<WindData> windData = historyFiveMinutelyMapper.getAreaWindData(params);
//        return getWindData(windData);
 
        params.put("timeUnits","202311");
        params.put("time","2023-11-05 09:00");
        params.put("macs",Arrays.asList("p5dnd7a0245472","p5dnd7a0245400","p5dnd7a0245385","p5dnd7a0245374"));
        List<WindData> windData = historyHourlyMapper.getArea(params);
        params.put("perdlen",50);
        return getWindData(windData,params);
    }
 
    //获取五分钟弹窗数据
    @Override
    public Map<String, Object> getPopDataByMac(Map<String, Object> params) {
//        Map<String, Object> datas = queryLastDataByMac(mac);
        String chooseTime = params.get("chooseTime").toString();
        String mac = params.get("mac").toString();
        Map<String, Object> datas = null;
        if (chooseTime.equals("true")) {
            String time = params.get("time").toString();
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            String dbDataStr = historyHourlyMapper.queryLastHourlyMac(mac, time, timeUnits);
            if (dbDataStr == null) {
                return null;
            }
//            String dbDataStr = historyFiveMinutelyMapper.queryLastFiveMinutely(mac, time, timeUnits);
            datas = JSON.parseObject(dbDataStr, HashMap.class);
            datas.put("dataTime", time);
        } else {
            datas = queryLastDataByMac(mac);
            if (ObjectUtils.isEmpty(datas)){
                return null;
            }
        }
        //构建返回对象
        Map<String, Object> result = new HashMap<>();
        //创建返回因子code集合
        List<String> sensorCodes = Arrays.asList("a34004", "a34002", "a21005", "a21004", "a21026", "a05024", "a99054", "a01008", "a01007");
        //获取设备信息
        Device device = deviceService.getDeviceUnitAlramInforByMac(mac);
        //转换数据单位和名称
        for (Sensor sensor : device.getVersion().getSensors()) {
            if (!sensorCodes.contains(sensor.getCode()))
                continue;
            Object dataO = datas.get(sensor.getCode());
            if (dataO != null) {
                String data = String.valueOf(dataO);
                //转换单位
                if (!sensor.getShowUnitKey().equals(sensor.getUnitKey()))
                    data = UnitConvertUtils.calculate(data, sensor.getFormula());
                //拼接单位
                data = data + " " + sensor.getShowUnit();
                result.put(sensor.getCode(), data);
            } else {
                if (sensor.getCode().equals("a01008") ||sensor.getCode().equals("a01007")){
                    result.put(sensor.getCode(), "-");
                }else {
                    result.put(sensor.getCode(), "0"+" "+sensor.getShowUnit());
                }
 
            }
        }
        //添加设备名称
        result.put("name", device.getName());
        //添加时间
        result.put("dataTime", datas.get("dataTime"));
        //排序
        return orderSixParam(result);
    }
 
    @Override
    public QueryFiveDataByMacVO queryFiveDataByMac(String mac, String chooseTime, String time) {
        Map<String, Object> datas = null;
        if (chooseTime.equals("true")) {
            String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
            String dbDataStr = historyHourlyMapper.queryLastHourlyMac(mac, time, timeUnits);
            if (dbDataStr == null) {
                return null;
            }
            datas = JSON.parseObject(dbDataStr, HashMap.class);
            datas.put("dataTime", time);
        } else {
            datas = queryLastDataByMac(mac);
            if (ObjectUtils.isEmpty(datas)){
                return null;
            }
        }
        //构建返回对象
        Map<String, Object> result = new HashMap<>();
 
        //创建返回因子code集合
        List<String> sensorCodes = Arrays.asList("a34004", "a34002", "a21005", "a21004", "a21026", "a05024", "a99054", "a01008", "a01007");
        //获取设备信息
        Device device = deviceService.getDeviceUnitAlramInforByMac(mac);
        //转换数据单位和名称
        for (Sensor sensor : device.getVersion().getSensors()) {
            if (!sensorCodes.contains(sensor.getCode()))
                continue;
            Object dataO = datas.get(sensor.getCode());
            if (dataO != null) {
                String data = String.valueOf(dataO);
                result.put(sensor.getCode(), data);
            } else {
                if (sensor.getCode().equals("a01008") ||sensor.getCode().equals("a01007")){
                    result.put(sensor.getCode(), "-");
                }else {
                    result.put(sensor.getCode(), "0");
                }
 
            }
        }
        String str = new DecimalFormat("#.######").format(Double.parseDouble(result.get("a21005").toString()));
        QueryFiveDataByMacVO queryFiveDataByMacVO = new QueryFiveDataByMacVO();
        queryFiveDataByMacVO.setName(device.getName());
        queryFiveDataByMacVO.setDataTime(datas.get("dataTime").toString());
        queryFiveDataByMacVO.setA34004(result.get("a34004").toString());
        queryFiveDataByMacVO.setA34002(result.get("a34002").toString());
        queryFiveDataByMacVO.setA21026(result.get("a21026").toString());
        queryFiveDataByMacVO.setA21004(result.get("a21004").toString());
        queryFiveDataByMacVO.setA21005(str);
        queryFiveDataByMacVO.setA05024(result.get("a05024").toString());
        queryFiveDataByMacVO.setA01008(result.get("a01008").toString());
        queryFiveDataByMacVO.setA01007(result.get("a01007").toString());
        queryFiveDataByMacVO.setA99054(result.get("a99054").toString());
 
        return queryFiveDataByMacVO;
    }
 
    //对六参以及时间进行排序
    private Map<String, Object> orderSixParam(Map<String, Object> data) {
        LinkedHashMap result = new LinkedHashMap();
        Object PM2_5 = data.get("a34004");
        Object PM10 = data.get("a34002");
        Object SO2 = data.get("a21026");
        Object NO2 = data.get("a21004");
        Object CO = data.get("a21005");
        Object O3 = data.get("a05024");
        Object a01008 = data.get("a01008");
        Object a01007 = data.get("a01007");
        Object name = data.get("name");
        Object time = data.get("dataTime");
        if (name != null)
            result.put("名称", name);
        if (PM2_5 != null)
            result.put("PM2.5", PM2_5);
        if (PM10 != null)
            result.put("PM10", PM10);
        if (SO2 != null)
            result.put("二氧化硫(SO₂)", SO2);
        if (NO2 != null)
            result.put("二氧化氮(NO₂)", NO2);
        if (CO != null)
            result.put("一氧化碳(CO)", CO);
        if (O3 != null)
            result.put("臭氧(O₃)", O3);
        if (time != null)
            result.put("时间", time);
        if (a01008 != null)
            result.put("风向", a01008);
        if (a01007 != null)
            result.put("风速", a01007);
        return result;
    }
 
 
    /**
     * 根据输入的地点坐标计算中心点
     *
     * @param geoCoordinateList
     * @return
     */
    public Map<String, Double> getCenterPoint(List<GeoCoordinates> geoCoordinateList) {
        int total = geoCoordinateList.size();
        double X = 0, Y = 0, Z = 0;
        for (GeoCoordinates g : geoCoordinateList) {
            double lat, lon, x, y, z;
            lat = (double) g.getY() * Math.PI / 180;
            lon = (double) g.getX() * Math.PI / 180;
            x = Math.cos(lat) * Math.cos(lon);
            y = Math.cos(lat) * Math.sin(lon);
            z = Math.sin(lat);
            X += x;
            Y += y;
            Z += z;
        }
 
        X = X / total;
        Y = Y / total;
        Z = Z / total;
        double Lon = Math.atan2(Y, X);
        double Hyp = Math.sqrt(X * X + Y * Y);
        double Lat = Math.atan2(Z, Hyp);
 
        Map<String, Double> map = new HashMap<>();
        map.put("lng", Lon * 180 / Math.PI);
        map.put("lat", Lat * 180 / Math.PI);
        return map;
    }
    private int windDate(String ids){
        int num = 200;
        if(StringUtils.isEmpty(ids)){
            return num;
        }
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s->Integer.parseInt(s.trim())).collect(Collectors.toList());
        LambdaQueryChainWrapper<MonitorPoint> wrapper = monitorPointService.lambdaQuery();
        wrapper.in(MonitorPoint::getId,list);
        List<MonitorPoint> resultList = wrapper.list();
        if(CollectionUtils.isEmpty(resultList)){
            return num;
        }
        List<String> integerList = resultList.stream().map(s->s.getOrganizationId().toString()).collect(Collectors.toList());
        List<SysDictData> sysDictList = sysDictTypeService.listWindAll(SysDictTypeEnum.WIND_FARM_SCOPE.value,integerList);
        if(CollectionUtils.isEmpty(sysDictList)){
            return num;
        }
        num = sysDictList.stream().mapToInt(s->Integer.parseInt(s.getDataValue())).max().getAsInt();
        return num;
    }
 
    public List<Object> getWindData(List<WindData> windData,Map<String, Object> params) {
        List<GeoCoordinates> points = new ArrayList<>();
        Double loma = 0d;
        Double lomi = 0d;
        Double lama = 0d;
        Double lami = 0d;
        List<Double> loList = new ArrayList<>();
        List<Double> laList = new ArrayList<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        Double U;
        Double V;
        for (WindData data : windData) {
            //经度
            Double longitude = data.getLongitude();
            //纬度
            Double latitude = data.getLatitude();
            GeoCoordinates geoCoordinates = GeoCoordinates.create(longitude, latitude);
            points.add(geoCoordinates);
            loList.add(longitude);
            laList.add(latitude);
 
            Map<String, Object> map = new HashMap<>();
            //风向
            Double windDir = data.getWindDir();
            //风速
            Double windSpeed = data.getWindSpeed();
            if (windDir == null) {
                windDir = 0d;
                windSpeed = 0d;
            }
            double dir = (360.0 - (windDir + 180.0) * Math.PI / 180.0);
            //余弦值
            U = windSpeed * Math.cos(dir);
            //三角正弦值
            V = windSpeed * Math.sin(dir);
            map.put("lo", longitude);
            map.put("la", latitude);
            map.put("U", U);
            map.put("V", V);
            map.put("mac", data.getMac());
            mapList.add(map);
        }
 
        //根据所有多个站点经纬度获取中心点
        Map<String, Double> centerPoint = getCenterPoint(points);
 
        if (loList.size() > 0) {
            loma = Collections.max(loList);
            lomi = Collections.min(loList);
        }
        if (laList.size() > 0) {
            lama = Collections.max(laList);
            lami = Collections.min(laList);
        }
        Map<String, Object> laLaMap = new HashMap<>();
        laLaMap.put("maxLo", loma);
        laLaMap.put("minLo", lomi);
        laLaMap.put("maxLa", lama);
        laLaMap.put("minLa", lami);
 
 
        int perdlen = Objects.nonNull(params.get("perdlen"))?Integer.parseInt(params.get("perdlen").toString()):windDate(Objects.nonNull(params.get("monitorPointList"))?params.get("monitorPointList").toString():null);
        int length = perdlen*10;
 
        Double lo1 = lomi - length * 0.00001141;
       //Double lo1 = lomi - length * 0.000001141 * 2;
        Double lo2 = loma + length * 0.00001141;
       //Double lo2 = loma + length * 0.000001141 * 2;
       //Double la2 = lami - length * 0.000000899 * 2;
        Double la2 = lami - length * 0.00000899;
        //Double la1 = lama + length * 0.000000899 * 2;
        Double la1 = lama + length * 0.00000899;
 
        double dx = 0.00001141 * perdlen;
        double dy = 0.00000899 * perdlen;
 
       //double dx = 0.000001141 * perdlen * 2;
       //double dy = 0.000000899 * perdlen * 2;
 
        int nx = (int) Math.floor((lo2 - lo1) / dx);
        int ny = (int) Math.floor((la1 - la2) / dy);
        int nums = Objects.nonNull(nx*ny)?nx*ny+100:100;
        List<Double> uList = new ArrayList<>(nums);
        List<Double> vList = new ArrayList<>(nums);
 
 
        for (int j = 0; j < nx * ny; j++) {
            uList.add(0.0);
            vList.add(0.0);
        }
        int x;
        int y;
        for (int i = 0; i < mapList.size(); i++) {
            Double lo = (Double) mapList.get(i).get("lo");
            Double la = (Double) mapList.get(i).get("la");
            x = (int) Math.floor((lo - lo1) / dx);
            y = Math.abs((int) Math.floor((la - la1) / dy));
            U = (Double) mapList.get(i).get("U");
            V = (Double) mapList.get(i).get("V");
            // 微动风场
           /* WindListExt windListExt = getWindList(mapList.get(i),loList,laList,500,50);
            if(Objects.nonNull(windListExt)&&Objects.nonNull(windListExt.getUValue()) ){
                U = windListExt.getUValue();
                V = windListExt.getVValue();
            }*/
            for (int j = 0; j < nx * ny; j++) {
                if (i == 0) {
                    if ((y >= 2 && j == (y) * nx + x)) {
                        int k;
                        for (k = j - 2 * nx; k <= j + 2 * nx; k = k + nx) {
                            uList.set(k, U);
                            uList.set(k - 1, U);
                            uList.set(k - 2, U);
                            uList.set(k + 1, U);
                            uList.set(k + 2, U);
                            vList.set(k, V);
                            vList.set(k - 1, V);
                            vList.set(k - 2, V);
                            vList.set(k + 1, V);
                            vList.set(k + 2, V);
                        }
                    }
                } else {
                    if (y >= 1 && j == y * nx + x) {
                        int k;
                        for (k = j - 2 * nx; k <= j + 2 * nx; ) {
                            uList.set(k - 1, U);
                            uList.set(k - 2, U);
                            uList.set(k + 1, U);
                            uList.set(k + 2, U);
                            vList.set(k - 1, V);
                            vList.set(k - 2, V);
                            vList.set(k + 1, V);
                            vList.set(k + 2, V);
                            k = k + nx;
                        }
                        uList.set(j, U);
                        vList.set(j, V);
                    }
                }
            }
        }
        /*WindListExt windList = getWindList(length,perdlen,params);
        for(int i =0; i< windList.getUList().size(); i++){
            Double d = windList.getUList().get(i);
            if(Objects.nonNull(d)&& (!d.equals(0d))){
                uList.set(i,d);
            }
        }
        for(int i =0; i< windList.getVList().size(); i++){
            Double d = windList.getVList().get(i);
            if(Objects.nonNull(d)&& (!d.equals(0d))){
                vList.set(i,d);
            }
        }*/
        /*//结果集
        List<Object> list = new ArrayList<>();
        String uData = "\"" + "data" + "\"" + ": " + uList;
        String vData = "\"" + "data" + "\"" + ": " + vList;
 
        String header1 = "\"" + "header" + "\"" + ": " + "{" + "\"" + "parameterUnit" + "\"" + ": " + "\"" + "m/s" + "\"" + ", " + "\"" + "parameterNumber" + "\"" + ": " + 2 +
                ", " + "\"" + "dx" + "\"" + ": " + dx + ", " + "\"" + "dy" + "\"" + ": " + dy +
                ", " + "\"" + "parameterNumberName" + "\"" + ": " + "\"" + "eastward_wind" + "\"" + ", " + "\"" + "la1" + "\"" + ": " + la1 + ", " + "\"" + "la2" + "\"" + ": " + la2 +
                ", " + "\"" + "parameterCategory" + "\"" + ": " + 2 + ", " + "\"" + "lo1" + "\"" + ": " + lo1 + ", " + "\"" + "lo2" + "\"" + ": " + lo2 +
                ", " + "\"" + "nx" + "\"" + ": " + nx + ", " + "\"" + "ny" + "\"" + ": " + ny + ", " + "\"" + "refTime" + "\"" + ": " + "\"" + "2020-07-22 23:00:00" + "\"" + "}";
 
        String header2 = "\"" + "header" + "\"" + ": " + "{" + "\"" + "parameterUnit" + "\"" + ": " + "\"" + "m/s" + "\"" + ", " + "\"" + "parameterNumber" + "\"" + ": " + 3 +
                ", " + "\"" + "dx" + "\"" + ": " + dx + ", " + "\"" + "dy" + "\"" + ": " + dy +
                ", " + "\"" + "parameterNumberName" + "\"" + ": " + "\"" + "eastward_wind" + "\"" + ", " + "\"" + "la1" + "\"" + ": " + la1 + ", " + "\"" + "la2" + "\"" + ": " + la2 +
                ", " + "\"" + "parameterCategory" + "\"" + ": " + 2 + ", " + "\"" + "lo1" + "\"" + ": " + lo1 + ", " + "\"" + "lo2" + "\"" + ": " + lo2 +
                ", " + "\"" + "nx" + "\"" + ": " + nx + ", " + "\"" + "ny" + "\"" + ": " + ny + ", " + "\"" + "refTime" + "\"" + ": " + "\"" + "2020-07-22 23:00:00" + "\"" + "}";
 
 
        String s1 = "[" + "{" + header1 + ", " + uData + "}" + ", " + "{" + header2 + ", " + vData + "}" + "]";
        //System.out.println(s1);
        JSONArray jsonArray = JSONArray.parseArray(s1);
 
        list.add(jsonArray);
        list.add(centerPoint.get("lng"));
        list.add(centerPoint.get("lat"));
        list.add(laLaMap);*/
 
        Map<String,Object> map = new HashMap<>();
        map.put("uList",uList);
        map.put("vList",vList);
        map.put("dx",dx);
        map.put("dy",dy);
        map.put("nx",nx);
        map.put("ny",ny);
        map.put("la1",la1);
        map.put("la2",la2);
        map.put("lo1",lo1);
        map.put("lo2",lo2);
        map.put("laLaMap",laLaMap);
        return getWindData(map,centerPoint);
    }
 
    private WindListExt getWindList(int length,int perdlen,Map<String, Object> params){
        params.put("time","2023-10-16 04:00");
        String time = params.get("time").toString();
        String timeUnits = DateUtils.stringToDateString(time, "yyyy-MM-dd HH:mm", DateUtils.yyyyMM_EN);
        params.put("timeUnits", timeUnits);
        List<WindData> windData = historyHourlyMapper.getArea(params);
        WindListExt windListExt = new WindListExt();
        List<String> stringList = Arrays.asList("p5dnd7a0245400","p5dnd7a0245472","p5dnd7a0245374","p5dnd7a0245385");
        List<GeoCoordinates> points = new ArrayList<>();
        Double loma = 0d;
        Double lomi = 0d;
        Double lama = 0d;
        Double lami = 0d;
        List<Double> loList = new ArrayList<>();
        List<Double> laList = new ArrayList<>();
        List<Map<String, Object>> mapList = new ArrayList<>();
        Double U = 0d;
        Double V = 0d;
        for (WindData data : windData) {
            //经度
            Double longitude = data.getLongitude();
            //纬度
            Double latitude = data.getLatitude();
            GeoCoordinates geoCoordinates = GeoCoordinates.create(longitude, latitude);
            points.add(geoCoordinates);
            loList.add(longitude);
            laList.add(latitude);
 
            Map<String, Object> map = new HashMap<>();
            //风向
            Double windDir = data.getWindDir();
            //风速
            Double windSpeed = data.getWindSpeed();
            if (windDir == null) {
                windDir = 0d;
                windSpeed = 0d;
            }
            double dir = (360.0 - (windDir + 180.0) * Math.PI / 180.0);
            //余弦值
            U = windSpeed * Math.cos(dir);
            //三角正弦值
            V = windSpeed * Math.sin(dir);
            if(stringList.contains(data.getMac())){
                map.put("lo", longitude);
                map.put("la", latitude);
                map.put("U", U);
                map.put("V", V);
                map.put("mac", data.getMac());
                mapList.add(map);
            }
        }
        if (loList.size() > 0) {
            loma = Collections.max(loList);
            lomi = Collections.min(loList);
        }
        if (laList.size() > 0) {
            lama = Collections.max(laList);
            lami = Collections.min(laList);
        }
        Double lo1 = lomi - length * 0.00001141;
        Double lo2 = loma + length * 0.00001141;
        Double la2 = lami - length * 0.00000899;
        Double la1 = lama + length * 0.00000899;
        double dx = 0.00001141 * perdlen;
        double dy = 0.00000899 * perdlen;
        int nx = (int) Math.floor((lo2 - lo1) / dx);
        int ny = (int) Math.floor((la1 - la2) / dy);
        int nums = Objects.nonNull(nx*ny)?nx*ny+100:100;
        List<Double> uList = new ArrayList<>(nums);
        List<Double> vList = new ArrayList<>(nums);
        for (int j = 0; j < nx * ny; j++) {
            uList.add(0.0);
            vList.add(0.0);
        }
        int x;
        int y;
        for (int i = 0; i < mapList.size(); i++) {
            Double lo = (Double) mapList.get(i).get("lo");
            Double la = (Double) mapList.get(i).get("la");
            x = (int) Math.floor((lo - lo1) / dx);
            y = Math.abs((int) Math.floor((la - la1) / dy));
            U = (Double) mapList.get(i).get("U");
            V = (Double) mapList.get(i).get("V");
 
            for (int j = 0; j < nx * ny; j++) {
                if (i == 0) {
                    if ((y >= 2 && j == (y) * nx + x)) {
                        int k;
                        for (k = j - 2 * nx; k <= j + 2 * nx; k = k + nx) {
                            uList.set(k, U);
                            uList.set(k - 1, U);
                            uList.set(k - 2, U);
                            uList.set(k + 1, U);
                            uList.set(k + 2, U);
                            vList.set(k, V);
                            vList.set(k - 1, V);
                            vList.set(k - 2, V);
                            vList.set(k + 1, V);
                            vList.set(k + 2, V);
                        }
                    }
                } else {
                    if (y >= 1 && j == y * nx + x) {
                        int k;
                        for (k = j - 2 * nx; k <= j + 2 * nx; ) {
                            uList.set(k - 1, U);
                            uList.set(k - 2, U);
                            uList.set(k + 1, U);
                            uList.set(k + 2, U);
                            vList.set(k - 1, V);
                            vList.set(k - 2, V);
                            vList.set(k + 1, V);
                            vList.set(k + 2, V);
                            k = k + nx;
                        }
                        uList.set(j, U);
                        vList.set(j, V);
                    }
                }
            }
        }
        windListExt.setUList(uList);
        windListExt.setVList(vList);
        return windListExt;
    }
 
    private List<Object> getWindData(Map<String,Object> map, Map<String, Double> centerPoint){
        List<Object> list = new ArrayList<>();
        String uData = "\"" + "data" + "\"" + ": " + map.get("uList");
        String vData = "\"" + "data" + "\"" + ": " + map.get("vList");
 
        String header1 = "\"" + "header" + "\"" + ": " + "{" + "\"" + "parameterUnit" + "\"" + ": " + "\"" + "m/s" + "\"" + ", " + "\"" + "parameterNumber" + "\"" + ": " + 2 +
                ", " + "\"" + "dx" + "\"" + ": " + map.get("dx") + ", " + "\"" + "dy" + "\"" + ": " + map.get("dy") +
                ", " + "\"" + "parameterNumberName" + "\"" + ": " + "\"" + "eastward_wind" + "\"" + ", " + "\"" + "la1" + "\"" + ": " + map.get("la1") + ", " + "\"" + "la2" + "\"" + ": " + map.get("la2") +
                ", " + "\"" + "parameterCategory" + "\"" + ": " + 2 + ", " + "\"" + "lo1" + "\"" + ": " + map.get("lo1") + ", " + "\"" + "lo2" + "\"" + ": " + map.get("lo2") +
                ", " + "\"" + "nx" + "\"" + ": " + map.get("nx") + ", " + "\"" + "ny" + "\"" + ": " + map.get("ny") + ", " + "\"" + "refTime" + "\"" + ": " + "\"" + "2020-07-22 23:00:00" + "\"" + "}";
 
        String header2 = "\"" + "header" + "\"" + ": " + "{" + "\"" + "parameterUnit" + "\"" + ": " + "\"" + "m/s" + "\"" + ", " + "\"" + "parameterNumber" + "\"" + ": " + 3 +
                ", " + "\"" + "dx" + "\"" + ": " + map.get("dx") + ", " + "\"" + "dy" + "\"" + ": " + map.get("dy") +
                ", " + "\"" + "parameterNumberName" + "\"" + ": " + "\"" + "eastward_wind" + "\"" + ", " + "\"" + "la1" + "\"" + ": " + map.get("la1") + ", " + "\"" + "la2" + "\"" + ": " + map.get("la2") +
                ", " + "\"" + "parameterCategory" + "\"" + ": " + 2 + ", " + "\"" + "lo1" + "\"" + ": " + map.get("lo1") + ", " + "\"" + "lo2" + "\"" + ": " + map.get("lo2") +
                ", " + "\"" + "nx" + "\"" + ": " + map.get("nx") + ", " + "\"" + "ny" + "\"" + ": " + map.get("ny") + ", " + "\"" + "refTime" + "\"" + ": " + "\"" + "2020-07-22 23:00:00" + "\"" + "}";
 
 
        String s1 = "[" + "{" + header1 + ", " + uData + "}" + ", " + "{" + header2 + ", " + vData + "}" + "]";
        //System.out.println(s1);
        JSONArray jsonArray = JSONArray.parseArray(s1);
 
        list.add(jsonArray);
        list.add(centerPoint.get("lng"));
        list.add(centerPoint.get("lat"));
        list.add(map.get("laLaMap"));
        return list;
    }
 
    /**
     * @Description: 查询一段时间内某一mac的数据
     * @Param: [mac, startDate, endDate]
     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
     * @Author: 陈凯裕
     * @Date: 2021/9/23
     */
    public List<HistoryFiveMinutely> getValueByMacAndTime(String mac, Date startDate, Date endDate) {
        Map<String, Object> mapParams = new HashMap<>();
        mapParams.put("startDate",startDate);
        mapParams.put("endDate",endDate);
        mapParams.put("mac",mac);
        List<String> tableNames = MybatisPLUSUtils.getTableNamesByWrapper(startDate, endDate, SeparateTableType.MONTH);
        List<HistoryFiveMinutely> datas = multiTableQueryResult(mapParams, tableNames);
        return datas;
    }
 
 
    /**
     * @Description: 多表查询,传入表名集合,以及条件wrapper,返回数据
     * @Param: [wrapper, tableNames]
     * @return: java.util.List<com.moral.api.entity.HistoryHourly>
     * @Author: 陈凯裕
     * @Date: 2021/9/23
     */
    private List<HistoryFiveMinutely> multiTableQuery(Map<String, Object> params, List<String> tableNames) {
        List<HistoryFiveMinutely> result = new ArrayList<>();
        for (String tableName : tableNames) {
            params.put("table",tableName);
            List<HistoryFiveMinutely> datas = historyFiveMinutelyMapper.listResultOne(params);
            result.addAll(datas);
        }
        return result;
    }
    private List<HistoryFiveMinutely> multiTableQueryResult(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;
    }
}