fix
Helius
2021-11-10 c4d8357ad30bb39968279957d1fb594ef8dfdf5f
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
package com.xzx.gc.order.service;
 
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.ExceptionEnum;
import com.xzx.gc.common.constant.OrderEnum;
import com.xzx.gc.common.constant.RedisKeyConstant;
import com.xzx.gc.common.dto.gdmap.GdReverseGEODto;
import com.xzx.gc.common.dto.gdmap.UploadTraceDto;
import com.xzx.gc.common.dto.gdmap.UploadTraceLocationDto;
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.common.utils.gdmap.GdTraceUtil;
import com.xzx.gc.entity.CityPartner;
import com.xzx.gc.entity.OtherUserInfo;
import com.xzx.gc.entity.PartnerTrace;
import com.xzx.gc.entity.UserTraceInfo;
import com.xzx.gc.order.dto.TraceAllDto;
import com.xzx.gc.order.dto.TraceLocationDto;
import com.xzx.gc.order.dto.TraceUploadDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
 
 
@Service
@Transactional
@Slf4j
public class TraceService {
 
    @Autowired
    private PartnerTraceService partnerTraceService;
 
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private OtherUserService otherUserService;
 
    @Autowired
    private UserTraceInfoService userTraceInfoService;
 
 
    public Result upload(TraceAllDto traceAllDto){
        List<TraceUploadDto> list = traceAllDto.getList();
 
        String userType=null;
        String type=traceAllDto.getType();
        //兼容原来的接口 自定义负值类型
        if(OrderEnum.回收员全天轨迹.getValue().equals(type)){
            userType=Constants.DEFAULT_ID;
        }
 
 
        String userId = traceAllDto.getUserId();
        //删除轨迹为空的数据
        for (Iterator<TraceUploadDto> iterator = list.iterator(); iterator.hasNext(); ) {
            TraceUploadDto traceUploadDto=iterator.next();
            if(StrUtil.isBlank(traceUploadDto.getTrids())){
                iterator.remove();
            }
        }
 
        //删除轨迹点为空的数据并处理经纬度
        if(CollUtil.isNotEmpty(list)){
            for (Iterator<TraceUploadDto> iterator = list.iterator(); iterator.hasNext(); ) {
                TraceUploadDto traceUploadDto =  iterator.next();
                List<TraceLocationDto> locations = traceUploadDto.getLocations();
                for (Iterator<TraceLocationDto> iterator2 = locations.iterator(); iterator2.hasNext(); ) {
                    TraceLocationDto next =  iterator2.next();
                    if(StrUtil.isBlank(next.getLatitude())||StrUtil.isBlank(next.getLongitude())){
                        iterator2.remove();
                    }else {
                        next.setLatitude(NumberUtil.roundStr(next.getLatitude(), 6, RoundingMode.DOWN));
                        next.setLongitude(NumberUtil.roundStr(next.getLongitude(), 6, RoundingMode.DOWN));
                    }
                }
 
                if(CollUtil.isEmpty(locations)){
                    iterator.remove();
                }
            }
        }
 
        String redisKey=RedisKeyConstant.TRACE_ERROR;
        //轨迹错误次数
        int errNum=3;
        if(CollUtil.isNotEmpty(list)){
 
            //更新回收员的实时轨迹
            if(OrderEnum.回收员全天轨迹.getValue().equals(type)) {
                List<TraceLocationDto> locationList = new ArrayList<>();
                for (TraceUploadDto traceUploadDto : list) {
                    locationList.addAll(traceUploadDto.getLocations());
                }
                TraceLocationDto traceLocationDto1 = locationList.stream().sorted(Comparator.comparing(TraceLocationDto::getTime).reversed()).collect(Collectors.toList()).get(0);
                OtherUserInfo otherUserInfo = new OtherUserInfo();
                otherUserInfo.setUserId(userId);
                otherUserInfo.setLatitude(traceLocationDto1.getLatitude());
                otherUserInfo.setLongitude(traceLocationDto1.getLongitude());
                otherUserService.updateById(otherUserInfo);
            }
 
            PartnerTrace partnerTrace = partnerTraceService.findByUserIdAndType(userId, userType);
            if (partnerTrace != null) {
                String key = partnerTrace.getPartnerKey();
                String sid = partnerTrace.getPartnerServiceId();
 
                OtherUserInfo byId = partnerTrace.getOtherUserInfo();
                String tid;
                if(OrderEnum.回收员全天轨迹.getValue().equals(type)){
                    tid=byId.getTerminalDayId();
                }else {
                    tid = byId.getTerminalId();
                }
 
                for (TraceUploadDto traceUploadDto : list) {
                    String trid = traceUploadDto.getTrids();
                    String redisField=userId+"_"+trid;
                    String hget = redisUtil.hget(redisKey, redisField);
                    if(StrUtil.isNotBlank(hget)&&Convert.toInt(hget)>=errNum){
                        continue;
                    }
                    UploadTraceDto uploadTraceDto = new UploadTraceDto();
                    uploadTraceDto.setKey(key);
                    uploadTraceDto.setSid(sid);
                    uploadTraceDto.setTid(tid);
                    uploadTraceDto.setTrid(trid);
                    JSONArray jsonArray = new JSONArray();
                    List<TraceLocationDto> locations = traceUploadDto.getLocations();
 
                    //处理相邻的相同的点只传最新的那个
                    List<TraceLocationDto> collect = locations.stream().sorted(Comparator.comparing(TraceLocationDto::getTime)).collect(Collectors.toList());
                    List<TraceLocationDto> list2 = new ArrayList<>();
                    list2.add(collect.get(0));
                    for(int i=1;i<collect.size();i++) {
 
                        TraceLocationDto trackDetailResultDto1 = collect.get(i);
                        String address = trackDetailResultDto1.getAddress();
 
                        //取list集合的最后1个进行地址比较
                        TraceLocationDto trackDetailResultDto2 = list2.get(list2.size() - 1);
 
                        String address1 = trackDetailResultDto2.getAddress();
 
                        if (!address.equals(address1)) {
                            list2.add(trackDetailResultDto1);
                        } else {
                            BeanUtil.copyProperties(trackDetailResultDto1,trackDetailResultDto2);
                        }
                    }
 
                    //上传轨迹点
                    for (TraceLocationDto traceLocationDto : list2) {
                        UploadTraceLocationDto uploadTraceLocationDto = new UploadTraceLocationDto();
                        String location = traceLocationDto.getLongitude() + "," + traceLocationDto.getLatitude();
                        uploadTraceLocationDto.setLocation(location);
                        uploadTraceLocationDto.setLocatetime(traceLocationDto.getTime());
                        JSONObject jsonObject = new JSONObject();
                        GdReverseGEODto gdReverseGEODto = new GdReverseGEODto();
                        gdReverseGEODto.setLocation(location);
                        jsonObject.put("address",traceLocationDto.getAddress());
                        uploadTraceLocationDto.setProps(JSONUtil.toJsonStr(jsonObject));
                        jsonArray.add(JSONUtil.parseObj(uploadTraceLocationDto));
                    }
 
                    if (jsonArray.size() > 0) {
                        uploadTraceDto.setPoints(JSONUtil.toJsonStr(jsonArray));
                        Result result = GdTraceUtil.uploadTrace(uploadTraceDto);
                        if(result.getCode()!=0){
                            if(StrUtil.isNotBlank(hget)){
                                redisUtil.hset(redisKey,redisField,Convert.toStr(Convert.toInt(hget)+1));
                            }else {
                                redisUtil.hset(redisKey,redisField,Convert.toStr(1));
                            }
                            log.warn("轨迹上传失败,ID:{},原因:{}",trid,result.getMsg());
                        }else {
                            if(StrUtil.isNotBlank(hget)) {
                                redisUtil.hdel(redisKey, redisField);
                            }
                        }
                    }
                }
 
            }
        }else {
            return Result.error(Convert.toInt(ExceptionEnum.可忽略异常.getValue()),"轨迹数据为空");
        }
        return Result.success();
    }
 
