package com.njcn.cloud.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
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.cloud.enums.app.TypeCodeEnum;
import com.njcn.cloud.mapper.AppStatisticsMapper;
import com.njcn.cloud.pojo.dto.AppStatistics;
import com.njcn.cloud.pojo.dto.AppSteadyUrl;
import com.njcn.cloud.pojo.vo.StatisticsData;
import com.njcn.cloud.pojo.vo.StatisticsDetail;
import com.njcn.cloud.pojo.vo.StatisticsEvent;
import com.njcn.cloud.service.IAppStatisticsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.cloud.service.IAppSteadyUrlService;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.pq.pojo.vo.LineDetailVO;
import com.njcn.event.api.EventDetailFeignClient;
import com.njcn.event.pojo.param.EventCountParam;
import com.njcn.event.pojo.po.RmpEventDetailPO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 统计消息 服务实现类
 * </p>
 *
 * @author wr
 * @since 2024-01-03
 */
@Service
@RequiredArgsConstructor
public class AppStatisticsServiceImpl extends ServiceImpl<AppStatisticsMapper, AppStatistics> implements IAppStatisticsService {

    private final LineFeignClient lineFeignClient;
    private final IAppSteadyUrlService appSteadyUrlService;
    private final EventDetailFeignClient eventDetailFeignClient;

