Explorar el Código

新增 地理位置索引自动创建

zhaiyifei hace 10 meses
padre
commit
b9bd09e6a3
Se han modificado 1 ficheros con 24 adiciones y 2 borrados
  1. 24 2
      src/main/java/com/yunfeiyun/agmp/iots/startup/MongoStartup.java

+ 24 - 2
src/main/java/com/yunfeiyun/agmp/iots/startup/MongoStartup.java

@@ -138,6 +138,18 @@ public class MongoStartup {
         return mongodbIndexEntity;
     }
 
+    public MongodbIndexEntity IotDeviceGeoLocationGeoCreateIndex() {
+        log.info("开始创建IotXphGssqdata索引");
+        List<String[]> indexNameList = new ArrayList<>();
+        indexNameList.add(new String[]{"devGeoLocation"});
+
+        MongodbIndexEntity mongodbIndexEntity = new MongodbIndexEntity();
+        mongodbIndexEntity.setIotBaseEntity(IotDeviceGeoLocation.class);
+        mongodbIndexEntity.setIndexNameList(indexNameList);
+
+        return mongodbIndexEntity;
+    }
+
     @PostConstruct
     public void start() {
         log.info("开始创建mongodb索引");
@@ -156,12 +168,22 @@ public class MongoStartup {
                 Class<?> entity =  mongodbIndexEntity.getIotBaseEntity();
 
                 List<IndexModel> indexModelList = new ArrayList<>();
+                List<IndexModel> geoIndexModelList = new ArrayList<>();
                 indexNameList.forEach((String[] indexNames) -> {
                     BasicDBObject basicDBObject = new BasicDBObject();
                     Arrays.stream(indexNames).forEach(indexName -> basicDBObject.put(indexName, 1));
-                    indexModelList.add(new IndexModel(basicDBObject, indexOptions));
+                    if(methodName.endsWith("GeoCreateIndex")){
+                        geoIndexModelList.add(new IndexModel(basicDBObject, indexOptions));
+                    }else{
+                        indexModelList.add(new IndexModel(basicDBObject, indexOptions));
+                    }
                 });
-                mongoService.createIndexes(entity, indexModelList);
+                if(!indexModelList.isEmpty()){
+                    mongoService.createIndexes(entity, indexModelList);
+                }
+                if(!geoIndexModelList.isEmpty()){
+                    mongoService.createGeoIndexes(entity, geoIndexModelList);
+                }
 
             }catch (Exception e) {
                 log.error("创建{}索引失败:{}", method.getName(), e.getMessage());