package com.njcn.device.pq.utils;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.device.pq.pojo.po.LineDataIntegrity;
import com.njcn.device.pq.pojo.po.TerminalOnlineRateData;
import com.njcn.device.pq.pojo.vo.LineIntegrityDataVO;
import com.njcn.device.pq.pojo.vo.TerminalOnlineRateDataVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 监测点完整性、终端在线率
 *
 * @author yzh
 * @date 2022/9/26
 */

public class DataStatisticsUtil {

    /**
     * 获取监测点数据完整性
     *
     * @param percentageOfCompleteDatas influxdb数据
     * @param lineList                  监测点集合
     */
    public static void getLineDataIntegrity(List<LineDataIntegrity> percentageOfCompleteDatas, List<LineIntegrityDataVO> lineList) {

        if (CollectionUtil.isNotEmpty(percentageOfCompleteDatas)) {

            // 根据监测点id分类
            Map<String, List<LineDataIntegrity>> groupByLineId = percentageOfCompleteDatas.stream().collect(Collectors.groupingBy(LineDataIntegrity::getLineId));
            // 新建集合处理数据
            List<LineDataIntegrity> lineIntegrityData = new ArrayList<>();
            // 获取map中的key（监测点id集合）
            Set<String> lineIds = groupByLineId.keySet();

            for (String lineId : lineIds) {
                // 创建对象处理数据
                LineDataIntegrity integrity = new LineDataIntegrity();
                // 期望值
                Double due = 0.0;
                // 实际值
                Double real = 0.0;

                // 取出值
                List<LineDataIntegrity> lineDataIntegrities = groupByLineId.get(lineId);

                for (LineDataIntegrity dataIntegrity : lineDataIntegrities) {
                    due += dataIntegrity.getDue();
                    real += dataIntegrity.getReal();
                }
                // 求出监测点数据完整性
                double integrityDataDouble = real / due * 100;
                String integrityDataString = String.format("%.2f", integrityDataDouble);
                integrity.setIntegrityData(Double.parseDouble(integrityDataString));
                integrity.setLineId(lineId);
                lineIntegrityData.add(integrity);
            }

            lineList = lineList.stream().peek(integrityDataVO -> {
                for (LineDataIntegrity lineIntegrityDatum : lineIntegrityData) {
                    if (integrityDataVO.getId().equals(lineIntegrityDatum.getLineId())) {
                        if (lineIntegrityDatum.getIntegrityData() != null) {
                            integrityDataVO.setIntegrityData(lineIntegrityDatum.getIntegrityData());
                        } else {
                            // 为空则给对应的监测点的监测点合设置为魔数
                            integrityDataVO.setIntegrityData(3.14159);
                        }
                    }
                }
            }).collect(Collectors.toList());
        } else {
            lineList = lineList.stream().peek(integrityDataVO -> integrityDataVO.setIntegrityData(3.14159)).collect(Collectors.toList());
        }
    }

    /**
     * 获取区域数据完整性
     *
     * @param percentageOfCompleteDatas
     */
    public static double getAreaDataIntegrity(List<LineDataIntegrity> percentageOfCompleteDatas) {
        double data = 0.0;
        if (CollectionUtil.isNotEmpty(percentageOfCompleteDatas)) {
            // 按时间分类
            Map<String, List<LineDataIntegrity>> groupByTime = percentageOfCompleteDatas.stream().peek(lineDataIntegrity -> {
                double integrityDataDouble = lineDataIntegrity.getReal() / lineDataIntegrity.getDue() * 100;
                String integrityDataString = String.format("%.2f", integrityDataDouble);
                lineDataIntegrity.setIntegrityData(Double.parseDouble(integrityDataString));
            }).collect(Collectors.groupingBy(LineDataIntegrity::getTime));
            Set<String> times = groupByTime.keySet();
            for (String time : times) {
                double integrityData = 0.0;
                List<LineDataIntegrity> lineDataIntegrities = groupByTime.get(time);
                int size = lineDataIntegrities.size();
                for (LineDataIntegrity lineData : lineDataIntegrities) {
                    integrityData += lineData.getIntegrityData();
                }

                Double integrityDataDouble = (integrityData / size);
                String integrityDataString = String.format("%.2f", integrityDataDouble);
                data = Double.parseDouble(integrityDataString);
            }
        } else {
            data = 3.14159;
        }
        return data;
    }

    /**
     * 获取终端在线率
     *
     * @param terminalOnlineRateList
     * @param deviceList
     */
    public static void getTerminalOnlineRateData(List<TerminalOnlineRateData> terminalOnlineRateList, List<TerminalOnlineRateDataVO> deviceList) {

        if (CollectionUtil.isNotEmpty(terminalOnlineRateList)) {
            // 根据监测点id分类

            deviceList = deviceList.stream().peek(onlineRateDataVO -> {
                boolean flag = false;
                for (TerminalOnlineRateData onlineRateData : terminalOnlineRateList) {
                    if (onlineRateDataVO.getId().equals(onlineRateData.getDevId())) {
                        flag = true;
                        onlineRateDataVO.setOnlineRate( dataLimits(onlineRateData.getOnlineRate()));
                        break;
                    }
                }
                if (!flag) {
                    onlineRateDataVO.setOnlineRate(3.14159);
                }
            }).collect(Collectors.toList());
        } else {
            deviceList = deviceList.stream().peek(onlineRateDataVO -> onlineRateDataVO.setOnlineRate(3.14159)).collect(Collectors.toList());
        }
    }

    /**
     *
     * @param data
     * @return
     */
    public static Double dataLimits(Double data){
        return data>100?100:data;
    }

}


