liujie
2026-06-09 c96fbe6b0aeb413a17c429133ee6d81d138144b9
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
package com.xiaopeng.maintenance.service;
 
import com.xiaopeng.api.vo.automaticPilot.VehiclePosition;
import com.xiaopeng.maintenance.utils.GDMapGeocodingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@Service
public class VehicleLocationFillService {
 
    private static final String DEFAULT_VEHICLE_LOCATION = "四川省成都市双流区西航港街道机场路近都段辅路空港16区";
    private static final BigDecimal DEFAULT_VEHICLE_LON = new BigDecimal("104.001118");
    private static final BigDecimal DEFAULT_VEHICLE_LAT = new BigDecimal("30.575685");
 
    @Autowired
    private MongoTemplate mongoTemplate;
 
    @Autowired
    private GDMapGeocodingUtil gdMapGeocodingUtil;
 
    public Map<String, VehiclePosition> loadPositionByVin(Collection<String> vins) {
        if (CollectionUtils.isEmpty(vins)) {
            return Collections.emptyMap();
        }
        List<String> distinctVins = vins.stream()
                .filter(StringUtils::hasLength)
                .distinct()
                .collect(java.util.stream.Collectors.toList());
        if (CollectionUtils.isEmpty(distinctVins)) {
            return Collections.emptyMap();
        }
        try {
            Query query = new Query(Criteria.where("vin").in(distinctVins));
            List<VehiclePosition> positions = mongoTemplate.find(query, VehiclePosition.class);
            if (CollectionUtils.isEmpty(positions)) {
                return Collections.emptyMap();
            }
            Map<String, VehiclePosition> map = new HashMap<>();
            for (VehiclePosition position : positions) {
                if (position == null || !StringUtils.hasLength(position.getVin())) {
                    continue;
                }
                map.putIfAbsent(position.getVin(), position);
            }
            return map;
        } catch (Exception e) {
            log.error("查询车辆实时定位失败 vins={}", vins, e);
            return Collections.emptyMap();
        }
    }
 
    public void fillFromVin(com.xiaopeng.api.model.maintenance.XpTask task,
                            String vin,
                            Map<String, VehiclePosition> positionByVin) {
        if (task == null) {
            return;
        }
        if (!StringUtils.hasLength(vin)) {
            applyDefault(task);
            return;
        }
        VehiclePosition vehiclePosition = positionByVin == null ? null : positionByVin.get(vin);
        fillFromPosition(task, vin, vehiclePosition);
    }
 
    public void fillFromPosition(com.xiaopeng.api.model.maintenance.XpTask task,
                                 String vin,
                                 VehiclePosition vehiclePosition) {
        if (task == null) {
            return;
        }
        if (vehiclePosition == null || vehiclePosition.getPoint() == null) {
            applyDefault(task);
            return;
        }
 
        double lon = vehiclePosition.getPoint().getX();
        double lat = vehiclePosition.getPoint().getY();
        task.setVehicleLon(BigDecimal.valueOf(lon));
        task.setVehicleLat(BigDecimal.valueOf(lat));
 
        try {
            Map<String, String> geocode = gdMapGeocodingUtil.geocode(String.valueOf(lon), String.valueOf(lat));
            String address = geocode == null ? null : geocode.get("address");
            if (StringUtils.hasLength(address)) {
                task.setVehicleLocation(address);
                return;
            }
            log.warn("解析车辆地址为空 vin={}, lon={}, lat={}", vin, lon, lat);
            task.setVehicleLocation(DEFAULT_VEHICLE_LOCATION);
        } catch (Exception e) {
            log.error("解析车辆地址失败 vin={}, lon={}, lat={}", vin, lon, lat, e);
            task.setVehicleLocation(DEFAULT_VEHICLE_LOCATION);
        }
    }
 
    private void applyDefault(com.xiaopeng.api.model.maintenance.XpTask task) {
        task.setVehicleLocation(DEFAULT_VEHICLE_LOCATION);
        task.setVehicleLon(DEFAULT_VEHICLE_LON);
        task.setVehicleLat(DEFAULT_VEHICLE_LAT);
    }
}