package com.njcn.device.pms.service.distribution.impl;

import cn.hutool.core.collection.CollUtil;
import com.njcn.device.pms.mapper.majornetwork.RStatMeasurementAccountDetailMapper;
import com.njcn.device.pms.mapper.majornetwork.RStatTargetDetailMapper;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.DataQualityDetailsParam;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.device.pms.pojo.po.RStatMeasurementAccountDetailPO;
import com.njcn.device.pms.pojo.po.RStatTargetDetailPO;
import com.njcn.device.pms.pojo.vo.PwRStatMeasurementAccountDetailVO;
import com.njcn.device.pms.pojo.vo.PwRStatTargetDetailVO;
import com.njcn.device.pms.service.distribution.IPwMonitorService;
import com.njcn.device.pms.service.distribution.PwDataQualityDetailsService;
import com.njcn.device.pms.service.majornetwork.IMonitorService;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
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.stream.Collectors;

/**
 * @author yzh
 * @date 2022/11/8
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class PwDataQualityDetailsServiceImpl implements PwDataQualityDetailsService {

    private final IPwMonitorService iPwMonitorService;

    private final RStatMeasurementAccountDetailMapper rStatMeasurementAccountDetailMapper;

    private final RStatTargetDetailMapper rStatTargetDetailMapper;

    private final IMonitorService iMonitorService;

    private final DicDataFeignClient dicDataFeignClient;

    /**
     * 获取配网数据质量-台账类数据指标核查详情-监测点台账数据质量问题
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.PwRStatMeasurementAccountDetailVO>
     * @author yzh
     * @date 2022/11/8
     */
    @Override
    public List<PwRStatMeasurementAccountDetailVO> getPwQualityProblemsOfMonitoringPointAccountData(DataQualityDetailsParam param) {
        // 获取配网监测点id
        List<PwPmsMonitorDTO> data = getPwmonitorInfo(param);
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 获取主网数据质量-台账类数据指标核查详情-监测点台账数据质量问题
        List<RStatMeasurementAccountDetailPO> pos =
                rStatMeasurementAccountDetailMapper
                        .getQualityProblemsOfMonitoringPointAccountData(data.stream().map(PwPmsMonitorDTO::getMonitorId).collect(Collectors.toList()),
                                param);
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }

        // 查询二类监测点字典
        DictData twoLine = dicDataFeignClient.getDicDataByCode(DicDataEnum.TWO_LINE.getCode()).getData();
        //将单位信息转为map集合 key: 单位id  value: 单位实体
        Map<String, PwPmsMonitorDTO> dataMap = getDataMap(data);
        // 属性赋值
        return pos.stream().map(po -> {
            PwRStatMeasurementAccountDetailVO vo = new PwRStatMeasurementAccountDetailVO();
            BeanUtils.copyProperties(po, vo);
            vo.setMonitorId(po.getMeasurementPointId());
            vo.setOrgName(dataMap.get(vo.getMonitorId()).getOrgName());
            vo.setMonitorName(dataMap.get(vo.getMonitorId()).getMonitorName());
            vo.setMonitorVoltageLevel(dataMap.get(vo.getMonitorId()).getVoltageLevel());
            vo.setPowerName(dataMap.get(vo.getMonitorId()).getPowerName());
            // TODO 线路
            vo.setMediumVoltageLine(dataMap.get(vo.getMonitorId()).getLineName());
            if (dataMap.get(vo.getMonitorId()).getMonitorSort().equals(twoLine.getId())) {
                vo.setPlatformAreaName(twoLine.getName());
            } else {
                vo.setPlatformAreaName("/");
            }
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取配网数据质量-稳态指标类数据质量问题查询
     *
     * @param param 条件参数
     * @return java.util.List<com.njcn.event.pojo.vo.PwRStatTargetDetailVO>
     * @author yzh
     * @date 2022/11/8
     */
    @Override
    public List<PwRStatTargetDetailVO> getPwRStatTargetDetail(DataQualityDetailsParam param) {
        // 获取配网监测点id
        List<PwPmsMonitorDTO> data = getPwmonitorInfo(param);
        if (CollUtil.isEmpty(data)) {
            return Collections.emptyList();
        }
        // 取配网数据质量-稳态指标类数据质量问题查询
        List<RStatTargetDetailPO> pos = rStatTargetDetailMapper.getRStatTargetDetail(param, data.stream().map(PwPmsMonitorDTO::getMonitorId).collect(Collectors.toList()));
        if (CollUtil.isEmpty(pos)) {
            return Collections.emptyList();
        }
        // 查询二类监测点字典
        DictData twoLine = dicDataFeignClient.getDicDataByCode(DicDataEnum.TWO_LINE.getCode()).getData();
        //将单位信息转为map集合 key: 单位id  value: 单位实体
        Map<String, PwPmsMonitorDTO> dataMap = getDataMap(data);
        // 属性赋值
        return pos.stream().map(po -> {
            PwRStatTargetDetailVO vo = new PwRStatTargetDetailVO();
            BeanUtils.copyProperties(po,vo);
            vo.setMonitorId(po.getMeasurementPointId());
            vo.setMonitorName(dataMap.get(vo.getMonitorId()).getMonitorName());
            vo.setMonitorVoltageLevel(dataMap.get(vo.getMonitorId()).getVoltageLevel());
            vo.setPowerName(dataMap.get(vo.getMonitorId()).getPowerName());
            vo.setMediumVoltageLine(dataMap.get(vo.getMonitorId()).getLineName());
            if (dataMap.get(vo.getMonitorId()).getMonitorSort().equals(twoLine.getId())) {
                vo.setPlatformAreaName(twoLine.getName());
            } else {
                vo.setPlatformAreaName("/");
            }
            // TODO 地市公司
            vo.setCityCompany(dataMap.get(vo.getMonitorId()).getOrgName());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取配网监测点id
     *
     * @param param 条件参数
     * @return List<PwPmsMonitorDTO>
     */
    private List<PwPmsMonitorDTO> getPwmonitorInfo(DataQualityDetailsParam param) {
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(param.getId());
        return iPwMonitorService.getPwMonitorList(pwPmsMonitorParam);
    }

    /**
     * 将单位信息转为map集合 key: 单位id  value: 单位实体
     */
    private Map<String, PwPmsMonitorDTO> getDataMap(List<PwPmsMonitorDTO> data) {
        return data
                .stream()
                .collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId, PwPmsMonitorDTO -> PwPmsMonitorDTO));
    }
}
