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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.constant.BizParamConstant;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.api.TractionStationClient;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.dto.PmsTractionStationDTO;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.device.pms.pojo.param.PmsTractionStationParam;
import com.njcn.harmonic.mapper.specialanalysis.RMpRailwayTractionDetailMapper;
import com.njcn.harmonic.mapper.specialanalysis.RMpWpPowerDetailQMapper;
import com.njcn.harmonic.mapper.specialanalysis.RMpWpPowerDetailYMapper;
import com.njcn.harmonic.pojo.param.PowerQualityParam;
import com.njcn.harmonic.pojo.po.*;
import com.njcn.harmonic.mapper.specialanalysis.RMpWpPowerDetailMMapper;
import com.njcn.harmonic.pojo.vo.RMpRailwayTractionDetailVO;
import com.njcn.harmonic.pojo.vo.RMpWpPowerDetailVO;
import com.njcn.harmonic.service.specialanalysis.RMpWpPowerDetailMService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.user.pojo.po.Dept;
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;

/**
 * <p>
 * 风电站_台区电能质量明细_月表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2022-11-30
 */
@Service
@RequiredArgsConstructor
public class RMpWpPowerDetailMServiceImpl extends ServiceImpl<RMpWpPowerDetailMMapper, RMpWpPowerDetailM> implements RMpWpPowerDetailMService {

    private final RMpWpPowerDetailQMapper rMpWpPowerDetailQMapper;
    private final RMpWpPowerDetailYMapper rMpWpPowerDetailYMapper;
    private final RMpRailwayTractionDetailMapper rMpRailwayTractionDetailMapper;
    private final MonitorClient monitorClient;
    private final DeptFeignClient deptFeignClient;
    private final TractionStationClient tractionStationClient;

    @Override
    public Page<RMpWpPowerDetailVO> getWindFarmDetailInfo(PowerQualityParam.PowerQualityInfoParam param) {
        //获取部门id集合
        List<String> codeIds = deptFeignClient.getDepSonSelfCodetByDeptId(param.getId()).getData();

        //根据条件查询单位下面的所有配网监测点
        PmsMonitorParam pmsMonitorParam = new PmsMonitorParam();
        //单位id
        pmsMonitorParam.setOrgIds(codeIds);
        pmsMonitorParam.setMonitorTag(param.getMonitorTag());
        pmsMonitorParam.setIsSpecialMonitor(1);

        //获取监测点信息
        List<PmsMonitorDTO> monitorDTOS = monitorClient.getMonitorInfoListByCond(pmsMonitorParam).getData();
        if (CollUtil.isEmpty(monitorDTOS)) {
            return new Page<>();
        }
        //监测点数据转换
        Map<String, PmsMonitorDTO> monitorMap = monitorDTOS.stream()
                .collect(Collectors.toMap(PmsMonitorDTO::getId, Function.identity()));
        //获取监测点id
        List<String> monitorIds = monitorDTOS
                .stream().map(PmsMonitorDTO::getId).collect(Collectors.toList());
        Page<RMpWpPowerDetailVO> page = new Page<>();
        //数据库分页查询
        switch (param.getType().toString()) {
            //查询区域超标统计-年数据
            case BizParamConstant.STAT_BIZ_YEAR:
                Page<RMpWpPowerDetailY> pageY = rMpWpPowerDetailYMapper.selectPage(new Page<>(param.getPageNum(), param.getPageSize())
                        , new LambdaQueryWrapper<RMpWpPowerDetailY>()
                                .in(RMpWpPowerDetailY::getMeasurementPointId, monitorIds)
                                .eq(RMpWpPowerDetailY::getMonitorTag, param.getMonitorTag())
                                .ge(StrUtil.isNotBlank(param.getStartTime()), RMpWpPowerDetailY::getDataDate, param.getStartTime())
                                .le(StrUtil.isNotBlank(param.getEndTime()), RMpWpPowerDetailY::getDataDate, param.getEndTime())
                );
                List<RMpWpPowerDetailVO> rMpWpPowerDetailVOS = BeanUtil.copyToList(pageY.getRecords(), RMpWpPowerDetailVO.class);
                page = BeanUtil.copyProperties(pageY, Page.class);
                page.setRecords(rMpWpPowerDetailVOS);
                break;
            //查询区域超标统计-季数据
            case BizParamConstant.STAT_BIZ_QUARTER:
                Page<RMpWpPowerDetailQ> pageList = rMpWpPowerDetailQMapper.selectPage(new Page<>(param.getPageNum(), param.getPageSize())
                        , new LambdaQueryWrapper<RMpWpPowerDetailQ>()
                                .in(RMpWpPowerDetailQ::getMeasurementPointId, monitorIds)
                                .eq(RMpWpPowerDetailQ::getMonitorTag, param.getMonitorTag())
                                .ge(StrUtil.isNotBlank(param.getStartTime()), RMpWpPowerDetailQ::getDataDate, param.getStartTime())
                                .le(StrUtil.isNotBlank(param.getEndTime()), RMpWpPowerDetailQ::getDataDate, param.getEndTime())
                );
                List<RMpWpPowerDetailVO> rMpWpPowerDetailVOS2 = BeanUtil.copyToList(pageList.getRecords(), RMpWpPowerDetailVO.class);
                page = BeanUtil.copyProperties(pageList, Page.class);
                page.setRecords(rMpWpPowerDetailVOS2);
                break;
            //查询各区域超标统计-月数据
            case BizParamConstant.STAT_BIZ_MONTH:
                Page<RMpWpPowerDetailM> page1 = this.page(new Page<>(param.getPageNum(), param.getPageSize())
                        , new LambdaQueryWrapper<RMpWpPowerDetailM>()
                                .in(RMpWpPowerDetailM::getMeasurementPointId, monitorIds)
                                .eq(RMpWpPowerDetailM::getMonitorTag, param.getMonitorTag())
                                .ge(StrUtil.isNotBlank(param.getStartTime()), RMpWpPowerDetailM::getDataDate, param.getStartTime())
                                .le(StrUtil.isNotBlank(param.getEndTime()), RMpWpPowerDetailM::getDataDate, param.getEndTime())
                );
                List<RMpWpPowerDetailVO> rMpWpPowerDetailVOS3 = BeanUtil.copyToList(page1.getRecords(), RMpWpPowerDetailVO.class);
                page = BeanUtil.copyProperties(page1, Page.class);
                page.setRecords(rMpWpPowerDetailVOS3);
                break;
            default:
                break;
        }
        page.getRecords().stream().forEach(vo -> {
            if (monitorMap.containsKey(vo.getMeasurementPointId())) {
                PmsMonitorDTO monitorDTO = monitorMap.get(vo.getMeasurementPointId());
                vo.setOrgName(monitorDTO.getOrgName());
                vo.setMeasurementPointName(monitorDTO.getName());
            }
        });
        return page;
    }

