puzhibing
2026-05-12 08cc6d5cf7e93b75cfabe7dcc2aeaa9afa1117eb
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
package com.xiaopeng.map.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaopeng.api.dto.WeatherDTO;
import com.xiaopeng.api.dto.map.HistoryPointDto;
import com.xiaopeng.api.dto.map.OrderRouteAndFareDto;
import com.xiaopeng.api.dto.map.RouteAndFareDto;
import com.xiaopeng.api.feignClient.order.XpOrderClient;
import com.xiaopeng.api.model.map.XpGeoFencingPolygon;
import com.xiaopeng.api.model.operation.XpElectronicFenceArea;
import com.xiaopeng.api.vo.map.PointByIpVo;
import com.xiaopeng.api.vo.operation.RouteVo;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.map.mangodb.model.GeoPoint;
import com.xiaopeng.map.mangodb.service.XpGeoFencingPolygonService;
import com.xiaopeng.map.service.XpElectronicFenceAreaService;
import com.xiaopeng.map.utils.AmapWalkingRouteUtil;
import com.xiaopeng.map.utils.GDMapGeocodingUtil;
import com.xiaopeng.unifiedverification.interceptor.XpClientCheck;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
 
@Slf4j
@RestController
@RequestMapping("")
@Api(tags = "地图调用服务")
public class XpMapController {
 
 
    private final GDMapGeocodingUtil gdMapGeocodingUtil;
    private final XpGeoFencingPolygonService xpGeoFencingPolygonService;
    private final RedisService redisService;
    private final XpOrderClient xpOrderClient;
    private final MongoTemplate mongoTemplate;
    @Resource
    private XpElectronicFenceAreaService xpElectronicFenceAreaService;
 
