package com.njcn.harmonic.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.njcn.device.pq.pojo.param.PulicTimeStatisParam;
import com.njcn.harmonic.pojo.vo.SteadyInfoData;
import com.njcn.harmonic.pojo.vo.hebeinorth.EvaluationLevelVo;
import com.njcn.harmonic.service.SteadyDataService;
import com.njcn.harmonic.service.hebeinorth.IGridService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

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

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

    private final IGridService gridService;

    @Override
    public SteadyInfoData getSteadyData(PulicTimeStatisParam param) {
        SteadyInfoData steadyInfoData = new SteadyInfoData();
        List<Double> steadyInfoList = getCondition(param.getId(), param.getSearchBeginTime(), param.getSearchEndTime());
        List<Double> steadyInfoDataRatioList = new ArrayList<>();
        if (StringUtils.isNotBlank(param.getPeriodBeginTime()) && StringUtils.isNotBlank(param.getPeriodEndTime())) {
            steadyInfoDataRatioList = getCondition(param.getId(), param.getPeriodBeginTime(), param.getPeriodEndTime());
        }
        steadyInfoData.setSteadyInfoList(steadyInfoList);
        steadyInfoData.setSteadyInfoData(steadyInfoDataRatioList);
        return steadyInfoData;
    }

    private List<Double> getCondition(String lineList, String startTime, String endTime) {
        List<Double> floatList = new ArrayList<>();
        EvaluationLevelVo.QualifiedDetail lineQualifiedDetail = gridService.getLineQualifiedDetail(lineList, startTime, endTime);
        if(ObjectUtil.isNotNull(lineQualifiedDetail)){
            //频率偏差
            floatList.add(lineQualifiedDetail.getFreqDev());
            //闪变
            floatList.add(lineQualifiedDetail.getPlt());
            //三项电压不平衡
            floatList.add(lineQualifiedDetail.getUbalance());
            //谐波电压
            floatList.add(lineQualifiedDetail.getUHarm());
            //谐波电流
            floatList.add(lineQualifiedDetail.getIHarm());
            //电压偏差
            floatList.add(lineQualifiedDetail.getVDev());
            //负序电流
            floatList.add(lineQualifiedDetail.getINeg());
            //间谐波含有率
            floatList.add(lineQualifiedDetail.getInuHarm());
        }
        return floatList;
    }
}
