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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.api.StatationStatClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PmsStatationStatInfoDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.device.pms.pojo.param.PmsStatationStatInfoParam;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationQMapper;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationYMapper;
import com.njcn.harmonic.pojo.param.StatSubstationBizBaseParam;
import com.njcn.harmonic.pojo.po.RStatSubstationM;
import com.njcn.harmonic.mapper.majornetwork.RStatSubstationMMapper;
import com.njcn.harmonic.pojo.vo.RSubstationIcon2VO;
import com.njcn.harmonic.pojo.vo.RSubstationIconVO;
import com.njcn.harmonic.service.majornetwork.RStatSubstationMService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.web.utils.RequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author rui.wu
 * @since 2022-10-18
 */
@Service
@RequiredArgsConstructor
public class RStatSubstationMServiceImpl extends ServiceImpl<RStatSubstationMMapper, RStatSubstationM> implements RStatSubstationMService {

    private final RStatSubstationMMapper rStatSubstationMMapper;
    private final RStatSubstationQMapper rStatSubstationQMapper;
    private final RStatSubstationYMapper rStatSubstationYMapper;
    private final StatationStatClient statationStatClient;
    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    @Override
    public RSubstationIconVO getStatSubstationIcon(StatisticsBizBaseParam param) {
        RSubstationIconVO rSubstationIconVO = new RSubstationIconVO();
        StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
        //根据部门获取变电站信息
        baseParam.setIds(this.powerIdList(param.getId()));
        String string = param.getType().toString();
        switch (string) {
            //查询变电站稳态指标超标分布（按超标天数）-年数据
            case BizParamConstant.STAT_BIZ_YEAR:
                rSubstationIconVO = rStatSubstationYMapper.getStatSubstationIconY(baseParam);
                break;
            //查询变电站稳态指标超标分布（按超标天数）-季数据
            case BizParamConstant.STAT_BIZ_QUARTER:
                rSubstationIconVO = rStatSubstationQMapper.getStatSubstationIconQ(baseParam);
                break;
            //查询变电站稳态指标超标分布（按超标天数）-月数据
            case BizParamConstant.STAT_BIZ_MONTH:
                rSubstationIconVO = rStatSubstationMMapper.getStatSubstationIconM(baseParam);
                break;
            default:
                break;
        }
        return rSubstationIconVO;
    }

    @Override
    public List<RSubstationIcon2VO> getStatSubstationIcon2(StatisticsBizBaseParam param) {
        //生成变电站id集合
        List<RSubstationIcon2VO> rSubstationIconVO = new ArrayList<>();
        StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
        //获取变电站id
        List<String> powerIds = this.powerIdList(param.getId());
        PmsStatationStatInfoParam pmsParam = new PmsStatationStatInfoParam();
        pmsParam.setPowerIds(powerIds);
        //变电站名称,获取台账的变电站名称
        List<PmsStatationStatInfoDTO> stationInfoDTOS = statationStatClient.getStatationStatInfo(pmsParam).getData();
        //获取变电站信息
        if(CollUtil.isNotEmpty(stationInfoDTOS)){
            List<String> ids = stationInfoDTOS.stream().map(PmsStatationStatInfoDTO::getPowerId).distinct().collect(Collectors.toList());
            baseParam.setIds(ids);
            String string = param.getType().toString();
            switch (string) {
                //查询变电站稳态指标平均超标天数-年数据
                case BizParamConstant.STAT_BIZ_YEAR:
                    rSubstationIconVO = rStatSubstationYMapper.getStatSubstationIcon2Y(baseParam);
                    break;
                //查询变电站稳态指标平均超标天数-季数据
                case BizParamConstant.STAT_BIZ_QUARTER:
                    rSubstationIconVO = rStatSubstationQMapper.getStatSubstationIcon2Q(baseParam);
                    break;
                //查询变电站稳态指标平均超标天数-月数据
                case BizParamConstant.STAT_BIZ_MONTH:
                    rSubstationIconVO = rStatSubstationMMapper.getStatSubstationIcon2M(baseParam);
                    break;
                default:
                    break;
            }
            //获取变电站名称属性替换
            if (CollUtil.isNotEmpty(rSubstationIconVO)) {
                Map<String, String> stationInfoMap = stationInfoDTOS.stream().collect(
                        Collectors.toMap(PmsStatationStatInfoDTO::getPowerId, PmsStatationStatInfoDTO::getPowerName, (Key1, Key2) -> Key2));
                    for (RSubstationIcon2VO dto : rSubstationIconVO) {
                       if(stationInfoMap.containsKey(dto.getSubstationId())){
                           dto.setSubstationName(stationInfoMap.get(dto.getSubstationId()));
                        }
                    }
            }
        }
        return rSubstationIconVO;
    }

    /***
     * 获取台账部门下的变电站
     * @author wr
     * @date 2023-02-24 13:48
     * @param id
     * @return List<String>
     */
    private List<String> powerIdList(String id) {
        //获取部门下面监测点信息
        PmsDeviceInfoParam pms = new PmsDeviceInfoParam();
        pms.setDeptIndex(id);
        pms.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> data = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pms).getData();
        List<String> addids = new ArrayList<>();
        data.stream().forEach(e -> addids.addAll(e.getPowerrIdList()));
        return addids;
    }
}
