| New file |
| | |
| | | package com.xiaopeng.itinerary.utils.mango; |
| | | |
| | | import com.xiaopeng.api.vo.automaticPilot.VehiclePosition; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.data.geo.GeoResults; |
| | | import org.springframework.data.geo.Metrics; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | | import org.springframework.data.mongodb.core.query.NearQuery; |
| | | import org.springframework.data.mongodb.core.query.Query; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | public class VehiclePositionService { |
| | | |
| | | @Resource |
| | | private MongoTemplate mongoTemplate; |
| | | |
| | | /** |
| | | * 查询附近指定距离内且状态正常的车辆 |
| | | * @param longitude 经度 |
| | | * @param latitude 纬度 |
| | | * @param distanceInMeters 距离(米) |
| | | * @return 符合条件的车辆列表 |
| | | */ |
| | | public List<VehiclePosition> findAvailableVehiclesNearby(double longitude, |
| | | double latitude, |
| | | double distanceInMeters,int num) { |
| | | try { |
| | | // 1. 创建查询点 |
| | | Point referencePoint = new Point(longitude, latitude); |
| | | |
| | | // 2. 构建 NearQuery - 完全使用 NearQuery 的 API |
| | | NearQuery nearQuery = NearQuery.near(referencePoint, Metrics.KILOMETERS) |
| | | .maxDistance(distanceInMeters / 1000) // 转成公里 |
| | | .spherical(true); |
| | | |
| | | |
| | | // 4. 执行查询 - 明确指定集合名 |
| | | GeoResults<VehiclePosition> geoResults = mongoTemplate.geoNear( |
| | | nearQuery, |
| | | VehiclePosition.class, |
| | | "vehicle_position" // 明确指定集合名,避免映射错误 |
| | | ); |
| | | |
| | | // 5. 提取结果 |
| | | return geoResults.getContent().stream() |
| | | .map(geoResult -> geoResult.getContent()) |
| | | .collect(Collectors.toList()); |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return new ArrayList<>(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 根据车辆ID查询车辆信息 |
| | | * @param vehicleId 车辆ID |
| | | * @return 车辆信息,不存在则返回null |
| | | */ |
| | | public VehiclePosition findByVehicleId(Long vehicleId) { |
| | | Query query = new Query(); |
| | | query.addCriteria(Criteria.where("vehicle_id").is(vehicleId)); |
| | | return mongoTemplate.findOne(query, VehiclePosition.class); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 所有的车辆 |
| | | */ |
| | | public List<VehiclePosition> findAll() { |
| | | |
| | | // 构建查询条件 |
| | | Query query = new Query(); |
| | | query.addCriteria(Criteria.where("status").is(1)); // 运营中 |
| | | query.addCriteria(Criteria.where("state").is(1)); // 接单中 |
| | | |
| | | // 可以添加排序,按距离排序 |
| | | query.with(Sort.by(Sort.Direction.ASC, "point")); |
| | | |
| | | return mongoTemplate.find(query, VehiclePosition.class); |
| | | } |
| | | |
| | | } |