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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
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.RMpSurplusHarmonicDetailMMapper;
import com.njcn.harmonic.pojo.param.RStatOrgParam;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RMpPartHarmonicDetailM;
import com.njcn.harmonic.mapper.majornetwork.RMpPartHarmonicDetailMMapper;
import com.njcn.harmonic.pojo.vo.RMpPartHarmonicDetailVO;
import com.njcn.harmonic.service.majornetwork.RMpPartHarmonicDetailMService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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 RMpPartHarmonicDetailMServiceImpl extends ServiceImpl<RMpPartHarmonicDetailMMapper, RMpPartHarmonicDetailM> implements RMpPartHarmonicDetailMService {

    private final RMpSurplusHarmonicDetailMMapper rMpSurplusHarmonicDetailMMapper;
    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;
    private final MonitorClient monitorClient;
    private final DicDataFeignClient dicDataFeignClient;

    @Override
    public List<RMpPartHarmonicDetailVO> getRMpPartHarmonicDetail(RStatOrgParam param) {
        //根据部门获取监测点信息(根据部门过滤基本信息)
        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);
            List<RMpPartHarmonicDetailVO> rMpPartHarmonicDetail = rMpSurplusHarmonicDetailMMapper.getRMpPartHarmonicDetail(baseParam);
            if (CollectionUtil.isNotEmpty(rMpPartHarmonicDetail)) {
                //因为只有月表，数据进行查询可能出现重复数据。进行分组
                Map<String, List<RMpPartHarmonicDetailVO>> detailVOMap = rMpPartHarmonicDetail.stream()
                        .collect(Collectors.groupingBy(RMpPartHarmonicDetailVO::getMeasurementPointId));
                detailVOMap.forEach((Key, value) -> {
                    if (pmsMonitorDTOMap.containsKey(Key)) {
                        PmsMonitorDTO dto1 = pmsMonitorDTOMap.get(Key);
                        for (RMpPartHarmonicDetailVO rmp : value) {
                            rmp.setOrgName(dto1.getOrgName());
                            rmp.setPowerrName(dto1.getPowerrName());
                            rmp.setMeasurementPointName(dto1.getName());
                            //获取字典电压等级名称
                            if (devVoltageMap.containsKey(dto1.getVoltageLevel())) {
                                DictData data1 = devVoltageMap.get(dto1.getVoltageLevel());
                                rmp.setMonitorVoltageName(data1.getName());
                            }
                        }

                    }
                });
            }
            return rMpPartHarmonicDetail;
        }
        return new ArrayList<>();
    }
}
