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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.pojo.param.PowerQualityParam;
import com.njcn.harmonic.pojo.po.RMpPvPowerDetailMPO;
import com.njcn.harmonic.pojo.vo.RMpPvPowerDetailVO;
import com.njcn.harmonic.service.specialanalysis.DistributedPvVolOverService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author yzh
 * @date 2022/11/22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class DistributedPvVolOverServiceImpl implements DistributedPvVolOverService {

    private final PwMonitorClient monitorClient;

    private final RMpPvPowerDetailMServiceImpl rMpPvPowerDetailMService;


    /**
     * 获取电压越限分析
     *
     * @param param 条件参数
     * @return com.njcn.common.pojo.response.HttpResult<java.util.List < ?>>
     * @author yzh
     * @date 2022/11/22
     */
    @Override
    public Page<RMpPvPowerDetailVO> getVoltageOutOfLimitAnalysis(PowerQualityParam param) {

        // 获取当前单位下的监测点信息
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(param.getId());
        List<PwPmsMonitorDTO> pwMonitorList = monitorClient.getPwPhotovoltaicMonitorList(pwPmsMonitorParam).getData();
        if (CollUtil.isEmpty(pwMonitorList)) {
            return new Page<>();
        }
        // 取出监测点id
        List<String> monitorIds = pwMonitorList
                .stream().map(PwPmsMonitorDTO::getMonitorId).collect(Collectors.toList());
        if (CollUtil.isEmpty(monitorIds)) {
            return new Page<>();
        }
        // 获取电压越限分析
        LambdaQueryWrapper<RMpPvPowerDetailMPO> lqw = new LambdaQueryWrapper<>();
        lqw.in(RMpPvPowerDetailMPO::getMeasurementPointId, monitorIds);
        lqw.ge(param.getStartTime() != null, RMpPvPowerDetailMPO::getDataDate, param.getStartTime());
        lqw.le(param.getEndTime() != null, RMpPvPowerDetailMPO::getDataDate, param.getEndTime());
        Page<RMpPvPowerDetailMPO> pos = rMpPvPowerDetailMService.page(new Page<>(param.getPageNum(), param.getPageSize()),lqw);
        // 将监测点信息转换成map集合
        Map<String, PwPmsMonitorDTO> monitorMap = pwMonitorList.stream()
                .collect(Collectors.toMap(PwPmsMonitorDTO::getMonitorId, PwPmsMonitorDTO -> PwPmsMonitorDTO));
        // 数据赋值
        List<RMpPvPowerDetailVO> infoList = pos.getRecords().stream().map(po -> {
            RMpPvPowerDetailVO vo = new RMpPvPowerDetailVO();
            BeanUtils.copyProperties(po, vo);
            vo.setMonitorSort(monitorMap.get(po.getMeasurementPointId()).getMonitorSort());
            vo.setOrgName(monitorMap.get(po.getMeasurementPointId()).getOrgName());
            return vo;
        }).collect(Collectors.toList());
        Page<RMpPvPowerDetailVO> page=BeanUtil.copyProperties(pos,Page.class);
        page.setRecords(infoList);
        return page;
    }
}
