jinpengyong
2021-08-26 3be7bd55ff0a4ab2ed25b46cdfd1dede92300ea3
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.SpecialDevice;
import com.moral.api.entity.SpecialDeviceHistory;
import com.moral.api.mapper.HistorySecondSpecialMapper;
import com.moral.api.mapper.SpecialDeviceMapper;
import com.moral.api.service.SpecialDeviceHistoryService;
import com.moral.api.service.SpecialDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.GeodesyUtils;
import com.moral.util.TokenUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-08-18
 */
@Service
public class SpecialDeviceServiceImpl extends ServiceImpl<SpecialDeviceMapper, SpecialDevice> implements SpecialDeviceService {
 
    @Autowired
    private HistorySecondSpecialMapper historySecondSpecialMapper;
 
    @Autowired
    private SpecialDeviceHistoryService specialDeviceHistoryService;
 
    private final static Double dis = 50d;
 
    @Override
    public List<Map<String, Object>> getCarsInfo() {
        //获取当前用户信息
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
        QueryWrapper<SpecialDeviceHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("mac", "name")
                .eq("organization_id", orgId)
                .eq("special_type", Constants.SPECIAL_DEVICE_CAR)
                .eq("is_delete", Constants.NOT_DELETE);
        return specialDeviceHistoryService.listMaps(queryWrapper);
    }
 
    @Override
    public List<Map<String, Object>> carTrajectory(Map<String, Object> params) {
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
        Integer orgId = (Integer) orgInfo.get("id");
        params.put("orgId", orgId);
        //从秒数据表获取走航车数据
        List<Map<String, Object>> data = historySecondSpecialMapper.getSpecialDeviceData(params);
        if (ObjectUtils.isEmpty(data)) {
            return data;
        }
        data.removeIf(o -> {
            Map<String, Object> value = JSONObject.parseObject(o.remove("value").toString(), Map.class);
            Object flylon = value.get("flylon");
            Object flylat = value.get("flylat");
            if (ObjectUtils.isEmpty(flylon) || ObjectUtils.isEmpty(flylat)) {
                return true;
            }
            double lon = Double.parseDouble(flylon.toString());
            double lat = Double.parseDouble(flylat.toString());
            if (lon < 70 || lon > 150 || lat < 20 || lat > 60) {
                return true;
            }
            o.putAll(value);
            return false;
        });
        return filterData(data);
    }
 
    //根据距离筛选数据
    private List<Map<String, Object>> filterData(List<Map<String, Object>> data) {
        List<Map<String, Object>> result = new ArrayList<>();
        result.add(data.remove(0));
        for (Map<String, Object> map : data) {
            boolean flag = true;
            for (Map<String, Object> resultMap : result) {
                double lng1 = Double.parseDouble(map.get(Constants.SENSOR_CODE_LON).toString());
                double lat1 = Double.parseDouble(map.get(Constants.SENSOR_CODE_LAT).toString());
                double lng2 = Double.parseDouble(resultMap.get(Constants.SENSOR_CODE_LON).toString());
                double lat2 = Double.parseDouble(resultMap.get(Constants.SENSOR_CODE_LAT).toString());
                double distance = GeodesyUtils.getDistance(lat1, lng1, lat2, lng2);
                if (distance < dis) {
                    flag = false;
                }
            }
            if (flag) {
                result.add(map);
            }
        }
        return result;
    }
}