package com.njcn.device.pms.service.majornetwork.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorBaseDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.vo.PmsSummaryStatisticsVO;
import com.njcn.device.pms.service.majornetwork.IDistributionMonitorService;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.IPmsGeneralDeviceService;
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.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年10月14日 09:58
 */
@Service
@RequiredArgsConstructor
public class PmsGeneralDeviceServiceImpl implements IPmsGeneralDeviceService {

    private final DeptFeignClient deptFeignClient;

    private final IMonitorService monitorService;

    private final IDistributionMonitorService iDistributionMonitorService;

    @Override
    public List<PmsGeneralDeviceDTO> getPmsDeviceInfoWithInOrg(PmsDeviceInfoParam pmsDeviceInfoParam) {
        //定义待返回终端信息
        List<PmsGeneralDeviceDTO> deviceInfos = new ArrayList<>();
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(pmsDeviceInfoParam.getDeptIndex(), Stream.of(0,1).collect(Collectors.toList())).getData();
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(pmsDeviceInfoParam.getDeptIndex()))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(pmsDeviceInfoParam.getDeptIndex()))
                    .collect(Collectors.toList());
            deviceInfos.add(getPmsGeneralDeviceInfo(dept.get(0), Collections.singletonList(dept.get(0).getCode()), pmsDeviceInfoParam));
        }else{
            for (DeptDTO deptDTO : directDeptInfos) {
                //筛选上级部门pids包含该id的所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> deptIdList = descendantDeptDTO.stream()
                        .map(DeptDTO::getCode)
                        .distinct()
                        .collect(Collectors.toList());
                deptIdList.add(deptDTO.getCode());
                deviceInfos.add(getPmsGeneralDeviceInfo(deptDTO, deptIdList, pmsDeviceInfoParam));
            }
        }
        return deviceInfos;
    }

    @Override
    public List<SimpleDTO> getPmsPowerrInfoWithInOrg(PmsDeviceInfoParam pmsDeviceInfoParam) {
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(pmsDeviceInfoParam.getDeptIndex(), Stream.of(0,1).collect(Collectors.toList())).getData();
        List<String> orgIdList = deptInfos.stream().map(deptDTO -> deptDTO.getCode()).collect(Collectors.toList());
        return monitorService.getPmsPowerrInfoWithInOrg(orgIdList,pmsDeviceInfoParam);
    }

    @Override
    public List<PmsGeneralDeviceDTO> getPmsDeviceInfoByOrgId(PmsDeviceInfoParam pmsDeviceInfoParam) {
        //定义待返回终端信息
        List<PmsGeneralDeviceDTO> deviceInfos = new ArrayList<>();
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(pmsDeviceInfoParam.getDeptIndex(), Stream.of(0,1).collect(Collectors.toList())).getData();
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(pmsDeviceInfoParam.getDeptIndex()))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(pmsDeviceInfoParam.getDeptIndex()))
                    .collect(Collectors.toList());
            deviceInfos.add(getPmsGeneralDeviceInfoByOrg(dept.get(0), Collections.singletonList(dept.get(0).getCode()), pmsDeviceInfoParam));
        }else{
            for (DeptDTO deptDTO : directDeptInfos) {
                //筛选上级部门pids包含该id的所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> deptIdList = descendantDeptDTO.stream()
                        .map(DeptDTO::getCode)
                        .distinct()
                        .collect(Collectors.toList());
                deptIdList.add(deptDTO.getCode());
                deviceInfos.add(getPmsGeneralDeviceInfoByOrg(deptDTO, deptIdList, pmsDeviceInfoParam));
            }
        }
        return deviceInfos;
    }

    @Override
    public List<String> getPwPmsMonitorIds(String deptId,List<String> monitorSortIds) {
        List<String> deptIds = deptFeignClient.getDepSonSelfCodetByDeptId(deptId).getData();
        return iDistributionMonitorService.getBaseDisMonitorIds(deptIds,monitorSortIds);
    }

    @Override
    public List<PmsSummaryStatisticsVO.SummaryVO> getPmsDeviceAndMonitor(PmsDeviceInfoParam pmsDeviceInfoParam) {
        List<PmsSummaryStatisticsVO.SummaryVO> deviceInfos = new ArrayList<>();
        List<DeptDTO> deptInfos = deptFeignClient.getDeptDescendantIndexes(pmsDeviceInfoParam.getDeptIndex(), Stream.of(0,1).collect(Collectors.toList())).getData();
        List<DeptDTO> directDeptInfos = deptInfos.stream()
                .filter(deptDTO -> deptDTO.getPid().equals(pmsDeviceInfoParam.getDeptIndex()))
                .collect(Collectors.toList());
        if (CollectionUtil.isEmpty(directDeptInfos)) {
            //没有直接子部门，获取当前部门所有信息
            List<DeptDTO> dept = deptInfos.stream()
                    .filter(deptDTO -> deptDTO.getId().equals(pmsDeviceInfoParam.getDeptIndex()))
                    .collect(Collectors.toList());
            deviceInfos.add(getPmsDeviceAndMonitorCount(dept.get(0), Collections.singletonList(dept.get(0).getCode())));
        }else{
            for (DeptDTO deptDTO : directDeptInfos) {
                //筛选上级部门pids包含该id的所有部门
                List<DeptDTO> descendantDeptDTO = deptInfos.stream()
                        .filter(d -> d.getPids().contains(deptDTO.getId()))
                        .collect(Collectors.toList());
                //形成需要查询监测点的部门索引
                List<String> deptIdList = descendantDeptDTO.stream()
                        .map(DeptDTO::getCode)
                        .distinct()
                        .collect(Collectors.toList());
                deptIdList.add(deptDTO.getCode());
                deviceInfos.add(getPmsDeviceAndMonitorCount(deptDTO, deptIdList));
            }
        }
        return deviceInfos;
    }

    /**
     * 查询当前部门pms台账信息
     * @param deptDTO 部门信息
     * @param deptIdList 所有子部门索引
     * @param pmsDeviceInfoParam 查询条件
     */
    private PmsGeneralDeviceDTO getPmsGeneralDeviceInfo(DeptDTO deptDTO, List<String> deptIdList, PmsDeviceInfoParam pmsDeviceInfoParam) {
        PmsGeneralDeviceDTO pmsGeneralDeviceDTO = new PmsGeneralDeviceDTO();
        pmsGeneralDeviceDTO.setIndex(deptDTO.getCode());
        if (deptDTO.getType() == 0) {
            pmsGeneralDeviceDTO.setName(deptDTO.getArea());
        } else {
            pmsGeneralDeviceDTO.setName(deptDTO.getName());
        }
        //获取monitor详细数据
        List<PmsMonitorBaseDTO> monitorList = monitorService.getMonitorByCondition(deptIdList,pmsDeviceInfoParam);
        pmsGeneralDeviceDTO.setPowerrIdList(monitorList.stream().map(PmsMonitorBaseDTO::getPowerrId).distinct().collect(Collectors.toList()));
        pmsGeneralDeviceDTO.setTerminalIdList(monitorList.stream().map(PmsMonitorBaseDTO::getTerminalId).distinct().collect(Collectors.toList()));
        pmsGeneralDeviceDTO.setGeneratrixIdList(monitorList.stream().map(PmsMonitorBaseDTO::getLineId).distinct().collect(Collectors.toList()));
        pmsGeneralDeviceDTO.setMonitorIdList(monitorList.stream().map(PmsMonitorBaseDTO::getMonitorId).distinct().collect(Collectors.toList()));
        return pmsGeneralDeviceDTO;
    }


    /**
     * 查询当前部门pms台账信息
     * @param deptDTO 部门信息
     * @param deptIdList 所有子部门索引
     * @param pmsDeviceInfoParam 查询条件
     */
    private PmsGeneralDeviceDTO getPmsGeneralDeviceInfoByOrg(DeptDTO deptDTO, List<String> deptIdList, PmsDeviceInfoParam pmsDeviceInfoParam) {
        PmsGeneralDeviceDTO pmsGeneralDeviceDTO = new PmsGeneralDeviceDTO();
        pmsGeneralDeviceDTO.setIndex(deptDTO.getCode());
        if (deptDTO.getType() == 0) {
            pmsGeneralDeviceDTO.setName(deptDTO.getArea());
        } else {
            pmsGeneralDeviceDTO.setName(deptDTO.getName());
        }
        //获取monitor详细数据
        List<PmsMonitorBaseDTO> monitorList = iDistributionMonitorService.getMonitorByCondition(deptIdList,pmsDeviceInfoParam);
        pmsGeneralDeviceDTO.setPowerrIdList(monitorList.stream().map(PmsMonitorBaseDTO::getPowerrId).distinct().collect(Collectors.toList()));
        pmsGeneralDeviceDTO.setTerminalIdList(monitorList.stream().map(PmsMonitorBaseDTO::getTerminalId).distinct().collect(Collectors.toList()));
        pmsGeneralDeviceDTO.setLineIdList(monitorList.stream().map(PmsMonitorBaseDTO::getLineId).distinct().collect(Collectors.toList()));
        pmsGeneralDeviceDTO.setMonitorIdList(monitorList.stream().map(PmsMonitorBaseDTO::getMonitorId).distinct().collect(Collectors.toList()));
        return pmsGeneralDeviceDTO;
    }


    private PmsSummaryStatisticsVO.SummaryVO getPmsDeviceAndMonitorCount(DeptDTO deptDTO, List<String> deptIdList) {
        PmsSummaryStatisticsVO.SummaryVO pmsGeneralDeviceDTO = new PmsSummaryStatisticsVO.SummaryVO();
        if (deptDTO.getType() == 0) {
            pmsGeneralDeviceDTO.setName(deptDTO.getArea());
        } else {
            pmsGeneralDeviceDTO.setName(deptDTO.getName());
        }
        //获取monitor详细数据
        PmsSummaryStatisticsVO.SummaryVO monitorAndDevice = iDistributionMonitorService.getMonitorAndDevice(deptIdList);
        pmsGeneralDeviceDTO.setMonitorCount(monitorAndDevice.getMonitorCount());
        pmsGeneralDeviceDTO.setTerminalCount(monitorAndDevice.getTerminalCount());
        return pmsGeneralDeviceDTO;
    }
}
