package com.njcn.harmonic.service.majornetwork.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.dto.SimpleDTO;
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.LargeScreenParam;
import com.njcn.device.pq.pojo.param.MonitoringPointScaleParam;
import com.njcn.device.pq.pojo.vo.*;
import com.njcn.harmonic.mapper.majornetwork.LargeScreenMapper;
import com.njcn.harmonic.service.majornetwork.LargeScreenService;
import com.njcn.system.pojo.enums.StatisticsEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @version 1.0.0
 * @author: zbj
 * @date: 2023/03/31
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class LargeScreenServiceImpl implements LargeScreenService {

    private final GeneralDeviceInfoClient generalDeviceInfoClient;

    private final LargeScreenMapper largeScreenMapper;

    /**
     * 稳态指标超标占比(区域)
     */
    @Override
    public List<HomeostasisAreaVO> getHomeostasisArea(LargeScreenParam largeScreenParam) {
        //创建返回VO
        List<HomeostasisAreaVO> result = new ArrayList<>();

        DeviceInfoParam.BusinessParam deviceInfoParam = new DeviceInfoParam.BusinessParam();
        //部门索引
        deviceInfoParam.setDeptIndex(largeScreenParam.getDeptIndex());
        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.POWER_NETWORK));
        deviceInfoParam.setStatisticalType(simpleDTO);
        //添加时间
        deviceInfoParam.setSearchBeginTime(largeScreenParam.getSearchBeginTime());
        deviceInfoParam.setSearchEndTime(largeScreenParam.getSearchEndTime());

        // 获取所有数据
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceInfoClient.getPracticalAllDeviceInfo(deviceInfoParam).getData();
        //部门id集合
        List<String> indexIds = generalDeviceDTOList.stream().map(GeneralDeviceDTO::getIndex).collect(Collectors.toList());

        List<Map<String, Object>> homeostasisArea = largeScreenMapper.getHomeostasisArea(indexIds, largeScreenParam.getSearchBeginTime(), largeScreenParam.getSearchEndTime());

        float f = 0.0f;
        for (int i = 0; i < generalDeviceDTOList.size(); i++) {
            for (Map<String, Object> map : homeostasisArea) {
                if (Objects.equals(generalDeviceDTOList.get(i).getIndex(), map.get("id"))) {
                    HomeostasisAreaVO homeostasisAreaVO = new HomeostasisAreaVO();
                    homeostasisAreaVO.setName(generalDeviceDTOList.get(i).getName());
                    //数据库里已经*100了这里去掉*100，见RStatHarmonicOrgM 65行
                    f = Float.parseFloat(map.get("avg").toString());
                    homeostasisAreaVO.setAvg(String.valueOf(f));
                    result.add(homeostasisAreaVO);
                }
            }
        }
        List<String> collect = result.stream().map(HomeostasisAreaVO::getName).collect(Collectors.toList());
        for (GeneralDeviceDTO generalDeviceDTO : generalDeviceDTOList) {
            if (!collect.contains(generalDeviceDTO.getName())) {
                HomeostasisAreaVO homeostasisAreaVO = new HomeostasisAreaVO();
                homeostasisAreaVO.setName(generalDeviceDTO.getName());
                homeostasisAreaVO.setAvg("0");
                result.add(homeostasisAreaVO);
            }
        }
        return result;
    }

    /**
     * 稳态指标超标占比(负荷类型)
     */
    @Override
    public List<HomeostasisAreaVO> getHomeostasisLoad(LargeScreenParam largeScreenParam) {
        //创建返回VO
        List<HomeostasisAreaVO> result = new ArrayList<>();

        DeviceInfoParam.BusinessParam deviceInfoParam = new DeviceInfoParam.BusinessParam();
        //部门索引
        deviceInfoParam.setDeptIndex(largeScreenParam.getDeptIndex());
        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.LOAD_TYPE));
        deviceInfoParam.setStatisticalType(simpleDTO);
        //添加时间
        deviceInfoParam.setSearchBeginTime(largeScreenParam.getSearchBeginTime());
        deviceInfoParam.setSearchEndTime(largeScreenParam.getSearchEndTime());

        // 获取所有数据
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceInfoClient.getPracticalAllDeviceInfo(deviceInfoParam).getData();
        //部门id集合
        List<String> indexIds = generalDeviceDTOList.stream().map(GeneralDeviceDTO::getIndex).collect(Collectors.toList());

        List<Map<String, Object>> homeostasisArea = largeScreenMapper.getHomeostasisLoad(indexIds, largeScreenParam.getSearchBeginTime(), largeScreenParam.getSearchEndTime());
        float f = 0.0f;
        for (int i = 0; i < generalDeviceDTOList.size(); i++) {
            for (Map<String, Object> map : homeostasisArea) {
                if (Objects.equals(generalDeviceDTOList.get(i).getIndex(), map.get("id"))) {
                    HomeostasisAreaVO homeostasisAreaVO = new HomeostasisAreaVO();
                    homeostasisAreaVO.setName(generalDeviceDTOList.get(i).getName());
                    f = Float.parseFloat(map.get("avg").toString()) * 100;
                    homeostasisAreaVO.setAvg(String.valueOf(f));
                    result.add(homeostasisAreaVO);
                }
            }
        }
        List<String> collect = result.stream().map(HomeostasisAreaVO::getName).collect(Collectors.toList());
        for (GeneralDeviceDTO generalDeviceDTO : generalDeviceDTOList) {
            if (!collect.contains(generalDeviceDTO.getName())) {
                HomeostasisAreaVO homeostasisAreaVO = new HomeostasisAreaVO();
                homeostasisAreaVO.setName(generalDeviceDTO.getName());
                homeostasisAreaVO.setAvg("0");
                result.add(homeostasisAreaVO);
            }
        }
        return result;
    }

    /**
     * 功率负荷排名
     */
    @Override
    public List<PowerOrderVO> getPowerOrder(LargeScreenParam largeScreenParam) {
        DeviceInfoParam.BusinessParam deviceInfoParam = new DeviceInfoParam.BusinessParam();
        //部门索引
        deviceInfoParam.setDeptIndex(largeScreenParam.getDeptIndex());
        //统计类型
        SimpleDTO simpleDTO = new SimpleDTO();
        simpleDTO.setCode(String.valueOf(StatisticsEnum.POWER_NETWORK));
        deviceInfoParam.setStatisticalType(simpleDTO);
        //添加时间
        deviceInfoParam.setSearchBeginTime(largeScreenParam.getSearchBeginTime());
        deviceInfoParam.setSearchEndTime(largeScreenParam.getSearchEndTime());

        // 获取所有数据
        List<GeneralDeviceDTO> generalDeviceDTOList = generalDeviceInfoClient.getPracticalAllDeviceInfo(deviceInfoParam).getData();

        //获取所有监测点集合
        List<String> lineIds = generalDeviceDTOList.stream().flatMap(dto -> dto.getLineIndexes().stream()).collect(Collectors.toList());

        return largeScreenMapper.getPowerOrder(lineIds,largeScreenParam);
    }
}
