jinpengyong
2023-12-20 8a9366f0ff6202144ef2cb1765b873d0f0c3c626
screen-api/src/main/java/com/moral/api/service/impl/DataDisplayServiceImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jhlabs.composite.AverageComposite;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.HistoryMonthlyMapper;
@@ -13,6 +14,7 @@
import com.moral.api.pojo.form.dataDisplay.MonitorPointDataDisplayForm;
import com.moral.api.pojo.form.dataDisplay.SensorComparisonDisplayForm;
import com.moral.api.service.*;
import com.moral.api.utils.GetCenterPointFromListOfCoordinates;
import com.moral.constant.Constants;
import com.moral.constant.SeparateTableType;
import com.moral.pojo.AQI;
@@ -21,6 +23,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import reactor.core.publisher.Flux;
import java.util.*;
import java.util.stream.Collectors;
@@ -446,6 +449,106 @@
        return distrinList(rsHeatMap);
    }
    @Override
    public List<HeatMapDTO> getHeatMapDataV2(Integer id, String startTime, String type, String form) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("start",startTime);
        map.put("type","$."+ type);
        //获取用户信息
//        Map<String, Object> userInfo = (Map<String, Object>) TokenUtils.getUserInfo();
//        Map<String, Object> orgInfo = (Map<String, Object>) userInfo.get("organization");
//        Integer organizationId = (Integer) orgInfo.get("id");
        //大账号
        ArrayList<Integer> list = new ArrayList<>();
        list.add(id);
//        if (organizationId==24){
////            LambdaQueryWrapper<Organization> wrapper = new LambdaQueryWrapper<>();
////            wrapper.eq(Organization::getIsDelete,Constants.NOT_DELETE);
////            wrapper.eq(Organization::getId,id);
////            List<Organization> organizations = organizationMapper.selectList(wrapper);
////            List<Integer> collect = organizations.stream().map(organization -> organization.getId()).collect(Collectors.toList());
////            list.addAll(collect);
//            list.add(id);
//        }else {
//            list.add(id);
//        }
        ArrayList<HeatMapDTO> rsHeatMap = new ArrayList<>();
        if (form.equals("hour")){ //小时
            log.info(""+startTime);
            String dateString = DateUtils.stringToDateString(startTime, DateUtils.yyyy_MM_dd_HH_EN, DateUtils.yyyyMM_EN);
//            Date date1 = DateUtils.getDate(startTime, DateUtils.yyyy_MM_dd_HH_EN);
            log.info(""+dateString);
            map.put("organizationIds",list);
            map.put("tableName","_"+dateString);
            List<HeatMapDTO> heatMap = deviceMapper.getHeatMapV1(map);
            log.info(heatMap.size()+"");
            List<HeatMapDTO> collect1 = heatMap.stream().filter(d -> d.getTime() != null).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(collect1)){
                return null;
            }
            getHeatMap(heatMap);
            rsHeatMap.addAll(heatMap);
        }else { //天
            map.put("organizationIds",list);
            List<HeatMapDTO> heatMap = deviceMapper.getHeatMapV1(map);
            List<HeatMapDTO> collect1 = heatMap.stream().filter(d -> d.getTime() != null).collect(Collectors.toList());
            if (ObjectUtils.isEmpty(collect1)){
                return null;
            }
            getHeatMap(heatMap);
            rsHeatMap.addAll(heatMap);
        }
        return distrinList(rsHeatMap);
    }
    //数据处理
    private void getHeatMap(List<HeatMapDTO> heatMap) {
        //数据过滤
        List<HeatMapDTO> collect1 = heatMap.stream().filter(d -> d.getGroupId() != null).collect(Collectors.toList());
        if (ObjectUtils.isEmpty(collect1)){
            return;
        }
        //数据分类
        Map<Integer, List<HeatMapDTO>> collect = collect1.parallelStream().collect(Collectors.groupingBy(o ->o.getGroupId()));
        Set<Integer> integers = collect.keySet();
        int i = 0 ;
        for (Integer integer : integers) {
            ArrayList<Double> doubleArrayList = new ArrayList<>();
            ArrayList<GeoCoordinate> geoCoordinates = new ArrayList<>();
            List<HeatMapDTO> heatMapDTOS = collect.get(integer);
            for (HeatMapDTO heatMapDTO : heatMapDTOS) {
                GeoCoordinate geoCoordinate = new GeoCoordinate();
                doubleArrayList.add(heatMapDTO.getCount());
//                heatMapDTO.setCount(heatMapDTO.getCount());
                heatMapDTO.setCount(0.0);
                geoCoordinate.setLongitude(heatMapDTO.getLng());
                geoCoordinate.setLatitude(heatMapDTO.getLat());
                geoCoordinates.add(geoCoordinate);
            }
            if (!ObjectUtils.isEmpty(doubleArrayList)){
                HeatMapDTO heatMapDTO = new HeatMapDTO();
                //计算均值集合
                double asDouble = doubleArrayList.stream().mapToDouble(Double::valueOf).max().getAsDouble();
                //获取中心点
                GeoCoordinate centerPoint = GetCenterPointFromListOfCoordinates.getCenterPoint(geoCoordinates);
                heatMapDTO.setCount(asDouble);
                heatMapDTO.setLng(centerPoint.getLongitude());
                heatMapDTO.setLat(centerPoint.getLatitude());
                heatMapDTO.setMac(i+"");
                heatMap.add(heatMapDTO);
                i++;
            }
        }
    }
    /**
     * 字段去重
     * @param responseList
@@ -463,8 +566,6 @@
                rsMap.add(heatMapDTO);
            }
        }
        log.info(responseList.size()+"");
        log.info(rsMap.size()+"");
        return rsMap;