|
|
@@ -1,25 +1,29 @@
|
|
|
package com.yunfeiyun.agmp.iotm.device.ybq.serviceImp;
|
|
|
|
|
|
-import cn.hutool.core.bean.BeanUtil;
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.yunfeiyun.agmp.common.core.page.PageDomain;
|
|
|
import com.yunfeiyun.agmp.common.framework.manager.RedisCacheManager;
|
|
|
import com.yunfeiyun.agmp.common.utils.JSONUtils;
|
|
|
import com.yunfeiyun.agmp.common.utils.StringUtils;
|
|
|
-import com.yunfeiyun.agmp.common.utils.uuid.IdUtils;
|
|
|
+import com.yunfeiyun.agmp.iot.common.constant.IotErrorCode;
|
|
|
import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
|
|
|
-import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconfig;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotDevicelasteddata;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.IotYbqEnvData;
|
|
|
-import com.yunfeiyun.agmp.iot.common.model.cmd.CmdGroupModel;
|
|
|
+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;
|
|
|
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;
|
|
|
@@ -44,6 +48,9 @@ public class IotYbqEnvDataServiceImpl extends IotDeviceBaseServiceImpl implement
|
|
|
@Autowired
|
|
|
private RedisCacheManager redisCacheManager;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private IIotDeviceService iotDeviceService;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public void insertData(IotYbqEnvData iotYbqEnvData) {
|
|
|
@@ -58,7 +65,19 @@ public class IotYbqEnvDataServiceImpl extends IotDeviceBaseServiceImpl implement
|
|
|
@Override
|
|
|
public List<IotYbqEnvData> selectIotYbqEnvDataList(IotDeviceDataListReqVo reqVo) {
|
|
|
|
|
|
- Map<String, Object> map = BeanUtil.beanToMap(reqVo);
|
|
|
+ String devBid = reqVo.getDevBid();
|
|
|
+ IotDevice iotDevice = iotDeviceService.selectIotDeviceByDevBid(devBid);
|
|
|
+ if(iotDevice == null) {
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备不存在");
|
|
|
+ }
|
|
|
+ YbqTypeConst ybqTypeConst = YbqTypeConst.getByServiceName(iotDevice.getDevtypeBid());
|
|
|
+ if(ybqTypeConst == null){
|
|
|
+ throw new IotBizException(IotErrorCode.INVALID_DEVICE_ID.getCode(), "设备类型不匹配");
|
|
|
+ }
|
|
|
+ String ybqType = ybqTypeConst.getCode();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ map.put("devBid", devBid);
|
|
|
+ map.put("dateDevType", ybqType);
|
|
|
if (StringUtils.isNotEmpty(reqVo.getStartTime()) && StringUtils.isEmpty(reqVo.getEndTime())) {
|
|
|
map.put("gte_ybqdataCreatedDate", reqVo.getStartTime());
|
|
|
}
|
|
|
@@ -68,8 +87,6 @@ public class IotYbqEnvDataServiceImpl extends IotDeviceBaseServiceImpl implement
|
|
|
if (StringUtils.isNotEmpty(reqVo.getStartTime()) && StringUtils.isNotEmpty(reqVo.getEndTime())) {
|
|
|
map.put("time_ybqdataCreatedDate", reqVo.getStartTime() + "," + reqVo.getEndTime());
|
|
|
}
|
|
|
- map.remove("startTime");
|
|
|
- map.remove("endTime");
|
|
|
List<IotYbqEnvData> iotYbqEnvDataResVos = mongoService.findAll(IotYbqEnvData.class, map, "ybqdataCreatedDate desc");
|
|
|
return iotYbqEnvDataResVos;
|
|
|
}
|
|
|
@@ -113,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) {
|
|
|
|
|
|
}
|