    @Override
    public List<StatisticsData> queryData(String lineIndex, String startTime, String endTime, int page, int num) {
        List<LineDetailVO.Detail> lineDetail = lineFeignClient.getLineDetailByIds(Collections.singletonList(lineIndex)).getData();
        if (CollUtil.isEmpty(lineDetail)) {
            throw new BusinessException("监测点信息不存在，监测点编号有误！");
        }
        List<StatisticsData> info = new ArrayList<>();
        Page<AppStatistics> statisticsPage = this.page(new Page<>(page, num), new LambdaQueryWrapper<AppStatistics>()
                .eq(AppStatistics::getLineIndex, lineIndex)
                .ge(StrUtil.isNotBlank(startTime), AppStatistics::getTimeId, DateUtil.beginOfDay(DateUtil.parse(startTime)))
                .le(StrUtil.isNotBlank(endTime), AppStatistics::getTimeId, DateUtil.endOfDay(DateUtil.parse(endTime)))
                .orderByDesc(AppStatistics::getTimeId)
        );
        //数据整理
        List<AppStatistics> records = statisticsPage.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            for (AppStatistics statistics : records) {
                StatisticsData data = new StatisticsData();
                data.setLineName(lineDetail.get(0).getLineName());
                data.setTime(statistics.getTimeId());
                String describe = "";
                String describe1 = "";
                if (statistics.getEventCount() != null && statistics.getEventCount() > 0) {
                    describe = describe + "暂降次数:" + statistics.getEventCount().toString() + "次;";
                }
                if (statistics.getFreqDevRate() != null && statistics.getFreqDevRate() > 0 && statistics.getFreqDevRate() < 100) {
                    describe1 = describe1 + "频率偏差合格率" + statistics.getFreqDevRate().toString() + "%;";
                }
                if (statistics.getVoltageDevRate() != null && statistics.getVoltageDevRate() > 0 && statistics.getVoltageDevRate() < 100) {
                    describe1 = describe1 + "电压偏差合格率" + statistics.getVoltageDevRate().toString() + "%;";
                }
                if (statistics.getUbalanceRate() != null && statistics.getUbalanceRate() > 0 && statistics.getUbalanceRate() < 100) {
                    describe1 = describe1 + "电压不平衡合格率" + statistics.getUbalanceRate().toString() + "%;";
                }
                if (statistics.getFlickerRate() != null && statistics.getFlickerRate() > 0 && statistics.getFlickerRate() < 100) {
                    describe1 = describe1 + "闪变合格率" + statistics.getFlickerRate().toString() + "%;";
                }
                if (statistics.getUaberranceRate() != null && statistics.getUaberranceRate() > 0 && statistics.getUaberranceRate() < 100) {
                    describe1 = describe1 + "电压谐波畸变率合格率" + statistics.getUaberranceRate().toString() + "%;";
                }
                if (statistics.getUharm2Rate() != null && statistics.getUharm2Rate() > 0 && statistics.getUharm2Rate() < 100) {
                    describe1 = describe1 + "2次电压谐波含有率合格率" + statistics.getUharm2Rate().toString() + "%;";
                }
                if (statistics.getUharm3Rate() != null && statistics.getUharm3Rate() > 0 && statistics.getUharm3Rate() < 100) {
                    describe1 = describe1 + "3次电压谐波含有率合格率" + statistics.getUharm3Rate().toString() + "%;";
                }
                if (statistics.getUharm4Rate() != null && statistics.getUharm4Rate() > 0 && statistics.getUharm4Rate() < 100) {
                    describe1 = describe1 + "4次电压谐波含有率合格率" + statistics.getUharm4Rate().toString() + "%;";
                }
                if (statistics.getUharm5Rate() != null && statistics.getUharm5Rate() > 0 && statistics.getUharm5Rate() < 100) {
                    describe1 = describe1 + "5次电压谐波含有率合格率" + statistics.getUharm5Rate().toString() + "%;";
                }
                if (statistics.getUharm6Rate() != null && statistics.getUharm6Rate() > 0 && statistics.getUharm6Rate() < 100) {
                    describe1 = describe1 + "6次电压谐波含有率合格率" + statistics.getUharm6Rate().toString() + "%;";
                }
                if (statistics.getUharm7Rate() != null && statistics.getUharm7Rate() > 0 && statistics.getUharm7Rate() < 100) {
                    describe1 = describe1 + "7次电压谐波含有率合格率" + statistics.getUharm7Rate().toString() + "%;";
                }
                if (statistics.getUharm8Rate() != null && statistics.getUharm8Rate() > 0 && statistics.getUharm8Rate() < 100) {
                    describe1 = describe1 + "8次电压谐波含有率合格率" + statistics.getUharm8Rate().toString() + "%;";
                }
                if (statistics.getUharm9Rate() != null && statistics.getUharm9Rate() > 0 && statistics.getUharm9Rate() < 100) {
                    describe1 = describe1 + "9次电压谐波含有率合格率" + statistics.getUharm9Rate().toString() + "%;";
                }
                if (statistics.getUharm10Rate() != null && statistics.getUharm10Rate() > 0 && statistics.getUharm10Rate() < 100) {
                    describe1 = describe1 + "10次电压谐波含有率合格率" + statistics.getUharm10Rate().toString() + "%;";
                }
                if (statistics.getUharm11Rate() != null && statistics.getUharm11Rate() > 0 && statistics.getUharm11Rate() < 100) {
                    describe1 = describe1 + "11次电压谐波含有率合格率" + statistics.getUharm11Rate().toString() + "%;";
                }
                if (statistics.getUharm12Rate() != null && statistics.getUharm12Rate() > 0 && statistics.getUharm12Rate() < 100) {
                    describe1 = describe1 + "12次电压谐波含有率合格率" + statistics.getUharm12Rate().toString() + "%;";
                }
                if (statistics.getUharm13Rate() != null && statistics.getUharm13Rate() > 0 && statistics.getUharm13Rate() < 100) {
                    describe1 = describe1 + "13次电压谐波含有率合格率" + statistics.getUharm13Rate().toString() + "%;";
                }
                if (statistics.getUharm14Rate() != null && statistics.getUharm14Rate() > 0 && statistics.getUharm14Rate() < 100) {
                    describe1 = describe1 + "14次电压谐波含有率合格率" + statistics.getUharm14Rate().toString() + "%;";
                }
                if (statistics.getUharm15Rate() != null && statistics.getUharm15Rate() > 0 && statistics.getUharm15Rate() < 100) {
                    describe1 = describe1 + "15次电压谐波含有率合格率" + statistics.getUharm15Rate().toString() + "%;";
                }
                if (statistics.getUharm16Rate() != null && statistics.getUharm16Rate() > 0 && statistics.getUharm16Rate() < 100) {
                    describe1 = describe1 + "16次电压谐波含有率合格率" + statistics.getUharm16Rate().toString() + "%;";
                }
                if (statistics.getUharm17Rate() != null && statistics.getUharm17Rate() > 0 && statistics.getUharm17Rate() < 100) {
                    describe1 = describe1 + "17次电压谐波含有率合格率" + statistics.getUharm17Rate().toString() + "%;";
                }
                if (statistics.getUharm18Rate() != null && statistics.getUharm18Rate() > 0 && statistics.getUharm18Rate() < 100) {
                    describe1 = describe1 + "18次电压谐波含有率合格率" + statistics.getUharm18Rate().toString() + "%;";
                }
                if (statistics.getUharm19Rate() != null && statistics.getUharm19Rate() > 0 && statistics.getUharm19Rate() < 100) {
                    describe1 = describe1 + "19次电压谐波含有率合格率" + statistics.getUharm19Rate().toString() + "%;";
                }
                if (statistics.getUharm20Rate() != null && statistics.getUharm20Rate() > 0 && statistics.getUharm20Rate() < 100) {
                    describe1 = describe1 + "20次电压谐波含有率合格率" + statistics.getUharm20Rate().toString() + "%;";
                }
                if (statistics.getUharm21Rate() != null && statistics.getUharm21Rate() > 0 && statistics.getUharm21Rate() < 100) {
                    describe1 = describe1 + "21次电压谐波含有率合格率" + statistics.getUharm21Rate().toString() + "%;";
                }
                if (statistics.getUharm22Rate() != null && statistics.getUharm22Rate() > 0 && statistics.getUharm22Rate() < 100) {
                    describe1 = describe1 + "22次电压谐波含有率合格率" + statistics.getUharm22Rate().toString() + "%;";
                }
                if (statistics.getUharm23Rate() != null && statistics.getUharm23Rate() > 0 && statistics.getUharm23Rate() < 100) {
                    describe1 = describe1 + "23次电压谐波含有率合格率" + statistics.getUharm23Rate().toString() + "%;";
                }
                if (statistics.getUharm24Rate() != null && statistics.getUharm24Rate() > 0 && statistics.getUharm24Rate() < 100) {
                    describe1 = describe1 + "24次电压谐波含有率合格率" + statistics.getUharm24Rate().toString() + "%;";
                }
                if (statistics.getUharm25Rate() != null && statistics.getUharm25Rate() > 0 && statistics.getUharm25Rate() < 100) {
                    describe1 = describe1 + "25次电压谐波含有率合格率" + statistics.getUharm25Rate().toString() + "%;";
                }
                if (statistics.getIharm2Rate() != null && statistics.getIharm2Rate() > 0 && statistics.getIharm2Rate() < 100) {
                    describe1 = describe1 + "2次电流谐波幅值合格率" + statistics.getIharm2Rate().toString() + "%;";
                }
                if (statistics.getIharm3Rate() != null && statistics.getIharm3Rate() > 0 && statistics.getIharm3Rate() < 100) {
                    describe1 = describe1 + "3次电流谐波幅值合格率" + statistics.getIharm3Rate().toString() + "%;";
                }
                if (statistics.getIharm4Rate() != null && statistics.getIharm4Rate() > 0 && statistics.getIharm4Rate() < 100) {
                    describe1 = describe1 + "4次电流谐波幅值合格率" + statistics.getIharm4Rate().toString() + "%;";
                }
                if (statistics.getIharm5Rate() != null && statistics.getIharm5Rate() > 0 && statistics.getIharm5Rate() < 100) {
                    describe1 = describe1 + "5次电流谐波幅值合格率" + statistics.getIharm5Rate().toString() + "%;";
                }
                if (statistics.getIharm6Rate() != null && statistics.getIharm6Rate() > 0 && statistics.getIharm6Rate() < 100) {
                    describe1 = describe1 + "6次电流谐波幅值合格率" + statistics.getIharm6Rate().toString() + "%;";
                }
                if (statistics.getIharm7Rate() != null && statistics.getIharm7Rate() > 0 && statistics.getIharm7Rate() < 100) {
                    describe1 = describe1 + "7次电流谐波幅值合格率" + statistics.getIharm7Rate().toString() + "%;";
                }
                if (statistics.getIharm8Rate() != null && statistics.getIharm8Rate() > 0 && statistics.getIharm8Rate() < 100) {
                    describe1 = describe1 + "8次电流谐波幅值合格率" + statistics.getIharm8Rate().toString() + "%;";
                }
                if (statistics.getIharm9Rate() != null && statistics.getIharm9Rate() > 0 && statistics.getIharm9Rate() < 100) {
                    describe1 = describe1 + "9次电流谐波幅值合格率" + statistics.getIharm9Rate().toString() + "%;";
                }
                if (statistics.getIharm10Rate() != null && statistics.getIharm10Rate() > 0 && statistics.getIharm10Rate() < 100) {
                    describe1 = describe1 + "10次电流谐波幅值合格率" + statistics.getIharm10Rate().toString() + "%;";
                }
                if (statistics.getIharm11Rate() != null && statistics.getIharm11Rate() > 0 && statistics.getIharm11Rate() < 100) {
                    describe1 = describe1 + "11次电流谐波幅值合格率" + statistics.getIharm11Rate().toString() + "%;";
                }
                if (statistics.getIharm12Rate() != null && statistics.getIharm12Rate() > 0 && statistics.getIharm12Rate() < 100) {
                    describe1 = describe1 + "12次电流谐波幅值合格率" + statistics.getIharm12Rate().toString() + "%;";
                }
                if (statistics.getIharm13Rate() != null && statistics.getIharm13Rate() > 0 && statistics.getIharm13Rate() < 100) {
                    describe1 = describe1 + "13次电流谐波幅值合格率" + statistics.getIharm13Rate().toString() + "%;";
                }
                if (statistics.getIharm14Rate() != null && statistics.getIharm14Rate() > 0 && statistics.getIharm14Rate() < 100) {
                    describe1 = describe1 + "14次电流谐波幅值合格率" + statistics.getIharm14Rate().toString() + "%;";
                }
                if (statistics.getIharm15Rate() != null && statistics.getIharm15Rate() > 0 && statistics.getIharm15Rate() < 100) {
                    describe1 = describe1 + "15次电流谐波幅值合格率" + statistics.getIharm15Rate().toString() + "%;";
                }
                if (statistics.getIharm16Rate() != null && statistics.getIharm16Rate() > 0 && statistics.getIharm16Rate() < 100) {
                    describe1 = describe1 + "16次电流谐波幅值合格率" + statistics.getIharm16Rate().toString() + "%;";
                }
                if (statistics.getIharm17Rate() != null && statistics.getIharm17Rate() > 0 && statistics.getIharm17Rate() < 100) {
                    describe1 = describe1 + "17次电流谐波幅值合格率" + statistics.getIharm17Rate().toString() + "%;";
                }
                if (statistics.getIharm18Rate() != null && statistics.getIharm18Rate() > 0 && statistics.getIharm18Rate() < 100) {
                    describe1 = describe1 + "18次电流谐波幅值合格率" + statistics.getIharm18Rate().toString() + "%;";
                }
                if (statistics.getIharm19Rate() != null && statistics.getIharm19Rate() > 0 && statistics.getIharm19Rate() < 100) {
                    describe1 = describe1 + "19次电流谐波幅值合格率" + statistics.getIharm19Rate().toString() + "%;";
                }
                if (statistics.getIharm20Rate() != null && statistics.getIharm20Rate() > 0 && statistics.getIharm20Rate() < 100) {
                    describe1 = describe1 + "20次电流谐波幅值合格率" + statistics.getIharm20Rate().toString() + "%;";
                }
                if (statistics.getIharm21Rate() != null && statistics.getIharm21Rate() > 0 && statistics.getIharm21Rate() < 100) {
                    describe1 = describe1 + "21次电流谐波幅值合格率" + statistics.getIharm21Rate().toString() + "%;";
                }
                if (statistics.getIharm22Rate() != null && statistics.getIharm22Rate() > 0 && statistics.getIharm22Rate() < 100) {
                    describe1 = describe1 + "22次电流谐波幅值合格率" + statistics.getIharm22Rate().toString() + "%;";
                }
                if (statistics.getIharm23Rate() != null && statistics.getIharm23Rate() > 0 && statistics.getIharm23Rate() < 100) {
                    describe1 = describe1 + "23次电流谐波幅值合格率" + statistics.getIharm23Rate().toString() + "%;";
                }
                if (statistics.getIharm24Rate() != null && statistics.getIharm24Rate() > 0 && statistics.getIharm24Rate() < 100) {
                    describe1 = describe1 + "24次电流谐波幅值合格率" + statistics.getIharm24Rate().toString() + "%;";
                }
                if (statistics.getIharm25Rate() != null && statistics.getIharm25Rate() > 0 && statistics.getIharm25Rate() < 100) {
                    describe1 = describe1 + "25次电流谐波幅值合格率" + statistics.getIharm25Rate().toString() + "%;";
                }
                boolean flag = false;
                if (describe.length() > 0) {
                    describe = describe.substring(0, describe.length() - 1);
                    data.setEventDescribe(describe);
                    flag = true;
                }
                if (describe1.length() > 0) {
                    describe1 = "越限详情:" + describe1;
                    describe1 = describe1.substring(0, describe1.length() - 1);
                    data.setSteadyDescribe(describe1);
                    flag = true;
                }
                if (flag) {
                    info.add(data);
                }
            }
        }
        return info;
    }

    @Override
    public StatisticsDetail queryDetail(String lineIndex, String timeID) {
        List<LineDetailVO.Detail> lineDetail = lineFeignClient.getLineDetailByIds(Collections.singletonList(lineIndex)).getData();
        if (CollUtil.isEmpty(lineDetail)) {
            throw new BusinessException("监测点信息不存在，监测点编号有误！");
        }
        StatisticsDetail result = new StatisticsDetail();
        //获取监测点完整名称
        result.setName(lineDetail.get(0).getGdName() + "->" +
                lineDetail.get(0).getSubName() + "->" +
                lineDetail.get(0).getDevName() + "->" +
                lineDetail.get(0).getLineName());
        //获取稳态消息详情
        String steadyDescribe = "";
        List<AppSteadyUrl> steadyUrlList = appSteadyUrlService.list(new LambdaQueryWrapper<AppSteadyUrl>()
                .eq(AppSteadyUrl::getLineIndex, lineIndex)
                .eq(AppSteadyUrl::getTimeId, timeID)
                .orderByAsc(AppSteadyUrl::getTypeCode)

        );
        if (CollUtil.isNotEmpty(steadyUrlList)) {
            for (int n = 0; n < steadyUrlList.size(); n++) {
                String typename = TypeCodeEnum.getMsgByCode(steadyUrlList.get(n).getTypeCode());
                if (n == steadyUrlList.size() - 1) {
                    steadyDescribe = steadyDescribe + typename + steadyUrlList.get(n).getLimitNum() + "次";
                } else {
                    steadyDescribe = steadyDescribe + typename + steadyUrlList.get(n).getLimitNum() + "次,";
                }

            }
            result.setSteadyInfo(steadyDescribe);
        }
        //获取暂态消息详情
        List<StatisticsEvent> eventInfo = new ArrayList<>();
        //获取监测点暂态消息信息
        String startTime = DateUtil.beginOfDay(DateUtil.parse(timeID)).toString();
        String endTime = DateUtil.endOfDay(DateUtil.parse(timeID)).toString();
        EventCountParam param=new EventCountParam();
        param.setIds(Collections.singletonList(lineIndex));
        param.setStartTime(startTime);
        param.setEndTime(endTime);
        List<RmpEventDetailPO> eventDetailList = eventDetailFeignClient.getAppEventDetailLtAmplitude(param).getData();
        if (CollUtil.isNotEmpty(eventDetailList)) {
            for (RmpEventDetailPO event : eventDetailList) {
                StatisticsEvent eventData = new StatisticsEvent();
                String describe = "";
                describe = describe + lineDetail.get(0).getLineName() + "于"
                        + LocalDateTimeUtil.format(event.getStartTime(), DatePattern.NORM_DATETIME_MS_PATTERN)
                        + "发生暂降事件,特征幅值:" + event.getFeatureAmplitude()
                        + "%,持续时间:" + event.getDuration() + "s";
                eventData.setDescribe(describe);
                eventData.setEventDetail_index(event.getEventId());
                eventInfo.add(eventData);
            }
            result.setEventInfo(eventInfo);

        }

        return result;
    }
}
