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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.device.pms.api.DistributionMonitorClient;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
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.event.mapper.distribution.PwRmpEventDetailMapper;
import com.njcn.event.pojo.param.UniversalFrontEndParam;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.event.pojo.vo.PwRmpEventDetailVO;
import com.njcn.event.service.distribution.PwRmpEventDetailService;
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 lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 配网-暂态事件明细
 *
 * @author yzh
 * @date 2022/10/31
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PwRmpEventDetailServiceImpl implements PwRmpEventDetailService {

    private final PwRmpEventDetailMapper pwRmpEventDetailMapper;

    private final PwMonitorClient pwMonitorClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final MonitorClient monitorClient;

    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    private final DistributionMonitorClient distributionMonitorClient;

    /**
     * 获取配网暂态事件明细
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.RmpEventDetailVO>
     * @author yzh
     * @date 2022/10/31
     */
    @Override
    public Page<PwRmpEventDetailVO> getPwRmpEventDetail(UniversalFrontEndParam param) {
        Page<PwRmpEventDetailVO> pageResult = new Page<>(param.getPageNum(),param.getPageSize());
        // 获取单位下的监测点id集合
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(param.getId());
        pwPmsMonitorParam.setMonitorName(param.getMonitorName());
        List<String> pwPmsMonitorDTOList = pmsGeneralDeviceInfoClient.getPwPmsMonitorIds(pwPmsMonitorParam).getData();
        if (CollectionUtil.isEmpty(pwPmsMonitorDTOList)) {
            return pageResult;
        }


        // 获取配网暂态事件明细
        Page<RmpEventDetailPO> rmpEventDetailsPO = pwRmpEventDetailMapper.getPwRmpEventDetail(new Page<>(param.getPageNum(),param.getPageSize()),param,
                pwPmsMonitorDTOList);

        List<RmpEventDetailPO> temList = rmpEventDetailsPO.getRecords();
        if (CollectionUtil.isEmpty(temList)) {
            return pageResult;
        }
        List<String> ids = temList.stream().map(RmpEventDetailPO::getMeasurementPointId).distinct().collect(Collectors.toList());
        List<PwPmsMonitorDTO> pList = distributionMonitorClient.pwMonitorAreaDetailInfo(ids).getData();
        Map<String,PwPmsMonitorDTO> pwPmsMonitorDTOMap = pList.stream().collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId,Function.identity()));

        // 查询检测点类型字典
        List<DictData> lineSortList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.LINE_SORT.getCode()).getData();
        // 将单位信息转为map集合 key: 单位id  value: 单位实体


        // 属性赋值
        List<PwRmpEventDetailVO> t = new ArrayList<>();
        temList.forEach(po -> {
            PwRmpEventDetailVO vo = new PwRmpEventDetailVO();
            BeanUtils.copyProperties(po, vo);
            vo.setMonitorId(po.getMeasurementPointId());
            vo.setEventId(po.getEventId());
            vo.setOrgId(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getOrgId());
            vo.setOrgName(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getOrgName());
            vo.setMonitorSort(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getMonitorSort());
            vo.setMonitorName(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getMonitorName());
            vo.setMonitorVoltageLevel(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getVoltageLevel());
            for (DictData dictData : lineSortList) {
                if (vo.getMonitorSort().equals(dictData.getId())) {
                    if (dictData.getCode().equals(DicDataEnum.ONE_LINE.getCode())) {
                        vo.setMonitorSort(dictData.getName());
                        vo.setSubName(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getPowerName());
                        vo.setPowerClient("/");
                        vo.setPowerDistributionArea("/");
                    } else if (dictData.getCode().equals(DicDataEnum.TWO_LINE.getCode())) {
                        vo.setMonitorSort(dictData.getName());
                        vo.setSubName("/");
                        vo.setPowerDistributionArea(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getMonitorName());
                        vo.setPowerClient("/");
                        vo.setMonitorName("/");
                    } else if (dictData.getCode().equals(DicDataEnum.THREE_LINE.getCode())) {
                        vo.setMonitorSort(dictData.getName());
                        vo.setSubName("/");
                        vo.setPowerDistributionArea("/");
                        vo.setPowerClient(pwPmsMonitorDTOMap.get(po.getMeasurementPointId()).getMonitorName());
                        vo.setMonitorName("/");
                    }
                }
            }
            t.add(vo);
        });
        pageResult.setRecords(t);
        return pageResult;
    }

}
