package com.njcn.harmonic.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.njcn.common.config.GeneralInfo;
import com.njcn.device.pq.api.GeneralDeviceInfoClient;
import com.njcn.device.pq.pojo.dto.GeneralDeviceDTO;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.param.OnlineRateParam;
import com.njcn.device.pq.pojo.vo.RStatOnlineRateVO;
import com.njcn.harmonic.mapper.OnlineRateDataMapper;
import com.njcn.harmonic.pojo.dto.PublicDTO;
import com.njcn.harmonic.pojo.vo.OnlineRateCensusVO;
import com.njcn.harmonic.pojo.vo.OnlineRateVO;
import com.njcn.harmonic.service.OnlineRateDataService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2021/12/31 9:50
 */
@Service
@AllArgsConstructor
public class OnlineRateDataServiceImpl implements OnlineRateDataService {

    private final OnlineRateDataMapper onlineRateDataMapper;

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final GeneralInfo generalInfo;

    @Override
    public List<OnlineRateVO> getOnlineRateData(DeviceInfoParam.BusinessParam onlineRateParam) {
        List<OnlineRateVO> onlineRateList = new ArrayList<>();

        //按照条件获取运行终端综合信息
        List<GeneralDeviceDTO> deviceDTOList = generalDeviceInfoClient.getPracticalRunDeviceInfo(new DeviceInfoParam(onlineRateParam.getStatisticalType(), onlineRateParam.getDeptIndex(), generalInfo.getMicroServiceName(), null, onlineRateParam.getManufacturer(), null)).getData();
        if (!CollectionUtils.isEmpty(deviceDTOList)){
            for (GeneralDeviceDTO generalDeviceDTO : deviceDTOList) {
                if (CollectionUtils.isEmpty(generalDeviceDTO.getDeviceIndexes())){
                    continue;
                }
                OnlineRateVO onlineRateVO = new OnlineRateVO();
                onlineRateVO.setId(generalDeviceDTO.getIndex());
                onlineRateVO.setName(generalDeviceDTO.getName());
                List<String> deviceIndexes = generalDeviceDTO.getDeviceIndexes();
                //查找在线率
                List<PublicDTO> condition = getCondition(deviceIndexes, onlineRateParam.getSearchBeginTime(), onlineRateParam.getSearchEndTime());
                onlineRateVO.setOnlineRateData(condition.stream().mapToDouble(PublicDTO::getData).average().orElse(3.14159));
                //组装表格树
                List<OnlineRateVO> treeList = getTreeData(deviceIndexes, onlineRateParam);
                onlineRateVO.setChildren(treeList);
                onlineRateList.add(onlineRateVO);
            }
        }

        return onlineRateList;
    }

    @Override
    public OnlineRateCensusVO getOnlineRateDataCensus(DeviceInfoParam.CompareBusinessParam onlineRateCensusParam) {
        OnlineRateCensusVO onlineRateCensusVO = new OnlineRateCensusVO();
        onlineRateCensusParam.setServerName(generalInfo.getMicroServiceName());
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceInfoClient.getPracticalRunDeviceInfo(onlineRateCensusParam).getData();
        List<String> type = new ArrayList<>();
        List<Double> single = new ArrayList<>(), ratio = new ArrayList<>();
        if (!CollectionUtils.isEmpty(generalDeviceDTOList)) {
            for (GeneralDeviceDTO generalDeviceDTO: generalDeviceDTOList){
                List<String> deviceIndexes = generalDeviceDTO.getDeviceIndexes();
                if (CollectionUtils.isEmpty(deviceIndexes)) {
                    continue;
                }
                type.add(generalDeviceDTO.getName());
                //根据终端索引集查询在线率
                List<PublicDTO> onlineRateList = getCondition(deviceIndexes, onlineRateCensusParam.getSearchBeginTime(), onlineRateCensusParam.getSearchEndTime());
                single.add(onlineRateList.stream().mapToDouble(PublicDTO::getData).average().orElse(3.14159));
                //如果存在需要比较的时间，再获取对应数据
                if(StrUtil.isNotBlank(onlineRateCensusParam.getPeriodBeginTime()) && StrUtil.isNotBlank(onlineRateCensusParam.getPeriodEndTime())){
                    List<PublicDTO> compareData = getCondition(deviceIndexes, onlineRateCensusParam.getPeriodBeginTime(), onlineRateCensusParam.getPeriodEndTime());
                    ratio.add(compareData.stream().mapToDouble(PublicDTO::getData).average().orElse(3.14159));
                }
            }
        }
        onlineRateCensusVO.setType(type);
        onlineRateCensusVO.setSingle(single);
        onlineRateCensusVO.setRatio(ratio);


        return onlineRateCensusVO;
    }

