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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.DeptGetChildrenDTO;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.api.StatationStatClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsStatationStatInfoDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsStatationStatInfoParam;
import com.njcn.device.pms.pojo.po.StatationStat;
import com.njcn.device.biz.pojo.dto.PollutionLineDTO;
import com.njcn.harmonic.mapper.RStatPollutionSubstationDPOMapper;
import com.njcn.harmonic.mapper.majornetwork.PVOverviewMapper;
import com.njcn.harmonic.pojo.param.PVOverviewParam;
import com.njcn.harmonic.pojo.po.RStatPollutionSubstationDPO;
import com.njcn.harmonic.service.majornetwork.PVOverviewService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author wr
 * @description
 * @date 2023/5/10 9:30
 */
@Service
@RequiredArgsConstructor
public class PVOverviewServiceImpl implements PVOverviewService {

    private final PVOverviewMapper pvOverviewMapper;
    private final CommTerminalGeneralClient commTerminalGeneralClient;

    private final RStatPollutionSubstationDPOMapper pollutionSubstationDPOMapper;
    private final StatationStatClient statationStatClient;
    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;
    private final DeptFeignClient deptFeignClient;



    @Override
    public List<Integer> getPVSynthesisCount(StatisticsBizBaseParam param) {
        DeptGetLineParam deptGetLineParam=new DeptGetLineParam();
        deptGetLineParam.setDeptId(param.getId());
        List<DeptGetChildrenDTO> data = commTerminalGeneralClient.deptGetLineList(deptGetLineParam).getData();
        if(CollUtil.isNotEmpty(data)){
            List<String> monitorIds = data.stream().flatMap(dto -> dto.getLineIds().stream()).collect(Collectors.toList());
            List<Integer> integers = pvOverviewMapper.selectPVSynthesisCount(param, monitorIds);
            integers.add(monitorIds.size());
            return integers;
        }
        return new ArrayList<Integer>(3){{add(0);add(0);add(0);}};
    }

    @Override
    public List<PollutionLineDTO.PVPollution> getPVPollution(PVOverviewParam param) {
        List<PollutionLineDTO.PVPollution> info=new ArrayList<>();
        //查询主网台账信息获取遍变电站信息
        List<String> powers;
        Boolean fly=param.getSum()!=null&&param.getSum()>0;
        if(fly){
            PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
            pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
            pmsDeviceInfoParam.setDeptIndex(param.getId());
            List<PmsGeneralDeviceDTO> data = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam).getData();
            powers= data.stream().flatMap(dto -> dto.getPowerrIdList().stream()).collect(Collectors.toList());
        }else{
            List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(param.getId(), Stream.of(0, 1).collect(Collectors.toList())).getData();
            List<String> orgIDs = deptInfos.stream().map(DeptDTO::getCode).collect(Collectors.toList());
            PmsStatationStatInfoParam infoParam = new PmsStatationStatInfoParam();
            infoParam.setOrgIds(orgIDs);
            List<StatationStat> data = statationStatClient.getPowerInfo(infoParam).getData();
            powers=data.stream().map(StatationStat::getPowerId).collect(Collectors.toList());
        }
        if(CollUtil.isEmpty(powers)){
            return info;
        }
        //数据查询
        List<RStatPollutionSubstationDPO> substationDPOS = pollutionSubstationDPOMapper.selectList(new LambdaQueryWrapper<RStatPollutionSubstationDPO>()
                .in(RStatPollutionSubstationDPO::getSubstationId,powers)
                .eq(StrUtil.isNotBlank(param.getSteadyStatis()), RStatPollutionSubstationDPO::getPollutionType, param.getSteadyStatis())
                .ge(StrUtil.isNotBlank(param.getStartTime()), RStatPollutionSubstationDPO::getDataDate,DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())))
                .le(StrUtil.isNotBlank(param.getEndTime()), RStatPollutionSubstationDPO::getDataDate, DateUtil.endOfDay(DateUtil.parse(param.getEndTime())))
                .orderByDesc(RStatPollutionSubstationDPO::getValue)
                .last(fly,"limit "+param.getSum()));
        if(CollUtil.isNotEmpty(substationDPOS)){
            List<String> powerIds = substationDPOS.stream().map(RStatPollutionSubstationDPO::getSubstationId).collect(Collectors.toList());
            PmsStatationStatInfoParam statInfoParam=new PmsStatationStatInfoParam();
            statInfoParam.setPowerIds(powerIds);
            List<PmsStatationStatInfoDTO> powerList = statationStatClient.getStatationStatInfo(statInfoParam).getData();
            if(CollUtil.isNotEmpty(powerList)){
                Map<String, PmsStatationStatInfoDTO> powerDetail = powerList.stream().collect(Collectors.toMap(PmsStatationStatInfoDTO::getPowerId, Function.identity(), (key1, key2) -> key1));
                for (RStatPollutionSubstationDPO substationDPO : substationDPOS) {
                    if(powerDetail.containsKey(substationDPO.getSubstationId())){
                        PmsStatationStatInfoDTO power = powerDetail.get((substationDPO.getSubstationId()));
                        info.add (getPollutionLineDTO(substationDPO,power)) ;
                    }
                }
            }
        }
        return info;
    }

    /**
     * 属性赋值
     * @param substationDPO
     * @param power
     * @return
     */
    public  PollutionLineDTO.PVPollution getPollutionLineDTO(RStatPollutionSubstationDPO substationDPO,PmsStatationStatInfoDTO power) {
        PollutionLineDTO.PVPollution dto=new PollutionLineDTO.PVPollution();
        dto.setId(power.getMonitorId());
        dto.setName(power.getMonitorName());
        dto.setPowerCompany(power.getOrgName());
        dto.setSubstation(power.getPowerName());
        dto.setSubstationId(power.getPowerId());
        dto.setPowerVoltageLevel(power.getPowerVoltageLevel());
        dto.setData(substationDPO.getValue());
        return dto;
    }
}
