Browse Source

水查查接口mongodb查询

lin 4 years ago
parent
commit
95094d862a

+ 5 - 0
water_query_api/pom.xml

@@ -119,6 +119,11 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-redis</artifactId>
         </dependency>
+        <!-- mongodb -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-mongodb</artifactId>
+        </dependency>
     </dependencies>
 
 

+ 2 - 2
water_query_api/src/main/java/com/zcxk/controller/WaterApiController.java

@@ -177,7 +177,7 @@ public class WaterApiController {
      * @return Response对象
      */
     @RequestMapping(value = "delWaringRule", method = RequestMethod.POST)
-    @ApiOperation(value = "用水量")
+    @ApiOperation(value = "删除警告规则")
     public AjaxMessage< Double> delWaringRule(
             @ApiParam(value = "id", required = true) @RequestParam Integer id
             ,@ApiParam(value = "用户名", required = true)  @RequestParam String userName
@@ -193,7 +193,7 @@ public class WaterApiController {
      * @return Response对象
      */
     @RequestMapping(value = "setWaringData", method = RequestMethod.POST)
-    @ApiOperation(value = "用水量")
+    @ApiOperation(value = "设置告警")
     public AjaxMessage< Double> setWaringData(
 
     ) {

+ 13 - 0
water_query_api/src/main/java/com/zcxk/repository/MeterReadRecordRepository.java

@@ -0,0 +1,13 @@
+package com.zcxk.repository;
+
+import org.springframework.data.mongodb.repository.MongoRepository;
+
+import java.util.List;
+
+public interface MeterReadRecordRepository extends MongoRepository<MongoMeterReadRecord, Long> {
+    MongoMeterReadRecord findByDeviceIdAndReadDate(Long deviceId, Integer date);
+
+    List<MongoMeterReadRecord> findByReadDateAndCustomerId(Integer date, Integer customerId);
+
+    List<MongoMeterReadRecord> findByReadDateAndCustomerIdAndMeterNo(Integer date, Integer customerId, String meterNo);
+}

+ 195 - 0
water_query_api/src/main/java/com/zcxk/repository/MongoMeterReadRecord.java

@@ -0,0 +1,195 @@
+package com.zcxk.repository;
+
+import lombok.Data;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * <p>抄表记录</p>
+ * sh.enableSharding("meter-reading-database");
+ * sh.shardCollection("meter-reading-database.sc_meter_read_record",{"readDate":1,"deviceId":1});
+ * db.sc_meter_read_record.createIndex({readDate:1,customerId:1,buildingId:1,siteId:1,sysId:1,readStatus:1,concentratorId:1},{background: true,name:'idx_1'})
+ * @Author wilian.peng
+ * @Date 2020/12/22 16:59
+ * @Version 1.0
+ */
+@Data
+@Document(collection = "sc_meter_read_record")
+public class MongoMeterReadRecord implements Serializable {
+    /**
+    * 主键
+    */
+    private Long id;
+
+    /**
+    * 读表日期
+    */
+    private Integer readDate;
+
+    /**
+     * 站点
+     */
+    private Integer siteId;
+
+    /**
+     * 场景
+     */
+    private Integer sysId;
+
+    /**
+    * 省
+    */
+    private Integer province;
+
+    /**
+    * 市
+    */
+    private Integer city;
+
+    /**
+    * 区
+    */
+    private Integer region;
+
+    /**
+    * 小区
+    */
+    private Integer community;
+
+    /**
+    * 客户
+    */
+    private Integer customerId;
+
+    /**
+    * 集中器
+    */
+    private Integer concentratorId;
+
+    /**
+    * 采集器
+    */
+    private Integer collectorId;
+
+    /**
+    * 建筑
+    */
+    private Integer buildingId;
+
+    /**
+    * 安装地址
+    */
+    private String location;
+
+    /**
+    * 设备类型
+    */
+    private Integer deviceTypeId;
+
+    /**
+    * 设备id
+    */
+    private Long deviceId;
+
+    /**
+    * 节点编号
+    */
+    private String deviceNo;
+
+    /**
+    * 电子号
+    */
+    private String meterNo;
+
+    /**
+    * 档案号
+    */
+    private String meterFileNo;
+
+    /**
+    * 读表时间
+    */
+    private Date readTime;
+
+    /**
+    * 读表状态
+    */
+    private String readStatus;
+
+    /**
+    * 读表数据
+    */
+    private String readData;
+
+    /**
+    * 最近有效数据
+    */
+    private String lastValid;
+
+    /**
+    * 距离上次的消耗
+    */
+    private BigDecimal lastCost;
+
+    /**
+    * 状态
+    */
+    private Integer status;
+
+    /**
+    * 创建时间
+    */
+    private Date dateCreate;
+
+    /**
+    * 更新时间
+    */
+    private Date dateUpdate;
+
+    /**
+    * 创建人
+    */
+    private String createBy;
+
+    /**
+    * 更新人
+    */
+    private String updateBy;
+
+    /***********************************以下字段将存入MongoDB中**************************************/
+    private String communityName;
+
+    private String buildingName ;
+
+    private String customerName ;
+
+    private String provinceName ;
+
+    private String cityName ;
+
+    private String regionName ;
+
+    private String deviceTypeName ;
+
+    private String concentratorNo ;
+
+    private String collectorNo ;
+
+    private Integer manufacturerId ;
+
+    private String manufacturerName ;
+
+    private Integer channelNumberId;
+
+    private String channelName ;
+
+    /**
+     * 最后上送数据,格式如下:
+     * {"数据ID":11111,data:{"WSV":"1","VOL":"2.5"}}
+     */
+    private Map<String, String> lastSendData ;
+}

+ 21 - 0
water_query_api/src/main/java/com/zcxk/service/MongoMeterReadRecordService.java

@@ -0,0 +1,21 @@
+package com.zcxk.service;
+
+
+import com.zcxk.dto.UseWaterDto;
+import com.zcxk.entity.MeterReadRecord;
+import com.zcxk.repository.MongoMeterReadRecord;
+import org.apache.ibatis.annotations.Param;
+import org.springframework.data.mongodb.core.query.Query;
+
+import java.util.List;
+
+public interface MongoMeterReadRecordService {
+
+    List<MongoMeterReadRecord> findMongoMeterReadRecordList(Query query);
+
+    List<UseWaterDto> getRealTimeUseWater(Long deviceId, Integer startDate, Integer endDate);
+
+    List<UseWaterDto> getUseWaterByMonth(Long deviceId, Integer startDate, Integer endDate);
+
+    Double getDeviceVolume(Long deviceId, Integer startDate, Integer endDate);
+}

+ 11 - 0
water_query_api/src/main/java/com/zcxk/service/impl/MeterReadRecordServiceImpl.java

@@ -3,8 +3,11 @@ package com.zcxk.service.impl;
 import com.zcxk.dao.MeterReadRecordMapper;
 import com.zcxk.dto.UseWaterAnalyze;
 import com.zcxk.dto.UseWaterDto;
+import com.zcxk.repository.MongoMeterReadRecord;
 import com.zcxk.service.MeterReadRecordService;
+import com.zcxk.service.MongoMeterReadRecordService;
 import com.zcxk.service.WarningRuleService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -16,12 +19,16 @@ import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.List;
 import java.util.stream.Collectors;
+
+@Slf4j
 @Service
 public class MeterReadRecordServiceImpl implements MeterReadRecordService {
     @Autowired
     private MeterReadRecordMapper meterReadRecordMapper;
     @Autowired
     private WarningRuleService warningRuleService;
+    @Autowired
+    private MongoMeterReadRecordService mongoMeterReadRecordService;
 
     @Override
     public List<UseWaterDto> getRealTimeUseWater(String meterNo, Integer period, String customerNo) {
@@ -32,6 +39,10 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService {
         Integer endDate = Integer.valueOf(endDateTime.format(df));
         Long deviceId=warningRuleService.getDeviceId(meterNo,customerNo);;
         List<UseWaterDto> list =  meterReadRecordMapper.getRealTimeUseWater(deviceId, startDate, endDate);
+
+        //切换MongoDB查询
+        //List<UseWaterDto> readRecordList = mongoMeterReadRecordService.getRealTimeUseWater(deviceId,startDate,endDate);
+
         //填充缺失月份数据
         fillDataForCertainMonths(period,list,startDateTime,df,0);
 

+ 75 - 0
water_query_api/src/main/java/com/zcxk/service/impl/MongoMeterReadRecordServiceImpl.java

@@ -0,0 +1,75 @@
+package com.zcxk.service.impl;
+
+import com.zcxk.dto.UseWaterDto;
+import com.zcxk.repository.MongoMeterReadRecord;
+import com.zcxk.service.MongoMeterReadRecordService;
+import lombok.extern.slf4j.Slf4j;
+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.aggregation.Aggregation;
+import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
+import org.springframework.data.mongodb.core.query.Criteria;
+import org.springframework.data.mongodb.core.query.Query;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Slf4j
+@Service
+public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordService {
+
+    @Autowired
+    private MongoTemplate mongoTemplate;
+
+    @Override
+    public List<MongoMeterReadRecord> findMongoMeterReadRecordList(Query query) {
+        return mongoTemplate.find(query, MongoMeterReadRecord.class,"sc_meter_read_record");
+    }
+
+
+
+    @Override
+    public List<UseWaterDto> getRealTimeUseWater(Long deviceId, Integer startDate, Integer endDate) {
+        Query query = new Query();
+        query.addCriteria(Criteria.where("deviceId").is(deviceId));
+        if(startDate!=null && endDate!=null)query.addCriteria(Criteria.where("readDate").gte(startDate).lte(endDate));
+        query.with(Sort.by(Sort.Order.asc("dateCreate")));
+        List<MongoMeterReadRecord> list =  this.findMongoMeterReadRecordList(query);
+
+
+
+        List<UseWaterDto> result = new ArrayList<>();
+        if (list != null && list.size() > 0) {
+            for (MongoMeterReadRecord record : list) {
+                UseWaterDto dto = new UseWaterDto();
+                dto.setUseVolume(record.getLastCost().doubleValue());
+                dto.setDate(record.getReadDate());
+                result.add(dto);
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public List<UseWaterDto> getUseWaterByMonth(Long deviceId, Integer startDate, Integer endDate) {
+        Query query = new Query();
+        query.addCriteria(Criteria.where("deviceId").is(deviceId));
+        if(startDate!=null && endDate!=null)query.addCriteria(Criteria.where("readDate").gte(startDate).lte(endDate));
+        query.with(Sort.by(Sort.Order.asc("dateCreate")));
+        List<MongoMeterReadRecord> list =  this.findMongoMeterReadRecordList(query);
+        return null;
+    }
+
+    @Override
+    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"));
+        return null;
+    }
+}

+ 12 - 7
water_query_api/src/main/resources/application-dev.properties

@@ -28,20 +28,25 @@ spring.servlet.multipart.location=e:/test/files
 #spring.redis.password=123456
 spring.redis.sentinel.master=mymaster
 spring.redis.sentinel.nodes=10.0.0.71:26379,10.0.0.72:26379,10.0.0.73:26379
-#连接超时时间
+#锟斤拷锟接筹拷时时锟斤拷
 spring.redis.timeout=6000ms
-##Redis数据库索引(默认为0)
+##Redis锟斤拷锟捷匡拷锟斤拷锟斤拷锟斤拷默锟斤拷为0锟斤拷
 spring.redis.database=0
-## 连接池配置,springboot2.0中直接使用jedis或者lettuce配置连接池,默认为lettuce连接池
-##连接池最大连接数(使用负值表示没有限制)
+## 锟斤拷锟接筹拷锟斤拷锟矫o拷springboot2.0锟斤拷直锟斤拷使锟斤拷jedis锟斤拷锟斤拷lettuce锟斤拷锟斤拷锟斤拷锟接池o拷默锟斤拷为lettuce锟斤拷锟接筹拷
+##锟斤拷锟接筹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷酶锟街碉拷锟绞久伙拷锟斤拷锟斤拷疲锟�
 spring.redis.jedis.pool.max-active=8
-##连接池最大阻塞等待时间(使用负值表示没有限制)
+##锟斤拷锟接筹拷锟斤拷锟斤拷锟斤拷锟斤拷却锟绞憋拷洌ㄊ癸拷酶锟街碉拷锟绞久伙拷锟斤拷锟斤拷疲锟�
 spring.redis.jedis.pool.max-wait=-1s
-##连接池中的最大空闲连接
+##锟斤拷锟接筹拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
 spring.redis.jedis.pool.max-idle=8
-##接池中的最小空闲连接
+##锟接筹拷锟叫碉拷锟斤拷小锟斤拷锟斤拷锟斤拷锟斤拷
 spring.redis.jedis.pool.min-idle=0
 
+#mongodb url
+spring.data.mongodb.uri=mongodb://114.135.61.188:17017/meter-reading-database
+#spring.data.mongodb.uri=mongodb://water:zcxk100@114.135.61.189:27071/meter-reading-database?authSource=meter-reading-database
+logging.level.org.springframework.data.mongodb.core=DEBUG
+
 
 
 

+ 12 - 7
water_query_api/src/main/resources/application-sit.properties

@@ -28,20 +28,25 @@ spring.servlet.multipart.location=e:/test/files
 #spring.redis.password=123456
 spring.redis.sentinel.master=mymaster
 spring.redis.sentinel.nodes=10.0.0.71:26379,10.0.0.72:26379,10.0.0.73:26379
-#连接超时时间
+#锟斤拷锟接筹拷时时锟斤拷
 spring.redis.timeout=6000ms
-##Redis数据库索引(默认为0)
+##Redis锟斤拷锟捷匡拷锟斤拷锟斤拷锟斤拷默锟斤拷为0锟斤拷
 spring.redis.database=0
-## 连接池配置,springboot2.0中直接使用jedis或者lettuce配置连接池,默认为lettuce连接池
-##连接池最大连接数(使用负值表示没有限制)
+## 锟斤拷锟接筹拷锟斤拷锟矫o拷springboot2.0锟斤拷直锟斤拷使锟斤拷jedis锟斤拷锟斤拷lettuce锟斤拷锟斤拷锟斤拷锟接池o拷默锟斤拷为lettuce锟斤拷锟接筹拷
+##锟斤拷锟接筹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷酶锟街碉拷锟绞久伙拷锟斤拷锟斤拷疲锟�
 spring.redis.jedis.pool.max-active=8
-##连接池最大阻塞等待时间(使用负值表示没有限制)
+##锟斤拷锟接筹拷锟斤拷锟斤拷锟斤拷锟斤拷却锟绞憋拷洌ㄊ癸拷酶锟街碉拷锟绞久伙拷锟斤拷锟斤拷疲锟�
 spring.redis.jedis.pool.max-wait=-1s
-##连接池中的最大空闲连接
+##锟斤拷锟接筹拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
 spring.redis.jedis.pool.max-idle=8
-##接池中的最小空闲连接
+##锟接筹拷锟叫碉拷锟斤拷小锟斤拷锟斤拷锟斤拷锟斤拷
 spring.redis.jedis.pool.min-idle=0
 
+#mongodb url
+spring.data.mongodb.uri=mongodb://114.135.61.188:17017/meter-reading-database
+#spring.data.mongodb.uri=mongodb://water:zcxk100@114.135.61.189:27071/meter-reading-database?authSource=meter-reading-database
+logging.level.org.springframework.data.mongodb.core=DEBUG
+