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
|
@Component public class MerchantGeoIndexService { @Autowired private MongoTemplate mongoTemplate; @Autowired private DistanceCalculator distanceCalculator;
@PostConstruct public void configureMerchantGeoIndexes() { String collectionName = "merchants"; try { Index locationIndex = new Index() .on("location", Sort.Direction.ASC) .named("merchant_location_2dsphere"); mongoTemplate.indexOps(collectionName).ensureIndex(locationIndex); Index nameIndex = new Index() .on("name", Sort.Direction.ASC) .named("merchant_name"); mongoTemplate.indexOps(collectionName).ensureIndex(nameIndex); Index categoryIndex = new Index() .on("category", Sort.Direction.ASC) .named("merchant_category"); mongoTemplate.indexOps(collectionName).ensureIndex(categoryIndex); Index statusIndex = new Index() .on("status", Sort.Direction.ASC) .named("merchant_status"); mongoTemplate.indexOps(collectionName).ensureIndex(statusIndex); Index categoryStatusIndex = new Index() .on("category", Sort.Direction.ASC) .on("status", Sort.Direction.ASC) .named("merchant_category_status"); mongoTemplate.indexOps(collectionName).ensureIndex(categoryStatusIndex); System.out.println("商家地理位置索引配置完成"); } catch (Exception e) { System.err.println("商家地理位置索引配置失败: " + e.getMessage()); } }
public void addMerchant(String name, double longitude, double latitude, String address, String category) { try { if (!isValidCoordinate(longitude, latitude)) { throw new IllegalArgumentException("无效的经纬度坐标"); } Document merchantDoc = new Document(); merchantDoc.put("name", name); merchantDoc.put("location", new Document("type", "Point") .append("coordinates", new double[]{longitude, latitude})); merchantDoc.put("address", address); merchantDoc.put("longitude", longitude); merchantDoc.put("latitude", latitude); merchantDoc.put("category", category); merchantDoc.put("status", "active"); merchantDoc.put("createdAt", new Date()); mongoTemplate.insert(merchantDoc, "merchants"); System.out.println("成功添加商家: " + name); } catch (Exception e) { System.err.println("添加商家失败: " + e.getMessage()); } }
public List<Document> findNearbyMerchants(double longitude, double latitude, double radius, String category) { try { Query query = new Query(); query.addCriteria(Criteria.where("location") .near(new Point(longitude, latitude)) .maxDistance(radius)); query.addCriteria(Criteria.where("status").is("active")); if (category != null && !category.isEmpty()) { query.addCriteria(Criteria.where("category").is(category)); } List<Document> results = mongoTemplate.find(query, Document.class, "merchants"); for (Document merchant : results) { Document location = (Document) merchant.get("location"); List<Double> coordinates = (List<Double>) location.get("coordinates"); double merchantLon = coordinates.get(0); double merchantLat = coordinates.get(1); double distance = distanceCalculator.calculateDistance(longitude, latitude, merchantLon, merchantLat); merchant.put("distance", distance); } results.sort((a, b) -> { double distanceA = (Double) a.get("distance"); double distanceB = (Double) b.get("distance"); return Double.compare(distanceA, distanceB); }); System.out.println("找到 " + results.size() + " 个附近商家"); return results; } catch (Exception e) { System.err.println("查找附近商家失败: " + e.getMessage()); return new ArrayList<>(); } }
private boolean isValidCoordinate(double longitude, double latitude) { return longitude >= -180 && longitude <= 180 && latitude >= -90 && latitude <= 90; } }
|