Просмотр исходного кода

Merge remote-tracking branch 'origin/develop' into develop

liuyaowen 10 месяцев назад
Родитель
Сommit
9368883313

+ 4 - 1
src/main/java/com/yunfeiyun/agmp/iotm/device/bzy/controller/IotDeviceBzyController.java

@@ -202,8 +202,11 @@ public class IotDeviceBzyController extends BaseController {
         if(iotDevice == null){
             throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备不存在");
         }
+        JSONObject jsonObject = new JSONObject();
         IotDeviceconfig iotDeviceconfig = iIotDeviceconfigService.selectIotDeviceConfigByDevBid(devBid);
-        JSONObject jsonObject = JSONObject.parseObject(iotDeviceconfig.getDevcfgContext());
+        if(iotDeviceconfig != null){
+            jsonObject = JSONObject.parseObject(iotDeviceconfig.getDevcfgContext());
+        }
         return AjaxResult.success(jsonObject);
     }
 

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iotm/device/common/domin/IotDeviceDataListReqVo.java

@@ -51,6 +51,6 @@ public class IotDeviceDataListReqVo
     /**
      * 是否不分页
      */
-    private boolean isNotPage;
+    private Boolean isNotPage;
 
 }

+ 3 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/pest/controller/IotDeviceCbdController.java

@@ -176,6 +176,9 @@ public class IotDeviceCbdController extends BaseController {
         devBidList.add(devInfo.getDevBid());
         Map<String, Long> imgCountMap = getImgCountMap(devBidList);
         devInfo.setImgCount(imgCountMap.getOrDefault(devBid, 0L));
+        if(!"1".equals(devInfo.getDevRecogtype())){
+            devInfo.setDevRecogtype("0");
+        }
         return success(devInfo);
     }
 

+ 1 - 1
src/main/java/com/yunfeiyun/agmp/iotm/device/qxsqz/controller/IotDeviceQxzController.java

@@ -203,7 +203,7 @@ public class IotDeviceQxzController extends BaseController {
         iotDeviceDataListReqVo.setDevBid(devBid);
         iotDeviceDataListReqVo.setStartTime(startTime);
         iotDeviceDataListReqVo.setEndTime(endTime);
-        iotDeviceDataListReqVo.setNotPage(true);
+        iotDeviceDataListReqVo.setIsNotPage(true);
 
         IotDeviceBaseFunReqVo iotDeviceBaseFunReqVo = new IotDeviceBaseFunReqVo();
         iotDeviceBaseFunReqVo.setDevBid(reqVo.getDevBid());

+ 28 - 11
src/main/java/com/yunfeiyun/agmp/iotm/device/qxsqz/service/IIotQxzCommService.java

@@ -89,7 +89,7 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
         rspData.setMsg(ErrorCode.SUCCESS.getMessage());
         rspData.setData(new ArrayList<>());
         rspData.setTotal(0);
-        boolean isNotPage = reqVo.isNotPage();
+        Boolean isNotPage = reqVo.getIsNotPage();
 
         IotDevice findDevice = iotDeviceService.selectIotDeviceByDevBid(reqVo.getDevBid());
 
@@ -111,11 +111,17 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
             );
         }
 
+        AddFieldsOperation addFieldsOperation = Aggregation.addFields().addField("numValue")
+               .withValueOfExpression("{$convert: {input: '$eValue', to: 'double', onError: -99, onNull: -99}}").build();
         MatchOperation matchOperation = Aggregation.match(criteria);
 
         GroupOperation groupOperation = Aggregation.group("time")
+                .min("numValue").as("minValue")
                 .push("$$ROOT").as("data");
 
+        Criteria criteria2 = new Criteria().and("minValue").gt(-99);
+        MatchOperation matchOperation2 = Aggregation.match(criteria2);
+
         ProjectionOperation projectionOperationResult = Aggregation.project("_id")
                 .and("_id").as("time")
                 .and("data").as("dataList");
@@ -124,7 +130,9 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
 
         Aggregation aggregation = Aggregation.newAggregation(
                 matchOperation,
+                addFieldsOperation,
                 groupOperation,
+                matchOperation2,
                 projectionOperationResult,
                 sortOperation
         );
