package com.njcn.harmonic.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.njcn.device.pq.pojo.param.PulicTimeStatisParam;
import com.njcn.harmonic.mapper.RStatLimitRateDMapper;
import com.njcn.harmonic.pojo.vo.RStatLimitRateDVO;
import com.njcn.harmonic.pojo.vo.SteadyInfoData;
import com.njcn.harmonic.service.SteadyDataService;
import com.njcn.web.pojo.vo.SteadyDataVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author denghuajun
 * @date 2022/3/4
 *
 */
@Service
@AllArgsConstructor
public class SteadyDataServiceImpl implements SteadyDataService {

    private final RStatLimitRateDMapper rateDMapper;

    @Override
    public SteadyInfoData getSteadyData(PulicTimeStatisParam pulicTimeStatisParam) {
        SteadyInfoData steadyInfoDatas = new SteadyInfoData();
        List<Float> steadyInfoList = new ArrayList<>();
        List<Float> steadyInfoData = new ArrayList<>();
        steadyInfoList = getCondition(pulicTimeStatisParam.getId(), pulicTimeStatisParam.getSearchBeginTime(), pulicTimeStatisParam.getSearchEndTime());
        if (StringUtils.isNotBlank(pulicTimeStatisParam.getPeriodBeginTime()) && StringUtils.isNotBlank(pulicTimeStatisParam.getPeriodEndTime())) {
            steadyInfoData = getCondition(pulicTimeStatisParam.getId(), pulicTimeStatisParam.getPeriodBeginTime(), pulicTimeStatisParam.getPeriodEndTime());
        }

        steadyInfoDatas.setSteadyInfoList(steadyInfoList);
        steadyInfoDatas.setSteadyInfoData(steadyInfoData);
        return steadyInfoDatas;
    }

    private List<Float> getCondition(String lineList, String startTime, String endTime) {
        SteadyDataVO steadyDataVO = new SteadyDataVO();
        SteadyDataVO steadyData = new SteadyDataVO();
        if (!lineList.isEmpty() || StringUtils.isNotBlank(lineList)) {
            List<RStatLimitRateDVO> list = rateDMapper.getSumPassRate(Arrays.asList(lineList),
                    DateUtil.beginOfDay(DateUtil.parse(startTime)).toString(),
                    DateUtil.beginOfDay(DateUtil.parse(endTime)).toString());
            if (CollUtil.isNotEmpty(list)) {
                if (ObjectUtil.isNull(list.get(0))) {
                    steadyDataVO.setVoltageDeviation(3.14159f);
                    steadyDataVO.setUBalance(3.14159f);
                    steadyDataVO.setFlicker(3.14159f);
                    steadyDataVO.setOddVoltage(3.14159f);
                    steadyDataVO.setEvenVoltage(3.14159f);
                    steadyDataVO.setOddElectric(3.14159f);
                    steadyDataVO.setEvenElectric(3.14159f);
                    steadyDataVO.setFrequency(3.14159f);
                    steadyDataVO.setUAberrance(3.14159f);
                    steadyDataVO.setHarmVoltage(3.14159f);
                    steadyDataVO.setHarmElec(3.14159f);
                    //负序电流
                    steadyDataVO.setINeg(3.14159f);
                    //间谐波
                    steadyDataVO.setInUharm(3.14159f);
                    steadyData = mergeSteady(steadyData, steadyDataVO);
                    SteadyDataVO steadyDataList = getSteadyAcc(steadyData);
                    return getSteadyData(steadyDataList);
                } else {
                    steadyDataVO.setAllTime(Float.parseFloat(list.get(0).getAlltime().toString()));
                    steadyDataVO.setFrequency(Float.parseFloat(list.get(0).getFrequency().toString()));
                    steadyDataVO.setUBalance(Float.parseFloat(list.get(0).getUBalance().toString()));
                    steadyDataVO.setINeg(Float.parseFloat(list.get(0).getINeg().toString()));
                    steadyDataVO.setHarmElec(Float.parseFloat(list.get(0).getHarmElec().toString()));
                    steadyDataVO.setHarmVoltage(Float.parseFloat(list.get(0).getHarmVoltage().toString()));
                    steadyDataVO.setFlicker(Float.parseFloat(list.get(0).getFlicker().toString()));
                    steadyDataVO.setAllFlicker(Float.parseFloat(list.get(0).getAllFlicker().toString()));
                    steadyDataVO.setVoltageDeviation(Float.parseFloat(list.get(0).getVoltageDeviation().toString()));
                    steadyDataVO.setInUharm(Float.parseFloat(list.get(0).getInUharm().toString()));
                }
            } else {
                //有监测点，但是无数据的处理
                steadyDataVO.setVoltageDeviation(3.14159f);
                steadyDataVO.setUBalance(3.14159f);
                steadyDataVO.setFlicker(3.14159f);
                steadyDataVO.setOddVoltage(3.14159f);
                steadyDataVO.setEvenVoltage(3.14159f);
                steadyDataVO.setOddElectric(3.14159f);
                steadyDataVO.setEvenElectric(3.14159f);
                steadyDataVO.setFrequency(3.14159f);
                steadyDataVO.setUAberrance(3.14159f);
                steadyDataVO.setHarmVoltage(3.14159f);
                steadyDataVO.setHarmElec(3.14159f);
                //负序电流
                steadyDataVO.setINeg(3.14159f);
                //间谐波
                steadyDataVO.setInUharm(3.14159f);
                steadyData = mergeSteady(steadyData, steadyDataVO);
                SteadyDataVO steadyDataList = getSteadyAcc(steadyData);
                return getSteadyData(steadyDataList);
            }
        } else {
            //steadyData = mergeSteady(steadyData, steadyDataVO);
            //无监测点前台统一替换为“/”
            // SteadyDataVO steadyDataList = getSteadyAcc(steadyData);
            return getSteadyData(steadyData);
        }
        steadyData = mergeSteady(steadyData, steadyDataVO);
        SteadyDataVO steadyDataList = getSteadyAcc(steadyData);
        return getSteadyData(steadyDataList);
    }

