jinpengyong
2023-10-30 14949a6a6e7dec654d13e75f46b24319a624ff8b
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
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.HistorySecondCruiser;
import com.moral.api.entity.ManageCoordinateDetail;
import com.moral.api.entity.Sensor;
import com.moral.api.entity.SpecialDevice;
import com.moral.api.mapper.HistorySecondCruiserMapper;
import com.moral.api.mapper.ManageCoordinateDetailMapper;
import com.moral.api.mapper.SpecialDeviceMapper;
import com.moral.api.pojo.dto.cruiser.CruiserDTO;
import com.moral.api.service.DeviceService;
import com.moral.api.service.HistorySecondCruiserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.SpecialDeviceService;
import com.moral.api.util.RoadUtils;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 走航车秒数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-09-02
 */
@Slf4j
@Service
public class HistorySecondCruiserServiceImpl extends ServiceImpl<HistorySecondCruiserMapper, HistorySecondCruiser> implements HistorySecondCruiserService {
 
    @Autowired
    private HistorySecondCruiserMapper historySecondCruiserMapper;
 
    @Autowired
    private SpecialDeviceService specialDeviceService;
    @Autowired
    private SpecialDeviceMapper specialDeviceMapper;
    @Autowired
    private ManageCoordinateDetailMapper manageCoordinateDetailMapper;
 
    @Autowired
    private RedisTemplate redisTemplate;
 