    @Override
    public Page<RMpRailwayTractionDetailVO> getRailwayDetailInfo(PowerQualityParam param) {
        //获取部门id集合
        List<DeptDTO> deptDTOS = deptFeignClient.getDepSonDetailByDeptId(param.getId()).getData();
        List<String> deptIds = deptDTOS.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        //根据部门信息，获取牵引站信息
        PmsTractionStationParam stationParam = new PmsTractionStationParam();
        stationParam.setOrgIds(deptIds);
        List<PmsTractionStationDTO> tractionStationDTOS = tractionStationClient.getPmsTractionStationInfo(stationParam).getData();
        if(CollUtil.isEmpty(tractionStationDTOS)){
            return new Page<>();
        }
        //获取牵引站id集合
        List<String> ids = tractionStationDTOS.stream().map(PmsTractionStationDTO::getId).collect(Collectors.toList());
        Map<String, PmsTractionStationDTO> dtoMap = tractionStationDTOS.stream()
                .collect(Collectors.toMap(PmsTractionStationDTO::getId, Function.identity()));

        Page<RMpRailwayTractionDetail> oldPage = rMpRailwayTractionDetailMapper.selectPage(new Page<>(param.getPageNum(), param.getPageSize()), new LambdaQueryWrapper<RMpRailwayTractionDetail>()
                .in(CollUtil.isNotEmpty(ids),RMpRailwayTractionDetail::getTractionStationId, ids)
                .ge(StrUtil.isNotBlank(param.getStartTime()), RMpRailwayTractionDetail::getDataDate, DateUtil.beginOfDay(DateUtil.parse(param.getStartTime())))
                .le(StrUtil.isNotBlank(param.getEndTime()), RMpRailwayTractionDetail::getDataDate,DateUtil.endOfDay(DateUtil.parse(param.getEndTime())))
        );

        List<RMpRailwayTractionDetailVO> newList = BeanUtil.copyToList(oldPage.getRecords(), RMpRailwayTractionDetailVO.class);
        newList.stream().forEach(vo -> {
            if (dtoMap.containsKey(vo.getTractionStationId())) {
                PmsTractionStationDTO dto = dtoMap.get(vo.getTractionStationId());
                vo.setOrgName(dto.getOrgName());
                vo.setRailwayType(dto.getRailwayType());
                vo.setTractionStationName(dto.getName());
                vo.setRailwayLineName(dto.getRailwayLineName());
                vo.setVoltageLevel(dto.getVoltageLevel());
                vo.setRatedCapacity(dto.getRatedCapacity());
            }
        });
        Page<RMpRailwayTractionDetailVO> page = BeanUtil.copyProperties(oldPage, Page.class);
        page.setRecords(newList);
        return page;
    }
}