    /**
     * 修复高德服务
     */
//    public void fixService() {
//        List<CityPartner> byType = cityPartnerService.findByType(OrderEnum.合伙人.getValue());
//        int i=0;
//        for (CityPartner cityPartner : byType) {
//            i++;
//            String partnerId=cityPartner.getId().toString();
//            String key=cityPartner.getPartnerKey();
//            if(i==1) {
//                //只删一次 因为同一账号下的 Key可以查出所有service。
//                Result<JSONArray> service = GdTraceUtil.findService(key);
//                if (service.getCode() == 0) {
//                    JSONArray data = service.getData();
//                    for (Object result : data) {
//                        JSONObject jsonObject1= (JSONObject) result;
//                        String sid = jsonObject1.getStr("sid");
//                        String name = jsonObject1.getStr("name");
//                        GdTraceUtil.deleteService(key, sid);
//                    }
//
//                }
//            }
//            List<PartnerTrace> byPartnerId1 = partnerTraceService.findByPartnerId(partnerId);
//            List<OtherUserInfo> byPartnerId = otherUserService.findByPartnerId(partnerId);
//            for (PartnerTrace partnerTrace : byPartnerId1) {
//                Short partnerServiceType = partnerTrace.getPartnerServiceType();
//                if(1==Convert.toInt(partnerServiceType)){
//                    fixTerminalId(key,OrderEnum.回收员订单轨迹,partnerTrace,byPartnerId);
//                }else if(2==Convert.toInt(partnerServiceType)){
//                    fixTerminalId(key,OrderEnum.推广员全天轨迹,partnerTrace,byPartnerId);
//                }else if(3==Convert.toInt(partnerServiceType)){
//                    fixTerminalId(key,OrderEnum.回收员全天轨迹,partnerTrace,byPartnerId);
//                }
//
//            }
//        }
//
//        log.debug("修复高德服务成功");
//    }
 
 
    private void fixTerminalId(String key,OrderEnum orderEnum,PartnerTrace partnerTrace, List<OtherUserInfo> byPartnerId){
 
        String extra="";
        Result service = GdTraceUtil.createService(key, key+"_"+orderEnum.name());
        if (service.getCode() == 0) {
            String serviceId = service.getData().toString();
            //创建自定义字段
            GdTraceUtil.createTraceProp(key,serviceId,"address","string");
            //更新服务id
            PartnerTrace obj=new PartnerTrace();
            obj.setId(partnerTrace.getId());
            obj.setPartnerServiceId(serviceId);
            partnerTraceService.updateById(obj);
 
            String userType;
            if(orderEnum.getValue().equals("1")||orderEnum.getValue().equals("3")){
                userType="2";
            }else{
                userType = "6";
            }
 
 
 
            for (OtherUserInfo otherUserInfo : byPartnerId) {
 
                if(otherUserInfo.getUserType().equals(userType)) {
 
                    Result<String> terminal = GdTraceUtil.createTerminal(key, serviceId, otherUserInfo.getUserId() + extra);
                    if (terminal.getCode() == 0) {
                        String termianlId = terminal.getData();
                        OtherUserInfo obj2 = new OtherUserInfo();
                        obj2.setUserId(otherUserInfo.getUserId());
                        if (orderEnum.getValue().equals("1") || orderEnum.getValue().equals("2")) {
                            obj2.setTerminalId(termianlId);
                        } else if (orderEnum.getValue().equals("3")) {
                            obj2.setTerminalDayId(termianlId);
 
                            Result<String> trace = GdTraceUtil.createTrace(key, serviceId, termianlId);
                            if(trace.getCode()==0){
                                String data = trace.getData();
                                UserTraceInfo byUserAndDate = userTraceInfoService.findByUserAndDate(otherUserInfo.getUserId(), "2020-07-04");
                                if(byUserAndDate!=null) {
                                    byUserAndDate.setKeyId(key);
                                    byUserAndDate.setServiceId(serviceId);
                                    byUserAndDate.setTraceId(data);
                                    byUserAndDate.setTerminalId(termianlId);
                                    userTraceInfoService.updateById(byUserAndDate);
                                }else {
                                    byUserAndDate=new UserTraceInfo();
                                    byUserAndDate.setUserId(otherUserInfo.getUserId());
                                    byUserAndDate.setKeyId(key);
                                    byUserAndDate.setServiceId(serviceId);
                                    byUserAndDate.setTraceId(data);
                                    byUserAndDate.setTerminalId(termianlId);
                                    userTraceInfoService.add(byUserAndDate);
 
                                }
                            }
                        }
 
                        otherUserService.updateById(obj2);
                    }
                }
            }
 
        }
    }
 
 
}