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.RMpEmissionMMapper;
import com.njcn.harmonic.pojo.param.RMpEmissionParam;
import com.njcn.harmonic.pojo.po.RMpEmissionIExcessRateM;
import com.njcn.harmonic.pojo.po.RMpEmissionIM;
import com.njcn.harmonic.pojo.po.RMpEmissionM;
import com.njcn.harmonic.pojo.vo.RMpEmissionChartVO;
import com.njcn.harmonic.pojo.vo.RMpEmissionIExcessRateMVO;
import com.njcn.harmonic.pojo.vo.RMpEmissionIMVO;
import com.njcn.harmonic.pojo.vo.RMpEmissionVO;
import com.njcn.harmonic.service.specialanalysis.RMpEmissionIExcessRateMService;
import com.njcn.harmonic.service.specialanalysis.RMpEmissionIMService;
import com.njcn.harmonic.service.specialanalysis.RMpEmissionMService;
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__emission_m(风电场发射特性-月表)】的数据库操作Service实现
* @createDate 2022-11-30 18:55:11
*/
@Service
@RequiredArgsConstructor
public class RMpEmissionMServiceImpl extends ServiceImpl<RMpEmissionMMapper, RMpEmissionM>
    implements RMpEmissionMService{

    private final DeptFeignClient deptFeignClient;

    private final RMpEmissionIMService rMpEmissionIMService;

    private final RMpEmissionIExcessRateMService rMpEmissionIExcessRateMService;

    private final MonitorClient monitorClient;

    /***
     * 风电场发射特性
     * @author jianghaifei
     * @date 2022-11-30 20:05
     * @param rMpEmissionParam
     * @return java.util.List<com.njcn.harmonic.pojo.vo.RMpEmissionVO>
     */
    @Override
    public List<RMpEmissionVO> getRMpEmissionList(RMpEmissionParam rMpEmissionParam) {
        //提取参数
        String id = rMpEmissionParam.getId(); //单位id
        String startTime = rMpEmissionParam.getStartTime(); //开始时间
        String endTime = rMpEmissionParam.getEndTime(); //结束时间
        String monitorTag = rMpEmissionParam.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<RMpEmissionM> emissionWrapper = new LambdaQueryWrapper<>();
        emissionWrapper.in(RMpEmissionM::getOrgNo, deptCodes)
                .eq(StringUtils.isNotBlank(monitorTag), RMpEmissionM::getMonitorTag, monitorTag)
//                .in(RMpEmissionM::getMeasurementPointId, monitorList)
                .ge(StringUtils.isNotBlank(startTime), RMpEmissionM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpEmissionM::getDataDate, endTime);
        List<RMpEmissionM> emissionList = this.list(emissionWrapper);

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


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

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

        return resultList;
    }

    /**
     * 发射特性-折线图
     * @param rMpEmissionParam
     * @return
     */
    @Override
    public RMpEmissionChartVO getRMpEmissionChart(RMpEmissionParam rMpEmissionParam) {
        //提取参数
        String id = rMpEmissionParam.getId(); //单位id
        String monitorTag = rMpEmissionParam.getMonitorTag(); //监测点标签
        String startTime = rMpEmissionParam.getStartTime(); //开始时间
        String endTime = rMpEmissionParam.getEndTime(); //结束时间

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

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

        //查询风电场发射特性/各次谐波电流超标占比
        LambdaQueryWrapper<RMpEmissionIExcessRateM> emissionIExcessRateWrapper = new LambdaQueryWrapper<>();
        emissionIExcessRateWrapper.eq(RMpEmissionIExcessRateM::getOrgNo, data.getCode())
//                .in(RMpEmissionIExcessRateM::getMeasurementPointId, monitorList)
                .eq(StringUtils.isNotBlank(monitorTag), RMpEmissionIExcessRateM::getMonitorTag, monitorTag)
                .ge(StringUtils.isNotBlank(startTime), RMpEmissionIExcessRateM::getDataDate, startTime)
                .le(StringUtils.isNotBlank(endTime), RMpEmissionIExcessRateM::getDataDate, endTime);
        List<RMpEmissionIExcessRateM> rateMList = rMpEmissionIExcessRateMService.list(emissionIExcessRateWrapper);
        RMpEmissionIExcessRateM rate =new RMpEmissionIExcessRateM();
        if(CollectionUtil.isNotEmpty(rateMList)){
            rate = rateMList.get(0);
        }

        RMpEmissionChartVO rMpEmissionChartVO = new RMpEmissionChartVO();
        RMpEmissionIMVO rMpEmissionIMVO = new RMpEmissionIMVO();
        BeanUtils.copyProperties(emissionI, rMpEmissionIMVO);
        rMpEmissionChartVO.setRMpEmissionIMVO(rMpEmissionIMVO);

        RMpEmissionIExcessRateMVO rMpEmissionIExcessRateMVO = new RMpEmissionIExcessRateMVO();
        BeanUtils.copyProperties(rate, rMpEmissionIExcessRateMVO);
        rMpEmissionChartVO.setRMpEmissionIExcessRateMVO(rMpEmissionIExcessRateMVO);

        return rMpEmissionChartVO;
    }
}




