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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.device.pms.api.PwMonitorClient;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.harmonic.mapper.specialanalysis.RStatOrgPvPowerQualityMMapper;
import com.njcn.harmonic.pojo.param.StreamReverseParam;
import com.njcn.harmonic.pojo.vo.StreamReverseCountyVO;
import com.njcn.harmonic.pojo.vo.StreamReverseOrgVO;
import com.njcn.harmonic.service.specialanalysis.StreamReverseService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.enums.DicDataTypeEnum;
import com.njcn.system.pojo.po.DictData;
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.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Title StreamReverseServiceImpl
 * @Package com.njcn.harmonic.service.specialanalysis.impl
 * @Author jianghaifei
 * @Date 2022-11-25 11:27
 * @Version V1.0
 */
@Service
@RequiredArgsConstructor
public class StreamReverseServiceImpl implements StreamReverseService {

    private final DeptFeignClient deptFeignClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final RStatOrgPvPowerQualityMMapper rStatOrgPvPowerQualityMMapper;

    private final PwMonitorClient pwMonitorClient;
    /***
     * 潮流倒送分析-单位
     * @author jianghaifei
     * @date 2022-11-25 14:02
     * @param streamReverseParam
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.njcn.harmonic.pojo.vo.StreamReverseOrgVO>
     */
    @Override
    public Page<StreamReverseOrgVO> streamReverseByOrg(StreamReverseParam streamReverseParam) {
        //提取参数
        String id = streamReverseParam.getId(); //单位
        String startTime = streamReverseParam.getStartTime(); //开始时间
        String endTime = streamReverseParam.getEndTime(); //结束时间
        Integer pageNum = streamReverseParam.getPageNum() != null && streamReverseParam.getPageNum() != 0 ? streamReverseParam.getPageNum() : 1; //页码
        Integer pageSize = streamReverseParam.getPageSize() != null && streamReverseParam.getPageSize() != 0 ? streamReverseParam.getPageSize() : 10; //页面尺寸

        //获取台区电能质量事件类型字典
        List<DictData> eventTypeDictList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.AREA_PQ_EVENT_TYPE.getCode()).getData();
        //将台区电能质量事件类型信息转成map，key：code  value：id
        Map<String, String> eventTypeDictMap = eventTypeDictList.stream().collect(Collectors.toMap(DictData::getCode, DictData::getId));

//        List<DeptDTO> data = deptFeignClient.getDeptDescendantIndexes(id, WebUtil.filterDeptType()).getData();
        //获取当前部门和子部门信息
        List<DeptDTO> data = deptFeignClient.getDepSonDetailByDeptId(id).getData();
        Map<String, DeptDTO> deptMap = data.stream().collect(Collectors.toMap(DeptDTO::getCode, dept -> dept));
        List<String> deptCodes = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());

//        if (!id.equals(orgIdList.get(0))) {
//            orgIdList.add(id); //把父单位的id传进去
//        }

        HashMap<String, Object> condMap = new HashMap<>();
        condMap.put("startTime", startTime);
        condMap.put("endTime", endTime);
        condMap.put("orgIdList", deptCodes);
        condMap.put(DicDataEnum.EVENT_TYPE_U.getCode(), eventTypeDictMap.get(DicDataEnum.EVENT_TYPE_U.getCode())); //潮流倒送
        condMap.put(DicDataEnum.EVENT_TYPE_A.getCode(), eventTypeDictMap.get(DicDataEnum.EVENT_TYPE_A.getCode())); //潮流倒送导致设备重载
        condMap.put(DicDataEnum.EVENT_TYPE_S.getCode(), eventTypeDictMap.get(DicDataEnum.EVENT_TYPE_S.getCode())); //潮流倒送导致设备过载
        Page<StreamReverseOrgVO> page = rStatOrgPvPowerQualityMMapper.getStreamReverseByOrg(new Page<>(pageNum, pageSize), condMap);
        List<StreamReverseOrgVO> records = page.getRecords();
        DecimalFormat df = new DecimalFormat("###.00");
        List<StreamReverseOrgVO> resultList = records.stream().peek(item -> {
            item.setOrgName(deptMap.get(item.getOrgId()).getName()); //单位名称

            int streamOverloadNum = item.getStreamOverloadNum(); //过载数
            int streamReloadNum = item.getStreamReloadNum(); //重载数
            int streamNum = item.getStreamNum(); //潮流倒送数
            if (streamNum != 0) {
                item.setStreamLoadRate(Double.parseDouble(df.format((streamOverloadNum + streamReloadNum) * 1.0 / (streamNum * 1.0))));
            } else {
                item.setStreamLoadRate(0.0);
            }
        }).collect(Collectors.toList());

        page.setRecords(resultList);

        return page;
    }

    /***
     * 潮流倒送分析-整县
     * @author jianghaifei
     * @date 2022-11-28 15:00
     * @param streamReverseParam
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.njcn.harmonic.pojo.vo.StreamReverseCountyVO>
     */
    @Override
    public Page<StreamReverseCountyVO> streamReverseByCounty(StreamReverseParam streamReverseParam) {
        //提取参数
        String id = streamReverseParam.getId(); //单位
        String startTime = streamReverseParam.getStartTime(); //开始时间
        String endTime = streamReverseParam.getEndTime(); //结束时间
        Integer pageNum = streamReverseParam.getPageNum() != null && streamReverseParam.getPageNum() != 0 ? streamReverseParam.getPageNum() : 1; //页码
        Integer pageSize = streamReverseParam.getPageSize() != null && streamReverseParam.getPageSize() != 0 ? streamReverseParam.getPageSize() : 10; //页面尺寸

        //根据条件查询单位下面的所有配网监测点
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(id); //单位id
        List<PwPmsMonitorDTO> pwMonitorList = pwMonitorClient.getPwMonitorList(pwPmsMonitorParam).getData();
        if (CollUtil.isEmpty(pwMonitorList)) {
            return new Page<>();
        }
        //监测点id集合
        List<String> monitorIdList = pwMonitorList.stream().map(PwPmsMonitorDTO::getMonitorId).collect(Collectors.toList());
        //监测点map  key:监测点id  value:监测点实体
        Map<String, PwPmsMonitorDTO> monitorMap = pwMonitorList.stream().collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId, monitor -> monitor));

        // todo： 潮流倒送时设备负载率只有月表，没法展示曲线
        return null;
    }
}
