package com.njcn.harmonic.service.impl;

import com.njcn.common.utils.PubUtils;
import com.njcn.device.api.LineFeignClient;
import com.njcn.device.pojo.po.Overlimit;
import com.njcn.harmonic.constant.Param;
import com.njcn.harmonic.pojo.param.HarmInHarmParam;
import com.njcn.harmonic.pojo.vo.HarmInHarmVO;
import com.njcn.harmonic.service.HarmInHarmService;
import com.njcn.influxdb.utils.InfluxDbUtils;
import lombok.AllArgsConstructor;
import org.influxdb.dto.QueryResult;
import org.springframework.stereotype.Service;

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

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

    private final LineFeignClient lineFeignClient;

    private final InfluxDbUtils influxDbUtils;

    @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 < 26; 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 < 26; 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<>();
        QueryResult queryResult;
        if (!lineId.isEmpty()) {
            //组装sql语句
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(Param.TIME + " >= '").append(startTime).append(Param.START_TIME).append("' and ").append(Param.TIME).append(" <= '").append(endTime).append(Param.END_TIME).append("' and (");
            //sql语句
            stringBuilder.append(Param.LINE_ID + "='").append(lineId).append("')");
            stringBuilder.append(" and ").append(Param.VALUETYPE + "='").append("CP95").append("'");
            String sql = "";
            if (harmState == 0) {
                sql = "SELECT  MEAN(V_2) as V2, MEAN(V_3) as V3, MEAN(V_4) as V4, MEAN(V_5) as V5, MEAN(V_6) as V6, MEAN(V_7) as V7," +
                        " MEAN(V_8) as V8, MEAN(V_9) as V9, MEAN(V_10) as V10, MEAN(V_11) as V11, MEAN(V_12) as V12, MEAN(V_13) as V13," +
                        " MEAN(V_14) as V14, MEAN(V_15) as V15, MEAN(V_16) as V16, MEAN(V_17) as V17, MEAN(V_18) as V18," +
                        " MEAN(V_19) as V19, MEAN(V_20) as V20, MEAN(V_21) as V21," +
                        " MEAN(V_22) as V22, MEAN(V_23) as V23, MEAN(V_24) as V24, MEAN(V_25) as V25, MEAN(V_26) as V25, MEAN(V_27) as V27, MEAN(V_28) as V28," +
                        " MEAN(V_29) as V29, MEAN(V_30) as V30, MEAN(V_31) as V31, MEAN(V_32) as V32, MEAN(V_33) as V33, MEAN(V_34) as V34, MEAN(V_35) as V35," +
                        " MEAN(V_36) as V36, MEAN(V_37) as V37, MEAN(V_38) as V38, MEAN(V_39) as V39, MEAN(V_40) as V40, MEAN(V_41) as V41, MEAN(V_42) as V42," +
                        " MEAN(V_43) as V43, MEAN(V_44) as V44, MEAN(V_45) as V45, MEAN(V_46) as V46, MEAN(V_47) as V47, MEAN(V_48) as V48, MEAN(V_49) as V49," +
                        " MEAN(V_50) as V50 FROM Data_HarmRate_V WHERE " + stringBuilder.toString() + " and phasic_type !='T' order by time asc;";
            } else {
                sql = "SELECT  MEAN(I_2) as I2, MEAN(I_3) as I3, MEAN(I_4) as I4, MEAN(I_5) as I5, MEAN(I_6) as I6, MEAN(I_7) as I7," +
                        " MEAN(I_8) as I8, MEAN(I_9) as I9, MEAN(I_10) as I10, MEAN(I_11) as I11, MEAN(I_12) as I12, MEAN(I_13) as I13," +
                        " MEAN(I_14) as I14, MEAN(I_15) as I15, MEAN(I_16) as I16, MEAN(I_17) as I17, MEAN(I_18) as I18," +
                        " MEAN(I_19) as I19, MEAN(I_20) as I20, MEAN(I_21) as I21," +
                        " MEAN(I_22) as I22, MEAN(I_23) as I23, MEAN(I_24) as I24, MEAN(I_25) as I25, MEAN(I_26) as I25, MEAN(I_27) as I27, MEAN(I_28) as I28," +
                        " MEAN(I_29) as I29, MEAN(I_30) as I30, MEAN(I_31) as I31, MEAN(I_32) as I32, MEAN(I_33) as I33, MEAN(I_34) as I34, MEAN(I_35) as I35," +
                        " MEAN(I_36) as I36, MEAN(I_37) as I37, MEAN(I_38) as I38, MEAN(I_39) as I39, MEAN(I_40) as I40, MEAN(I_41) as I41, MEAN(I_42) as I42," +
                        " MEAN(I_43) as I43, MEAN(I_44) as I44, MEAN(I_45) as I45, MEAN(I_46) as I46, MEAN(I_47) as I47, MEAN(I_48) as I48, MEAN(I_49) as I49," +
                        " MEAN(I_50) as I50 FROM Data_I WHERE " + stringBuilder.toString() + " and phasic_type !='T' order by time asc;";
            }
            queryResult = influxDbUtils.query(sql);
            if (queryResult.getResults().get(0).getSeries() != null) {
                List<Object> resultList = queryResult.getResults().get(0).getSeries().get(0).getValues().get(0);
                if (resultList.size() != 0) {
                    for (int i = 1; i < resultList.size(); i++) {
                        floatList.add(BigDecimal.valueOf(Float.parseFloat(resultList.get(i).toString())).setScale(4, BigDecimal.ROUND_HALF_UP).floatValue());
                    }
                }
            }
        }

        return floatList;
    }
}
