|
@@ -5,8 +5,20 @@ import com.bz.smart_city.commom.exception.ServiceException;
|
|
|
import com.bz.smart_city.commom.util.*;
|
|
|
import com.bz.smart_city.dto.*;
|
|
|
|
|
|
+import com.github.pagehelper.Page;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+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.aggregation.AggregationResults;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.ConditionalOperators;
|
|
|
+import org.springframework.data.mongodb.core.mapreduce.GroupBy;
|
|
|
+import org.springframework.data.mongodb.core.mapreduce.GroupByResults;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import javax.annotation.Resource;
|
|
@@ -97,10 +109,13 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService{
|
|
|
private StatMeterReadRateByBuildingMapper statMeterReadRateByBuildingMapper;
|
|
|
|
|
|
@Resource
|
|
|
- CustomerService customerService;
|
|
|
+ CustomerService customerService;
|
|
|
+ @Autowired
|
|
|
+ private MongoTemplate mongoTemplate;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
+ @Deprecated
|
|
|
public Pagination<MeterReadRecord> queryMeterReadRecord(CommonQueryCondition condition, int pageNum, int pageSize) {
|
|
|
log.info("begin MeterReadRecordService queryMeterReadRecord , param = "+JSON.toJSONString(condition));
|
|
|
List<Integer> siteList = new ArrayList<Integer>();
|
|
@@ -116,6 +131,7 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService{
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Deprecated
|
|
|
public List<MeterReadRate> analysisMeterReadRate(CommonQueryCondition condition){
|
|
|
List<MeterReadRate> result = new ArrayList<MeterReadRate>();
|
|
|
// 1,计算查询日期
|
|
@@ -516,60 +532,217 @@ public class MeterReadRecordServiceImpl implements MeterReadRecordService{
|
|
|
@Override
|
|
|
public WaterConsumptionSummaryDto waterConsumptionSummary(CommonQueryCondition condition) {
|
|
|
log.info("begin MeterReadRecordService waterConsumptionSummary, params = " + JSON.toJSONString(condition));
|
|
|
- WaterConsumptionSummaryDto result = meterReadRecordMapper.getSummary(condition);
|
|
|
- log.info("end MeterReadRecordService waterConsumptionSummary, result = " + JSON.toJSONString(result));
|
|
|
- return result;
|
|
|
+ //WaterConsumptionSummaryDto result = meterReadRecordMapper.getSummary(condition);
|
|
|
+ /*
|
|
|
+ db.sc_meter_read_record.aggregate(
|
|
|
+ [
|
|
|
+ {$match:{siteId:1,status:1,readDate:20200701}},
|
|
|
+ {$group:{_id:{
|
|
|
+ "deviceId": "$deviceId",
|
|
|
+ },
|
|
|
+ lastCost:{$sum:"$lastCost"}
|
|
|
+
|
|
|
+ }},
|
|
|
+ {$group:{
|
|
|
+ _id:{},
|
|
|
+ total:{$sum:1},
|
|
|
+ lastCost:{$sum:"$lastCost"}
|
|
|
+
|
|
|
+ }},
|
|
|
+ { $project : {
|
|
|
+ total : 1,
|
|
|
+ lastCost : 1 ,
|
|
|
+ _id:0
|
|
|
+ }}
|
|
|
+
|
|
|
+
|
|
|
+ ]
|
|
|
+ ,{ allowDiskUse: true }
|
|
|
+ )
|
|
|
+ */
|
|
|
+ List<AggregationOperation> operations = new ArrayList<>();
|
|
|
+ operations.add(Aggregation.match(Criteria.where("status").is(1)));
|
|
|
+ operations.add(Aggregation.match(Criteria.where("siteId").is(condition.getSiteId())));
|
|
|
+ operations.add(Aggregation.match(Criteria.where("readDate").gte(condition.getStartDate()).lte(condition.getEndDate())));
|
|
|
+ if (condition.getCustormerId() != null ) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("customerId").is(condition.getCustormerId())));
|
|
|
+ }
|
|
|
+ if (condition.getProvinces() != null && condition.getProvinces().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("provinceId").in(condition.getProvinces())));
|
|
|
+ }
|
|
|
+ if (condition.getCities() != null && condition.getCities().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("cityId").in(condition.getCities())));
|
|
|
+ }
|
|
|
+ if (condition.getRegions() != null && condition.getRegions().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("regionId").in(condition.getRegions())));
|
|
|
+ }
|
|
|
+ if (condition.getCommunities() != null && condition.getCommunities().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("communityId").in(condition.getCommunities())));
|
|
|
+ }
|
|
|
+ if (condition.getBuildingIds() != null && condition.getBuildingIds().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("buildingId").in(condition.getBuildingIds())));
|
|
|
+ }
|
|
|
+ // 1,添加分组条件
|
|
|
+ operations.add(Aggregation.group("deviceId")
|
|
|
+ .sum("lastCost").as("waterConsumption"));
|
|
|
+
|
|
|
+ if (condition.getBeginRate() != null) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("waterConsumption").gte(condition.getBeginRate())));
|
|
|
+ }
|
|
|
+ if (condition.getEndRate() != null) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("waterConsumption").lte(condition.getEndRate())));
|
|
|
+ }
|
|
|
+
|
|
|
+ operations.add(Aggregation.group()
|
|
|
+ .sum("waterConsumption").as("totalWaterConsumption")
|
|
|
+ .count().as("totalDeviceCount"));
|
|
|
+ operations.add(Aggregation.project("totalWaterConsumption","totalDeviceCount").
|
|
|
+ andExclude("_id"));
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(operations);
|
|
|
+ AggregationResults<WaterConsumptionSummaryDto> aggregationResults = mongoTemplate.aggregate(aggregation, "sc_meter_read_record", WaterConsumptionSummaryDto.class);
|
|
|
+ List<WaterConsumptionSummaryDto> list = aggregationResults.getMappedResults();
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
+ return list.get(0);
|
|
|
+ }else {
|
|
|
+ WaterConsumptionSummaryDto dto = new WaterConsumptionSummaryDto();
|
|
|
+ dto.setTotalDeviceCount(0);
|
|
|
+ dto.setTotalWaterConsumption(0.0);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Pagination<WaterConsumptionDto> getWaterConsumptionList(CommonQueryCondition condition, Integer pageNum, Integer pageSize) {
|
|
|
log.info("begin MeterReadRecordService getWaterConsumptionList, params = " + JSON.toJSONString(condition) + ", pageNum = " + pageNum + ", pageSize = " + pageSize);
|
|
|
- PageHelper.startPage(pageNum, pageSize);
|
|
|
- List<WaterConsumptionDto> result = meterReadRecordMapper.getList(condition);
|
|
|
- log.info("end MeterReadRecordService getWaterConsumptionList, result.size = "+result.size());
|
|
|
- return new Pagination<>(result);
|
|
|
+ Pagination pagination = new Pagination<>();
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ db.sc_meter_read_record.aggregate(
|
|
|
+ [
|
|
|
+ {$match:{siteId:1,status:1,readDate:20200701}},
|
|
|
+ {$group:{_id:{
|
|
|
+ "deviceId": "$deviceId",
|
|
|
+ },
|
|
|
+ meterNo:{$last:"$meterNo"},
|
|
|
+ deviceTypeName:{$last:"$deviceTypeName"},
|
|
|
+ customerName:{$last:"$customerName"},
|
|
|
+ communityName:{$last:"$communityName"},
|
|
|
+ lastCost:{$sum:"$lastCost"}
|
|
|
+
|
|
|
+ }},
|
|
|
+ { $project : {
|
|
|
+ deviceId : "$_id.deviceId" ,
|
|
|
+ meterNo : 1 ,
|
|
|
+ lastCost : 1 ,
|
|
|
+ deviceNo : 1 ,
|
|
|
+ total : {$sum:1} ,
|
|
|
+ _id:0
|
|
|
+ }}
|
|
|
+ ,{$sort:{"lastCost":-1}}
|
|
|
+ ,{$skip:0}
|
|
|
+ ,{$limit:10}
|
|
|
+
|
|
|
+ ]
|
|
|
+ ,{ allowDiskUse: true }
|
|
|
+ )
|
|
|
+ */
|
|
|
+ List<AggregationOperation> operations = new ArrayList<>();
|
|
|
+ operations.add(Aggregation.match(Criteria.where("status").is(1)));
|
|
|
+ operations.add(Aggregation.match(Criteria.where("siteId").is(condition.getSiteId())));
|
|
|
+ operations.add(Aggregation.match(Criteria.where("readDate").gte(condition.getStartDate()).lte(condition.getEndDate())));
|
|
|
+ if (condition.getCustormerId() != null ) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("customerId").is(condition.getCustormerId())));
|
|
|
+ }
|
|
|
+ if (condition.getProvinces() != null && condition.getProvinces().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("provinceId").in(condition.getProvinces())));
|
|
|
+ }
|
|
|
+ if (condition.getCities() != null && condition.getCities().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("cityId").in(condition.getCities())));
|
|
|
+ }
|
|
|
+ if (condition.getRegions() != null && condition.getRegions().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("regionId").in(condition.getRegions())));
|
|
|
+ }
|
|
|
+ if (condition.getCommunities() != null && condition.getCommunities().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("communityId").in(condition.getCommunities())));
|
|
|
+ }
|
|
|
+ if (condition.getBuildingIds() != null && condition.getBuildingIds().size() > 0) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("buildingId").in(condition.getBuildingIds())));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1,添加分组条件
|
|
|
+ operations.add(Aggregation.group("deviceId")
|
|
|
+ .last("meterNo").as("meterNo")
|
|
|
+ .last("deviceTypeName").as("deviceTypeName")
|
|
|
+ .last("customerName").as("customerName")
|
|
|
+ .last("communityName").as("communityName")
|
|
|
+ .last("buildingName").as("buildingName")
|
|
|
+ .last("location").as("location")
|
|
|
+ .sum("lastCost").as("waterConsumption"));
|
|
|
+ operations.add(Aggregation.project("meterNo").
|
|
|
+ and("deviceTypeName").as("deviceType").
|
|
|
+ and("customerName").as("customer").
|
|
|
+ and("communityName").as("community").
|
|
|
+ and("buildingName").as("building").
|
|
|
+ and("location").as("location").
|
|
|
+ and("waterConsumption").as("waterConsumption").
|
|
|
+ andExclude("_id"));
|
|
|
+
|
|
|
+ if (condition.getBeginRate() != null) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("waterConsumption").gte(condition.getBeginRate())));
|
|
|
+ }
|
|
|
+ if (condition.getEndRate() != null) {
|
|
|
+ operations.add(Aggregation.match(Criteria.where("waterConsumption").lte(condition.getEndRate())));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ operations.add(Aggregation.count().as("total"));
|
|
|
+
|
|
|
+
|
|
|
+ // 3,聚合查询所有信息
|
|
|
+ Aggregation aggregationTotal = Aggregation.newAggregation(operations);
|
|
|
+ // 4,查询结果总条数
|
|
|
+ AggregationResults<TotalDto> total = mongoTemplate.aggregate(aggregationTotal, "sc_meter_read_record", TotalDto.class);
|
|
|
+ List<TotalDto> totalList = total.getMappedResults();
|
|
|
+ if (totalList != null && totalList.size() > 0) {
|
|
|
+ pagination.setTotal(totalList.get(0).getTotal());
|
|
|
+ }else {
|
|
|
+ pagination.setTotal(0);
|
|
|
+ }
|
|
|
+ operations.remove(operations.size()-1);
|
|
|
+
|
|
|
+
|
|
|
+ if (condition.getSortOrder() != null) {
|
|
|
+ if(condition.getSortOrder().equals("ASC")){
|
|
|
+ operations.add(Aggregation.sort(Sort.Direction.ASC, "waterConsumption"));
|
|
|
+ }
|
|
|
+ if(condition.getSortOrder().equals("DESC")){
|
|
|
+ operations.add(Aggregation.sort(Sort.Direction.DESC, "waterConsumption"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ operations.add(Aggregation.skip(Long.valueOf((pageNum-1) * pageSize)));
|
|
|
+ operations.add(Aggregation.limit(pageSize));
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(operations);
|
|
|
+ AggregationResults<WaterConsumptionDto> aggregationResults = mongoTemplate.aggregate(aggregation, "sc_meter_read_record", WaterConsumptionDto.class);
|
|
|
+ // 5,获取结果
|
|
|
+ List<WaterConsumptionDto> results = aggregationResults.getMappedResults();
|
|
|
+ log.info("end MeterReadRecordService getWaterConsumptionList, result.size = "+results);
|
|
|
+
|
|
|
+ pagination.setList(results);
|
|
|
+ return pagination;
|
|
|
}
|
|
|
|
|
|
@Async
|
|
|
@Override
|
|
|
+ @Deprecated
|
|
|
public void recoverAll(Integer startDate) {
|
|
|
log.info("开始用水量恢复");
|
|
|
- List<Long> allDeviceId = meterReadRecordMapper.getAllDeviceId(startDate);
|
|
|
- for (Long deviceId : allDeviceId) {
|
|
|
- log.info("开始恢复设备id : " + deviceId + " 的用水量");
|
|
|
- List<MeterReadRecord> meterReadRecords = meterReadRecordMapper.getMeterReadRecordByDeviceId(deviceId, startDate);
|
|
|
- int last = 0;
|
|
|
- for (int i = 0; i < meterReadRecords.size(); i++) {
|
|
|
- MeterReadRecord meterReadRecord = meterReadRecords.get(i);
|
|
|
- if (i == 0) {
|
|
|
- if (meterReadRecord.getReadData() == null || meterReadRecord.getReadData().equals("")) {
|
|
|
- meterReadRecord.setLastValid("0");
|
|
|
- meterReadRecord.setLastCost(BigDecimal.ZERO);
|
|
|
- } else {
|
|
|
- meterReadRecord.setLastValid(meterReadRecord.getReadData());
|
|
|
- BigDecimal cost = BigDecimal.valueOf(Double.parseDouble(meterReadRecord.getReadData()));
|
|
|
- meterReadRecord.setLastCost(cost);
|
|
|
- }
|
|
|
- } else {
|
|
|
- MeterReadRecord lastRecord = meterReadRecords.get(last);
|
|
|
- if (meterReadRecord.getReadData() == null || meterReadRecord.getReadData().equals("")) {
|
|
|
- meterReadRecord.setLastValid(lastRecord.getLastValid());
|
|
|
- meterReadRecord.setLastCost(BigDecimal.ZERO);
|
|
|
- } else {
|
|
|
- meterReadRecord.setLastValid(meterReadRecord.getReadData());
|
|
|
- BigDecimal cost = BigDecimal.valueOf(Double.parseDouble(meterReadRecord.getReadData()) - Double.parseDouble(lastRecord.getLastValid()));
|
|
|
- meterReadRecord.setLastCost(cost);
|
|
|
- }
|
|
|
- last++;
|
|
|
- }
|
|
|
- }
|
|
|
- meterReadRecordMapper.batchUpdate(meterReadRecords);
|
|
|
- log.info("结束恢复设备id : " + deviceId + " 的用水量");
|
|
|
- }
|
|
|
+
|
|
|
log.info("结束用水量恢复");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Deprecated
|
|
|
public int createMeterReadRecordForRollback(Date date) {
|
|
|
log.info("begin MeterReadRecordService createMeterReadRecord ,date = "+ JSON.toJSONString(date));
|
|
|
Integer result = 0;
|