    /**
     * 获取每层树数据
     */
    public List<OnlineRateVO> getTreeData(List<String> deviceIndexes, DeviceInfoParam.BusinessParam onlineRateParam) {
        //终端集合
        List<OnlineRateVO> deviceList = onlineRateDataMapper.getOnlineRateData(deviceIndexes);
        //变电站集合
        List<OnlineRateVO> substationList = onlineRateDataMapper.getLineInfoByList(deviceList.stream().map(OnlineRateVO::getPid).distinct().collect(Collectors.toList()));
        //供电公司集合
        List<OnlineRateVO> powerCompanyList = onlineRateDataMapper.getLineInfoByList(substationList.stream().map(OnlineRateVO::getPid).distinct().collect(Collectors.toList()));

        //查询终端的在线率
        List<PublicDTO> onlineRateList = getCondition(deviceIndexes, onlineRateParam.getSearchBeginTime(), onlineRateParam.getSearchEndTime());
        //根据终端集合组装终端的在线率
        if (!CollectionUtils.isEmpty(deviceList)) {
            deviceList.stream().map(list1 -> onlineRateList.stream().filter(list2 -> Objects.equals(list1.getId(), list2.getId())).findAny().map(m -> {
                list1.setOnlineRateData(m.getData());
                return list1;
            })).collect(Collectors.toList());
        }
        substationList.stream().peek(item -> item.setChildren(getChildCategoryList(item, deviceList))).collect(Collectors.toList());
        setSubstationListOnlineRate(substationList);
        powerCompanyList.stream().peek(item -> item.setChildren(getChildCategoryList(item, substationList))).collect(Collectors.toList());
        setPowerCompanyListOnlineRate(powerCompanyList);
        return powerCompanyList;
    }

    /**
     * 组装树
     */
    private List<OnlineRateVO> getChildCategoryList(OnlineRateVO item, List<OnlineRateVO> all) {
        return all.stream().filter(allItem -> allItem.getPid().equals(item.getId())).collect(Collectors.toList());
    }


    private List<PublicDTO> getCondition(List<String> deviceIndexes, String startTime, String endTime) {
        List<PublicDTO> publicDTOList = new ArrayList<>();
        OnlineRateParam param=new OnlineRateParam();
        param.setIds(deviceIndexes);
        param.setStartTime(DateUtil.beginOfDay(DateUtil.parse(startTime)).toString());
        param.setEndTime( DateUtil.endOfDay(DateUtil.parse(endTime)).toString());
        List<RStatOnlineRateVO> data = generalDeviceInfoClient.getOnlineRateByDevIds(param).getData();
        if (CollUtil.isNotEmpty(data)) {
            data.forEach(po -> {
                PublicDTO publicDTO = new PublicDTO();
                //终端在线率 保留两位小数
                Double onlineRate = po.getOnlineRate() == null ? null : BigDecimal.valueOf(Double.parseDouble(po.getOnlineRate().toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                publicDTO.setId(po.getDevIndex());
                publicDTO.setData(onlineRate);
                publicDTOList.add(publicDTO);
            });
        }
        return publicDTOList;


//        //组装sql语句
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append(TIME + " >= '").append(startTime).append(START_TIME).append("' and ").append(TIME).append(" <= '").append(endTime).append(END_TIME).append("' and (");
//        for (int i = 0; i < deviceIndexes.size(); i++) {
//            if (deviceIndexes.size() - i != 1) {
//                stringBuilder.append(DEV_INDEX + "='").append(deviceIndexes.get(i)).append("' or ");
//            } else {
//                stringBuilder.append(DEV_INDEX + "='").append(deviceIndexes.get(i)).append("')");
//            }
//        }
//        //sql语句
//        String sql = "SELECT (SUM(" + ONLINE_MIN + ")/(SUM(" + OFFLINE_MIN + ")+SUM(" + ONLINE_MIN + ")))*100 AS onlineRate FROM "+PQS_ONLINERATE+" WHERE " + stringBuilder + " group by " + DEV_INDEX +" tz('Asia/Shanghai')";
//        //结果集
//        QueryResult result = influxDbUtils.query(sql);
//        //处理结果集
//        List<QueryResult.Series> list = result.getResults().get(0).getSeries();
//        if (!CollectionUtils.isEmpty(list)) {
//            list.forEach(po -> {
//                PublicDTO publicDTO = new PublicDTO();
//                List<List<Object>> valueList = po.getValues();
//                String index = po.getTags().get(DEV_INDEX);
//                if (!CollectionUtils.isEmpty(valueList)) {
//                    for (List<Object> value : valueList) {
//                        //终端在线率 保留两位小数
//                        Double onlineRate = value.get(1) == null ? null : BigDecimal.valueOf(Double.parseDouble(value.get(1).toString())).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
//                        publicDTO.setId(index);
//                        publicDTO.setData(onlineRate);
//                    }
//                }
//                publicDTOList.add(publicDTO);
//            });
//        }
//        return publicDTOList;
    }


    /**
     * 计算变电站层级在线率
     */
    private void setSubstationListOnlineRate(List<OnlineRateVO> list) {
        list.forEach(item -> {
            List<OnlineRateVO> children = item.getChildren();
            List<OnlineRateVO> tempList = children.stream().filter(s -> !Objects.equals(s.getOnlineRateData(), 3.14159)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(tempList)) {
                item.setOnlineRateData(tempList.stream().mapToDouble(OnlineRateVO::getOnlineRateData).sum() / tempList.size());
            } else {
                item.setOnlineRateData(3.14159);
            }
        });
    }
    /**
     * 计算供电公司层级在线率
     */
    private void setPowerCompanyListOnlineRate(List<OnlineRateVO> list) {
        list.forEach(item -> {
            List<OnlineRateVO> rateVOS = new ArrayList<>();
            item.getChildren().forEach(item1 -> {
                item1.getChildren().forEach(item2 -> {
                    rateVOS.add(item2);
                });
            });
            List<OnlineRateVO> tempList = rateVOS.stream().filter(s -> !Objects.equals(s.getOnlineRateData(), 3.14159)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(tempList)) {
                item.setOnlineRateData(tempList.stream().mapToDouble(OnlineRateVO::getOnlineRateData).sum() / tempList.size());
            } else {
                item.setOnlineRateData(3.14159);
            }
        });
    }



}