    /**
     * 合并数据
     *
     * @param steadyData 原始稳态实体
     */
    private SteadyDataVO mergeSteady(SteadyDataVO steadyData, SteadyDataVO tempT) {
        if (tempT == null) {
            return steadyData;
        } else {
            if (steadyData.getAllTime() == 0) {
                steadyData.setAllTime(tempT.getAllTime());
            } else {
                steadyData.setAllTime(steadyData.getAllTime() + tempT.getAllTime());
            }

            if (steadyData.getFrequency() == 3.1415f) {
                steadyData.setFrequency(tempT.getFrequency());
            } else {
                steadyData.setFrequency(steadyData.getFrequency() + tempT.getFrequency());
            }

            if (steadyData.getUBalance() == 3.1415f) {
                steadyData.setUBalance(tempT.getUBalance());
            } else {
                steadyData.setUBalance(steadyData.getUBalance() + tempT.getUBalance());
            }

            if (steadyData.getVoltageDeviation() == 3.1415f) {
                steadyData.setVoltageDeviation(tempT.getVoltageDeviation());
            } else {
                steadyData.setVoltageDeviation(steadyData.getVoltageDeviation() + tempT.getVoltageDeviation());
            }

            if (steadyData.getAllFlicker() == 3.1415f) {
                steadyData.setAllFlicker(tempT.getAllFlicker());
            } else {
                steadyData.setAllFlicker(steadyData.getAllFlicker() + tempT.getAllFlicker());
            }

            if (steadyData.getFlicker() == 3.1415f) {
                steadyData.setFlicker(tempT.getFlicker());
            } else {
                steadyData.setFlicker(steadyData.getFlicker() + tempT.getFlicker());
            }

            if (steadyData.getINeg() == 3.1415f) {
                steadyData.setINeg(tempT.getINeg());
            } else {
                steadyData.setINeg(steadyData.getINeg() + tempT.getINeg());
            }
            if (steadyData.getInUharm() == 3.1415f) {
                steadyData.setInUharm(tempT.getInUharm());
            } else {
                steadyData.setInUharm(steadyData.getInUharm() + tempT.getInUharm());
            }

            if (steadyData.getHarmVoltage() == 3.1415f) {
                steadyData.setHarmVoltage(tempT.getHarmVoltage());
            } else {
                steadyData.setHarmVoltage(steadyData.getHarmVoltage() + tempT.getHarmVoltage());
            }

            if (steadyData.getHarmElec() == 3.1415f) {
                steadyData.setHarmElec(tempT.getHarmElec());
            } else {
                steadyData.setHarmElec(steadyData.getHarmElec() + tempT.getHarmElec());
            }
        }
        return steadyData;
    }

