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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.MonitorClient;
import com.njcn.harmonic.mapper.specialanalysis.RMpInfluenceMMapper;
import com.njcn.harmonic.pojo.param.RMpInfluenceParam;
import com.njcn.harmonic.pojo.po.RMpEmissionIExcessRateM;
import com.njcn.harmonic.pojo.po.RMpInfluenceM;
import com.njcn.harmonic.pojo.po.RMpInfluenceVExcessRateM;
import com.njcn.harmonic.pojo.po.RMpInfluenceVM;
import com.njcn.harmonic.pojo.vo.RMpInfluenceChartVO;
import com.njcn.harmonic.pojo.vo.RMpInfluenceVExcessRateMVO;
import com.njcn.harmonic.pojo.vo.RMpInfluenceVMVO;
import com.njcn.harmonic.pojo.vo.RMpInfluenceVO;
import com.njcn.harmonic.service.specialanalysis.RMpInfluenceMService;
import com.njcn.harmonic.service.specialanalysis.RMpInfluenceVExcessRateMService;
import com.njcn.harmonic.service.specialanalysis.RMpInfluenceVMService;
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.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
* @author jianghf
* @description 针对表【r_mp__influence_m(风电场影响特性-月表)】的数据库操作Service实现
* @createDate 2022-12-01 19:28:15
*/
@Service
@RequiredArgsConstructor
public class RMpInfluenceMServiceImpl extends ServiceImpl<RMpInfluenceMMapper, RMpInfluenceM>
    implements RMpInfluenceMService{

    private final DeptFeignClient deptFeignClient;

    private final RMpInfluenceVMService rMpInfluenceVMService;

    private final RMpInfluenceVExcessRateMService rMpInfluenceVExcessRateMService;

    private final MonitorClient monitorClient;

    @Override
    public List<RMpInfluenceVO> getRMpInfluenceList(RMpInfluenceParam rMpInfluenceParam) {
        //提取参数
        String id = rMpInfluenceParam.getId(); //单位id
        String startTime = rMpInfluenceParam.getStartTime(); //开始时间
        String endTime = rMpInfluenceParam.getEndTime(); //结束时间
        String monitorTag = rMpInfluenceParam.getMonitorTag(); //监测点标签
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "单位id不可为空");
        }
        //获取当前部门和子部门信息
        List<DeptDTO> data = deptFeignClient.getDepSonDetailByDeptId(id).getData();
        Map<String, DeptDTO> deptMap = data.stream().collect(Collectors.toMap(DeptDTO::getCode, dept -> dept));
        List<String> deptCodes = data.stream().map(DeptDTO::getCode).collect(Collectors.toList());

        //查询风电场信息
//        PmsMonitorParam pmsMonitorParam = new PmsMonitorParam();
//        pmsMonitorParam.setMonitorTag("风电场");
//        pmsMonitorParam.setOrgIds(deptCodes);
//        List<PmsMonitorDTO> monitorList = monitorClient.getMonitorInfoListByCond(pmsMonitorParam).getData();
//        if (CollUtil.isEmpty(monitorList)) {
//            new ArrayList<>();
//        }

        //查询风电场发射特性信息
        LambdaQueryWrapper<RMpInfluenceM> emissionWrapper = new LambdaQueryWrapper<>();
        emissionWrapper.in(RMpInfluenceM::getOrgNo, deptCodes)
                .eq(StringUtils.isNotBlank(monitorTag), RMpInfluenceM::getMonitorTag, monitorTag)
