package com.njcn.event.service.majornetwork.Impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.njcn.common.pojo.dto.SimpleDTO;
import com.njcn.common.pojo.param.StatisticsBizBaseParam;
import com.njcn.device.pms.api.PmsGeneralDeviceInfoClient;
import com.njcn.device.pms.pojo.dto.PmsGeneralDeviceDTO;
import com.njcn.device.pms.pojo.param.PmsDeviceInfoParam;
import com.njcn.event.common.mapper.EventDistributionStatisticsMapper;
import com.njcn.event.pojo.po.EventDistributionStatisticsPO;
import com.njcn.event.pojo.po.EventDurationLineChartPO;
import com.njcn.event.pojo.po.EventFeatureAmplitudeLineChartPO;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import com.njcn.event.pojo.vo.EventDistributionStatisticsTableVO;
import com.njcn.event.pojo.vo.EventFeatureAmplitudeCurveVO;
import com.njcn.event.pojo.vo.SimpleVO;
import com.njcn.event.service.majornetwork.EventDistributionStatisticsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 暂态指标分布统计
 *
 * @author yzh
 * @date 2022/10/20
 */

@Service
@Slf4j
@RequiredArgsConstructor
public class EventDistributionStatisticsServiceImpl implements EventDistributionStatisticsService {

    private final PmsGeneralDeviceInfoClient pmsGeneralDeviceInfoClient;

    private final EventDistributionStatisticsMapper eventDistributionStatisticsMapper;

    /**
     * 获取暂态指标分布统计
     *
     * @param param 条件参数
     * @return 暂态指标分布统计
     */
    @Override
    public List<EventDistributionStatisticsTableVO> getEventDistributionStatisticsTable(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> pmsGeneralDeviceDTOList = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam).getData();

        if (CollectionUtil.isEmpty(pmsGeneralDeviceDTOList)) {
            return Collections.emptyList();
        }
        // 创建集合，返回数据
        List<EventDistributionStatisticsTableVO> vos = new ArrayList<>();

        // 将监测点id取出
        List<String> monitorIdList = getAllLineIdList(pmsGeneralDeviceDTOList);

        if (CollectionUtil.isEmpty(monitorIdList)) {
            return Collections.emptyList();

        }
        // 获取暂态指标分布统计
        EventDistributionStatisticsPO po = eventDistributionStatisticsMapper.getEventDistributionStatistics(
                monitorIdList,
                param.getStartTime(),
                param.getEndTime());

        if (ObjectUtil.isEmpty(po)) {
            return Collections.emptyList();
        }

        // 特征幅值10%
        // 创建对象返回参数
        EventDistributionStatisticsTableVO featureAmplitude10 = new EventDistributionStatisticsTableVO();
        featureAmplitude10.setFeatureAmplitude("10%");
        featureAmplitude10.setDuration20(po.getFeatureAmplitude10with20s());
        featureAmplitude10.setDuration100(po.getFeatureAmplitude10with100s());
        featureAmplitude10.setDuration500(po.getFeatureAmplitude10with500s());
        featureAmplitude10.setDuration1000(po.getFeatureAmplitude10with1000s());
        featureAmplitude10.setDuration3000(po.getFeatureAmplitude10with3000s());
        featureAmplitude10.setDuration20000(po.getFeatureAmplitude10with20000s());
        featureAmplitude10.setDuration60000(po.getFeatureAmplitude10with60000s());
        featureAmplitude10.setDuration180000(po.getFeatureAmplitude10with180000s());

        // 特征幅值40%
        // 创建对象返回参数
        EventDistributionStatisticsTableVO featureAmplitude40 = new EventDistributionStatisticsTableVO();
        featureAmplitude40.setFeatureAmplitude("40%");
        featureAmplitude40.setDuration20(po.getFeatureAmplitude40with20s());
        featureAmplitude40.setDuration100(po.getFeatureAmplitude40with100s());
        featureAmplitude40.setDuration500(po.getFeatureAmplitude40with500s());
        featureAmplitude40.setDuration1000(po.getFeatureAmplitude40with1000s());
        featureAmplitude40.setDuration3000(po.getFeatureAmplitude40with3000s());
        featureAmplitude40.setDuration20000(po.getFeatureAmplitude40with20000s());
        featureAmplitude40.setDuration60000(po.getFeatureAmplitude40with60000s());
        featureAmplitude40.setDuration180000(po.getFeatureAmplitude40with180000s());

