package com.njcn.utils;

import com.njcn.pojo.balance.QtIdx;
import com.njcn.pojo.balance.QtIdxArray;
import com.njcn.pojo.balance.QvvrDataStruct;
import com.njcn.serviceimpl.commons.WaveServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.util.NestedServletException;

import java.util.Date;
import java.util.List;

@Component
public class BalanceInfo {
    @Autowired
    private GetBalance getBalance;
    @Autowired
    private WaveServiceImpl waveServiceImpl;

    private Date start;
    private Date end;

    public Float getBalanceInfo(List<Integer> list, Date start, Date end) { //根据传入的监测点ID获取暂降综合评估结果
        this.start = start;
        this.end = end;

        if (list == null || list.size() < 4) { //监测点数目小于4个不带入计算
            return null;
        }

        QvvrDataStruct qvvrDataStruct = new QvvrDataStruct();
        qvvrDataStruct.sys_num = 1;
        QtIdxArray qtIdxArray = new QtIdxArray();
        int count = 0;

        for (int i = 0; i < list.size(); i++) {
            QtIdx qtIdx = getQtIdx(list.get(i).intValue());

            if (qtIdx.sarfi_90 > 0) {
                qtIdxArray.qtIdxs[count] = qtIdx;
                count++;
            }
        }

        if (count < 4) {
            return null;
        }

        int kk = 0;
        for (int i = 1; i < count; i++) {//如果所有数据都一样也不带入计算
            if (qtIdxArray.qtIdxs[0].equals(qtIdxArray.qtIdxs[i])) {
                kk++;
            }
        }

        if (kk == count - 1) { //如果数据都完全相同，则不带入计算
            return null;
        }

        qvvrDataStruct.line_idx[0] = qtIdxArray;
        qvvrDataStruct.line_num[0] = count;
        //调用dll计算结果

        JnaCallDll jnaCallDll = new JnaCallBalance("qvvr_balance.dll");
        jnaCallDll.setPath();

        try {
            JnaCallBalance.Balancelibrary INSTANTCE = JnaCallBalance.Balancelibrary.INSTANTCE;
            INSTANTCE.qvvr_fun_cause(qvvrDataStruct); // 计算暂降综合评估
        } catch (Exception e) {
            return new Float(3.14159);
        } catch (Throwable e) { //这里使用exception get不到，所以使用Throwable，这是程序错误，不是异常
            return new Float(3.14159);
        }

        return new Float(qvvrDataStruct.sys_res[0].CI);
    }

    private QtIdx getQtIdx(int pointIdx) {
        QtIdx qtIdx = new QtIdx();
        Float value = getBalance.getSarfiValue(this.start, this.end, pointIdx, 0.9f); // 计算平均值
        qtIdx.r_esm = (value == null ? 0f : value.floatValue());
        qtIdx.sarfi_90 = getBalance.getSarfiCount(this.start, this.end, pointIdx, 0.9f).intValue(); // 统计小于0.9的总数
        qtIdx.sarifi_50 = getBalance.getSarfiCount(this.start, this.end, pointIdx, 0.9f).intValue(); // 统计小于0.9的总数
        List<GetBalance.Sarifi> listSarifi = getBalance.getSarifiList(this.start, this.end, pointIdx, 0.9f);

        if (listSarifi.size() > 0) {
            float floatYzd = 0f;
            float floatNl = 0f;

            for (GetBalance.Sarifi sarifi : listSarifi) {
                floatYzd += Float.parseFloat(waveServiceImpl.getYzd(sarifi.getTime().floatValue(),
                        sarifi.getSarifiValue().floatValue()));// 调用接口计算严重程度
                floatNl += BalanceUtil.calculateNl(sarifi.getTime().floatValue() / 1000,
                        sarifi.getSarifiValue().floatValue()); // 调用接口计算暂降能量
            }

            qtIdx.r_assi = floatYzd / listSarifi.size();
            qtIdx.r_asei = floatNl / listSarifi.size();
        } else {
            qtIdx.r_assi = 0f;
            qtIdx.r_asei = 0f;
        }

        return qtIdx;
    }
}
