| | |
| | | |
| | | import com.xiaopeng.api.vo.automaticPilot.VehiclePosition; |
| | | import org.springframework.data.domain.Sort; |
| | | import org.springframework.data.geo.GeoResults; |
| | | import org.springframework.data.geo.Point; |
| | | import org.springframework.data.geo.Distance; |
| | | import org.springframework.data.geo.Metrics; |
| | | 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 经度 |
| | |
| | | public List<VehiclePosition> findAvailableVehiclesNearby(double longitude, |
| | | double latitude, |
| | | double distanceInMeters,int num) { |
| | | // 创建查询点 |
| | | Point referencePoint = new Point(longitude, latitude); |
| | | |
| | | // 设置最大距离(MongoDB使用公里或米,这里转换为公里) |
| | | distanceInMeters = distanceInMeters / 1000; |
| | | try { |
| | | // 1. 创建查询点 |
| | | Point referencePoint = new Point(longitude, latitude); |
| | | |
| | | // 构建查询条件 |
| | | Query query = new Query(); |
| | | query.addCriteria(Criteria.where("point") |
| | | .near(referencePoint) |
| | | .maxDistance(distanceInMeters)); |
| | | query.addCriteria(Criteria.where("status").is(1)); // 运营中 |
| | | query.addCriteria(Criteria.where("state").is(1)); // 接单中 |
| | | |
| | | // 可以添加排序,按距离排序 |
| | | query.with(Sort.by(Sort.Direction.ASC, "point")); |
| | | |
| | | // 可以限制返回数量 |
| | | query.limit(num); |
| | | |
| | | return mongoTemplate.find(query, VehiclePosition.class); |
| | | // 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<>(); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | 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); |
| | | } |
| | | |
| | | } |