|
|
@@ -9,6 +9,7 @@ import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevicelasteddata;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotYbqEnvData;
|
|
|
+import com.yunfeiyun.agmp.iot.common.domain.reqvo.IotYbqdataReqVo;
|
|
|
import com.yunfeiyun.agmp.iot.common.enums.ybq.YbqTypeConst;
|
|
|
import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
import com.yunfeiyun.agmp.iot.common.service.MongoService;
|
|
|
@@ -16,9 +17,13 @@ import com.yunfeiyun.agmp.iotm.device.common.domin.DeviceRefreshDto;
|
|
|
import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
|
|
|
import com.yunfeiyun.agmp.iotm.device.common.service.impl.IotDeviceBaseServiceImpl;
|
|
|
import com.yunfeiyun.agmp.iotm.device.ybq.service.IotYbqEnvDataService;
|
|
|
+import com.yunfeiyun.agmp.iotm.util.MongoUtil;
|
|
|
import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.*;
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.data.redis.core.ValueOperations;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -125,6 +130,103 @@ public class IotYbqEnvDataServiceImpl extends IotDeviceBaseServiceImpl implement
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<IotYbqEnvData> selectIotYbqEnvStatList(IotYbqdataReqVo reqVo) {
|
|
|
+ // 预测时间可能会被更新,所以用修改时间,创建时候这个值会默认有
|
|
|
+ String devBid = reqVo.getDevBid();
|
|
|
+ if (StringUtils.isEmpty(devBid)) {
|
|
|
+ throw new IotBizException(IotErrorCode.FAILURE.getCode(), "设备id不可为空");
|
|
|
+ }
|
|
|
+ String ybqType = reqVo.getDateDevType();
|
|
|
+ if (StringUtils.isEmpty(ybqType)) {
|
|
|
+ throw new IotBizException(IotErrorCode.FAILURE.getCode(), "数据类型不可为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ String startDate = reqVo.getStartDate();
|
|
|
+ String endDate = reqVo.getEndDate();
|
|
|
+ String unit = "day";
|
|
|
+
|
|
|
+ Criteria criteria = new Criteria().and("devBid").is(devBid);
|
|
|
+ if(startDate == null || endDate == null){
|
|
|
+ throw new IotBizException(IotErrorCode.FAILURE.getCode(), "时间不可为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ //必须设置起止时间
|
|
|
+ if(StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)){
|
|
|
+ //限制 起止时间
|
|
|
+ criteria = criteria.andOperator(
|
|
|
+ Criteria.where("ybqdataCreatedDate").gte(startDate),
|
|
|
+ Criteria.where("ybqdataCreatedDate").lte(endDate)
|
|
|
+ );
|
|
|
+ //选择 数据粒度
|
|
|
+ unit = MongoUtil.getDateTruncUnit(startDate, endDate);
|
|
|
+ }
|
|
|
+ MatchOperation matchOperation = Aggregation.match(criteria);
|
|
|
+
|
|
|
+ Map<String, String> keyMap = new HashMap<>();
|
|
|
+// keyMap.put("ybqdataContent.tuRangDaoDianLv", "tuRangDaoDianLv");
|
|
|
+// keyMap.put("ybqdataContent.twentyFiveTuRangHanShuiLiang", "twentyFiveTuRangHanShuiLiang");
|
|
|
+// keyMap.put("ybqdataContent.twentyFiveTuRangWenDu", "twentyFiveTuRangWenDu");
|
|
|
+// keyMap.put("ybqdataContent.dianChiDianYa", "dianChiDianYa");
|
|
|
+// keyMap.put("ybqdataContent.fiftyTuRangHanShuiLiang", "fiftyTuRangHanShuiLiang");
|
|
|
+// keyMap.put("ybqdataContent.fiftyTuRangWenDu", "fiftyTuRangWenDu");
|
|
|
+// keyMap.put("ybqdataContent.fiveTuRangHanShuiLiang", "fiveTuRangHanShuiLiang");
|
|
|
+// keyMap.put("ybqdataContent.fiveTuRangWenDu", "fiveTuRangWenDu");
|
|
|
+ keyMap.put("kongQiWenDu", "kongQiWenDu");
|
|
|
+ keyMap.put("luDianWenDu", "luDianWenDu");
|
|
|
+ keyMap.put("riZhaoShiShu", "riZhaoShiShu");
|
|
|
+ keyMap.put("leafWater", "leafWater");
|
|
|
+ keyMap.put("tenTuRangHanShuiLiang", "tenTuRangHanShuiLiang");
|
|
|
+ keyMap.put("tenTuRangWenDu", "tenTuRangWenDu");
|
|
|
+ keyMap.put("tianJiangYuLiang", "tianJiangYuLiang");
|
|
|
+ keyMap.put("twentyTuRangHanShuiLiang", "twentyTuRangHanShuiLiang");
|
|
|
+ keyMap.put("twentyTuRangWenDu", "twentyTuRangWenDu");
|
|
|
+ keyMap.put("xiangDuiShiDu", "xiangDuiShiDu");
|
|
|
+ keyMap.put("xiaoShiJiangYuLiang", "xiaoShiJiangYuLiang");
|
|
|
+
|
|
|
+ ProjectionOperation projectionOperation = Aggregation.project()
|
|
|
+ .and("collectTime").as("collectTime")
|
|
|
+ .andExpression("{$dateTrunc: {date: {'$dateFromString': {'dateString': '$collectTime', 'format': '%Y-%m-%d %H:%M:%S'}}, unit:'" + unit + "'}}").as("dateTime");
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> entry : keyMap.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String alias = entry.getValue();
|
|
|
+ projectionOperation = projectionOperation.andExpression(
|
|
|
+ "{$convert: {input: '$" + key + "', to: 'double', onError: -99, onNull: -99}}").as(alias);
|
|
|
+ }
|
|
|
+
|
|
|
+ GroupOperation groupOperation = Aggregation.group("dateTime");
|
|
|
+ for (Map.Entry<String, String> entry : keyMap.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String alias = entry.getValue();
|
|
|
+ groupOperation = groupOperation.avg(key).as(alias);
|
|
|
+ }
|
|
|
+
|
|
|
+ SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "_id");
|
|
|
+
|
|
|
+ ProjectionOperation projectionOperation2 = Aggregation.project()
|
|
|
+ .andExclude("_id")
|
|
|
+ .andExpression("{$dateToString: {format:'%Y-%m-%d %H:%M:%S', date: '$_id'}}").as("collectTime");
|
|
|
+ for (Map.Entry<String, String> entry : keyMap.entrySet()) {
|
|
|
+ String key = entry.getKey();
|
|
|
+ String alias = entry.getValue();
|
|
|
+ projectionOperation2 = projectionOperation2.andExpression(
|
|
|
+ "{$round: {'$" + key + "', 2}}").as(alias);
|
|
|
+ }
|
|
|
+
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ matchOperation,
|
|
|
+ projectionOperation,
|
|
|
+ groupOperation,
|
|
|
+ sortOperation,
|
|
|
+ projectionOperation2
|
|
|
+ );
|
|
|
+
|
|
|
+ List<IotYbqEnvData> dataList = mongoService.aggregate(IotYbqEnvData.class, aggregation, IotYbqEnvData.class);
|
|
|
+
|
|
|
+ return dataList;
|
|
|
+ }
|
|
|
+
|
|
|
public void refresh(DeviceRefreshDto reqVo) {
|
|
|
|
|
|
}
|