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

import cn.hutool.core.collection.CollUtil;
import com.njcn.harmonic.mapper.specialanalysis.WindFarmIndexOverviewMapper;
import com.njcn.harmonic.pojo.param.RMpInfluenceParam;
import com.njcn.harmonic.pojo.po.WindFarmIndexOverviewPO;
import com.njcn.harmonic.pojo.vo.RStatOrgWfIndexOverviewVO;
import com.njcn.harmonic.service.specialanalysis.WindFarmIndexOverview;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 专项分析-风电场-指标总览
 *
 * @author yzh
 * @date 2022/11/30
 */

@Service
@RequiredArgsConstructor
public class WindFarmIndexOverviewServiceImpl implements WindFarmIndexOverview {

    private final WindFarmIndexOverviewMapper windFarmIndexOverviewMapper;

    private final DeptFeignClient deptFeignClient;

    /**
     * 获取风电场指标总览-电流指标
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.harmonic.pojo.vo.RMpPvPowerDetailVO>>
     * @author yzh
     * @date 2022/11/30
     */
    @Override
    public List<RStatOrgWfIndexOverviewVO> getHarmonicCurrent(RMpInfluenceParam param) {
        return getResult(param, "1");
    }

    /**
     * 获取风电场指标总览-电压指标
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.harmonic.pojo.vo.RMpPvPowerDetailVO>>
     * @author yzh
     * @date 2022/11/30
     */
    @Override
    public List<RStatOrgWfIndexOverviewVO> getHarmonicVoltage(RMpInfluenceParam param) {
        return getResult(param, "2");
    }

    /**
     * 获取风电场指标总览-各单位指标
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.harmonic.pojo.vo.RStatOrgWfIndexOverviewVO>>
     * @author yzh
     * @date 2022/11/30
     */
    @Override
    public List<RStatOrgWfIndexOverviewVO> getUnitCurrent(RMpInfluenceParam param) {
        return getResult(param, "3");
    }

    /**
     * 获取风电场指标总览-各单位电压
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.harmonic.pojo.vo.RStatOrgWfIndexOverviewVO>>
     * @author yzh
     * @date 2022/12/1
     */
    @Override
    public List<RStatOrgWfIndexOverviewVO> getUnitVoltage(RMpInfluenceParam param) {
        return getResult(param, "4");
    }

    /**
     * 业务逻辑
     *
     * @param param 条件参数
     * @param tag   标记
     * @return RStatOrgWfIndexOverviewVO
     */
    private List<RStatOrgWfIndexOverviewVO> getResult(RMpInfluenceParam param, String tag) {
        // 获取单位信息
        List<DeptDTO> data = deptFeignClient.getDepSonDetailByDeptId(param.getId()).getData();
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 获取单位code
        List<String> deptCode = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        if (CollUtil.isEmpty(deptCode)) {
            return Collections.emptyList();
        }
        // 获取风电场指标总览-电流指标
        List<WindFarmIndexOverviewPO> pos = getDataInfo(deptCode, param, tag);
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }
        // 将单位信息转换成map集合
        Map<String, String> dataMap = data.stream().collect(Collectors.toMap(DeptDTO::getCode, DeptDTO::getName));
        return pos.stream().map(po -> {
            RStatOrgWfIndexOverviewVO vo = new RStatOrgWfIndexOverviewVO();
            vo.setMonitorNum(po.getMonitorNum());
            vo.setType(dataMap.get(po.getOrgNo()));
            vo.setHistograms(po.getAvgExcessiveDays().toString());
            vo.setLineCharts(po.getMonitorNum().toString());
            vo.setMonitorTag(po.getMonitorTag());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 查询数据库
     *
     * @param deptCode 单位code
     * @param param    条件参数
     * @param tag      标记
     * @return 数据库信息
     */
    private List<WindFarmIndexOverviewPO> getDataInfo(List<String> deptCode, RMpInfluenceParam param, String tag) {
        List<WindFarmIndexOverviewPO> info = new ArrayList<>();
        switch (tag) {
            case "1":
                info = windFarmIndexOverviewMapper.getWindFarmIndexOverviewHarmonicCurrent(deptCode, param);
                break;
            case "2":
                info = windFarmIndexOverviewMapper.getWindFarmIndexOverviewHarmonicVoltage(deptCode, param);
                break;
            case "3":
                info = windFarmIndexOverviewMapper.getWindFarmIndexOverviewUnitCurrent(deptCode, param);
                break;
            case "4":
                info = windFarmIndexOverviewMapper.getWindFarmIndexOverviewUnitVoltage(deptCode, param);
                break;
            default:
                break;
        }
        return info;
    }

}
