package com.njcn.event.service.majornetwork.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.event.mapper.majornetwork.RStatSubstationMMapper;
import com.njcn.event.mapper.majornetwork.RStatSubstationQMapper;
import com.njcn.event.mapper.majornetwork.RStatSubstationYMapper;
import com.njcn.event.pojo.param.StatSubstationBizBaseParam;
import com.njcn.event.pojo.po.RStatSubstationM;
import com.njcn.event.pojo.vo.RSubstationIcon2VO;
import com.njcn.event.pojo.vo.RSubstationIconVO;
import com.njcn.event.service.majornetwork.RStatSubstationMService;
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.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);
        //变电站id
        baseParam.setIds(this.powerrIdList(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) {
        List<RSubstationIcon2VO> rSubstationIconVO = new ArrayList<>();
        StatSubstationBizBaseParam baseParam = BeanUtil.copyProperties(param, StatSubstationBizBaseParam.class);
        //获取变电站id
        baseParam.setIds(this.powerrIdList(param.getId()));
        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)) {
            List<String> ids = rSubstationIconVO.stream().map(RSubstationIcon2VO::getSubstationId).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(ids)) {
                PmsStatationStatInfoParam pmsParam = new PmsStatationStatInfoParam();
                pmsParam.setPowerIds(ids);
                List<PmsStatationStatInfoDTO> data1 = statationStatClient.getStatationStatInfo(pmsParam).getData();
                if (CollUtil.isNotEmpty(data1)) {
                    for (PmsStatationStatInfoDTO dto : data1) {
                        for (RSubstationIcon2VO vo : rSubstationIconVO) {
                            if (dto.getPowerId().equals(vo.getSubstationId())) {
                                vo.setSubstationName(dto.getPowerName());
                            }
                        }
                    }
                }
            }
        }
        return rSubstationIconVO;
    }

    //根据部门获取监测点id信息
    private List<String> powerrIdList(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;
    }
}
