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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pms.api.DistributionMonitorClient;
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.harmonic.mapper.distribution.PwRMpBenchmarkLevelYMapper;
import com.njcn.harmonic.pojo.param.RMpBenchmarkLevelParam;
import com.njcn.harmonic.pojo.po.RMpBenchmarkLevelM;
import com.njcn.harmonic.pojo.po.RMpBenchmarkLevelY;
import com.njcn.harmonic.pojo.vo.PwRMpBenchmarkLevelVO;
import com.njcn.harmonic.service.distribution.PwRMpBenchmarkLevelYService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author jianghf
* @description 针对表【r_mp_benchmark_level_y】的数据库操作Service实现
* @createDate 2022-10-11 10:32:18
*/
@Service
@RequiredArgsConstructor
public class PwRMpBenchmarkLevelYServiceImpl extends ServiceImpl<PwRMpBenchmarkLevelYMapper, RMpBenchmarkLevelY>
implements PwRMpBenchmarkLevelYService {


    private final PwMonitorClient pwMonitorClient;


    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    private final DistributionMonitorClient distributionMonitorClient;

    /***
     * 配网-全网基准水平
     * @author jianghaifei
     * @date 2022-11-04 10:32
     * @param rMpBenchmarkLevelParam
     * @return java.util.List<com.njcn.harmonic.pojo.vo.RMpBenchmarkLevelVO>
     */
    @Override
    public Page<PwRMpBenchmarkLevelVO> getPwRMpBenchmarkLevelList(RMpBenchmarkLevelParam rMpBenchmarkLevelParam) {
        Page<PwRMpBenchmarkLevelVO> pageResult = new Page<>(rMpBenchmarkLevelParam.getPageNum(),rMpBenchmarkLevelParam.getPageSize());

        //提取查询条件
        String id = rMpBenchmarkLevelParam.getId(); //单位id
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "单位id不可为空");
        }
        String startTime = rMpBenchmarkLevelParam.getStartTime(); //开始时间
        String endTime = rMpBenchmarkLevelParam.getEndTime(); //截止时间
        List <String> voltageLevelParamList = StringUtils.isNotBlank(rMpBenchmarkLevelParam.getVoltageLevel()) ? Arrays.asList(rMpBenchmarkLevelParam.getVoltageLevel().split(",")) : null; //电压等级
        Integer pageNum = rMpBenchmarkLevelParam.getPageNum() != null && rMpBenchmarkLevelParam.getPageNum() != 0 ? rMpBenchmarkLevelParam.getPageNum() : 1; //页码
        Integer pageSize = rMpBenchmarkLevelParam.getPageSize() != null && rMpBenchmarkLevelParam.getPageSize() != 0 ? rMpBenchmarkLevelParam.getPageSize() : 10; //页面尺寸




        //根据条件查询单位下面的所有配网监测点
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(id); //单位id
        pwPmsMonitorParam.setVoltageLevels(voltageLevelParamList); //电压等级

        List<String> monitorIds = pmsGeneralDeviceInfoClient.getPwPmsMonitorIds(pwPmsMonitorParam).getData();
        if (CollUtil.isEmpty(monitorIds)) {
            return new Page<>();
        }


      //查询【基准水平-年】数据

        LambdaQueryWrapper<RMpBenchmarkLevelY> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RMpBenchmarkLevelY::getMeasurementPointId, monitorIds)
                .ge(StringUtils.isNotBlank(startTime), RMpBenchmarkLevelY::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpBenchmarkLevelY::getDataDate, endTime);
        Page<RMpBenchmarkLevelY> listPage = this.page(new Page<>(pageNum, pageSize), lambdaQueryWrapper);
        List<RMpBenchmarkLevelY> temList = listPage.getRecords();
        if(CollUtil.isEmpty(temList)){
            pageResult.setTotal(listPage.getTotal());
            return pageResult;
        }

        //监测点id集合
        List<String> monitorIdList = temList.stream().map(RMpBenchmarkLevelY::getMeasurementPointId).distinct().collect(Collectors.toList());
        List<PwPmsMonitorDTO> pwMonitorList = distributionMonitorClient.pwMonitorAreaDetailInfo(monitorIdList).getData();
        //监测点map  key:监测点id  value:监测点实体
        Map<String, PwPmsMonitorDTO> monitorMap = pwMonitorList.stream().collect(
                Collectors.toMap(PwPmsMonitorDTO::getMonitorId, Function.identity(), (key1, key2) -> key1));

        List<PwRMpBenchmarkLevelVO> resultList = listPage.getRecords().stream().map(item -> {
            //封装前端需要的对象
            PwRMpBenchmarkLevelVO rMpBenchmarkLevelVO = new PwRMpBenchmarkLevelVO();
            BeanUtils.copyProperties(item, rMpBenchmarkLevelVO);
            //单位信息
            rMpBenchmarkLevelVO.setOrgNo(monitorMap.get(item.getMeasurementPointId()).getOrgId()); //单位id
            rMpBenchmarkLevelVO.setOrgName(monitorMap.get(item.getMeasurementPointId()).getOrgName()); //单位名称
            //监测点信息
            rMpBenchmarkLevelVO.setMeasurementPointName(monitorMap.get(item.getMeasurementPointId()).getMonitorName()); //监测点名称
            //电压等级
            rMpBenchmarkLevelVO.setVoltageLevel(monitorMap.get(item.getMeasurementPointId()).getVoltageLevel());
            return rMpBenchmarkLevelVO;
        }).collect(Collectors.toList());

        Page<PwRMpBenchmarkLevelVO> resultPage = new Page<>();
        BeanUtils.copyProperties(listPage, resultPage);
        resultPage.setRecords(resultList);

        return resultPage;
    }
}