    @Override
    public void insertHistorySecond(Map<String, Object> data) {
        Date batchTime = DateUtils.getDate((String) data.remove(Constants.SENSOR_CODE_SPECIAL_BATCH), DateUtils.yyyyMMddHHmmss_EN);
        Date time = DateUtils.getDate((String) data.get("DataTime"), DateUtils.yyyyMMddHHmmss_EN);
        String mac = data.get("mac").toString();
 
        //根据mac从redis中获取因子
        SpecialDevice specialDevice = (SpecialDevice) redisTemplate.opsForHash().get(RedisConstants.SPECIAL_DEVICE_INFO, mac);
        List<Sensor> sensors = specialDevice.getVersion().getSensors();
        //过滤因子
        data = data.entrySet().stream()
                .filter(m -> {
                    boolean flag = false;
                    String key = m.getKey();
                    if (!"mac".equals(key) && !"DataTime".equals(key)) {
                        for (Sensor sensor : sensors) {
                            if (sensor.getCode().equals(key)) {
                                flag = true;
                                break;
                            }
                        }
                        return flag;
                    }
                    return true;
                }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
 
        //数据校准
        data = specialDeviceService.adjustSpecialDeviceData(data);
 
        HistorySecondCruiser historySecondCruiser = new HistorySecondCruiser();
        historySecondCruiser.setMac(mac);
        historySecondCruiser.setTime(time);
        historySecondCruiser.setValue(JSONObject.toJSONString(data));
        historySecondCruiser.setOrganizationId(specialDevice.getOrganizationId());
        historySecondCruiser.setBatch(batchTime);
        historySecondCruiserMapper.insert(historySecondCruiser);
    }
 
    /**
     * 获取所有走航车信息
     * @return
     */
    @Override
    public List<SpecialDevice> selectCruisers() {
        QueryWrapper<SpecialDevice> wrapper = new QueryWrapper<>();
        wrapper.select("name","mac");
        wrapper.eq("is_delete",Constants.NOT_DELETE);
        List<SpecialDevice> specialDevices = specialDeviceMapper.selectList(wrapper);
        return specialDevices;
    }
 
    /**
     * 查看走航车轨迹
     * @param params
     * @return
     */
    @Override
    public Map<String,Object> getCruiserInFo(Map<String, Object> params) {
        HashMap<String, Object> map = new HashMap<>();
        String mac = params.get("mac").toString();
        String time1 = params.get("time1").toString();
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        ArrayList<Object> list = new ArrayList<>();
        List<CruiserDTO> data = (List<CruiserDTO>) redisTemplate.opsForHash().get(RedisConstants.DATE_COORDINATE, mac + time1);
//        List<CruiserDTO> data = (List<CruiserDTO>) redisTemplate.opsForValue().get(mac + time1);
        if (ObjectUtils.isEmpty(data)){
            data = historySecondCruiserMapper.getCruiserInfo(params);
            data = data.stream().distinct().collect(Collectors.toList());
            data.removeIf(o->{
                if (ObjectUtils.isEmpty(o.getFlyLon()) || ObjectUtils.isEmpty(o.getFlyLat())) {
                    return true;
                }
                double lon = o.getFlyLon();
                double lat = o.getFlyLat();
                double[] doubles = RoadUtils.transformWGS84ToBD09(lon, lat);
                Matcher matcher = Pattern.compile("\\d*\\.\\d{8}").matcher(""+doubles[0]);
                matcher.find();
                String s = matcher.group();
                Matcher matcher1 = Pattern.compile("\\d*\\.\\d{8}").matcher(""+doubles[1]);
                matcher1.find();
                String s1 = matcher1.group();
                o.setFlyLon(Double.parseDouble(s));
                o.setFlyLat(Double.parseDouble(s1));
                o.setData(lon+"_"+lat);
//            o.setFlyLon(doubles[0]);
//            o.setFlyLat(doubles[1]);
                if (lon < 70 || lon > 150 || lat < 20 || lat > 60) {
                    return true;
                }
                return false;
            });
 
            redisTemplate.opsForHash().put(RedisConstants.DATE_COORDINATE,mac+time1,data);
            //设置过期时间
            redisTemplate.opsForHash().getOperations().expire(RedisConstants.DATE_COORDINATE,600, TimeUnit.SECONDS);
 
        }
 
//        redisTemplate.opsForValue().set(mac+time1,rsCruiser,600, TimeUnit.SECONDS);
 
        map.put("rsData",data);
        map.put("data",list);
        log.info(data.size()+"");
        log.info(data.size()+"");
        return map;
    }
 
    @Override
    public Map<String, Object> getCruiserCompare(Map<String, Object> params) {
        HashMap<String,Object> rsMap = new HashMap<>();
        String mac = params.get("mac").toString();
        String time1 = params.get("time1").toString();
        ArrayList<ManageCoordinateDetail> rsList1 = new ArrayList<>();
        params.put("dateFormat", "%Y-%m-%d %H:%i:%s");
        List<ManageCoordinateDetail> manageCoordinateDetails = manageCoordinateDetailMapper.selectList(null);
        List<CruiserDTO> cruiserInfo = (List<CruiserDTO>) redisTemplate.opsForHash().get(RedisConstants.DATE_COORDINATE, mac + time1);
 
        if (ObjectUtils.isEmpty(cruiserInfo)){
            cruiserInfo = historySecondCruiserMapper.getCruiserInfo(params);
            cruiserInfo = cruiserInfo.stream().distinct().collect(Collectors.toList());
            cruiserInfo.removeIf(o->{
                if (ObjectUtils.isEmpty(o.getFlyLon()) || ObjectUtils.isEmpty(o.getFlyLat())) {
                    return true;
                }
                double lon = o.getFlyLon();
                double lat = o.getFlyLat();
                double[] doubles = RoadUtils.transformWGS84ToBD09(lon, lat);
                Matcher matcher = Pattern.compile("\\d*\\.\\d{8}").matcher(""+doubles[0]);
                matcher.find();
                String s = matcher.group();
                Matcher matcher1 = Pattern.compile("\\d*\\.\\d{8}").matcher(""+doubles[1]);
                matcher1.find();
                String s1 = matcher1.group();
                o.setFlyLon(Double.parseDouble(s));
                o.setFlyLat(Double.parseDouble(s1));
                o.setData(lon+"_"+lat);
//            o.setFlyLon(doubles[0]);
//            o.setFlyLat(doubles[1]);
                if (lon < 70 || lon > 150 || lat < 20 || lat > 60) {
                    return true;
                }
                return false;
            });
 
            redisTemplate.opsForHash().put(RedisConstants.DATE_COORDINATE,mac+time1,cruiserInfo);
            //设置过期时间
            redisTemplate.opsForHash().getOperations().expire(RedisConstants.DATE_COORDINATE,6000, TimeUnit.SECONDS);
 
        }
//        List<CruiserDTO> cruiserInfo = historySecondCruiserMapper.getCruiserInfo(params);
        log.info(""+cruiserInfo.size());
        if (ObjectUtils.isEmpty(manageCoordinateDetails)){
            rsMap.put("rsData",cruiserInfo);
            rsMap.put("data",rsList1);
            return rsMap;
        }
        for (int i=cruiserInfo.size()-1;i>0;i--) {
            CruiserDTO cruiserDTO = cruiserInfo.get(i);
            String data = cruiserDTO.getData();
            String[] rs = data.split("_");
            String flyLon = rs[0];
            String flyLat = rs[1];
//            double lonDouble = Double.parseDouble(flyLon);
//            double latDouble = Double.parseDouble(flyLat);
            for (ManageCoordinateDetail manageCoordinateDetail : manageCoordinateDetails) {
                String latitude = manageCoordinateDetail.getLatitude().toString();
                String longitude = manageCoordinateDetail.getLongitude().toString();
//                double lonDouble1 = Double.parseDouble(longitude);
//                double latDouble1 = Double.parseDouble(latitude);
//                if (latDouble1==latDouble && lonDouble==lonDouble1){
                    if (flyLon.equals(longitude) && flyLat.equals(latitude)){
                    manageCoordinateDetail.setCode(data);
                    manageCoordinateDetail.setLongitude(cruiserDTO.getFlyLon());
                    manageCoordinateDetail.setLatitude(cruiserDTO.getFlyLat());
                    rsList1.add(manageCoordinateDetail);
                    cruiserInfo.remove(i);
                    break;
                }
            }
 
        }
        rsMap.put("rsData",cruiserInfo);
        rsMap.put("data",rsList1);
        rsMap.put("message","有"+cruiserInfo.size()+"个不在里面,"+"一共有"+(cruiserInfo.size()+rsList1.size())+"个");
        return rsMap;
    }
 
 
}