        // 特征幅值70%
        // 创建对象返回参数
        EventDistributionStatisticsTableVO featureAmplitude70 = new EventDistributionStatisticsTableVO();
        featureAmplitude70.setFeatureAmplitude("70%");
        featureAmplitude70.setDuration20(po.getFeatureAmplitude70with20s());
        featureAmplitude70.setDuration100(po.getFeatureAmplitude70with100s());
        featureAmplitude70.setDuration500(po.getFeatureAmplitude70with500s());
        featureAmplitude70.setDuration1000(po.getFeatureAmplitude70with1000s());
        featureAmplitude70.setDuration3000(po.getFeatureAmplitude70with3000s());
        featureAmplitude70.setDuration20000(po.getFeatureAmplitude70with20000s());
        featureAmplitude70.setDuration60000(po.getFeatureAmplitude70with60000s());
        featureAmplitude70.setDuration180000(po.getFeatureAmplitude70with180000s());
        // 特征幅值85%
        // 创建对象返回参数
        EventDistributionStatisticsTableVO featureAmplitude85 = new EventDistributionStatisticsTableVO();
        featureAmplitude85.setFeatureAmplitude("85%");
        featureAmplitude85.setDuration20(po.getFeatureAmplitude85with20s());
        featureAmplitude85.setDuration100(po.getFeatureAmplitude85with100s());
        featureAmplitude85.setDuration500(po.getFeatureAmplitude85with500s());
        featureAmplitude85.setDuration1000(po.getFeatureAmplitude85with1000s());
        featureAmplitude85.setDuration3000(po.getFeatureAmplitude85with3000s());
        featureAmplitude85.setDuration20000(po.getFeatureAmplitude85with20000s());
        featureAmplitude85.setDuration60000(po.getFeatureAmplitude85with60000s());
        featureAmplitude85.setDuration180000(po.getFeatureAmplitude85with180000s());
        // 特征幅值90%
        // 创建对象返回参数
        EventDistributionStatisticsTableVO featureAmplitude90 = new EventDistributionStatisticsTableVO();
        featureAmplitude90.setFeatureAmplitude("90%");
        featureAmplitude90.setDuration20(po.getFeatureAmplitude90with20s());
        featureAmplitude90.setDuration100(po.getFeatureAmplitude90with100s());
        featureAmplitude90.setDuration500(po.getFeatureAmplitude90with500s());
        featureAmplitude90.setDuration1000(po.getFeatureAmplitude90with1000s());
        featureAmplitude90.setDuration3000(po.getFeatureAmplitude90with3000s());
        featureAmplitude90.setDuration20000(po.getFeatureAmplitude90with20000s());
        featureAmplitude90.setDuration60000(po.getFeatureAmplitude90with60000s());
        featureAmplitude90.setDuration180000(po.getFeatureAmplitude90with180000s());
        // 属性赋值
        vos.add(featureAmplitude10);
        vos.add(featureAmplitude40);
        vos.add(featureAmplitude70);
        vos.add(featureAmplitude85);
        vos.add(featureAmplitude90);

