package com.njcn.harmonic.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.common.utils.HarmonicTimesUtil;
import com.njcn.common.utils.PubUtils;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.device.biz.pojo.po.Overlimit;
import com.njcn.harmonic.common.mapper.RStatDataHarmRateVDMapper;
import com.njcn.harmonic.common.mapper.RStatDataIDMapper;
import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.mapper.RStatDataHarmRateIDMapper;
import com.njcn.harmonic.pojo.param.HarmInHarmParam;
import com.njcn.harmonic.pojo.po.day.RStatDataHarmrateIDPO;
import com.njcn.harmonic.pojo.po.day.RStatDataHarmrateVDPO;
import com.njcn.harmonic.pojo.po.day.RStatDataIDPO;
import com.njcn.harmonic.pojo.vo.HarmInHarmVO;
import com.njcn.harmonic.service.HarmInHarmService;
import com.njcn.influx.pojo.constant.InfluxDBTableConstant;
import com.njcn.influx.pojo.po.DataHarmRateV;
import com.njcn.influx.pojo.po.DataI;
import com.njcn.influx.query.InfluxQueryWrapper;
import com.njcn.influx.service.DataHarmRateVService;
import com.njcn.influx.service.IDataIService;
import lombok.AllArgsConstructor;
import org.influxdb.dto.QueryResult;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 类的介绍
 *
 * @author denghuajun
 * @version 1.0.0
 * @date 2022/3/17
 */
@Service
@AllArgsConstructor
public class HarmInHarmServiceImpl implements HarmInHarmService {

    private final LineFeignClient lineFeignClient;

    private final DataHarmRateVService dataHarmRateVService;

    private final IDataIService dataIService;

    private final RStatDataHarmRateVDMapper dataHarmRateVDMapper;
    private final RStatDataIDMapper dataIDMapper;
    @Override
    public HarmInHarmVO getHarmInHarmData(HarmInHarmParam harmInHarmParam) {
        HarmInHarmVO harmInHarmVO = new HarmInHarmVO();
        //获取限值
        Overlimit overlimit = lineFeignClient.getOverLimitData(harmInHarmParam.getId()).getData();
        if (overlimit != null) {
            List<Float> harmInOverLimit = new ArrayList<>();
            List<Float> harmInHarmValue;
            if (harmInHarmParam.getHarmState() == 0) {
                for (int i = 2; i < 51; i++) {
                    harmInOverLimit.add(PubUtils.getValueByMethod(overlimit, "getUharm", i));
                }
                //获取2-50次谐波含有率值
                harmInHarmValue = getCondition(harmInHarmParam.getSearchBeginTime(), harmInHarmParam.getSearchEndTime(), harmInHarmParam.getId(), harmInHarmParam.getHarmState());
            } else {
                for (int i = 2; i < 51; i++) {
                    harmInOverLimit.add(PubUtils.getValueByMethod(overlimit, "getIharm", i));
                }
                //获取2-50次谐波电流幅值
                harmInHarmValue = getCondition(harmInHarmParam.getSearchBeginTime(), harmInHarmParam.getSearchEndTime(), harmInHarmParam.getId(), harmInHarmParam.getHarmState());
            }
            harmInHarmVO.setHarmInHarmValue(harmInHarmValue);
            harmInHarmVO.setHarmInOverLimit(harmInOverLimit);
        }
        return harmInHarmVO;
    }

