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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.harmonic.mapper.majornetwork.RMpPassRateMMapper;
import com.njcn.harmonic.mapper.majornetwork.RMpPassRateYMapper;
import com.njcn.harmonic.pojo.param.RStatOrgParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RMpPassRateQ;
import com.njcn.harmonic.mapper.majornetwork.RMpPassRateQMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.harmonic.pojo.vo.RStatOrgIndexVO;
import com.njcn.harmonic.service.majornetwork.RMpPassRateService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
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;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rui.wu
 * @since 2022-10-24
 */
@Service
@RequiredArgsConstructor
public class RMpPassRateServiceImpl extends ServiceImpl<RMpPassRateQMapper, RMpPassRateQ> implements RMpPassRateService {

    private final RMpPassRateYMapper rMpPassRateYMapper;
    private final RMpPassRateQMapper rMpPassRateQMapper;
    private final RMpPassRateMMapper rMpPassRateMMapper;
    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;
    private final MonitorClient monitorClient;
    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public List<RStatOrgIndexVO> getRMpPassRateInfo(RStatOrgParam param) {
        //初始化对象
        List<RStatOrgIndexVO> temp = new ArrayList<>();
        //根据部门获取监测点信息
        PmsDeviceInfoParam pms = new PmsDeviceInfoParam();
        pms.setDeptIndex(param.getId());
        pms.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> data = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pms).getData();
        List<String> addIds = new ArrayList<>();
        data.stream().forEach(e -> addIds.addAll(e.getMonitorIdList()));

        if (CollectionUtil.isNotEmpty(addIds)) {
            //根据监测点ids获取监测点相关详细信息(变电站id，电压id，监测点名称)
            PmsMonitorParam dto = BeanUtil.copyProperties(param, PmsMonitorParam.class);
            dto.setMonitorIds(addIds);
            dto.setMonitorName(param.getMonitorName());
            dto.setVoltageLevels(param.getVoltageLevels());
            dto.setPowerrIds(param.getPowerrIds());
            List<PmsMonitorDTO> pmsMonitorDTOS = monitorClient.getMonitorInfoListByCond(dto).getData();
            Map<String, PmsMonitorDTO> pmsMonitorDTOMap = pmsMonitorDTOS.stream()
                    .collect(Collectors.toMap(PmsMonitorDTO::getId, Function.identity()));
            //获取电压等级
            List<DictData> devVoltageData = dicDataFeignClient.getDicDataByTypeCode(
                    DicDataTypeEnum.DEV_VOLTAGE.getCode()).getData();
            Map<String, DictData> devVoltageMap = devVoltageData.stream()
                    .collect(Collectors.toMap(DictData::getId, Function.identity()));

            //获取最新过滤的监测点id
            List<String> ids = pmsMonitorDTOS.stream().map(PmsMonitorDTO::getId).collect(Collectors.toList());
            StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
            baseParam.setIds(ids);

            if (CollectionUtil.isNotEmpty(addIds)) {
                // 类型（1年 2季度 3月份
                switch (param.getType().toString()) {
                    case BizParamConstant.STAT_BIZ_YEAR:
                        // 获取年稳态指标合格率明细表
                        temp = rMpPassRateYMapper.getYearPassRate(baseParam);
                        break;
                    case BizParamConstant.STAT_BIZ_QUARTER:
                        // 获取季稳态指标合格率明细表
                        temp = rMpPassRateQMapper.getQuarterPassRate(baseParam);
                        break;
                    case BizParamConstant.STAT_BIZ_MONTH:
                        // 获取月稳态指标合格率明细表
                        temp = rMpPassRateMMapper.getMonthPassRate(baseParam);
                        break;
                    default:
                        break;

                }
            }
            if (CollectionUtil.isNotEmpty(temp)) {
                Map<String, RStatOrgIndexVO> collect = temp.stream()
                        .collect(Collectors.toMap(RStatOrgIndexVO::getMeasurementPointId, Function.identity()));
                collect.forEach((Key, value) -> {
                    if (pmsMonitorDTOMap.containsKey(Key)) {
                        //根据监测点信息，进行属性赋值
                        PmsMonitorDTO dto1 = pmsMonitorDTOMap.get(Key);
                        value.setOrgNo(dto1.getOrgId());
                        value.setOrgName(dto1.getOrgName());
                        value.setMeasurementPointName(dto1.getName());
                        value.setPowerrName(dto1.getPowerrName());
                        //获取电压字典的名称
                        if (devVoltageMap.containsKey(dto1.getVoltageLevel())) {
                            DictData data1 = devVoltageMap.get(dto1.getVoltageLevel());
                            value.setMonitorVoltageName(data1.getName());
                        }
                    }
                });
                return new ArrayList<>(collect.values());
            }

        }
        return new ArrayList<>();
    }
}
