Sfoglia il codice sorgente

设备模型信息-通过区域id查询接口减少不必要的返回字段,并且添加索引

lingpeng.li 5 mesi fa
parent
commit
243730a67b

+ 90 - 1
zgztBus/jeecg-module-lesm/src/main/java/org/jeecg/modules/deviceLesm/service/impl/DeviceInformationServiceImpl.java

@@ -14,9 +14,12 @@ import org.jeecg.modules.fpgLeanModel.entity.DeviceStatiscsModelMongodb;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Sort;
 import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.data.mongodb.core.index.Index;
+import org.springframework.data.mongodb.core.index.IndexInfo;
 import org.springframework.data.mongodb.core.query.Criteria;
 import org.springframework.data.mongodb.core.query.Query;
 import org.springframework.stereotype.Service;
+import org.jeecg.modules.utils.*;
 
 import java.time.LocalDate;
 import java.time.LocalDateTime;
@@ -37,6 +40,7 @@ public class DeviceInformationServiceImpl extends ServiceImpl<DeviceInformationM
     @Autowired
     MongoTemplate mongoTemplate;
 
+
     @Override
     public DeviceModelInfo queryDeviceModelById(String deviceRegionId, String deviceType, String dates) {
         DeviceModelInfo result = new DeviceModelInfo();
@@ -54,8 +58,13 @@ public class DeviceInformationServiceImpl extends ServiceImpl<DeviceInformationM
         result.setDeviceInformationList(deviceInformationList);
         List<String> deviceInformationIds = deviceInformationList.stream().map(DeviceInformation::getId).collect(Collectors.toList());
 
+        createInboxIndex("deviceInformationId","leanmodel_run_realtime");
+        createInboxIndex("deviceInformationId", "total_startstop");
+        createInboxIndex("deviceRegionId", "deviceInformationId", "total_startstop");
+
         Query querylr = new Query();
-        querylr.addCriteria(Criteria.where("deviceInformationId").in(deviceInformationIds));
+        querylr.addCriteria(Criteria.where("deviceInformationId").in(deviceInformationIds))
+                .fields().include("deviceInformationId", "datestr", "power", "selectricCurrent"); // 只查询必要的字段
         // 执行查询
         List<DeviceStatiscsModelMongodb> deviceStatiscsModelMongodbList = mongoTemplate.find(querylr, DeviceStatiscsModelMongodb.class, "leanmodel_run_realtime");
         if (oConvertUtils.listIsEmpty(deviceStatiscsModelMongodbList)) {
@@ -186,4 +195,84 @@ public class DeviceInformationServiceImpl extends ServiceImpl<DeviceInformationM
                 .collect(Collectors.toList());
         return resultList;
     }
+
+
+    /**
+     * 创建联合索引(如果不存在)
+     *
+     * @param indexKey1 索引字段1
+     * @param indexKey2 索引字段2
+     * @param collectionName 集合名称
+     * @return
+     */
+    public boolean createInboxIndex(String indexKey1, String indexKey2, String collectionName) {
+        boolean success = true;
+        try {
+            // 获取当前集合的所有索引信息
+            List<IndexInfo> existingIndexes = mongoTemplate.indexOps(collectionName).getIndexInfo();
+
+            // 判断索引是否已经存在
+            boolean indexExists = existingIndexes.stream()
+                    .anyMatch(index -> index.getName().equals(createIndexName(indexKey1, indexKey2)));
+
+            // 如果索引不存在,创建索引
+            if (!indexExists) {
+                Index index = new Index().on(indexKey1, Sort.Direction.ASC).on(indexKey2, Sort.Direction.ASC);
+                mongoTemplate.indexOps(collectionName).ensureIndex(index);
+            }
+        } catch (Exception ex) {
+            success = false;
+        }
+        return success;
+    }
+
+    /**
+     * 创建单个索引(如果不存在)
+     *
+     * @param indexKey 索引字段
+     * @param collectionName 集合名称
+     * @return
+     */
+    public boolean createInboxIndex(String indexKey, String collectionName) {
+        boolean success = true;
+        try {
+            // 获取当前集合的所有索引信息
+            List<IndexInfo> existingIndexes = mongoTemplate.indexOps(collectionName).getIndexInfo();
+
+            // 判断索引是否已经存在
+            boolean indexExists = existingIndexes.stream()
+                    .anyMatch(index -> index.getName().equals(createIndexName(indexKey)));
+
+            // 如果索引不存在,创建索引
+            if (!indexExists) {
+                Index index = new Index().on(indexKey, Sort.Direction.ASC);
+                mongoTemplate.indexOps(collectionName).ensureIndex(index);
+            }
+        } catch (Exception ex) {
+            success = false;
+        }
+        return success;
+    }
+
+    /**
+     * 根据索引字段名生成索引名称
+     *
+     * @param indexKey1 索引字段1
+     * @param indexKey2 索引字段2(可选)
+     * @return 索引名称
+     */
+    private String createIndexName(String indexKey1, String indexKey2) {
+        return indexKey1 + "_" + indexKey2;
+    }
+
+    /**
+     * 根据单个索引字段名生成索引名称
+     *
+     * @param indexKey 索引字段
+     * @return 索引名称
+     */
+    private String createIndexName(String indexKey) {
+        return indexKey;
+    }
+
 }