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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.device.pms.mapper.majornetwork.RStatMeasurementAccountDetailMapper;
import com.njcn.device.pms.mapper.majornetwork.RStatTargetDetailMapper;
import com.njcn.device.pms.mapper.majornetwork.RStatTractionStationAccountDetailMapper;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorInfoDTO;
import com.njcn.device.pms.pojo.dto.PmsTractionStationDTO;
import com.njcn.device.pms.pojo.param.DataQualityDetailsParam;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsMonitorInfoParam;
import com.njcn.device.pms.pojo.param.PmsTractionStationParam;
import com.njcn.device.pms.pojo.po.RStatMeasurementAccountDetailPO;
import com.njcn.device.pms.pojo.po.RStatTargetDetailPO;
import com.njcn.device.pms.pojo.po.RStatTractionStationAccountDetailPO;
import com.njcn.device.pms.pojo.vo.RStatMeasurementAccountDetailVO;
import com.njcn.device.pms.pojo.vo.RStatTargetDetailVO;
import com.njcn.device.pms.pojo.vo.RStatTractionStationAccountDetailVO;
import com.njcn.device.pms.service.majornetwork.DataQualityDetailsService;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.device.pms.service.majornetwork.IPmsGeneralDeviceService;
import com.njcn.device.pms.service.majornetwork.ITractionStationService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 主网数据质量-数据质量核查详情
 *
 * @author yzh
 * @date 2022/11/4
 */

@Service
@Slf4j
@RequiredArgsConstructor
public class DataQualityDetailsServiceImpl implements DataQualityDetailsService {

    private final IPmsGeneralDeviceService iPmsGeneralDeviceService;

    private final RStatMeasurementAccountDetailMapper rStatMeasurementAccountDetailMapper;

    private final IMonitorService iMonitorService;

    private final ITractionStationService iTractionStationService;

    private final RStatTractionStationAccountDetailMapper rStatTractionStationAccountDetailMapper;

    private final RStatTargetDetailMapper rStatTargetDetailMapper;

    private final DeptFeignClient deptFeignClient;

    /**
     * 获取主网数据质量-台账类数据指标核查详情-监测点台账数据质量问题
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RStatAccountCheckDataVO>
     * @author yzh
     * @date 2022/11/4
     */
    @Override
    public List<RStatMeasurementAccountDetailVO> getQualityProblemsOfMonitoringPointAccountData(DataQualityDetailsParam param) {
        // 获取单位下的子单位信息
        List<PmsGeneralDeviceDTO> data1 = getDeptInfo(param);
        /*获取下级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (param.getId ()).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            return Collections.emptyList();
        }
        List<String> deptIdList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        /*过滤单位只留下直接下属单位*/
        List<PmsGeneralDeviceDTO> data = data1.stream ( ).
                filter (t -> deptIdList.contains (t.getIndex ( ))).
                collect (Collectors.toList ( ));
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 创建集合封装返回数据
        List<RStatMeasurementAccountDetailVO> result = new ArrayList<>();
        for (PmsGeneralDeviceDTO dto : data) {
            if (CollUtil.isEmpty(dto.getMonitorIdList())) {
                continue;
            }
            // 获取主网数据质量-台账类数据指标核查详情-监测点台账数据质量问题
            List<RStatMeasurementAccountDetailPO> pos =
                    rStatMeasurementAccountDetailMapper
                            .getQualityProblemsOfMonitoringPointAccountData(dto.getMonitorIdList(), param);
            if (CollUtil.isEmpty(pos)) {
                return Collections.emptyList();
            }
            // 获取监测点信息
            PmsMonitorInfoParam pmsMonitorInfoParam = new PmsMonitorInfoParam();
            pmsMonitorInfoParam.setMonitorIds(pos
                    .stream()
                    .map(RStatMeasurementAccountDetailPO::getMeasurementPointId)
                    .collect(Collectors.toList()));
            List<PmsMonitorInfoDTO> monitorInfoList = iMonitorService.getMonitorInfo(pmsMonitorInfoParam);
            // 属性赋值
            for (RStatMeasurementAccountDetailPO po : pos) {
                for (PmsMonitorInfoDTO monitorInfoDTO : monitorInfoList) {
                    if (po.getMeasurementPointId().equals(monitorInfoDTO.getMonitorId())) {
                        RStatMeasurementAccountDetailVO vo = new RStatMeasurementAccountDetailVO();
                        vo.setOrgId(dto.getIndex());
                        vo.setOrgName(dto.getName());
                        vo.setDataDate(po.getDataDate());
                        vo.setPowerId(monitorInfoDTO.getPowerId());
                        vo.setPowerName(monitorInfoDTO.getPowerName());
                        vo.setMonitorId(po.getMeasurementPointId());
                        vo.setMonitorName(monitorInfoDTO.getMonitorName());
                        vo.setCheckRules(po.getCheckRules());
                        vo.setMonitorVoltageLevel(monitorInfoDTO.getMonitorVoltageLevel());
                        result.add(vo);
                    }
                }
            }
        }
        return result;
    }

