gongfuzhu 1 жил өмнө
parent
commit
9ed97a055c

+ 29 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/dao/Impl/XiaoJuStoreDaoImpl.java

@@ -0,0 +1,29 @@
+package com.zswl.cloud.springBtach.server.core.dao.Impl;
+
+import com.zswl.cloud.springBtach.server.core.dao.extend.XiaoJuStoreDaoExtend;
+import com.zswl.cloud.springBtach.server.core.domain.XiaoJuStore;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.geo.*;
+import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.query.NearQuery;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class XiaoJuStoreDaoImpl implements XiaoJuStoreDaoExtend{
+
+
+    @Autowired
+    MongoTemplate mongoTemplate;
+
+    public List<XiaoJuStore> findNearbyTargets(double longitude, double latitude, double maxDistanceInKilometers) {
+        Point point = new Point(longitude, latitude);
+        NearQuery query = NearQuery.near(point).maxDistance(new Distance(maxDistanceInKilometers, Metrics.KILOMETERS));
+        GeoResults<XiaoJuStore> results = mongoTemplate.geoNear(query, XiaoJuStore.class);
+        List<XiaoJuStore> nearbyTargets = new ArrayList<>();
+        for (GeoResult<XiaoJuStore> result : results) {
+            nearbyTargets.add(result.getContent());
+        }
+        return nearbyTargets;
+    }
+}

+ 2 - 1
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/dao/XiaoJuStoreDao.java

@@ -1,9 +1,10 @@
 package com.zswl.cloud.springBtach.server.core.dao;
 
 import com.github.microservice.components.data.mongo.mongo.dao.MongoDao;
+import com.zswl.cloud.springBtach.server.core.dao.extend.XiaoJuStoreDaoExtend;
 import com.zswl.cloud.springBtach.server.core.domain.XiaoJuStore;
 
-public interface XiaoJuStoreDao extends MongoDao<XiaoJuStore> {
+public interface XiaoJuStoreDao extends MongoDao<XiaoJuStore>, XiaoJuStoreDaoExtend {
 
 
     XiaoJuStore findByStoreId(String storeId);

+ 4 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/dao/extend/XiaoJuStoreDaoExtend.java

@@ -0,0 +1,4 @@
+package com.zswl.cloud.springBtach.server.core.dao.extend;
+
+public interface XiaoJuStoreDaoExtend {
+}

+ 5 - 5
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/domain/XiaoJuStore.java

@@ -6,6 +6,7 @@ import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
 import lombok.NoArgsConstructor;
+import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
 import org.springframework.data.mongodb.core.index.Indexed;
 import org.springframework.data.mongodb.core.mapping.Document;
 
@@ -16,19 +17,18 @@ import java.util.List;
 @Document
 @AllArgsConstructor
 @NoArgsConstructor
-public class XiaoJuStore  extends SuperEntity{
+public class XiaoJuStore extends SuperEntity {
 
 
     @Indexed(unique = true)
     private String storeId;
     private String storeName;
     private String address;
-
-    @Indexed
     private Float lat;
-
-    @Indexed
     private Float lon;
+
+    @GeoSpatialIndexed
+    private double[] location;
     private String cityName;
     private String brandId;
     private String provinceName;

+ 2 - 0
SpringBatchServiceServer/src/main/java/com/zswl/cloud/springBtach/server/core/service/XiaoJuService.java

@@ -56,8 +56,10 @@ public class XiaoJuService {
         XiaoJuStore byStoreId = xiaoJuStoreDao.findByStoreId(xiaoJuStore.getStoreId());
         if (byStoreId != null) {
             BeanUtils.copyProperties(byStoreId, xiaoJuStore, "id","updateTime");
+            byStoreId.setLocation(new double[]{xiaoJuStore.getLon(), xiaoJuStore.getLat()});
             xiaoJuStoreDao.save(byStoreId);
         }else {
+            xiaoJuStore.setLocation(new double[]{xiaoJuStore.getLon(), xiaoJuStore.getLat()});
             xiaoJuStoreDao.save(xiaoJuStore);
         }