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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.device.pms.api.PmsGeneratrixClient;
import com.njcn.device.pms.pojo.dto.GeneratrixAndPowerStationDTO;
import com.njcn.device.pms.pojo.param.PmsGeneratrixParam;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgBusbarVoltageMMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgBusbarVoltageQMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatOrgBusbarVoltageYMapper;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RStatOrgBusbarVoltageM;
import com.njcn.harmonic.pojo.vo.RStatOrgBusbarVoltageVO;
import com.njcn.harmonic.service.majornetwork.RStatOrgBusbarVoltageService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
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-19
 */
@Service
@RequiredArgsConstructor
public class RStatOrgBusbarVoltageServiceImpl extends ServiceImpl<RStatOrgBusbarVoltageMMapper, RStatOrgBusbarVoltageM> implements RStatOrgBusbarVoltageService {

    private final DeptFeignClient deptFeignClient;
    private final RStatOrgBusbarVoltageYMapper rStatOrgBusbarVoltageYMapper;
    private final RStatOrgBusbarVoltageQMapper rStatOrgBusbarVoltageQMapper;
    private final RStatOrgBusbarVoltageMMapper rStatOrgBusbarVoltageMMapper;
    private final PmsGeneratrixClient pmsGeneratrixClient;

    @Override
    public List<RStatOrgBusbarVoltageVO> getRStatOrgBusbarVoltage(StatSubstationBizBaseParam param) {
        //获取部门信息
        List<DeptDTO> data = deptFeignClient.getDeptDescendantIndexes(param.getId(), WebUtil.filterDeptType()).getData();
        Map<String, DeptDTO> deptMap = data.stream().collect(Collectors.toMap(DeptDTO::getCode, Function.identity()));

        //获取部门id集合
        List<String> deptIds = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        List<RStatOrgBusbarVoltageVO> info = new ArrayList<>();
        List<RStatOrgBusbarVoltageVO> temp = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(data)) {
            StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
            baseParam.setIds(deptIds);
            // 类型（1年 2季度 3月份)
            switch (param.getType().toString()) {
                case BizParamConstant.STAT_BIZ_YEAR:
                    // 获取年区域电站母线电压类指标超标计
                    temp = rStatOrgBusbarVoltageYMapper.getYstatOrgBusbarVoltageVO(baseParam);
                    break;
                case BizParamConstant.STAT_BIZ_QUARTER:
                    // 获取季区域电站母线电压类指标超标计
                    temp = rStatOrgBusbarVoltageQMapper.getQstatOrgBusbarVoltageVO(baseParam);
                    break;
                case BizParamConstant.STAT_BIZ_MONTH:
                    // 获取月区域电站母线电压类指标超标计
                    temp = rStatOrgBusbarVoltageMMapper.getMstatOrgBusbarVoltageVO(baseParam);
                    break;
                default:
                    break;
            }
        }
        if (CollectionUtil.isNotEmpty(temp)) {
            //获取母线集合
            List<String> busbarIds = temp.stream().map(RStatOrgBusbarVoltageVO::getBusbarId).collect(Collectors.toList());
            PmsGeneratrixParam generatrixParam=new PmsGeneratrixParam();
            generatrixParam.setGeneratrixIds(busbarIds);
            generatrixParam.setGeneratrixVoltageLevel(param.getIds());
            List<GeneratrixAndPowerStationDTO> generatrixDTO = pmsGeneratrixClient.getGeneratrixAndPowerStationInfo(generatrixParam).getData();
            //根据母线信息
            Map<String, GeneratrixAndPowerStationDTO> generatrixMap = generatrixDTO.stream().collect(
                    Collectors.toMap(GeneratrixAndPowerStationDTO::getGeneratrixId, Function.identity()));
            Map<String, RStatOrgBusbarVoltageVO> orgMap = temp.stream().collect(Collectors.toMap(x-> x.getOrgNo()+"_"+x.getBusbarId(), Function.identity()));
            orgMap.forEach((key, value) -> {
                String[] split = key.split("_");
                if (deptMap.containsKey(split[0])) {
                    DeptDTO deptDTO = deptMap.get(split[0]);
                    value.setOrgName(deptDTO.getName());
                    if(generatrixMap.containsKey(split[1])){
                        GeneratrixAndPowerStationDTO dto = generatrixMap.get(split[1]);
                        value.setBusbarVoltageLevel(dto.getGeneratrixVoltageLevel());
                        value.setPowerStationName(dto.getPowerName());
                        info.add(value);
                    }
                }
            });
        }
        if(CollUtil.isNotEmpty(info)){
            info.sort(Comparator.comparing(RStatOrgBusbarVoltageVO::getOrgNo));
        }
        return info;
    }

}
