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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.harmonic.pojo.po.WfOfOverviewPO;
import com.njcn.harmonic.pojo.po.WfOfOverviewVoltagePO;
import com.njcn.harmonic.pojo.vo.WfOfOverviewVO;
import com.njcn.harmonic.pojo.vo.WfOfOverviewVoltageVO;
import com.njcn.harmonic.service.specialanalysis.WfOfOverviewService;
import com.njcn.harmonic.service.specialanalysis.WfOfOverviewVoltageService;
import com.njcn.harmonic.service.specialanalysis.WindFarmOverviewService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 专项分析-风电场-概述
 *
 * @author yzh
 * @date 2022/11/30
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class WindFarmOverviewServiceImpl implements WindFarmOverviewService {

    private final DeptFeignClient deptFeignClient;

    private final WfOfOverviewService wfOfOverviewService;

    private final WfOfOverviewVoltageService wfOfOverviewVoltageService;

    private final DicDataFeignClient dicDataFeignClient;

    /**
     * 获取风电场概述地图
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.harmonic.pojo.vo.RMpPvPowerDetailVO>
     * @author yzh
     * @date 2022/12/5
     */
    @Override
    public List<?> getWindFarmOverviewMap(StatisticsBizBaseParam param) {
        return null;
    }

    /**
     * 获取风电场概述数值
     *
     * @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 WfOfOverviewVO getWindFarmOverviewValues(StatisticsBizBaseParam param) {
        // 获取单位信息
        Dept data = deptFeignClient.getDeptById(param.getId()).getData();
        if (StringUtils.isEmpty(data)) {
            return new WfOfOverviewVO();
        }
        // 获取单位code
        String code = data.getCode();
        // 获取风电场概述数值
        LambdaQueryWrapper<WfOfOverviewPO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WfOfOverviewPO::getOrgNo, code);
        WfOfOverviewPO po = wfOfOverviewService.getOne(lqw);
        if (StringUtils.isEmpty(po)) {
            return new WfOfOverviewVO();
        }
        WfOfOverviewVO vo = new WfOfOverviewVO();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    /**
     * 获取风电场概述环形图
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < com.njcn.harmonic.pojo.vo.RMpPvPowerDetailVO>>
     * @author yzh
     * @date 2022/12/5
     */
    @Override
    public List<WfOfOverviewVoltageVO> getWindFarmOverviewCircularGraph(StatisticsBizBaseParam param) {
        // 获取单位信息
        Dept data = deptFeignClient.getDeptById(param.getId()).getData();
        if (StringUtils.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 获取单位code
        String code = data.getCode();
        // 获取风电场概述数值
        LambdaQueryWrapper<WfOfOverviewVoltagePO> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WfOfOverviewVoltagePO::getOrgNo, code);
        List<WfOfOverviewVoltagePO> pos = wfOfOverviewVoltageService.list(lqw);
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }
        // 获取电压等级字典
        DictData tenKv
                = dicDataFeignClient.getDicDataByCode(DicDataEnum.DY_10KV.getCode()).getData();
        DictData thirtyFiveKv
                = dicDataFeignClient.getDicDataByCode(DicDataEnum.DY_35KV.getCode()).getData();
        DictData oneHundredAndTenKv
                = dicDataFeignClient.getDicDataByCode(DicDataEnum.DY_110KV.getCode()).getData();
        // 属性赋值
        List<WfOfOverviewVoltageVO> result = new ArrayList<>();
        int count = 0;
        for (WfOfOverviewVoltagePO po : pos) {
            WfOfOverviewVoltageVO vo = new WfOfOverviewVoltageVO();
            BeanUtils.copyProperties(po,vo);
            if (po.getVoltageLevel().equals(tenKv.getId())){
                vo.setVoltageLevel(po.getVoltageLevel());
                vo.setWfSum(po.getWfSum());
                result.add(vo);
            }else if (po.getVoltageLevel().equals(thirtyFiveKv.getId())){
                vo.setVoltageLevel(po.getVoltageLevel());
                vo.setWfSum(po.getWfSum());
                result.add(vo);
            }else if (po.getVoltageLevel().equals(oneHundredAndTenKv.getId())){
                vo.setVoltageLevel(po.getVoltageLevel());
                vo.setWfSum(po.getWfSum());
                result.add(vo);
            }else {
                count=count+po.getWfSum();
            }
        }
        WfOfOverviewVoltageVO vo = new WfOfOverviewVoltageVO();
        vo.setVoltageLevel("22OKV以上");
        vo.setWfSum(count);
        result.add(vo);
        return result;
    }
}