    /**
     * 根据查询出来的数据，计算出每个指标的合格率
     *
     * @param steadyData 稳态指标数据实体
     */
    private SteadyDataVO getSteadyAcc(SteadyDataVO steadyData) {
        if (steadyData.getAllTime() == 0 || steadyData.getAllTime() < 0) {
            steadyData.setVoltageDeviation(3.14159f);
            steadyData.setUBalance(3.14159f);
            steadyData.setFlicker(3.14159f);
            steadyData.setOddVoltage(3.14159f);
            steadyData.setEvenVoltage(3.14159f);
            steadyData.setOddElectric(3.14159f);
            steadyData.setEvenElectric(3.14159f);
            steadyData.setFrequency(3.14159f);
            steadyData.setUAberrance(3.14159f);
            steadyData.setHarmVoltage(3.14159f);
            steadyData.setHarmElec(3.14159f);
            //负序电流
            steadyData.setINeg(3.14159f);
            //间谐波
            steadyData.setInUharm(3.14159f);
            return steadyData;
        } else {
            float allTime = steadyData.getAllTime();
            //冀北二期  谐波电流 谐波电压 负序电流 间谐波电压含有率 start
            steadyData.setHarmVoltage(getAcceptlityByData(allTime, steadyData.getHarmVoltage()));
            steadyData.setHarmElec(getAcceptlityByData(allTime, steadyData.getHarmElec()));
            steadyData.setINeg(getAcceptlityByData(allTime, steadyData.getINeg()));
            steadyData.setInUharm(getAcceptlityByData(allTime, steadyData.getInUharm()));
            //冀北二期  谐波电流 谐波电压 负序电流 间谐波电压含有率 end
            steadyData.setVoltageDeviation(getAcceptlityByData(allTime, steadyData.getVoltageDeviation()));
            steadyData.setUBalance(getAcceptlityByData(allTime, steadyData.getUBalance()));
            steadyData.setFlicker(getAcceptlityByData(steadyData.getAllFlicker(), steadyData.getFlicker()));
            steadyData.setOddVoltage(getAcceptlityByData(allTime, steadyData.getOddVoltage()));
            steadyData.setEvenVoltage(getAcceptlityByData(allTime, steadyData.getEvenVoltage()));
            steadyData.setOddElectric(getAcceptlityByData(allTime, steadyData.getOddElectric()));
            steadyData.setEvenElectric(getAcceptlityByData(allTime, steadyData.getEvenElectric()));
            steadyData.setFrequency(getAcceptlityByData(allTime, steadyData.getFrequency()));
            steadyData.setUAberrance(getAcceptlityByData(allTime, steadyData.getUAberrance()));
        }
        return steadyData;
    }

    //计算合格率结果
    private float getAcceptlityByData(float allTime, float data) {
        float accpet = allTime - data;
        if (accpet < 0) {
            accpet = 0;
        }
        if (allTime == 0) {
            return 100.0f;
        }
        float num = accpet / allTime;
        // 格式化小数
        DecimalFormat df = new DecimalFormat("0.00");
        return Float.valueOf(df.format(num * 100));
    }

    private List<Float> getSteadyData(SteadyDataVO steadyDataVO) {
        List<Float> floatList = new ArrayList<>();
        Float frequency = steadyDataVO.getFrequency();
        if (frequency > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(BigDecimal.valueOf(frequency).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }

        Float flicker = steadyDataVO.getFlicker();
        if (flicker > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(BigDecimal.valueOf(flicker).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }

        Float uBalance = steadyDataVO.getUBalance();
        if (uBalance > 100) {
            floatList.add(100f);
        } else if (uBalance == 3.1415f) {
            floatList.add(3.1415f);
        } else if (uBalance == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(BigDecimal.valueOf(uBalance).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }

        Float uAberrance = steadyDataVO.getUAberrance();


        Float oddVoltage = steadyDataVO.getOddVoltage();

        Float evenVoltage = steadyDataVO.getEvenVoltage();

        if (getHarmVoltageValue(uAberrance, oddVoltage, evenVoltage) > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(getHarmVoltageValue(uAberrance, oddVoltage, evenVoltage));
        }

        Float voltageDeviation = steadyDataVO.getVoltageDeviation();
        if (voltageDeviation > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(BigDecimal.valueOf(voltageDeviation).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }

        Float oddElectric = steadyDataVO.getOddElectric();

        Float evenElectric = steadyDataVO.getEvenElectric();
        if (getHarmElecValue(oddElectric, evenElectric) > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(getHarmElecValue(oddElectric, evenElectric));
        }

        Float iNeg = steadyDataVO.getINeg();
        if (iNeg > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(BigDecimal.valueOf(iNeg).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }


        Float inUharm = steadyDataVO.getInUharm();
        if (inUharm > 100) {
            floatList.add(100f);
        } else if (frequency == 3.1415f) {
            floatList.add(3.1415f);
        } else if (frequency == 3.14159f) {
            floatList.add(3.14159f);
        } else {
            floatList.add(BigDecimal.valueOf(inUharm).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
        }

        return floatList;
    }

    private Float getHarmVoltageValue(Float uAberrance, Float evenVoltage, Float oddVoltage) {
        float value = uAberrance + oddVoltage + evenVoltage;
        value = value / 3;
        return BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    private Float getHarmElecValue(Float oddElectric, Float evenElectric) {
        float value = oddElectric + evenElectric;
        value = value / 2;
        return BigDecimal.valueOf(value).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
    }
}