    public XpMapController(GDMapGeocodingUtil gdMapGeocodingUtil, XpGeoFencingPolygonService xpGeoFencingPolygonService, RedisService redisService, XpOrderClient xpOrderClient, MongoTemplate mongoTemplate) {
        this.gdMapGeocodingUtil = gdMapGeocodingUtil;
        this.xpGeoFencingPolygonService = xpGeoFencingPolygonService;
        this.redisService = redisService;
        this.xpOrderClient = xpOrderClient;
        this.mongoTemplate = mongoTemplate;
    }
 
 
    @PostMapping("/base/v1/map/getDistanceAndDurationList")
    public R<List<RouteVo>> getDistanceAndDurationList(@RequestBody RouteAndFareDto dto){
        // 获取电子围栏的禁行区域点位
        GeoPoint startPoint = new GeoPoint(dto.getOrigin().getLongitude(),dto.getOrigin().getLatitude());
        List<String> operatingArea = xpGeoFencingPolygonService.findFencesContainingPoint(startPoint, "operating_area", "points");
        String fenceId = operatingArea.get(0);
 
        List<Document> pointsInFence = xpGeoFencingPolygonService.findPointsByParentId(fenceId, "fenceId", "prohibited_area");
 
        // 避让区域
        ArrayList<JSONObject> pointStrParam = new ArrayList<>();
        try {
            for (Document document : pointsInFence) {
                // 获取points字段(它是GeoJSON对象)
                Document geoJsonDoc = document.get("points", Document.class);
 
                if (geoJsonDoc != null && "Polygon".equals(geoJsonDoc.getString("type"))) {
                    // 获取coordinates数组
                    List<List<List<Double>>> coordinates = (List<List<List<Double>>>) geoJsonDoc.get("coordinates");
 
                    if (coordinates != null && !coordinates.isEmpty()) {
                        // 获取第一个多边形环(通常只有一个)
                        List<List<Double>> polygonPoints = coordinates.get(0);
                        List<String> coordinatesList = new ArrayList<>();
                        // 转换为经纬度字符串
                        for (List<Double> point : polygonPoints) {
                            if (point.size() >= 2) {
                                coordinatesList.add(point.get(0) + "," + point.get(1));
                            }
                        }
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("coordinates", coordinatesList);
                        pointStrParam.add(jsonObject);
                    }
                }
            }
 
            List<HistoryPointDto> history = new ArrayList<>();
            if(null != dto.getVehicleId()){
//                history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//                if(history.size() > 10){
//                    history = history.subList(history.size() - 10, history.size());
//                }
//                history.forEach(s->{
//                    s.setDirection(new BigDecimal(s.getDirection()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//                });
//                redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
            }
            List<RouteVo> routeVo = gdMapGeocodingUtil.getDistanceAndTimeList(dto.getOrigin(), dto.getDestination(), pointStrParam, fenceId,dto.getRoutePlanRule(), history);
            if(routeVo==null || routeVo.isEmpty()){
                return R.fail("没有找到路线");
            }
 
            return R.ok(routeVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail("没有找到路线");
 
 
    }
 
 
 
 
    @PostMapping("/base/v1/map/getDistanceAndDuration")
    public R<RouteVo> getDistanceAndDuration(@RequestBody RouteAndFareDto dto){
        // 获取电子围栏的禁行区域点位
        GeoPoint startPoint = new GeoPoint(dto.getOrigin().getLongitude(),dto.getOrigin().getLatitude());
        List<String> operatingArea = xpGeoFencingPolygonService.findFencesContainingPoint(startPoint, "operating_area", "points");
        String fenceId = operatingArea.get(0);
 
        List<Document> pointsInFence = xpGeoFencingPolygonService.findPointsByParentId(fenceId, "fenceId", "prohibited_area");
 
        // 避让区域
        ArrayList<JSONObject> pointStrParam = new ArrayList<>();
        try {
            for (Document document : pointsInFence) {
                // 获取points字段(它是GeoJSON对象)
                Document geoJsonDoc = document.get("points", Document.class);
 
                if (geoJsonDoc != null && "Polygon".equals(geoJsonDoc.getString("type"))) {
                    // 获取coordinates数组
                    List<List<List<Double>>> coordinates = (List<List<List<Double>>>) geoJsonDoc.get("coordinates");
 
                    if (coordinates != null && !coordinates.isEmpty()) {
                        // 获取第一个多边形环(通常只有一个)
                        List<List<Double>> polygonPoints = coordinates.get(0);
                        List<String> coordinatesList = new ArrayList<>();
                        // 转换为经纬度字符串
                        for (List<Double> point : polygonPoints) {
                            if (point.size() >= 2) {
                                coordinatesList.add(point.get(0) + "," + point.get(1));
                            }
                        }
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("coordinates", coordinatesList);
                        pointStrParam.add(jsonObject);
                    }
                }
            }
 
            List<HistoryPointDto> history = new ArrayList<>();
            if(null != dto.getVehicleId()){
//                history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//                if(history.size() > 10){
//                    history = history.subList(history.size() - 10, history.size());
//                }
//                history.forEach(s->{
//                    s.setDirection(new BigDecimal(s.getDirection()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//                });
//                redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
            }
            RouteVo routeVo = gdMapGeocodingUtil.getDistanceAndTime(dto.getOrigin(), dto.getDestination(), pointStrParam, fenceId,dto.getRoutePlanRule(), history);
            if(routeVo==null){
                return R.fail("没有找到路线");
            }
 
            return R.ok(routeVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail("没有找到路线");
 
 
    }
 
 
    /**
     * 获取订单距离和时长
     * @param dto
     * @return
     */
    @PostMapping("/base/v1/map/getOrderDistanceAndDuration")
    public R<RouteVo> getOrderDistanceAndDuration(@Validated @RequestBody OrderRouteAndFareDto dto){
        // 获取电子围栏的禁行区域点位
        GeoPoint startPoint = new GeoPoint(dto.getOrigin().getLongitude(), dto.getOrigin().getLatitude());
        List<String> operatingArea = xpGeoFencingPolygonService.findFencesContainingPoint(startPoint, "operating_area", "points");
        // 避让区域
        ArrayList<JSONObject> pointStrParam = new ArrayList<>();
        String fenceId = null;
        if(!operatingArea.isEmpty()){
            fenceId = operatingArea.get(0);
            List<XpElectronicFenceArea> list = xpElectronicFenceAreaService.list(new QueryWrapper<XpElectronicFenceArea>().eq("electronic_id", fenceId).eq("is_deleted", 0).eq("area_type", 2));
            for (XpElectronicFenceArea xpElectronicFenceArea : list) {
                String fencePoints = xpElectronicFenceArea.getFencePoints();
                List<String> coordinatesList = new ArrayList<>();
                for (String s : fencePoints.split(",")) {
                    coordinatesList.add(s.replace("_", ","));
                }
 
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("coordinates", coordinatesList);
                pointStrParam.add(jsonObject);
            }
        }
        List<HistoryPointDto> history = new ArrayList<>();
        if(null != dto.getVehicleId()){
//            history = redisService.getCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId());
//            if(history.size() > 10){
//                history = history.subList(history.size() - 10, history.size());
//            }
//            history.forEach(s->{
//                s.setDirection(new BigDecimal(s.getDirection()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
//            });
//            redisService.setCacheList("VEHICLE_HISTORY_POINT_LIST:" + dto.getVehicleId(), history);
        }
        RouteVo routeVo = gdMapGeocodingUtil.getDistanceAndTime(dto.getOrigin(), dto.getDestination(), pointStrParam, fenceId,dto.getRoutePlanRule(), history);
        if(routeVo==null){
            return R.fail("没有找到路线");
        }
        return R.ok(routeVo);
    }
 
    /**
     * 根据导航ID获取车端需要的导航数据
     * @param routedID
     * @return
     */
    @PostMapping("/base/v1/map/queryRouted")
    public R<String> getOrderDistanceAndDuration(@RequestParam("routedID") String routedID){
        return R.ok(gdMapGeocodingUtil.routed(routedID));
    }
 
 
    /**
     * 根据ip获取点位
     */
    @ApiOperation(value = "根据ip获取点位", tags = {"小程序-站点导航"})
    @GetMapping("/base/v1/mini-program/point/getPointByIp")
    @XpClientCheck(checkToken = false, checkOtpToken = true)
    public R<PointByIpVo> getPointByIp(HttpServletRequest request) throws IOException {
        String ip = request.getRemoteAddr();
        PointByIpVo pointByIpVo = AmapWalkingRouteUtil.ipLocation(ip);
        return R.ok(pointByIpVo);
    }
 
 
 
    @Value("${map.urls.weather:https://restapi.amap.com/v3/weather/weatherInfo?key=%s&city=%s}")
    String WEATHER_URL;
 
    @Autowired
    ObjectMapper om;
 
    @Value("${map.apikey:66ae577f63898dae3016d599fda57221}")
    String key;
 
    @ApiOperation(value = "获取天气信息", tags = {"内部-调价"})
    @PostMapping("/logan/inner/weatherInfo")
    public R<WeatherDTO> wether(@RequestParam("cityCode") String cityCode) {
        try{
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(String.format(WEATHER_URL, key, cityCode));
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String resp = EntityUtils.toString(entity, StandardCharsets.UTF_8);
                log.info("天气预报查询结果:{}", resp);
                return R.ok(om.readValue(resp, WeatherDTO.class));
            }
            return R.fail("获取天气信息失败");
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail("获取天气信息失败");
    }
 
 
}