    /**
     * 获取主网数据质量-台账类数据指标核查详情-牵引站台账数据质量问题
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RStatAccountCheckDataVO>
     * @author yzh
     * @date 2022/11/4
     */
    @Override
    public List<RStatTractionStationAccountDetailVO> getTractionPlatformAccountDataQualityProblem(DataQualityDetailsParam param) {
        // 获取单位下的子单位信息
        List<PmsGeneralDeviceDTO> data1 = getDeptInfo(param);
        /*获取下级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (param.getId ()).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            return Collections.emptyList();
        }
        List<String> deptIdList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        /*过滤单位只留下直接下属单位*/
        List<PmsGeneralDeviceDTO> data = data1.stream ( ).
                filter (t -> deptIdList.contains (t.getIndex ( ))).
                collect (Collectors.toList ( ));
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 获取牵引站信息
        PmsTractionStationParam pmsTractionStationParam = new PmsTractionStationParam();
        pmsTractionStationParam.setOrgIds(data.stream().map(PmsGeneralDeviceDTO::getIndex).collect(Collectors.toList()));
        List<PmsTractionStationDTO> tractionStationInfo = iTractionStationService.getPmsTractionStationInfo(pmsTractionStationParam);
        if (CollUtil.isEmpty(tractionStationInfo)) {
            return Collections.emptyList();
        }
        // 将单位信息转为map集合 key: 单位id  value: 单位实体
        Map<String, PmsTractionStationDTO> tractionStationInfoMap = getDataMap(tractionStationInfo);
        // 获取主网数据质量-台账类数据指标核查详情-牵引站台账数据质量问题
        List<RStatTractionStationAccountDetailPO> pos = rStatTractionStationAccountDetailMapper.getTractionPlatformAccountDataQualityProblem(param,
                tractionStationInfo.stream().map(PmsTractionStationDTO::getId).collect(Collectors.toList()));
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }
        // 属性赋值
        return pos.stream().map(po -> {
            RStatTractionStationAccountDetailVO vo = new RStatTractionStationAccountDetailVO();
            BeanUtils.copyProperties(po, vo);
            vo.setSubstationName(tractionStationInfoMap.get(vo.getSubstationId()).getName());
            vo.setElectricRailwayLineName(tractionStationInfoMap.get(vo.getSubstationId()).getRailwayLineName());
            vo.setPowerName((tractionStationInfoMap.get(vo.getSubstationId()).getPowerName()));
            vo.setPowerVoltageLevel((tractionStationInfoMap.get(vo.getSubstationId()).getPowerVoltageLevel()));
            // TODO 铁路类型
            String railwayLineId = tractionStationInfoMap
                    .get(vo.getSubstationId())
                    .getRailwayLineId()
                    .substring(0, tractionStationInfoMap.get(vo.getSubstationId()).getRailwayLineId().length() - 1);
            if (!railwayLineId.equals(RStatTractionStationAccountDetailPO.highSpeedRail)
                    &&
                    !railwayLineId.equals(RStatTractionStationAccountDetailPO.ordinaryRailway)) {
                vo.setTypeOfRailway(RStatTractionStationAccountDetailPO.other);
            } else {
                vo.setTypeOfRailway(railwayLineId);
            }
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取主网数据质量-稳态指标类数据质量问题查询
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RStatTargetDetailVO>
     * @author yzh
     * @date 2022/11/8
     */
    @Override
    public List<RStatTargetDetailVO> getRStatTargetDetail(DataQualityDetailsParam param) {
        // 获取单位下的子单位信息
        List<PmsGeneralDeviceDTO> data1 = getDeptInfo(param);
        /*获取下级子部门信息*/
        List<DeptDTO> deptDTOList = deptFeignClient.getDepSonDetailByDeptId (param.getId ()).getData ( );
        if (CollUtil.isEmpty(deptDTOList)) {
            return Collections.emptyList();
        }
        List<String> deptIdList = deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        /*过滤单位只留下直接下属单位*/
        List<PmsGeneralDeviceDTO> data = data1.stream ( ).
                filter (t -> deptIdList.contains (t.getIndex ( ))).
                collect (Collectors.toList ( ));
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 获取主网数据质量-稳态指标类数据质量问题查询
        List<RStatTargetDetailPO> pos = rStatTargetDetailMapper.getRStatTargetDetail(param, data.stream()
                .flatMap(pmsGeneralDeviceDTO -> pmsGeneralDeviceDTO.getMonitorIdList().stream())
                .collect(Collectors.toList()));
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }
        // 取出监测点id
        List<String> measurementPointId = pos.stream().map(RStatTargetDetailPO::getMeasurementPointId).collect(Collectors.toList());
        PmsMonitorInfoParam pmsMonitorInfoParam = new PmsMonitorInfoParam();
        pmsMonitorInfoParam.setMonitorIds(measurementPointId);
        List<PmsMonitorInfoDTO> pmsMonitorInfoList = iMonitorService.getMonitorInfo(pmsMonitorInfoParam);
        //将单位信息转为map集合 key: 单位id  value: 单位实体
        Map<String, PmsMonitorInfoDTO> pmsMonitorInfoMap = pmsMonitorInfoList
                .stream()
                .collect(Collectors.toMap(PmsMonitorInfoDTO::getMonitorId, PmsMonitorInfoDTO -> PmsMonitorInfoDTO));
        // 创建集合封装返回数据
        return pos.stream().map(po -> {
            RStatTargetDetailVO vo = new RStatTargetDetailVO();
            BeanUtils.copyProperties(po, vo);
            vo.setMonitorId(po.getMeasurementPointId());
            vo.setPowerName(pmsMonitorInfoMap.get(vo.getMonitorId()).getPowerName());
            vo.setMonitorName(pmsMonitorInfoMap.get(vo.getMonitorId()).getMonitorName());
            vo.setMonitorVoltageLevel(pmsMonitorInfoMap.get(vo.getMonitorId()).getMonitorVoltageLevel());
            // TODO 地市公司
            vo.setCityCompany(pmsMonitorInfoMap.get(vo.getMonitorId()).getOrgName());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取单位下的子单位信息
     */
    private List<PmsGeneralDeviceDTO> getDeptInfo(DataQualityDetailsParam param) {
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        return iPmsGeneralDeviceService.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam);
    }

    /**
     * 将单位信息转为map集合 key: 单位id  value: 单位实体
     */
    private Map<String, PmsTractionStationDTO> getDataMap(List<PmsTractionStationDTO> tractionStationInfo) {
        return tractionStationInfo
                .stream()
                .collect(Collectors.toMap(PmsTractionStationDTO::getId, PmsTractionStationDTO -> PmsTractionStationDTO));
    }
}