        return vos;
    }

    /**
     * 获取持续时间折线图
     *
     * @param param 条件参数
     * @return 持续时间折线图
     */
    @Override
    public List<SimpleVO> getEventDurationLineChart(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> pmsGeneralDeviceDTOList = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam).getData();

        if (CollectionUtil.isEmpty(pmsGeneralDeviceDTOList)) {
            return Collections.emptyList();
        }
        // 创建集合返回数据
        List<SimpleVO> result = new ArrayList<>();
        // 将监测点id取出
        List<String> monitorIdList = getAllLineIdList(pmsGeneralDeviceDTOList);

        if (CollectionUtil.isEmpty(monitorIdList)) {
            return Collections.emptyList();
        }
        // 获取持续时间折线图
        EventDurationLineChartPO po = eventDistributionStatisticsMapper.getEventDurationLineChart(
                monitorIdList,
                param.getStartTime(),
                param.getEndTime());

        SimpleVO vo100 = new SimpleVO();
        vo100.setName("0.1");
        vo100.setValue(po.getDuration100());
        SimpleVO vo250 = new SimpleVO();
        vo250.setName("0.25");
        vo250.setValue(po.getDuration250());
        SimpleVO vo500 = new SimpleVO();
        vo500.setName("0.5");
        vo500.setValue(po.getDuration500());
        SimpleVO vo1000 = new SimpleVO();
        vo1000.setName("1");
        vo1000.setValue(po.getDuration1000());
        SimpleVO vo1100 = new SimpleVO();
        vo1100.setName("1.1");
        vo1100.setValue(po.getDuration1100());

        // 创建map集合，封装数据
        result.add(vo100);
        result.add(vo250);
        result.add(vo500);
        result.add(vo1000);
        result.add(vo1100);

        return result;

    }

    /**
     * 获取特征幅值折线图
     *
     * @param param 条件参数
     * @return 特征幅值折线图
     */
    @Override
    public List<SimpleVO> getEventFeatureAmplitudeLineChart(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> pmsGeneralDeviceDTOList = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam).getData();

        if (CollectionUtil.isEmpty(pmsGeneralDeviceDTOList)) {
            return Collections.emptyList();
        }
        // 创建集合返回数据
        List<SimpleVO> result = new ArrayList<>();
        // 取出所有监测点id
        List<String> monitorIdList = getAllLineIdList(pmsGeneralDeviceDTOList);

        if (CollectionUtil.isEmpty(monitorIdList)) {
            return Collections.emptyList();
        }
        // 获取特征幅值折线图
        EventFeatureAmplitudeLineChartPO po = eventDistributionStatisticsMapper.getEventFeatureAmplitudeLineChart(monitorIdList,
                param.getStartTime(),
                param.getEndTime());

        if (ObjectUtil.isEmpty(po)) {
            return Collections.emptyList();
        }

        SimpleVO vo10 = new SimpleVO();
        vo10.setName("10%");
        vo10.setValue(po.getFeatureAmplitude10());
        SimpleVO vo20 = new SimpleVO();
        vo20.setName("20%");
        vo20.setValue(po.getFeatureAmplitude20());
        SimpleVO vo30 = new SimpleVO();
        vo30.setName("30%");
        vo30.setValue(po.getFeatureAmplitude30());
        SimpleVO vo40 = new SimpleVO();
        vo40.setName("40%");
        vo40.setValue(po.getFeatureAmplitude40());
        SimpleVO vo50 = new SimpleVO();
        vo50.setName("50%");
        vo50.setValue(po.getFeatureAmplitude50());
        SimpleVO vo60 = new SimpleVO();
        vo60.setName("60%");
        vo60.setValue(po.getFeatureAmplitude60());
        SimpleVO vo70 = new SimpleVO();
        vo70.setName("70%");
        vo70.setValue(po.getFeatureAmplitude70());
        SimpleVO vo80 = new SimpleVO();
        vo80.setName("80%");
        vo80.setValue(po.getFeatureAmplitude80());
        SimpleVO vo90 = new SimpleVO();
        vo90.setName("90%");
        vo90.setValue(po.getFeatureAmplitude90());

        // 创建map集合，封装数据
        result.add(vo10);
        result.add(vo20);
        result.add(vo30);
        result.add(vo40);
        result.add(vo50);
        result.add(vo60);
        result.add(vo70);
        result.add(vo80);
        result.add(vo90);

        return result;

    }

    /**
     * 获取暂态指标分布统计曲线图
     *
     * @param param 条件参数
     * @return 暂态指标分布统计曲线图
     */
    @Override
    public EventFeatureAmplitudeCurveVO getEventFeatureAmplitudeCurve(StatisticsBizBaseParam param) {
        // 获取当前用户的部门的子部门信息
        PmsDeviceInfoParam pmsDeviceInfoParam = new PmsDeviceInfoParam();
        pmsDeviceInfoParam.setDeptIndex(param.getId());
        pmsDeviceInfoParam.setStatisticalType(new SimpleDTO());
        List<PmsGeneralDeviceDTO> pmsGeneralDeviceDTOList = pmsGeneralDeviceInfoClient.getPmsDeviceInfoWithInOrg(pmsDeviceInfoParam).getData();

        if (CollUtil.isEmpty(pmsGeneralDeviceDTOList)) {
            return EventFeatureAmplitudeCurveVO.empty();
        }

        // 取出所有监测点id
        List<String> monitorIdList = getAllLineIdList(pmsGeneralDeviceDTOList);
        if (CollUtil.isEmpty(monitorIdList)) {
            return EventFeatureAmplitudeCurveVO.empty();
        }

        // 获取监测点暂态事件明细数据
        List<RmpEventDetailPO> rmpEventDetailList = eventDistributionStatisticsMapper.getRmpEventDetail(monitorIdList, param.getStartTime(), param.getEndTime());
        if (CollUtil.isEmpty(rmpEventDetailList)) {
            return EventFeatureAmplitudeCurveVO.empty();
        }

        List<EventFeatureAmplitudeCurveVO.EventFeatureAmplitudeCurveDataList> curveDataList = rmpEventDetailList.parallelStream()
                .map(dto -> {
                    EventFeatureAmplitudeCurveVO.EventFeatureAmplitudeCurveDataList eventFeatureAmplitudeCurve = new EventFeatureAmplitudeCurveVO.EventFeatureAmplitudeCurveDataList();
                    eventFeatureAmplitudeCurve.setMeasurementPointId(dto.getMeasurementPointId());
                    eventFeatureAmplitudeCurve.setFeatureAmplitude(dto.getFeatureAmplitude());
                    eventFeatureAmplitudeCurve.setDuration(dto.getDuration());

                    eventFeatureAmplitudeCurve.setStartTime(dto.getStartTime());
                    return eventFeatureAmplitudeCurve;
                }).collect(Collectors.toCollection(() -> Collections.synchronizedList(new ArrayList<>())));

        return EventFeatureAmplitudeCurveVO.buildVO(rmpEventDetailList.size(), curveDataList);
    }

    /**
     * 获取全部的LineID
     *
     * @param pmsGeneralDeviceDTOList 部门信息集合
     * @return lineIds
     */
    private List<String> getAllLineIdList(List<PmsGeneralDeviceDTO> pmsGeneralDeviceDTOList) {

        if (CollUtil.isEmpty(pmsGeneralDeviceDTOList)) {
            return Collections.emptyList();
        }
        return pmsGeneralDeviceDTOList
                .stream()
                .flatMap(pmsGeneralDeviceDTO -> pmsGeneralDeviceDTO.getMonitorIdList().stream())
                .collect(Collectors.toList());
    }
}