//                .in(RMpInfluenceM::getMeasurementPointId, monitorList)
                .ge(StringUtils.isNotBlank(startTime), RMpInfluenceM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpInfluenceM::getDataDate, endTime);
        List<RMpInfluenceM> emissionList = this.list(emissionWrapper);

        if (CollUtil.isEmpty(emissionList)) {
            return new ArrayList<>();
        }

        //封装返回数
        List<RMpInfluenceVO> resultList = emissionList.stream().map(item -> {
            RMpInfluenceVO rMpInfluenceVO = new RMpInfluenceVO();
            //单位ID、单位名称、负序电压不平衡度、负序电压不平衡度平均超标天数、负序电压不平衡度超标占比、各次谐波电压平均超标天数
            BeanUtils.copyProperties(item, rMpInfluenceVO);
            rMpInfluenceVO.setOrgName(deptMap.get(item.getOrgNo()).getName()); //单位名称

            return rMpInfluenceVO;
        }).collect(Collectors.toList());

        return resultList;
    }


    /**
     * 影响特性-图表
     * @param rMpInfluenceParam
     * @return
     */
    @Override
    public RMpInfluenceChartVO getRMpInfluenceChart(RMpInfluenceParam rMpInfluenceParam) {
        //提取参数
        String id = rMpInfluenceParam.getId(); //单位id
        String monitorTag = rMpInfluenceParam.getMonitorTag(); //监测点标签
        String startTime = rMpInfluenceParam.getStartTime(); //开始时间
        String endTime = rMpInfluenceParam.getEndTime(); //结束时间

        if (StringUtils.isBlank(id)) {
            throw new BusinessException(CommonResponseEnum.NO_DATA, "单位id不可为空");
        }
        //获取当前部门
        Dept data = deptFeignClient.getDeptById(id).getData();

        //查询风电场发射特性/各次谐波电压含有率信息
        LambdaQueryWrapper<RMpInfluenceVM> emissionIWrapper = new LambdaQueryWrapper<>();
        emissionIWrapper.eq(RMpInfluenceVM::getOrgNo, data.getCode())
//                .in(RMpInfluenceVM::getMeasurementPointId, monitorList)
                .eq(StringUtils.isNotBlank(monitorTag), RMpInfluenceVM::getMonitorTag, monitorTag)
                .ge(StringUtils.isNotBlank(startTime), RMpInfluenceVM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpInfluenceVM::getDataDate, endTime);
        List<RMpInfluenceVM> list = rMpInfluenceVMService.list(emissionIWrapper);
        RMpInfluenceVM emissionI =new RMpInfluenceVM();
        if(CollectionUtil.isNotEmpty(list)){
            emissionI = list.get(0);
        }

        //查询风电场发射特性/各次谐波电压超标占比
        LambdaQueryWrapper<RMpInfluenceVExcessRateM> emissionIExcessRateWrapper = new LambdaQueryWrapper<>();
        emissionIExcessRateWrapper.eq(RMpInfluenceVExcessRateM::getOrgNo, data.getCode())
//                .in(RMpInfluenceVExcessRateM::getMeasurementPointId, monitorList)
                .eq(StringUtils.isNotBlank(monitorTag), RMpInfluenceVExcessRateM::getMonitorTag, monitorTag)
                .ge(StringUtils.isNotBlank(startTime), RMpInfluenceVExcessRateM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpInfluenceVExcessRateM::getDataDate, endTime);

        List<RMpInfluenceVExcessRateM> rateMS = rMpInfluenceVExcessRateMService.list(emissionIExcessRateWrapper);
        RMpInfluenceVExcessRateM rate=new RMpInfluenceVExcessRateM();
        if(CollectionUtil.isNotEmpty(list)){
            rate=rateMS.get(0);
        }

        RMpInfluenceChartVO rMpInfluenceChartVO = new RMpInfluenceChartVO();

        RMpInfluenceVMVO rMpInfluenceVMVO = new RMpInfluenceVMVO();
        BeanUtils.copyProperties(emissionI, rMpInfluenceVMVO);
        rMpInfluenceChartVO.setRMpInfluenceVMVO(rMpInfluenceVMVO);

        RMpInfluenceVExcessRateMVO rMpInfluenceVExcessRateMVO = new RMpInfluenceVExcessRateMVO();
        BeanUtils.copyProperties(rate, rMpInfluenceVExcessRateMVO);
        rMpInfluenceChartVO.setRMpInfluenceVExcessRateMVO(rMpInfluenceVExcessRateMVO);

        return rMpInfluenceChartVO;
    }
}




