package com.njcn.utils;

import com.njcn.mapper.advanced.GetBalanceMapper;
import com.njcn.pojo.balance.BalanceInfo;
import com.njcn.pojo.balance.BalanceInfo.PointInfo;
import com.njcn.pojo.balance.QtIdx;
import com.njcn.pojo.balance.QtIdxArray;
import com.njcn.pojo.balance.QvvrDataStruct;
import com.njcn.utils.JnaCallBalance.Balancelibrary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class GetBalance {
    public static class Sarifi {
        private Float sarifiValue;
        private Float time;
        private Float pt1;
        private Float pt2;

        public Float getPt1() {
            return pt1;
        }

        public void setPt1(Float pt1) {
            this.pt1 = pt1;
        }

        public Float getPt2() {
            return pt2;
        }

        public void setPt2(Float pt2) {
            this.pt2 = pt2;
        }

        public Float getSarifiValue() {
            return sarifiValue;
        }

        public void setSarifiValue(Float sarifiValue) {
            this.sarifiValue = sarifiValue;
        }

        public Float getTime() {
            return time;
        }

        public void setTime(Float time) {
            this.time = time;
        }
    }

    @Autowired
    private GetBalanceMapper getBalanceMapper;

    private static final Logger logger = LoggerFactory.getLogger(GetBalance.class);

    public Integer getSarfiCount(Date startTime, Date endTime, int lineId, float sarfi) {
        return getBalanceMapper.getSarfiCount(startTime, endTime, lineId, sarfi);
    }

    public Float getSarfiValue(Date startTime, Date endTime, int lineId, float fvalue) {
        return getBalanceMapper.getSarfiValue(startTime, endTime, lineId, fvalue);
    }

    public List<Sarifi> getSarifiList(Date startTime, Date endTime, int lineId, float sarifi) {
        return getBalanceMapper.getSarifiList(startTime, endTime, lineId, sarifi);
    }

    public void translateData(List<BalanceInfo> list) {
        QvvrDataStruct qvvrDataStruct = new QvvrDataStruct();
        //packageBalance(list); // 组装传入dll的函数，测试使用

        List<BalanceInfo> newList = new ArrayList<>();
        filterData(list, newList); // 过滤数据

        if (newList.size() > 0) { // 如果newList为空则不带入dll计算
            packageData(newList, qvvrDataStruct);
            JnaCallDll jnaCallDll = new JnaCallBalance("qvvr_balance.dll");
            jnaCallDll.setPath();

            try {
                Balancelibrary INSTANTCE = Balancelibrary.INSTANTCE;
                INSTANTCE.qvvr_fun_cause(qvvrDataStruct); // 计算暂降综合评估
            } catch (Exception e) {
                return;
            } catch (Throwable e) {
                return;
            }

            resultPackage(newList, qvvrDataStruct, list);
        }
    }

    public void resultPackage(List<BalanceInfo> list, QvvrDataStruct qvvrDataStruct, List<BalanceInfo> list2) {
        for (int i = 0; i < list.size(); i++) {
            for (BalanceInfo balanceInfo : list2) {
                if (balanceInfo.getAreaIndex().equals(list.get(i).getAreaIndex())) {
                    balanceInfo.setCi(qvvrDataStruct.sys_res[i].CI);
                    balanceInfo.setIsCount(1); // 已计算
                }
            }

            for (int j = 0; j < list.get(i).getList().size(); j++) {
                list.get(i).getList().get(j).setCiv(qvvrDataStruct.sys_res[i].CIV[j]); // 设置监测点的评估数据
                list.get(i).getList().get(j).setL(qvvrDataStruct.sys_res[i].L[j]); // 设置监测点的评估等级
            }
        }
    }

    public void packageData(List<BalanceInfo> list, QvvrDataStruct qvvrDataStruct) {
        qvvrDataStruct.sys_num = list.size(); // 系统数目

        for (int i = 0; i < qvvrDataStruct.sys_num; i++) {
            qvvrDataStruct.line_num[i] = list.get(i).getList().size(); // 监测点数目
            QtIdxArray qtIdxArray = new QtIdxArray();

            for (int j = 0; j < list.get(i).getList().size(); j++) {
                qtIdxArray.qtIdxs[j] = list.get(i).getList().get(j).getQtIdx(); // 各监测点数据
            }

            qvvrDataStruct.line_idx[i] = qtIdxArray;
        }
    }

    // 过滤掉没有监测点，没有暂降事件的数据
    private void filterData(List<BalanceInfo> list, List<BalanceInfo> listResult) {
        for (BalanceInfo balanceInfo : list) {
            BalanceInfo balanceInfo2 = new BalanceInfo();
            List<PointInfo> list2 = new ArrayList<>();

            if (balanceInfo.getList() == null) {
                continue;
            }

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

                if (qtIdx == null || qtIdx.sarfi_90 == 0) {
                    continue;
                }

                list2.add(balanceInfo.getList().get(i));
            }

            if (list2.size() > 4) { // 发生事件的监测点数目少于4组不带入计算
                balanceInfo2.setAreaIndex(balanceInfo.getAreaIndex());
                balanceInfo2.setList(list2);
                listResult.add(balanceInfo2);
            }
        }
    }

    private PointInfo newObj(PointInfo info, double v1, int c1, int c2, double v2, double v3) {
        info.getQtIdx().r_esm = (float) v1;
        info.getQtIdx().sarfi_90 = c1;
        info.getQtIdx().sarifi_50 = c2;
        info.getQtIdx().r_asei = (float) v2;
        info.getQtIdx().r_assi = (float) v3;
        return info;
    }

    // 测试数据
    private void packageBalance(List<BalanceInfo> list) {
        list.clear(); // 清空数据
        BalanceInfo balanceInfo = new BalanceInfo();
        List<PointInfo> list1 = new ArrayList<>();
        BalanceInfo balanceInfo1 = new BalanceInfo();
        List<PointInfo> list2 = new ArrayList<>();
        BalanceInfo balanceInfo2 = new BalanceInfo();
        List<PointInfo> list3 = new ArrayList<>();
        BalanceInfo balanceInfo3 = new BalanceInfo();
        List<PointInfo> list4 = new ArrayList<>();

        // balance
        list1.add(newObj(balanceInfo.new PointInfo(), 0.6118, 6, 0, 1.3728396, 1.8294998));
        list1.add(newObj(balanceInfo.new PointInfo(), 0.6095625, 8, 0, 1.5268255, 1.784375));
        // list1.add(newObj(balanceInfo.new PointInfo(), 0.6095625, 8, 0,
        // 0.94490665, 1.784375));
        /*
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.513278059, 11, 3,
         * 0.038943409, 0.973443882)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.841824777, 7, 0, 0.017636202, 0.316350446));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.633568946, 12, 4,
         * 0.033197831, 0.732862107)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.650689061, 10, 1, 0.03251221, 0.698621878));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.703827045, 10, 1,
         * 0.028379529, 0.59234591)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.774549378, 19, 0, 0.021868389, 0.450901244));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.737499211, 20, 0,
         * 0.025129927, 0.525001577)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.712080988, 3, 1, 0.021317915, 0.575838025));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.74504486, 18, 0,
         * 0.024364546, 0.50991028)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.85877374, 7, 0, 0.013992348, 0.28245252));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.64540808, 19, 4,
         * 0.029085915, 0.709183839)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.673738002, 20, 3, 0.028639768, 0.652523996));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.749849756, 20, 0,
         * 0.024037334, 0.500300488)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.739687883, 20, 0, 0.024960553, 0.520624233));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.70017467, 21, 2,
         * 0.027404159, 0.59965066)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.711459137, 21, 0, 0.026871713, 0.577081725));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.728631134, 22, 0,
         * 0.025512003, 0.542737732)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.713742993, 20, 1, 0.026911718, 0.572514015));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.707457038, 20, 1,
         * 0.027343898, 0.585085925)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.645892647, 21, 4, 0.030657829, 0.708214706));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.652833328, 22, 3,
         * 0.031039637, 0.694333344)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.641684405, 26, 5, 0.031476379, 0.71663119));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.627076664, 26, 5,
         * 0.032241729, 0.745846671)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.695131293, 28, 4, 0.028534804, 0.609737415));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.689796272, 15, 2,
         * 0.028628872, 0.620407456)); list1.add(newObj(balanceInfo.new
         * PointInfo(), 0.661236046, 29, 6, 0.030676247, 0.677527909));
         * list1.add(newObj(balanceInfo.new PointInfo(), 0.653012983, 29, 6,
         * 0.031181928, 0.693974033));
         */
        balanceInfo.setList(list1);

        // balance1
        list2.add(newObj(balanceInfo.new PointInfo(), 0.29946667, 3, 3, 0.098330684, 1.401));
        /*
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.4516762, 21, 11,
         * 3.7077909, 2.535)); list2.add(newObj(balanceInfo.new PointInfo(),
         * 0.29946667, 3, 3, 0.089499444, 1.401));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.5308, 20, 9,
         * 1.7142252, 1.86955)); list2.add(newObj(balanceInfo.new PointInfo(),
         * 0.50882727, 11, 6, 0.47896016, 1.3647273));
         */
        /*
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.704727554, 33, 7,
         * 0.027857897, 0.590544891)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.592968211, 22, 7, 0.035418672, 0.814063578));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.719964439, 20, 1,
         * 0.027610646, 0.560071123)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.764363869, 53, 1, 0.024799052, 0.471272262));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.688865847, 56, 4,
         * 0.030678757, 0.622268305)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.802212721, 12, 2, 0.022408617, 0.395574558));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.696726748, 50, 4,
         * 0.030074771, 0.606546503)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.820490677, 27, 1, 0.018774963, 0.359018646));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.608039496, 56, 13,
         * 0.034504053, 0.783921008)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.623321831, 56, 10, 0.035270293, 0.753356339));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.66861619, 54, 8,
         * 0.031530855, 0.66276762)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.66963643, 55, 7, 0.031685635, 0.66072714));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.622283259, 58, 12,
         * 0.035360125, 0.755433482)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.636218389, 60, 11, 0.034125553, 0.727563222));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.637830365, 58, 11,
         * 0.034056446, 0.724339269)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.663441712, 56, 4, 0.032586648, 0.673116575));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.658294921, 56, 5,
         * 0.033023316, 0.683410157)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.570830728, 56, 15, 0.038527374, 0.858338544));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.59784393, 58, 15,
         * 0.0368393, 0.804312139)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.654805188, 68, 14, 0.031510504, 0.690389625));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.645193478, 70, 14,
         * 0.032292297, 0.709613045)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.684727268, 62, 10, 0.029295388, 0.630545465));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.733929761, 43, 7,
         * 0.024778525, 0.532140478)); list2.add(newObj(balanceInfo.new
         * PointInfo(), 0.684619457, 69, 12, 0.02895538, 0.630761086));
         * list2.add(newObj(balanceInfo.new PointInfo(), 0.677826984, 70, 12,
         * 0.029369456, 0.644346032));
         */
        balanceInfo1.setList(list2);

        // balance2
        list3.add(newObj(balanceInfo.new PointInfo(), 0.390028, 50, 41, 544.2123, 3.4621797));
        list3.add(newObj(balanceInfo.new PointInfo(), 0.5229, 33, 21, 819.30927, 3.3126361));
        list3.add(newObj(balanceInfo.new PointInfo(), 0.5152214, 42, 27, 644.3919, 3.1522856));
        /*
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.700450435, 73, 11,
         * 0.027605203, 0.59909913)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.741474832, 32, 3, 0.024470759, 0.517050335));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.704243024, 72, 11,
         * 0.027608248, 0.591513952)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.66106807, 59, 15, 0.030029295, 0.67786386));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.750368079, 55, 7,
         * 0.024028211, 0.499263843)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.728953762, 103, 15, 0.026671261, 0.542092476));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.632611966, 105, 22,
         * 0.03286451, 0.734776069)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.815696876, 28, 1, 0.019571619, 0.368606249));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.729433014, 94, 6,
         * 0.026905343, 0.541133973)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.817164327, 42, 4, 0.017836151, 0.365671346));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.68728277, 104, 11,
         * 0.029880744, 0.625434461)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.658593957, 105, 16, 0.032286184, 0.682812085));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.680137828, 105, 18,
         * 0.030239847, 0.639724344)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.637057472, 106, 25, 0.03251419, 0.725885057));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.602499687, 108, 32,
         * 0.034936903, 0.795000626)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.606259415, 111, 34, 0.034678796, 0.78748117));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.59383151, 105, 35,
         * 0.035731576, 0.812336981)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.60892978, 106, 23, 0.034201708, 0.782140439));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.60738294, 106, 24,
         * 0.034483481, 0.78523412)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.625348084, 112, 33, 0.034258522, 0.749303832));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.611056431, 118, 33,
         * 0.034767456, 0.777887138)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.659139434, 139, 29, 0.031521634, 0.681721132));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.657169585, 145, 29,
         * 0.031573884, 0.685660831)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.690097815, 138, 18, 0.029011806, 0.619804371));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.731761025, 89, 10,
         * 0.025402823, 0.53647795)); list3.add(newObj(balanceInfo.new
         * PointInfo(), 0.67168011, 156, 26, 0.02988264, 0.656639781));
         * list3.add(newObj(balanceInfo.new PointInfo(), 0.658366576, 154, 26,
         * 0.03079815, 0.683266847));
         */
        balanceInfo2.setList(list3);

        // balance3
        list4.add(newObj(balanceInfo.new PointInfo(), 0.835164305, 17, 1, 0.01749912, 0.32967139));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.785030796, 30, 2, 0.022174046, 0.429938408));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.700324877, 57, 12, 0.027287363, 0.599350245));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.698388491, 63, 12, 0.027379777, 0.603223018));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.809593783, 26, 0, 0.019760881, 0.380812435));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.707607681, 62, 13, 0.027098098, 0.584784638));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.715335439, 41, 4, 0.028286854, 0.569329123));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.711811304, 35, 4, 0.028204876, 0.576377391));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.701237201, 75, 8, 0.028616343, 0.597525597));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.604489818, 78, 18, 0.034956953, 0.791020365));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.816744093, 27, 2, 0.017514986, 0.366511815));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.682378122, 73, 8, 0.030941043, 0.635243755));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.823843254, 43, 3, 0.018296481, 0.352313492));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.605309012, 76, 17, 0.035232859, 0.789381976));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.590466491, 77, 14, 0.036513456, 0.819067019));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.610477407, 75, 15, 0.035276875, 0.779045186));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.593096466, 77, 21, 0.035886524, 0.813807069));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.576491132, 80, 23, 0.037477541, 0.847017735));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.569121661, 79, 26, 0.037921953, 0.861756678));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.578847242, 80, 26, 0.03717259, 0.842305515));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.592821418, 82, 19, 0.035243781, 0.814357164));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.599484239, 85, 20, 0.034688785, 0.801031523));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.594456343, 86, 23, 0.036430544, 0.811087314));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.597078439, 89, 24, 0.036381782, 0.805843123));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.654545191, 107, 24, 0.031959476, 0.690909618));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.637371511, 110, 27, 0.032851766, 0.725256979));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.667215673, 98, 21, 0.030840293, 0.665568653));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.714190776, 74, 13, 0.025811784, 0.571618448));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.673536135, 119, 23, 0.02976116, 0.65292773));
        list4.add(newObj(balanceInfo.new PointInfo(), 0.658906846, 117, 24, 0.031112434, 0.682186309));
        // balanceInfo3.setList(list4);

        list.add(balanceInfo);
        // list.add(balanceInfo1);
        // list.add(balanceInfo2);
        // list.add(balanceInfo3);
    }
}