@@ -134,7 +142,7 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
         }
         long total = 0;
         List<QxzDataListDto> qxzDataListDtoList = new ArrayList<>();
-        if(isNotPage) {
+        if(isNotPage != null && isNotPage) {
             List<QxzDataListDto> dtoList = mongoService.aggregate(tableClass, aggregation, QxzDataListDto.class);
             if(dtoList != null){
                 total = dtoList.size();
@@ -204,19 +212,25 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
                 .and("eName").as("eName")
                 .and("eNum").as("eNum")
                 .and("eKey").as("eKey")
+                .and("time").as("oldTime")
                 .andExpression("{$convert: {input: '$eValue', to: 'double', onError: -99, onNull: -99}}").as("eValue")
                 .andExpression("{$dateTrunc: {date: {$toDate: '$time'}, unit:'" + unit + "'}}").as("time");
 
-        GroupOperation groupOperation = Aggregation.group("eName", "eNum", "eKey")
+        GroupOperation groupOperation1 = Aggregation.group("oldTime")
+                .min("eValue").as("minValue")
                 .push("$$ROOT").as("data");
 
-        UnwindOperation unwindOperation = Aggregation.unwind("$data");
+        Criteria criteria2 = new Criteria().and("minValue").gt(-99);
+        MatchOperation matchOperation2 = Aggregation.match(criteria2);
+
+        UnwindOperation unwindOperation1 = Aggregation.unwind("$data");
+
         GroupOperation groupOperation2 = Aggregation.group("data.eName", "data.eNum", "data.eKey", "data.time")
-                .avg("data.eValue").as("eValue");
+                .avg("data.eValue").as("avgValue");
 
         SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "_id.time");
         JSONArray roundArray = new JSONArray();
-        roundArray.add("$eValue");
+        roundArray.add("$avgValue");
         roundArray.add(2);
         Map<String, Object> roundMap = new HashMap<>();
         roundMap.put("$round", roundArray);
@@ -227,14 +241,14 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
         dateMap.put("$dateSubtract", new HashMap<String, Object>(){{
             put("startDate", "$_id.time");
             put("unit", "hour");
-            put("amount", 8);
+            put("amount", -8);
         }});
 
         Map<String, Object> pushMap = new HashMap<>();
         pushMap.put("number", pushMapStr);
         pushMap.put("time", dateMap);
 
-        GroupOperation groupOperation3 = Aggregation.group("_id.eName", "_id.eNum", "_id.eKey")
+        GroupOperation groupOperation4 = Aggregation.group("_id.eName", "_id.eNum", "_id.eKey")
                 .push(pushMap).as("dataList");
 
         ProjectionOperation projectionOperation2 = Aggregation.project()
@@ -247,11 +261,12 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
         Aggregation aggregation = Aggregation.newAggregation(
                 matchOperation,
                 projectionOperation,
-                groupOperation,
-                unwindOperation,
+                groupOperation1,
+                matchOperation2,
+                unwindOperation1,
                 groupOperation2,
                 sortOperation,
-                groupOperation3,
+                groupOperation4,
                 projectionOperation2,
                 sortOperation2
         );
@@ -291,6 +306,7 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
                 .addField("eValueNum")
                 .withValueOfExpression("{$convert: {input: '$eValue', to: 'double', onError: -99, onNull: -99}}")
                 .build();
+        MatchOperation matchOperation2 = Aggregation.match(new Criteria().and("eValueNum").gt(-99));
 
         SortOperation sortOperation = Aggregation.sort(Sort.Direction.ASC, "eValueNum");
 
@@ -321,6 +337,7 @@ public class IIotQxzCommService extends IotDeviceBaseServiceImpl implements IotD
         Aggregation aggregation = Aggregation.newAggregation(
                 matchOperation,
                 addFieldsOperation,
+                matchOperation2,
                 sortOperation,
                 groupOperation,
                 sortOperation2,

+ 26 - 23
src/main/java/com/yunfeiyun/agmp/iotm/device/ybq/controller/IotYbqController.java

@@ -27,7 +27,6 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -172,29 +171,33 @@ public class IotYbqController extends BaseController {
      */
     @RequestMapping("/data/env/stat")
     public AjaxResult getEnvStatList(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(), "数据类型不可为空");
-        }
 
-        // 预测时间可能会被更新,所以用修改时间,创建时候这个值会默认有
-        Map<String, Object> map = new HashMap<>();
-        map.put("devBid", devBid);
-        map.put("dateDevType", ybqType);
-        if (StringUtils.isNotEmpty(reqVo.getStartDate()) && StringUtils.isEmpty(reqVo.getEndDate())) {
-            map.put("gte_ybqdataCreatedDate", reqVo.getStartDate());
-        }
-        if (StringUtils.isEmpty(reqVo.getStartDate()) && StringUtils.isNotEmpty(reqVo.getEndDate())) {
-            map.put("lte_ybqdataCreatedDate", reqVo.getEndDate());
-        }
-        if (StringUtils.isNotEmpty(reqVo.getStartDate()) && StringUtils.isNotEmpty(reqVo.getEndDate())) {
-            map.put("time_ybqdataCreatedDate", reqVo.getStartDate() + "," + reqVo.getEndDate());
-        }
-        return AjaxResult.success(mongoService.findAll(IotYbqEnvData.class, map, "ybqdataCreatedDate"));
+        List<IotYbqEnvData> iotYbqEnvDataList = iotYbqEnvDataService.selectIotYbqEnvStatList(reqVo);
+        return AjaxResult.success(iotYbqEnvDataList);
+//
+//        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(), "数据类型不可为空");
+//        }
+//
+//        // 预测时间可能会被更新,所以用修改时间,创建时候这个值会默认有
+//        Map<String, Object> map = new HashMap<>();
+//        map.put("devBid", devBid);
+//        map.put("dateDevType", ybqType);
+//        if (StringUtils.isNotEmpty(reqVo.getStartDate()) && StringUtils.isEmpty(reqVo.getEndDate())) {
+//            map.put("gte_ybqdataCreatedDate", reqVo.getStartDate());
+//        }
+//        if (StringUtils.isEmpty(reqVo.getStartDate()) && StringUtils.isNotEmpty(reqVo.getEndDate())) {
+//            map.put("lte_ybqdataCreatedDate", reqVo.getEndDate());
+//        }
+//        if (StringUtils.isNotEmpty(reqVo.getStartDate()) && StringUtils.isNotEmpty(reqVo.getEndDate())) {
+//            map.put("time_ybqdataCreatedDate", reqVo.getStartDate() + "," + reqVo.getEndDate());
+//        }
+//        return AjaxResult.success(mongoService.findAll(IotYbqEnvData.class, map, "ybqdataCreatedDate"));
     }
 
     @GetMapping("/refresh/{devBid}")

+ 3 - 0
src/main/java/com/yunfeiyun/agmp/iotm/device/ybq/service/IotYbqEnvDataService.java

@@ -1,6 +1,7 @@
 package com.yunfeiyun.agmp.iotm.device.ybq.service;
 
 import com.yunfeiyun.agmp.iot.common.domain.IotYbqEnvData;
+import com.yunfeiyun.agmp.iot.common.domain.reqvo.IotYbqdataReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.domin.IotDeviceDataListReqVo;
 import com.yunfeiyun.agmp.iotm.device.common.service.IotDeviceBaseService;
 
@@ -25,4 +26,6 @@ public interface IotYbqEnvDataService  extends IotDeviceBaseService {
     List<IotYbqEnvData> selectIotYbqEnvDataList(IotDeviceDataListReqVo reqVo);
 
     IotYbqEnvData selectIotYbqEnvLastedData(String devBid);
+
+    List<IotYbqEnvData> selectIotYbqEnvStatList(IotYbqdataReqVo reqVo);
 }

+ 122 - 8
src/main/java/com/yunfeiyun/agmp/iotm/device/ybq/serviceImp/IotYbqEnvDataServiceImpl.java

@@ -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) {
 
     }