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

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.PmsMonitorDTO;
import com.njcn.device.pms.pojo.dto.PwPmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PwPmsMonitorParam;
import com.njcn.harmonic.mapper.distribution.RStatPwPermeabilityMMapper;
import com.njcn.harmonic.pojo.param.RStatPwPermeabilityMParam;
import com.njcn.harmonic.pojo.po.RStatPwPermeabilityM;
import com.njcn.harmonic.pojo.vo.RStatPwPermeabilityMVO;
import com.njcn.harmonic.service.distribution.RStatPwPermeabilityMService;
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 org.springframework.stereotype.Service;

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

/**
 * 各渗透率光伏台区基准水平
* @author jianghf
* @description 针对表【r_stat_pw_permeability_m】的数据库操作Service实现
* @createDate 2022-11-07 10:35:21
*/
@Service
@RequiredArgsConstructor
public class RStatPwPermeabilityMServiceImpl extends ServiceImpl<RStatPwPermeabilityMMapper, RStatPwPermeabilityM>
    implements RStatPwPermeabilityMService{

    private final PwMonitorClient pwMonitorClient;

    private final DicDataFeignClient dicDataFeignClient;

    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    private final DistributionMonitorClient distributionMonitorClient;



    /***
     * 获取各渗透率光伏台区基准水平
     * @author jianghaifei
     * @date 2022-11-07 14:38
     * @param rStatPwPermeabilityMParam
     * @return java.util.List<com.njcn.harmonic.pojo.vo.RStatPwPermeabilityMVO>
     */
    @Override
    public Page<RStatPwPermeabilityMVO> getPwPermeabilityList(RStatPwPermeabilityMParam rStatPwPermeabilityMParam) {
        Page<RStatPwPermeabilityMVO> pageResult = new Page<>(rStatPwPermeabilityMParam.getPageNum(),rStatPwPermeabilityMParam.getPageSize());

        //提起参数
        String id = rStatPwPermeabilityMParam.getId(); //单位id
        String psrName = rStatPwPermeabilityMParam.getPsrName(); //台区名称
        String startTime = rStatPwPermeabilityMParam.getStartTime(); //开始时间
        String endTime = rStatPwPermeabilityMParam.getEndTime(); //结束时间
        Integer pageNum = rStatPwPermeabilityMParam.getPageNum() != null && rStatPwPermeabilityMParam.getPageNum() != 0 ? rStatPwPermeabilityMParam.getPageNum() : 1; //页码
        Integer pageSize = rStatPwPermeabilityMParam.getPageSize() != null && rStatPwPermeabilityMParam.getPageSize() != 0 ? rStatPwPermeabilityMParam.getPageSize() : 10; //页面尺寸

        //根据条件查询单位下面的所有配网监测点
        PwPmsMonitorParam pwPmsMonitorParam = new PwPmsMonitorParam();
        pwPmsMonitorParam.setOrgId(id); //单位id
        DictData dictData = dicDataFeignClient.getDicDataByCodeAndType(DicDataEnum.THREE_LINE.getCode(), DicDataTypeEnum.LINE_SORT.getCode()).getData();
        pwPmsMonitorParam.setMonitorSort(Stream.of(dictData.getId()).collect(Collectors.toList()));
        List<String> monitorIds = pmsGeneralDeviceInfoClient.getPwPmsMonitorIds(pwPmsMonitorParam).getData();
        if (CollUtil.isEmpty(monitorIds)) {
            return new Page<>();
        }

        //获取各渗透率字典
        List<DictData> rateList = dicDataFeignClient.getDicDataByTypeCode(DicDataTypeEnum.PERMEABILITY_TYPE.getCode()).getData();
        Map<String, Object> condMap = rateList.stream().collect(Collectors.toMap(DictData::getCode, DictData::getId));
        condMap.put("startTime", startTime);
        condMap.put("endTime", endTime);
        condMap.put("idList", monitorIds);

        Page<RStatPwPermeabilityMVO> listPage = this.baseMapper.getPwPermeabilityList(
                new Page<>(pageNum, pageSize), condMap);
        List<RStatPwPermeabilityMVO> temList = listPage.getRecords();
        if(CollUtil.isEmpty(temList)){
            pageResult.setTotal(listPage.getTotal());
            return pageResult;
        }

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


        List<RStatPwPermeabilityMVO> list = temList.stream().peek(item -> {
            item.setOrgName(monitorMap.get(item.getPsrId()).getOrgName()); //单位名称
            item.setPsrName(monitorMap.get(item.getPsrId()).getMonitorName()); //台区名称
        }).collect(Collectors.toList());
        listPage.setRecords(list);

        return listPage;
    }
}




