Browse Source

mongoDB查询

lin 4 years ago
parent
commit
b7c98f8899

+ 5 - 3
water_query_api/src/main/java/com/zcxk/service/impl/MeterReadRecordServiceImpl.java

@@ -119,11 +119,11 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
         LocalDateTime endDateTime = LocalDateTime.now().plusMonths(-1).with(TemporalAdjusters.lastDayOfMonth());
         Integer startDate = Integer.valueOf(startDateTime.format(df));
         Integer endDate = Integer.valueOf(endDateTime.format(df));
-        List<UseWaterDto> list = meterReadRecordMapper.getUseWaterByMonth(deviceId, startDate, endDate);
+        List<UseWaterDto> list= meterReadRecordMapper.getUseWaterByMonth(deviceId, startDate, endDate);
 
         //切换MongoDB查询
-        //List<UseWaterDto> useWaterByMonth = mongoMeterReadRecordService.getUseWaterByMonth(deviceId, startDate, endDate);
-        //log.info("useWaterByMonth = {}",useWaterByMonth);
+        //List<UseWaterDto> list = mongoMeterReadRecordService.getUseWaterByMonth(deviceId, startDate, endDate);
+        log.info("useWaterByMonth = {}",list);
         //填充缺失月份数据
         fillDataForCertainMonths(12,list,startDateTime,dateTimeFormatter,1);
         useWaterAnalyze.setList(list);
@@ -135,5 +135,7 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
     public Double getDeviceVolume(String meterNo, String customerNo, Integer startDate, Integer endDate) {
         Long deviceId=warningRuleService.getDeviceId(meterNo,customerNo);
         return meterReadRecordMapper.getDeviceVolume(deviceId, startDate, endDate);
+        //切换mongo查询
+        //return mongoMeterReadRecordService.getDeviceVolume(deviceId, startDate, endDate);
     }
 }

+ 27 - 9
water_query_api/src/main/java/com/zcxk/service/impl/MongoMeterReadRecordServiceImpl.java

@@ -65,21 +65,32 @@ public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordServ
         ConditionalOperators.Cond readTimesCond = ConditionalOperators.when(new Criteria("readStatus").is(2)).then(1).otherwise(0);
 
         operations.add(Aggregation.project("lastCost").
-                and("readDate").divide(100).substring(0,6).as("month")
+                and("readDate").divide(100).as("readDateTemp")
                 );
-
+        operations.add(Aggregation.project("lastCost").
+                and("readDateTemp").substring(0,6).as("month")
+        );
         operations.add(Aggregation.group("month")
                 .sum("lastCost").as("lastCost")
                 );
         operations.add(Aggregation.project().
-                and("month").as("date").
-                and("lastCost").as("useVolume"));
+                and("_id").as("date").
+                and("lastCost").as("useVolume").andExclude("_id"));
         // 3,聚合查询所有信息
         Aggregation aggregation = Aggregation.newAggregation(operations).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
         // 4,查询结果
         AggregationResults<UseWaterDto> aggregationResults = mongoTemplate.aggregate(aggregation, "sc_meter_read_record", UseWaterDto.class);
         // 5,获取结果
-        List<UseWaterDto> results = aggregationResults.getMappedResults();
+        List<UseWaterDto> list = aggregationResults.getMappedResults();
+        List<UseWaterDto> results = new ArrayList<>();
+        if (list != null && list.size() > 0) {
+            for (UseWaterDto dto : list) {
+                UseWaterDto useWaterDto = new UseWaterDto();
+                useWaterDto.setUseVolume(dto.getUseVolume());
+                useWaterDto.setDate(dto.getDate());
+                results.add(useWaterDto);
+            }
+        }
         return results;
     }
 
@@ -87,11 +98,18 @@ public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordServ
     public Double getDeviceVolume(Long deviceId, Integer startDate, Integer endDate) {
         List<AggregationOperation> operations = new ArrayList<>();
         operations.add(Aggregation.match(Criteria.where("deviceId").is(deviceId)));
-        operations.add(Aggregation.match(Criteria.where("status").is(1)));
         operations.add(Aggregation.match(Criteria.where("readDate").gte(startDate).lte(endDate)));
-        operations.add(Aggregation.group("deviceId")
-                .sum("").as("realReadTimes"));
-
+        operations.add(Aggregation.group()
+                .sum("lastCost").as("useVolume"));
+        // 3,聚合查询所有信息
+        Aggregation aggregation = Aggregation.newAggregation(operations).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
+        // 4,查询结果
+        AggregationResults<UseWaterDto> aggregationResults = mongoTemplate.aggregate(aggregation, "sc_meter_read_record", UseWaterDto.class);
+        // 5,获取结果
+        List<UseWaterDto> list = aggregationResults.getMappedResults();
+        if (list != null && list.size() > 0) {
+            return list.get(0).getUseVolume();
+        }
         return null;
     }
 }