    /**
     * influxDB相关操作
     * 查询稳态数据分析
     */
    private List<Float> getCondition(String startTime, String endTime, String lineId, Integer harmState) {
        List<Float> floatList = new ArrayList<>();
        if (StrUtil.isNotBlank(lineId)) {
            if (harmState == 0) {
//                InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataHarmRateV.class);
//                influxQueryWrapper.meanSamePrefixAndSuffix("v_", "", HarmonicTimesUtil.harmonicTimesList(2, 50, 1))
//                        .between(DataHarmRateV::getTime, startTime, endTime)
//                        .eq(DataHarmRateV::getLineId, lineId)
//                        .eq(DataHarmRateV::getValueType, InfluxDBTableConstant.CP95)
//                        .ne(DataHarmRateV::getPhaseType, InfluxDBTableConstant.PHASE_TYPE_T);
//                DataHarmRateV dataHarmRateV = dataHarmRateVService.getMeanAllTimesData(influxQueryWrapper);
//                if (Objects.nonNull(dataHarmRateV)) {
//                    for (int i = 2; i < 51; i++) {
//                        floatList.add(PubUtils.getValueByMethodDouble(dataHarmRateV, "getV", i).floatValue());
//                    }
//                }else {
//                    for (int i = 2; i < 51; i++) {
//                        floatList.add(null);
//                    }
//                }
                //转换成查mysql统计数据，分钟数据没有值
                List<RStatDataHarmrateVDPO> harmV = dataHarmRateVDMapper.selectList(new LambdaQueryWrapper<RStatDataHarmrateVDPO>()
                        .eq(RStatDataHarmrateVDPO::getLineId, lineId)
                        .eq(RStatDataHarmrateVDPO::getValueType,"CP95")
                        .in(RStatDataHarmrateVDPO::getPhaseType, Arrays.asList("A","B","C"))
                        .ge(RStatDataHarmrateVDPO::getTime, DateUtil.beginOfDay(DateUtil.parse(startTime)))
                        .le(RStatDataHarmrateVDPO::getTime, DateUtil.endOfDay(DateUtil.parse(endTime)))
                );

                if (Objects.nonNull(harmV)) {
                    for (int i = 2; i < 51; i++) {
                        floatList.add(PubUtils.getValueByMethodDouble(harmV,RStatDataHarmrateVDPO.class, "getV", i).floatValue());
                    }
                }else {
                    for (int i = 2; i < 51; i++) {
                        floatList.add(null);
                    }
                }

            } else {
//                InfluxQueryWrapper influxQueryWrapper = new InfluxQueryWrapper(DataI.class);
//                influxQueryWrapper.meanSamePrefixAndSuffix("i_", "", HarmonicTimesUtil.harmonicTimesList(2, 50, 1))
//                        .between(DataHarmRateV::getTime, startTime, endTime)
//                        .eq(DataHarmRateV::getLineId, lineId)
//                        .eq(DataHarmRateV::getValueType, InfluxDBTableConstant.CP95)
//                        .ne(DataHarmRateV::getPhaseType, InfluxDBTableConstant.PHASE_TYPE_T);
//                DataI dataI = dataIService.getMeanAllTimesData(influxQueryWrapper);
//                if (Objects.nonNull(dataI)) {
//                    for (int i = 2; i < 51; i++) {
//                        floatList.add(PubUtils.getValueByMethodDouble(dataI, "getI", i).floatValue());
//                    }
//                }else {
//                    for (int i = 2; i < 51; i++) {
//                        floatList.add(null);
//                    }
//                }

                //转换成查mysql统计数据，分钟数据没有值
                List<RStatDataIDPO> harmI = dataIDMapper.selectList(new LambdaQueryWrapper<RStatDataIDPO>()
                        .eq(RStatDataIDPO::getLineId, lineId)
                        .eq(RStatDataIDPO::getValueType,"CP95")
                        .in(RStatDataIDPO::getPhaseType, Arrays.asList("A","B","C"))
                        .ge(RStatDataIDPO::getTime, DateUtil.beginOfDay(DateUtil.parse(startTime)))
                        .le(RStatDataIDPO::getTime, DateUtil.endOfDay(DateUtil.parse(endTime)))
                );
                if (Objects.nonNull(harmI)) {
                    for (int i = 2; i < 51; i++) {
                        floatList.add(PubUtils.getValueByMethodDouble(harmI,RStatDataIDPO.class, "getI", i).floatValue());
                    }
                }else {
                    for (int i = 2; i < 51; i++) {
                        floatList.add(null);
                    }
                }
            }
        }
        return floatList;
    }
}
