package com.njcn.serviceimpl.commons;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.util.StringUtil;
import com.njcn.mapper.configuration.LineDetailMapper;
import com.njcn.mapper.data.EventDetailMapper;
import com.njcn.pojo.advanced.*;
import com.njcn.pojo.commons.WaveData;
import com.njcn.pojo.commons.WaveResponseBody;
import com.njcn.pojo.commons.wave.AnalyWaveModel;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.configuration.Line;
import com.njcn.pojo.configuration.LineDetail;
import com.njcn.pojo.data.EventDetail;
import com.njcn.pojo.data.WaveSvg;
import com.njcn.pojo.entityEnum.EnumEvt;
import com.njcn.pojo.file.FileProperty;
import com.njcn.pojo.file.JsonFile;
import com.njcn.pojo.wave.InstantF;
import com.njcn.pojo.wave.RmsF;
import com.njcn.pojo.wave.WaveDataDetail;
import com.njcn.service.commons.IWaveService;
import com.njcn.service.configuration.DeviceService;
import com.njcn.service.configuration.ILineService;
import com.njcn.serviceimpl.advanced.GetWaveNameImpl;
import com.njcn.utils.*;
import com.njcn.utils.JnaCallC.Clibrary;
import com.njcn.utils.JnaCallCause.Causelibrary;
import com.njcn.utils.echarts.EchartsUtil;
import com.njcn.utils.echarts.FreemarkerUtil;
import com.njcn.utils.redis.JedisManager;
import com.njcn.utils.redis.RedisCacheUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class WaveServiceImpl implements IWaveService {

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

    private static final int MAXLENGTH = 128 * 3000; //波形最大值计算

    @Autowired
    private GetWaveNameImpl getWaveNameImpl;

    @Autowired
    private AppConfig appConfig;

    @Autowired
    private EventDetailMapper eventDetailMapper;

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private ILineService lineService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private JedisManager jedisManager;

    @Autowired
    private LineDetailMapper lineDetailMapper;

    /************************************************************
     * modify by sw 高级算法的计算需要抽点计算 与瞬时波形展示的波形不一致，故每次都需要重新抽点
     ************************************************************/
    private WaveResponseBody wp = new WaveResponseBody();
    private WaveResponseBody wpBody = new WaveResponseBody();

    private Rect rect = null;
    private CauseStruct causeStruct = null;
    private JsonFile jsonFile;
    private EntityAdvancedData entityAdvancedData;

    /**
     * 根据事件索引获取需要下载文件的路径
     *
     * @param index 事件索引
     */
    @Override
    public Map<String, List<String>> getEventDetailIndexList(List<String> index) {
        //map的key为装置IP，value为下载波形的本地路径
        Map<String, List<String>> files = new HashMap<>();
        for (int i = 0; i < index.size(); i++) {
            String ip = deviceService.getIPByEventIndex(index.get(i));
            if (StringUtils.isBlank(ip)) {
                continue;
            }
            List<String> filePath = files.get(ip);
            if (CollectionUtils.isEmpty(filePath)) {
                filePath = new ArrayList<>();
            }
            String path = appConfig.getWavePath();
            path = path + File.separator + ip;
            //根据事件索引获取事件日志数据
            EventDetail eventDetail;
            eventDetail = eventDetailMapper.selectByIndex(index.get(i));
            if (eventDetail != null) {
                if (Integer.parseInt(eventDetail.getFileFlag()) == 1) {
                    //根据暂降事件，返回波形文件名
                    String waveName = getWaveName(eventDetail, path);
                    //获取文件名有误，跳走，下一个
                    if (StringUtils.isBlank(waveName)) {
                        continue;
                    }
                    String pathTemp = path + File.separator + waveName;
                    File file = new File(pathTemp);
                    // 判断文件是否存在
                    if (file.isFile() && file.exists()) {
                        //CFG文件路径
                        filePath.add(pathTemp);
                        //DAT文件路径
                        filePath.add(pathTemp.replace("CFG", "DAT"));
                    }
                    files.put(ip, filePath);
                }
            }
        }
        return files;
    }


    //根据暂降事件获取波形文件名 CFG为结尾
    private String getWaveName(EventDetail eventDetail, String path) {
        String waveName = eventDetail.getWaveName();
        if (StringUtils.isNotBlank(waveName)) {
            waveName = waveName + ".CFG";
        } else {
            String timeID;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = simpleDateFormat.format(eventDetail.getTimeId());
            String year = dateString.substring(0, 10);
            year = year.replaceAll("-", "");
            String hour = dateString.substring(11);
            hour = hour.replaceAll(":", "");
            DecimalFormat df=new DecimalFormat("000");
            String msStr = df.format(eventDetail.getMs());
            timeID = year + "_" + hour + "_" + msStr;
            int lineID = eventDetail.getLineId().intValue() % 10;
            timeID = lineID + "_" + timeID;
            waveName = timeID + ".CFG";
        }
        return waveName;
    }


    /**
     * 获取该事件的严重度
     *
     * @param persisttime 持续时间 ms单位
     * @param eventvaule  暂降、暂升幅值
     */
    public static String getYzd(Float persisttime, Float eventvaule) {
        float yzd;
        DecimalFormat df = new DecimalFormat("0.000");// 格式化小数
        if (persisttime <= 20) {
            yzd = 1 - eventvaule;
        } else if (persisttime > 20 && persisttime <= 200) {
            yzd = 2 * (1 - eventvaule);
        } else if (persisttime > 200 && persisttime <= 500) {
            yzd = 3.3f * (1 - eventvaule);
        } else if (persisttime > 500 && persisttime <= 10000) {
            yzd = 5 * (1 - eventvaule);
        } else {
            yzd = 10 * (1 - eventvaule);
        }
        String tempOnlineRate = df.format(yzd);// 返回的是String类型
        return tempOnlineRate;
    }

    /**
     * 根据暂降事件索引获取波形数据
     * 注：当前只考虑本地的波形文件
     *
     * @param eventIndex 事件索引
     * @param flag       抽点方式
     */
    @Override
    public WaveData getWavedata(String eventIndex, int flag) {
        WaveData waveData = new WaveData();
        EventDetail eventDetail = eventDetailMapper.selectByIndex(eventIndex);
        if (eventDetail != null) {
            //获取PT变比
            Line line = lineService.getLineByEventDetailIndex(eventIndex);
            waveData.setLineName(line.getName());
            /*********** Modify by xuyang 添加变电站名称---Start **************/
            waveData.setSubName(lineService.getSubNameById(line.getSubIndex()));
            /***********  Modify by xuyang ---End  **************/
            waveData.setEventValue(PubUtils.floatRound(2, eventDetail.getEventValue() * 100));
            waveData.setPersistTime(PubUtils.floatRound(3, eventDetail.getPersistTime() / 1000));
            waveData.setTrigeTime(AnalyWave.getTimeWave());
            if (Objects.nonNull(line)) {
                float pt1 = line.getPt1();
                float pt2 = line.getPt2();
                float pt = (pt1) / (pt2);
                waveData.setPt(pt);
                float ct1 = line.getCt1();
                float ct2 = line.getCt2();
                float ct = (ct1) / (ct2);
                waveData.setCt(ct);
                //获取接线方式
                LineDetail lineDetail = new LineDetail();
                lineDetail.setLineIndex(line.getLineIndex());
                lineDetail = lineDetailMapper.selectOne(lineDetail);
                if (null != lineDetail && lineDetail.getPtType() != 0) {
                    //赋值abc三相名称
                    if (lineDetail.getPtType() == 2) {
                        waveData.setOpen(true);
                    }
                }
            }
            //严重度
            Float yzd = eventDetail.getSeverity();
            if (null == yzd || yzd == 0.0) {
                // 前去计算严重度
                yzd = Float.parseFloat(getYzd(eventDetail.getPersistTime(), eventDetail.getEventValue()));
                eventDetail.setSeverity(yzd);
                //回填严重度到数据库中
                updateEventDeatilYzd(eventDetail);
            }
            waveData.setYzd(yzd);
            //暂降发生时间
            String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(eventDetail.getTimeId()) + "." + PubUtils.getMs(eventDetail.getMs());
            waveData.setTime(time);
            //暂降类型
            // 获取暂降类型的数据字典 暂降类型
            List<DicData> voltageType = redisCacheUtil.getVoltageType();
            for (DicData dicData : voltageType) {
                if (dicData.getDicIndex().equals(eventDetail.getEventType())) {
                    waveData.setWaveType(dicData.getDicName());
                    break;
                }
            }
            if (Integer.parseInt(eventDetail.getFileFlag()) == 1) {
                //根据暂降事件，返回波形文件名
                String ip = deviceService.getIPByEventIndex(eventIndex);
                if (StringUtils.isBlank(ip)) {
                    return waveData;
                }
                String path = appConfig.getWavePath();
                path = path + File.separator + ip;
                String waveName = getWaveName(eventDetail, path);
                String pathTemp = path + File.separator + waveName;
                File file = new File(pathTemp);
                // 判断文件是否存在
                if (file.isFile() && file.exists()) {
                    logger.error(pathTemp + "路径下，文件找到了");
                    //CFG文件路径 pathTemp
                    //获取波形的瞬时值、RMS值数据
                    AnalyWave analyWave = new AnalyWave();
                    AnalyWaveModel.tagDataValue tagDataValue = analyWave.readComtrade(pathTemp, null, flag);
                    List<List<Float>> shunWave = tagDataValue.getListWaveData();//获取瞬时波形值
                    List<List<Float>> rmsWave = analyWave.showValidData(shunWave);//RMS值波形
                    waveData.setnOneWaveNum(analyWave.getnOneWaveNum());
                    waveData.setSunData(shunWave);
                    waveData.setTmpWaveTitle(tagDataValue.getTmpWaveTitle());
                    waveData.setFirstMs(analyWave.getFirstMs());
                    waveData.setFirstTime(analyWave.getFirstTime());
                    waveData.setRmsData(rmsWave);
                    waveData.setRatesCfg(analyWave.getRatesCfg());
                    /*********** Modify by yexibao ---Start **************/
                    waveData.setiPhasic(tagDataValue.getiPhasic());
                    /***********  Modify by yexibao ---End  **************/
                    List<String> tmpWaveTitle = tagDataValue.getTmpWaveTitle();
                    /*********** Modify by yexibao ---Start **************/
                    for (int i = 0; i < tagDataValue.getiPhasic(); i++) {
                        if (tmpWaveTitle.get(i + 1).substring(1).indexOf("A") > -1)
                            waveData.setA(tmpWaveTitle.get(i + 1).substring(1));
                        if (tmpWaveTitle.get(i + 1).substring(1).indexOf("B") > -1)
                            waveData.setB(tmpWaveTitle.get(i + 1).substring(1));
                        if (tmpWaveTitle.get(i + 1).substring(1).indexOf("C") > -1)
                            waveData.setC(tmpWaveTitle.get(i + 1).substring(1));
                    }
                    /***********  Modify by yexibao ---End  **************/

                } else {
                    logger.error(pathTemp + "路径下，文件找不到了");
                }
            }
        }
        return waveData;
    }


    /**
     * 获取多事件波形数据
     *
     * @param indexArray 事件索引数组
     * @param flag       抽点方式
     */
    @Override
    public List<WaveData> getWaveDataCompare(String[] indexArray, int flag) {
        List<WaveData> waveDatas = new ArrayList<>();
        for (String index : indexArray) {
            WaveData waveData = null;
            EventDetail eventDetail = eventDetailMapper.selectByIndex(index);
            if (Objects.nonNull(eventDetail) && Integer.parseInt(eventDetail.getFileFlag()) == 1) {
                waveData = new WaveData();
                Line line = lineService.getLineByEventDetailIndex(index);
                waveData.setLineName(line.getName());
                waveData.setSubName(lineService.getSubNameById(line.getSubIndex()));
                waveData.setEventValue(PubUtils.floatRound(2, eventDetail.getEventValue() * 100));
                waveData.setPersistTime(PubUtils.floatRound(3, eventDetail.getPersistTime() / 1000));
                waveData.setTrigeTime(AnalyWave.getTimeWave());
                if (Objects.nonNull(line)) {
                    float pt1 = line.getPt1();
                    float pt2 = line.getPt2();
                    float pt = (pt1) / (pt2);
                    waveData.setPt(pt);
                    LineDetail lineDetail = new LineDetail();
                    lineDetail.setLineIndex(line.getLineIndex());
                    lineDetail = lineDetailMapper.selectOne(lineDetail);
                    if (null != lineDetail && lineDetail.getPtType() != 0) {
                        if (lineDetail.getPtType() == 2) {
                            waveData.setOpen(true);
                        }
                    }
                }
                //严重度
                Float yzd = eventDetail.getSeverity();
                if (null == yzd || yzd == 0.0f) {
                    yzd = Float.parseFloat(getYzd(eventDetail.getPersistTime(), eventDetail.getEventValue()));
                    eventDetail.setSeverity(yzd);
                    updateEventDeatilYzd(eventDetail);
                }
                waveData.setYzd(yzd);
                //暂降发生时间
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(eventDetail.getTimeId()) + "." + PubUtils.getMs(eventDetail.getMs());
                waveData.setTime(time);
                String ip = deviceService.getIPByEventIndex(index);
                String path = appConfig.getWavePath();
                path = path + File.separator + ip;
                //根据事件索引获取事件日志数据
                if (StrUtil.isNotBlank(ip)) {
                    //根据暂降事件，返回波形文件名
                    String waveName = getWaveName(eventDetail, path);
                    String pathTemp = path + File.separator + waveName;
                    File file = new File(pathTemp);
                    // 判断文件是否存在
                    if (file.isFile() && file.exists()) {
                        logger.error(pathTemp + "路径下，文件找到了");
                        AnalyWave analyWave = new AnalyWave();
                        AnalyWaveModel.tagDataValue tagDataValue = analyWave.readComtrade(pathTemp, null, flag);
                        List<List<Float>> shunWave = tagDataValue.getListWaveData();
                        List<List<Float>> rmsWave = analyWave.showValidData(shunWave);
                        waveData.setnOneWaveNum(analyWave.getnOneWaveNum());
                        waveData.setSunData(shunWave);
                        waveData.setRmsData(rmsWave);
                        waveData.setRatesCfg(analyWave.getRatesCfg());
                        waveData.setiPhasic(tagDataValue.getiPhasic());
                        List<String> tmpWaveTitle = tagDataValue.getTmpWaveTitle();
                        for (int i = 0; i < tagDataValue.getiPhasic(); i++) {
                            if (tmpWaveTitle.get(i + 1).substring(1).contains("A")) {
                                waveData.setA(tmpWaveTitle.get(i + 1).substring(1));
                            }
                            if (tmpWaveTitle.get(i + 1).substring(1).contains("B")) {
                                waveData.setB(tmpWaveTitle.get(i + 1).substring(1));
                            }
                            if (tmpWaveTitle.get(i + 1).substring(1).contains("C")) {
                                waveData.setC(tmpWaveTitle.get(i + 1).substring(1));
                            }
                        }
                    } else {
                        logger.error(pathTemp + "路径下，文件找不到了");
                    }
                }
            }
            waveDatas.add(waveData);
        }
        return waveDatas;
    }


    /**
     * 根据文件路径获取波形数据
     *
     * @param path 文件路径
     */
    @Override
    public WaveData getWavedataByPath(String path) throws ParseException {
        WaveData waveData = new WaveData();
        //获取波形的瞬时值、RMS值数据
        AnalyWave analyWave = new AnalyWave();
        List<List<Float>> shunWave = analyWave.readComtrade(path, null, 1).getListWaveData();//获取瞬时波形值
        List<List<Float>> rmsWave = analyWave.showValidData(shunWave);//RMS值波形
        waveData.setSunData(shunWave);
        waveData.setRmsData(rmsWave);
//        if(path.indexOf("_")>0){
//            //暂降发生时间
//            String dateTime=path.substring(path.indexOf("_")+1,path.lastIndexOf("_"));
//            String ms=path.substring(path.lastIndexOf("_")+1,path.lastIndexOf("."));
//            Date dateTemp=new SimpleDateFormat("yyyyMMdd_HHmmss").parse(dateTime);
//            String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dateTemp)+"."+ms;
//            waveData.setTime(time);
//        }
//        waveData.setTime(path.substring(path.lastIndexOf("\\")+1,path.lastIndexOf(".")));
        return waveData;
    }


    /**
     * 重新回填事件严重度
     */
    public void updateEventDeatilYzd(EventDetail eventDetail) {
        eventDetailMapper.updateSeverity(eventDetail.getEventDetailIndex(), eventDetail.getSeverity());
    }

    /**
     * @return
     * @throws Exception
     * @调用s获取rect数据 add by sw
     * @增加判断：事件是否经过高级算法处2理
     * @已处理：直接从文件获取所需信息
     * @未处理：调用dll处理，将结果保存到文件并更新数据库字段
     */
    @Override
    public EntityAdvancedData analysis(String eventIndex) throws Exception {
        //获取波形数据
        WaveData waveData = modifyRect(eventIndex);

        int b = eventDetailMapper.queryDealFlag(eventIndex).intValue(); // 数据库标识是否已经调用dll计算
        boolean exist = getFile(eventIndex);
        if (0 == b) { // 未计算
            judgeEvt(2, eventIndex, waveData);
        } else {
            if (exist) {// 获取暂降原因，从数据库获取
                try {
                    getDataFromFile(waveData.getTrigeTime());
                } catch (Exception e) {
                    judgeEvt(1, eventIndex, waveData);
                }
            } else {
                judgeEvt(1, eventIndex, waveData);
            }

            entityAdvancedData.sagReason[0] = eventDetailMapper.querySagDescriptionbyIndex(eventIndex);
        }

        /****************************************************************
         * 根据返回的结果计算，获取暂降类型描述
         ****************************************************************/
        if (entityAdvancedData.backNumber > 0) {
            for (int i = 0; i < entityAdvancedData.backNumber; i++) {
                entityAdvancedData.sagType[i] = eventDetailMapper
                        .querySagDescriptionbyId(entityAdvancedData.evt_buf[i].qvvr_cata_type[0], "暂降类型");

                switch (entityAdvancedData.evt_buf[i].qvvr_phasetype[0]) {
                    case 1:
                        entityAdvancedData.sagPhaseType[i] = "单相";
                        break;
                    case 2:
                        entityAdvancedData.sagPhaseType[i] = "两相";
                        break;
                    case 3:
                        entityAdvancedData.sagPhaseType[i] = "三相";
                        break;
                    default:
                        ;
                }
            }
        }

        return entityAdvancedData;
    }

    // 1-值调用暂降类型dll 2-两个dll都调用
    private void judgeEvt(int type, String eventIndex, WaveData waveData) throws Exception {
        try {
            getDataFromDLL(type, eventIndex, waveData);
        } catch (Exception e) {
            eventDetailMapper.updateDealFlag(3, eventIndex); //计算失败
            logger.error(e.toString());
        } catch (Throwable e) {
            eventDetailMapper.updateDealFlag(3, eventIndex); //计算失败
            logger.error(e.toString());
        }
    }

    public WaveData modifyRect(String eventIndex) throws Exception {
        WaveData waveData = getWavedata(eventIndex, 0); //抽点方法计算波形
        List<List<Float>> list = waveData.getSunData();

        WaveData wavedata = getWavedata(eventIndex, 3); //原始波形
        List<List<Float>> lists = wavedata.getSunData();
        entityAdvancedData = null;
        entityAdvancedData = new EntityAdvancedData(lists.size());

        for (int i = 0; i < lists.size(); i++) {
            entityAdvancedData.smp_x[i] = lists.get(i).get(0).floatValue();//坐标轴
            entityAdvancedData.smp_a[i] = lists.get(i).get(1).floatValue();
            entityAdvancedData.smp_b[i] = lists.get(i).get(2).floatValue();
            entityAdvancedData.smp_c[i] = lists.get(i).get(3).floatValue();
        }

        entityAdvancedData.smp_len = lists.size();

        // 将获取到的数据填充到结构体内
        rect = new Rect();
        causeStruct = new CauseStruct();

        for (int i = 0; i < list.size(); i++) {
            rect.smp_va[i] = list.get(i).get(1).floatValue();
            causeStruct.smp_va[i] = list.get(i).get(1).floatValue();

            rect.smp_vb[i] = list.get(i).get(2).floatValue();
            causeStruct.smp_vb[i] = list.get(i).get(2).floatValue();

            rect.smp_vc[i] = list.get(i).get(3).floatValue();
            causeStruct.smp_vc[i] = list.get(i).get(3).floatValue();
        }

        rect.smp_len = list.size();

        if (rect.smp_len >= MAXLENGTH) { //超过60s的波形直接抛异常给上面处理
            throw new Exception();
        }

        rect.smp_rate = (int) waveData.getnOneWaveNum();
        causeStruct.smp_len = list.size();
        causeStruct.smp_rate = (int) waveData.getnOneWaveNum();

        return waveData;
    }

    public void setEntityAdvanceData() {
        entityAdvancedData.backNumber = rect.evt_num;

        for (int i = 0; i < rect.evt_num; i++) {
            entityAdvancedData.evt_buf[i] = rect.evt_buf[i];
        }
    }

    /**
     * @return
     * @获取文件名： 1、网络共享文件 2、本地文件
     */
    public boolean getFile(String eventIndex) throws Exception {
        ReadWaveData readWaveData = getWaveNameImpl.getWaveName(eventIndex);
        FileProperty fileProperty;

        if (StringUtil.isEmpty(readWaveData.getFileName())) {
            EventDetail tmp = eventDetailMapper.selectByIndex(eventIndex);
            fileProperty = new FileProperty(
                    readWaveData.getFilePath().substring(0, readWaveData.getFilePath().lastIndexOf("\\") + 1),
                    tmp.getWaveName(), ".HDR");
        } else if (null == readWaveData.getAuth()) {
            fileProperty = new FileProperty(
                    readWaveData.getFilePath().substring(0, readWaveData.getFilePath().lastIndexOf("\\") + 1),
                    readWaveData.getFileName().replaceAll(".CFG", ""), ".HDR");
        } else {
            fileProperty = new FileProperty(
                    readWaveData.getFilePath().substring(0, readWaveData.getFilePath().lastIndexOf("\\") + 1),
                    readWaveData.getFileName().replaceAll(".CFG", ""), ".HDR", 1, readWaveData.getAuth());
        }

        jsonFile = new JsonFile(fileProperty);

        return jsonFile.isExitFile();
    }

    /**
     * @return
     * @事件未进行高级算法处理：系统调用dll处理并保存结果
     */
    @Transactional
    public void getDataFromDLL(int type, String eventIndex, WaveData waveData) throws Exception {
        String os = System.getProperty("os.name");  // 获取当前操作系统的类型
        int beginIndex = os != null && os.startsWith("Windows") ? 1 : 0;// windows操作系统为1 否则为0
        JnaCallDll jnaCallDll;
        if (beginIndex == 1) {
            jnaCallDll = new JnaCallC("qvvr_dll.dll");
        } else {
            jnaCallDll = new JnaCallC("libqvvr_dll.so");
        }
        jnaCallDll.setPath();

        Clibrary INSTANTCE = Clibrary.INSTANTCE;
        INSTANTCE.qvvr_fun(rect); // 计算暂降类

        initBackData(rect.evt_num);

        if (type == 2) {
            JnaCallDll jnaCallDll1;
            if (beginIndex == 1) {
                jnaCallDll1 = new JnaCallCause("qvvr_dll_cause.dll");
            } else {
                jnaCallDll1 = new JnaCallCause("libqvvr_cause_dll.so");
            }
            jnaCallDll1.setPath();

            Causelibrary CAUSE = Causelibrary.INSTANTCE;
            CAUSE.qvvr_fun_cause(causeStruct);// 计算暂降原因

            if (causeStruct.no_cal == 1) {// 暂降原因的结果更新到数据库
                causeStruct.cause = 0;
            }

            eventDetailMapper.updateCause(causeStruct.cause, eventIndex);
            entityAdvancedData.sagReason[0] = eventDetailMapper.querySagDescriptionbyId(causeStruct.cause, "暂降原因");
        }

        /**
         * @判断是否调用成功
         * @成功执行更新pqs_eventdetail操作:更新dq持续时间、暂降类型、暂降原因、总分段数目
         */
        if (rect.evt_num > 0) {
            SagEvent sagEvent = new SagEvent();
            sagEvent.setIndexEventDetail(eventIndex);
            sagEvent.setNumber(rect.evt_num);
            sagEvent.setDqTime(rect.evt_buf[0].hold_time_dq);
            // sagEvent.setSagReason(Integer.toString(rect.evt_buf[0].qvvr_cata_cause[0]));
            // (暂不支持)
            sagEvent.setFirstType((Integer.toString(rect.evt_buf[0].qvvr_cata_type[0])));
            sagEvent.setFirstTime(waveData.getFirstTime());
            sagEvent.setFirstMs(waveData.getFirstMs());

            // 排序并获取多段中最小的数据
            float[] f = new float[rect.evt_num];

            for (int i = 0; i < rect.evt_num; i++) {
                Arrays.parallelSort(rect.evt_buf[i].u3_min, 0, rect.evt_buf[i].u_min_num - 1 /*< 0 ? 0 : rect.evt_buf[i].u_min_num - 1*/);
                f[i] = rect.evt_buf[i].u3_min[0];
            }

            float min = f[0];
            int index = 0;

            for (int len = 1; len < f.length; len++) {
                if (min - f[len] > 0) {
                    index = len;
                    min = f[len];
                }
            }

            /********************************************************************
             * add by sw 返回的分段位置需根据采样率进行恢复
             ********************************************************************/
            List<AnalyWaveModel.tagOneRate> list = waveData.getRatesCfg().OneRate;
            int rate = (int) waveData.getnOneWaveNum();
            int k = 0;
            int l = 0;

            for (int i = 0; i < list.size(); i++) {
                if (i == 0) {
                    for (int j = 0; j < rect.evt_buf[0].SEG_T_num; j++) {
                        if (list.get(i).nOneSample >= rate && rect.evt_buf[0].SEG_T_idx[j] < list.get(i).nSampleNum
                                / (list.get(i).nOneSample / rate)) {
                            rect.evt_buf[0].SEG_T_idx[j] = (int) (rect.evt_buf[0].SEG_T_idx[j]
                                    * (list.get(i).nOneSample / rate));
                            k++;
                        }
                    }

                    for (int j = 0; j < rect.evt_buf[0].u_min_num; j++) {
                        if (list.get(i).nOneSample >= rate && rect.evt_buf[0].order_min_idx[j] < list.get(i).nSampleNum
                                / (list.get(i).nOneSample / rate)) {
                            rect.evt_buf[0].order_min_idx[j] = (int) (rect.evt_buf[0].order_min_idx[j]
                                    * (list.get(i).nOneSample / rate));
                            l++;
                        }
                    }
                } else {
                    for (int j = k; j < rect.evt_buf[0].SEG_T_num; j++) {
                        int posb = 0;
                        int pose = 0;
                        int pos = 0;

                        for (int kk = 0; kk <= i; kk++) {
                            pose += list.get(kk).nSampleNum / list.get(kk).nOneSample * rate;

                            if (kk < i) {
                                posb += list.get(kk).nSampleNum / list.get(kk).nOneSample * rate;
                                pos += list.get(kk).nSampleNum;
                            }
                        }

                        if (list.get(i).nOneSample >= rate
                                && rect.evt_buf[0].SEG_T_idx[j] > posb
                                && rect.evt_buf[0].SEG_T_idx[j] < pose) {
                            rect.evt_buf[0].SEG_T_idx[j] = (int) ((rect.evt_buf[0].SEG_T_idx[j] - posb) * list.get(i).nOneSample / rate) + pos;
                            k++;
                        }
                    }

                    for (int j = l; j < rect.evt_buf[0].u_min_num; j++) {
                        int posb = 0;
                        int pose = 0;
                        int pos = 0;

                        for (int kk = 0; kk <= i; kk++) {
                            pose += list.get(kk).nSampleNum / list.get(kk).nOneSample * rate;

                            if (kk < i) {
                                posb += list.get(kk).nSampleNum / list.get(kk).nOneSample * rate;
                                pos += list.get(kk).nSampleNum;
                            }
                        }

                        if (list.get(i).nOneSample >= rate
                                && rect.evt_buf[0].order_min_idx[j] > posb
                                && rect.evt_buf[0].order_min_idx[j] < pose) {
                            rect.evt_buf[0].order_min_idx[j] = (int) ((rect.evt_buf[0].order_min_idx[j] - posb) * list.get(i).nSampleNum / rate) + pos;
                            l++;
                        }
                    }
                }
            }

            sagEvent.setSagType(Integer.toString(rect.evt_buf[index].qvvr_cata_type[0]));
            eventDetailMapper.updateSagData(sagEvent);
            setEntityAdvanceData();
            WriteData2File();
            eventDetailMapper.updateDealFlag(1, eventIndex); //计算有结果
        } else if (rect.evt_num == 0) {
            eventDetailMapper.updateDealFlag(2, eventIndex);//已计算无结果
        }
    }

    /**
     * @throws Exception
     * @向文件中写数据
     */
    public void WriteData2File() throws Exception {
        StringBuilder stringBuilder = new StringBuilder("{" + EnumEvt.NEWLINE.getProperty());

        /**
         * @写入返回事件总数
         */
        stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.EVT_NUM.getProperty()));
        stringBuilder.append(EnumEvt.setEnter(Utils.int2String(rect.evt_num), 1));
        stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.EVT_BUF.getProperty()) + "[");

        /**
         * @写入返回事件参数
         */
        for (int i = 0; i < rect.evt_num; i++) {
            stringBuilder.append(EnumEvt.setNewLine() + EnumEvt.setTab(2));
            /**
             * @波形起始点
             */
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.POW_A.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].POW_a), 3));
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.POW_B.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].POW_b), 3));
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.POW_C.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].POW_c), 3));
            /**
             * @跳变段电压变化率
             */
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.VOLTAGECHANGE_VA.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].Voltagechange_Va), 3));
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.VOLTAGECHANGE_VB.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].Voltagechange_Vb), 3));
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.VOLTAGECHANGE_VC.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].Voltagechange_Vc), 3));
            /**
             * @持续时间
             */
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.HOLD_TIME_RMS.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].hold_time_rms), 3));
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.HOLD_TIME_DQ.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(rect.evt_buf[i].hold_time_dq), 3));
            /**
             * @分段数目
             */
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.SEG_T_NUM.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.int2String(rect.evt_buf[i].SEG_T_num), 3));
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.SEG_T_IDX.getProperty()));
            stringBuilder.append(EnumEvt.setNewLine() + EnumEvt.setTab(3));

            for (int j = 0; j < rect.evt_buf[i].SEG_T_num; j++) {
                stringBuilder.append(EnumEvt.setEvtProperty(Utils.int2String(j)));

                if (rect.evt_buf[i].SEG_T_num - 1 == j) {
                    stringBuilder.append(Utils.int2String(rect.evt_buf[i].SEG_T_idx[j]));
                } else {
                    stringBuilder.append(EnumEvt.setEnter(Utils.int2String(rect.evt_buf[i].SEG_T_idx[j]), 4));
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @特征幅值
             */
            stringBuilder.append(EnumEvt.setEvtProperty(EnumEvt.U_MIN_NUM.getProperty()));
            stringBuilder.append(EnumEvt.setEnter(Utils.int2String(rect.evt_buf[i].u_min_num), 3));

            /**
             * @最小值位置
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ORDER_MIN_IDX.getProperty(), null,
                        rect.evt_buf[i].order_min_idx[k]);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @A相电压特征值
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.UA_MIN.getProperty(), rect.evt_buf[i].ua_min[k], null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @B相电压特征值
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.UB_MIN.getProperty(), rect.evt_buf[i].ub_min[k], null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @C相电压特征值
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.UC_MIN.getProperty(), rect.evt_buf[i].uc_min[k], null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @三相电压特征值
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.U3_MIN.getProperty(), rect.evt_buf[i].u3_min[k], null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @A相相位正跳变
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ANGLE_DIFF_AP.getProperty(), rect.evt_buf[i].angle_diff_ap[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @B相相位正跳变
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ANGLE_DIFF_BP.getProperty(), rect.evt_buf[i].angle_diff_bp[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @C相相位正跳变
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ANGLE_DIFF_CP.getProperty(), rect.evt_buf[i].angle_diff_cp[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @A相相位负跳变
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ANGLE_DIFF_AN.getProperty(), rect.evt_buf[i].angle_diff_an[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @B相相位负跳变
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ANGLE_DIFF_BN.getProperty(), rect.evt_buf[i].angle_diff_bn[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @C相相位负跳变
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.ANGLE_DIFF_CN.getProperty(), rect.evt_buf[i].angle_diff_cn[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @不平衡度
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.BPH_MAX_VALUE.getProperty(), rect.evt_buf[i].bph_max_value[k],
                        null);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @暂降原因
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.QVVR_CATA_CAUSE.getProperty(), null,
                        rect.evt_buf[i].qvvr_cata_cause[k]);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @暂降类型
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.QVVR_CATA_TYPE.getProperty(), null,
                        rect.evt_buf[i].qvvr_cata_type[k]);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(3), 3));

            /**
             * @暂降相别
             */
            for (int k = 0; k < rect.evt_buf[i].u_min_num; k++) {
                setEigenVlaue(k, stringBuilder, EnumEvt.QVVR_PHASETYPE.getProperty(), null,
                        rect.evt_buf[i].qvvr_phasetype[k]);

                if (rect.evt_buf[i].u_min_num - 1 == k) {
                    stringBuilder.delete(stringBuilder.lastIndexOf(","), stringBuilder.length() - 1);
                }
            }

            stringBuilder.append(EnumEvt.setClose(3));

            if (rect.evt_num - 1 == i) {
                stringBuilder.append(EnumEvt.setClose(2));
                stringBuilder.append("]" + EnumEvt.ENTER.getProperty());
            } else {
                stringBuilder.append(EnumEvt.setEnter(EnumEvt.setClose(2), 2));
            }
        }

        stringBuilder.append("}");
        jsonFile.writeFile(stringBuilder.toString());
    }

    /**
     * @特征值处理
     */
    public void setEigenVlaue(int len, StringBuilder stringBuilder, String string, Float f, Integer integer) {
        if (0 == len) {
            stringBuilder.append(EnumEvt.setEvtProperty(string));
            stringBuilder.append(EnumEvt.setNewLine() + EnumEvt.setTab(3));
        }

        stringBuilder.append(EnumEvt.setEvtProperty(Integer.toString(len)));

        if (null == f) {
            stringBuilder.append(EnumEvt.setEnter(Utils.int2String(integer.intValue()), 4));
        } else {
            stringBuilder.append(EnumEvt.setEnter(Utils.float2String(f.floatValue()), 4));
        }
    }

    /**
     * @return
     * @throws Exception
     * @事件进行高级算法处理：从文件中获取数据展示
     */
    public void getDataFromFile(Date trigeTime) throws Exception {
        translateData(jsonFile.string2T(jsonFile.readFile()), trigeTime);
    }

    public void initBackData(int len) {
        len = (len == 0 ? 1 : len);

        entityAdvancedData.evt_buf = new BackData[len];

        for (int i = 0; i < len; i++) {
            entityAdvancedData.evt_buf[i] = new BackData();
        }
    }

    public void translateData(JSONObject jsonObject, Date trigeTime) throws Exception {
        int len = Utils.getIntValue(jsonObject.get(EnumEvt.EVT_NUM.getProperty()).toString(), 0);

        entityAdvancedData.backNumber = len;
        //初始化EntityAdvancedData的BackData数据
        initBackData(len);

        for (int offset = 0; offset < len; offset++) {
            BackData backData = new BackData();
            /**
             * @波形起始点(3相)
             */
            backData.POW_a = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.POW_A.getProperty()).toString(), 0.0f);
            backData.POW_b = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.POW_B.getProperty()).toString(), 0.0f);
            backData.POW_c = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.POW_C.getProperty()).toString(), 0.0f);

            /**
             * @跳变段电压变化
             */
            backData.Voltagechange_Va = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.VOLTAGECHANGE_VA.getProperty()).toString(), 0.0f);
            backData.Voltagechange_Vb = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.VOLTAGECHANGE_VB.getProperty()).toString(), 0.0f);
            backData.Voltagechange_Vc = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.VOLTAGECHANGE_VC.getProperty()).toString(), 0.0f);

            /**
             * @持续时间
             */
            backData.hold_time_rms = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.HOLD_TIME_RMS.getProperty()).toString(), 0.0f);
            backData.hold_time_dq = Utils.getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.HOLD_TIME_DQ.getProperty()).toString(), 0.0f);

            /**
             * @分段信息
             */
            backData.SEG_T_num = Utils.getIntValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.SEG_T_NUM.getProperty()).toString(), 0);

            for (int i = 0; i < backData.SEG_T_num; i++) {
                backData.SEG_T_idx[i] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.SEG_T_IDX.getProperty()).get(Integer.toString(i)).toString(),
                        -1);
            }

            /**
             * @特征幅值
             */
            backData.u_min_num = Utils.getIntValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                    .getJSONObject(offset).get(EnumEvt.U_MIN_NUM.getProperty()).toString(), 0);

            for (int j = 0; j < backData.u_min_num; j++) {
                backData.ua_min[j] = Utils
                        .getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.UA_MIN.getProperty()).get(Integer.toString(j)).toString(), 0.0f);
                backData.ub_min[j] = Utils
                        .getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.UB_MIN.getProperty()).get(Integer.toString(j)).toString(), 0.0f);
                backData.uc_min[j] = Utils
                        .getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.UC_MIN.getProperty()).get(Integer.toString(j)).toString(), 0.0f);
                backData.u3_min[j] = Utils
                        .getFloatValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.U3_MIN.getProperty()).get(Integer.toString(j)).toString(), 0.0f);

                /**
                 * @相位跳变
                 */
                backData.angle_diff_an[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.ANGLE_DIFF_AN.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);
                backData.angle_diff_bn[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.ANGLE_DIFF_BN.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);
                backData.angle_diff_cn[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.ANGLE_DIFF_CN.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);
                backData.angle_diff_ap[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.ANGLE_DIFF_AP.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);
                backData.angle_diff_bp[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.ANGLE_DIFF_BP.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);
                backData.angle_diff_cp[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.ANGLE_DIFF_CP.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);

                /**
                 * @不平衡度
                 */
                backData.bph_max_value[j] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty()).getJSONObject(offset)
                                .getJSONObject(EnumEvt.BPH_MAX_VALUE.getProperty()).get(Integer.toString(j)).toString(),
                        0.0f);

                /**
                 * @暂降原因
                 */
                backData.qvvr_cata_cause[j] = Utils.getIntValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                        .getJSONObject(offset).getJSONObject(EnumEvt.QVVR_CATA_CAUSE.getProperty())
                        .get(Integer.toString(j)).toString(), -1);

                /**
                 * @暂降类型
                 */
                backData.qvvr_cata_type[j] = Utils.getIntValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                        .getJSONObject(offset).getJSONObject(EnumEvt.QVVR_CATA_TYPE.getProperty())
                        .get(Integer.toString(j)).toString(), -1);

                /**
                 * @暂降相别
                 */
                backData.qvvr_phasetype[j] = Utils.getIntValue(jsonObject.getJSONArray(EnumEvt.EVT_BUF.getProperty())
                        .getJSONObject(offset).getJSONObject(EnumEvt.QVVR_PHASETYPE.getProperty())
                        .get(Integer.toString(j)).toString(), -1);
            }

            entityAdvancedData.evt_buf[offset] = backData;
        }
      /*  if(jsonObject.containsKey(EnumEvt.QVVR_DIRECTION_INFO.getProperty())){
            if (!jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).isEmpty()) {
                entityAdvancedData.power_before_buf = new PowerData[1];
                PowerData power = new PowerData();
                float fundP[] = new float[5];
                fundP[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("A").toString(), 0.0f);
                fundP[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("B").toString(), 0.0f);
                fundP[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("C").toString(), 0.0f);
                fundP[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("total").toString(), 0.0f);
                fundP[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("zero").toString(), 0.0f);
                power.setFundP(fundP);

                float fundQ[] = new float[5];
                fundQ[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("A").toString(), 0.0f);
                fundQ[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("B").toString(), 0.0f);
                fundQ[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("C").toString(), 0.0f);
                fundQ[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("total").toString(), 0.0f);
                fundQ[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("zero").toString(), 0.0f);
                power.setFundQ(fundQ);

                float fundS[] = new float[5];
                fundS[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("A").toString(), 0.0f);
                fundS[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("B").toString(), 0.0f);
                fundS[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("C").toString(), 0.0f);
                fundS[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("total").toString(), 0.0f);
                fundS[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_BEFORE_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("zero").toString(), 0.0f);
                power.setFundS(fundS);
               // entityAdvancedData.power_before_buf[0] = power;
            }
        }*/

        if(jsonObject.containsKey(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty())){
            if (!jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).isEmpty()) {
                entityAdvancedData.power_after_buf = new PowerData[1];
                PowerData power = new PowerData();
                float fundP[] = new float[5];
                fundP[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("A").toString(), 0.0f);
                fundP[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("B").toString(), 0.0f);
                fundP[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("C").toString(), 0.0f);
                fundP[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("total").toString(), 0.0f);
                fundP[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_P.getProperty()).get("zero").toString(), 0.0f);
                power.setFundP(fundP);

                float fundQ[] = new float[5];
                fundQ[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("A").toString(), 0.0f);
                fundQ[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("B").toString(), 0.0f);
                fundQ[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("C").toString(), 0.0f);
                fundQ[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("total").toString(), 0.0f);
                fundQ[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_Q.getProperty()).get("zero").toString(), 0.0f);
                power.setFundQ(fundQ);

                float fundS[] = new float[5];
                fundS[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("A").toString(), 0.0f);
                fundS[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("B").toString(), 0.0f);
                fundS[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("C").toString(), 0.0f);
                fundS[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("total").toString(), 0.0f);
                fundS[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.POWER_QVVR_AFTER_BUF.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.FUND_S.getProperty()).get("zero").toString(), 0.0f);
                power.setFundS(fundS);
                entityAdvancedData.power_after_buf[0] = power;
            }
        }
        if(jsonObject.containsKey(EnumEvt.QVVR_DIRECTION_INFO.getProperty())){
            if (!jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).isEmpty()) {
                entityAdvancedData.qvvr_direction_info = new DirectionData[1];
                DirectionData direction = new DirectionData();
                int trigTime[] = new int[7];
                trigTime[0] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("year").toString(), 0);
                trigTime[1] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("month").toString(), 0);
                trigTime[2] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("date").toString(), 0);
                trigTime[3] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("hour").toString(), 0);
                trigTime[4] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("minute").toString(), 0);
                trigTime[5] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("second").toString(), 0);
                trigTime[6] = Utils.getIntValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("millisecond").toString(), 0);
                direction.setTrigTime(trigTime);

                float qvvrBeforeRms[] = new float[6];
                qvvrBeforeRms[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_RMS.getProperty()).get("Fund_Ua").toString(), 0.0f);
                qvvrBeforeRms[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_RMS.getProperty()).get("Fund_Ub").toString(), 0.0f);
                qvvrBeforeRms[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_RMS.getProperty()).get("Fund_Uc").toString(), 0.0f);
                qvvrBeforeRms[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_RMS.getProperty()).get("Fund_Ia").toString(), 0.0f);
                qvvrBeforeRms[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_RMS.getProperty()).get("Fund_Ib").toString(), 0.0f);
                qvvrBeforeRms[5] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_RMS.getProperty()).get("Fund_Ic").toString(), 0.0f);
                direction.setQvvrBeforeRms(qvvrBeforeRms);

                float qvvrBeforeZk[] = new float[6];
                qvvrBeforeZk[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_ZK.getProperty()).get("Za").toString(), 0.0f);
                qvvrBeforeZk[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_ZK.getProperty()).get("Zb").toString(), 0.0f);
                qvvrBeforeZk[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_ZK.getProperty()).get("Zc").toString(), 0.0f);
                qvvrBeforeZk[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_ZK.getProperty()).get("Zab").toString(), 0.0f);
                qvvrBeforeZk[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_ZK.getProperty()).get("Zbc").toString(), 0.0f);
                qvvrBeforeZk[5] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_BEFORE_ZK.getProperty()).get("Zca").toString(), 0.0f);
                direction.setQvvrBeforeZk(qvvrBeforeZk);

                float qvvrOccurRms[] = new float[6];
                qvvrOccurRms[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_RMS.getProperty()).get("Fund_Ua").toString(), 0.0f);
                qvvrOccurRms[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_RMS.getProperty()).get("Fund_Ub").toString(), 0.0f);
                qvvrOccurRms[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_RMS.getProperty()).get("Fund_Uc").toString(), 0.0f);
                qvvrOccurRms[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_RMS.getProperty()).get("Fund_Ia").toString(), 0.0f);
                qvvrOccurRms[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_RMS.getProperty()).get("Fund_Ib").toString(), 0.0f);
                qvvrOccurRms[5] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_RMS.getProperty()).get("Fund_Ic").toString(), 0.0f);
                direction.setQvvrOccurRms(qvvrOccurRms);

                float qvvrOccurZk[] = new float[6];
                qvvrOccurZk[0] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_ZK.getProperty()).get("Za").toString(), 0.0f);
                qvvrOccurZk[1] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_ZK.getProperty()).get("Zb").toString(), 0.0f);
                qvvrOccurZk[2] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_ZK.getProperty()).get("Zc").toString(), 0.0f);
                qvvrOccurZk[3] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_ZK.getProperty()).get("Zab").toString(), 0.0f);
                qvvrOccurZk[4] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_ZK.getProperty()).get("Zbc").toString(), 0.0f);
                qvvrOccurZk[5] = Utils.getFloatValue(
                        jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                                .getJSONObject(EnumEvt.QVVR_OCCUR_ZK.getProperty()).get("Zca").toString(), 0.0f);
                direction.setQvvrOccurZk(qvvrOccurZk);

                String qvvrPosInfo[] = new String[1];
                qvvrPosInfo[0] = jsonObject.getJSONArray(EnumEvt.QVVR_DIRECTION_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.QVVR_POS_INFO.getProperty()).get("direction").toString();
                direction.setQvvrPosInfo(qvvrPosInfo);

                entityAdvancedData.qvvr_direction_info[0] = direction;
            }
        }
        if(jsonObject.containsKey(EnumEvt.QVVR_UTBL_INFO.getProperty())){
            if (!jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).isEmpty()) {
                entityAdvancedData.qvvr_utbl_info = new UtblData[1];
                UtblData direction = new UtblData();
                long trigTime[] = new long[1];
                String year = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("year").toString();
                String month = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("month").toString();
                String day = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("date").toString();
                String hour = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("hour").toString();
                String min = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("minute").toString();
                String sec = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("second").toString();
                String mil = jsonObject.getJSONArray(EnumEvt.QVVR_UTBL_INFO.getProperty()).getJSONObject(0)
                        .getJSONObject(EnumEvt.TRIG_TIME.getProperty()).get("millisecond").toString();
                SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                Date utbl = dfs.parse(year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + "." + mil);
                long between = utbl.getTime() - trigeTime.getTime();
                trigTime[0] = between;
                direction.setTrigTime(trigTime);
                entityAdvancedData.qvvr_utbl_info[0] = direction;
            }
        }





    }

    @Override
    public Integer getPtType(String index) {
        return eventDetailMapper.getPtType(index);
    }


    /**
     * 筛选后台绘图所需的瞬时、RMS等一次值数据
     */
    @Override
    public List<WaveDataDetail> filteWaveData(WaveData waveData) {
        List<WaveDataDetail> waveDataDetails = new ArrayList<>();
        List<String> tmpWaveTitle = waveData.getTmpWaveTitle();
        boolean openTri = waveData.getOpenTri();
        /************ 一个图形的相别决定了图形个数 Modify by yexb -----start ***********/
        int picCounts = (tmpWaveTitle.size() - 1) / waveData.getiPhasic();
        /**处理相别的颜色**/
        List<String> colors = new ArrayList<>();
        //遍历图形个数
        for (int i = 0; i < picCounts; i++) {
            WaveDataDetail waveDataDetail = new WaveDataDetail();
            int iphasic = waveData.getiPhasic();
            switch (iphasic) {
                case 1:
                    waveDataDetail.setA(tmpWaveTitle.get(i + 1).substring(1));
                    waveDataDetail.setB("");
                    waveDataDetail.setC("");
                    colors.add("#DAA520");
                    colors.add("#fff");
                    colors.add("#fff");
                    break;
                case 2:
                    waveDataDetail.setA(tmpWaveTitle.get(i * 2 + 1).substring(1));
                    waveDataDetail.setB(tmpWaveTitle.get(i * 2 + 2).substring(1));
                    waveDataDetail.setC("");
                    colors.add("#DAA520");
                    colors.add("#2E8B57");
                    colors.add("#fff");
                    break;
                case 3:
                    waveDataDetail.setA(tmpWaveTitle.get(i * 3 + 1).substring(1));
                    waveDataDetail.setB(tmpWaveTitle.get(i * 3 + 2).substring(1));
                    waveDataDetail.setC(tmpWaveTitle.get(i * 3 + 3).substring(1));
                    colors.add("#DAA520");
                    colors.add("#2E8B57");
                    colors.add("#A52a2a");
                    break;
            }
            waveDataDetail.setColors(colors);

            Float xishu = waveData.getPt();
            if (tmpWaveTitle.get(waveData.getiPhasic() * i + 1).substring(0, 1).equalsIgnoreCase("U")) {
                waveDataDetail.setUnit("kV");
                xishu = xishu / 1000;
            } else {
                waveDataDetail.setUnit("A");
                xishu = waveData.getCt();
            }
            Float sfMax = 0f, sfMin = 0f, rfMax = 0f, rfMin = 0f;
            List<List<Float>> sAValue = new ArrayList<>(), sBValue = new ArrayList<>(), sCValue = new ArrayList<>(), rAValue = new ArrayList<>(), rBValue = new ArrayList<>(), rCValue = new ArrayList<>();
            List<List<Float>> sunData = waveData.getSunData();
            for (int j = 0; j < sunData.size(); j++) {
                float x = sunData.get(j).get(0);
                float shunFirstA = 0f, shunFirstB = 0f, shunFirstC = 0f;
                //根据相别来确认标题的名称
                for (int m = 0; m < waveData.getiPhasic(); m++) {
                    if (tmpWaveTitle.get(waveData.getiPhasic() * i + m + 1).substring(1).indexOf("A") > -1) {
                        float tmpShunFirstA = sunData.get(j).get(waveData.getiPhasic() * i + m + 1) * xishu;
                        shunFirstA = tmpShunFirstA;
                        sAValue.add(new ArrayList<Float>() {{
                            add(x);
                            add(tmpShunFirstA);
                        }});
                    }
                    if (tmpWaveTitle.get(waveData.getiPhasic() * i + m + 1).substring(1).indexOf("B") > -1) {
                        float tmpShunFirstB = sunData.get(j).get(waveData.getiPhasic() * i + m + 1) * xishu;
                        shunFirstB = tmpShunFirstB;
                        sBValue.add(new ArrayList<Float>() {{
                            add(x);
                            add(tmpShunFirstB);
                        }});
                    }
                    if (tmpWaveTitle.get(waveData.getiPhasic() * i + m + 1).substring(1).indexOf("C") > -1) {
                        float tmpShunFirstC = sunData.get(j).get(waveData.getiPhasic() * i + m + 1) * xishu;
                        shunFirstC = tmpShunFirstC;
                        sCValue.add(new ArrayList<Float>() {{
                            add(x);
                            add(tmpShunFirstC);
                        }});
                    }
                }
                sfMax = getMax(sfMax, shunFirstA, shunFirstB, shunFirstC);
                if (openTri) {
                    sfMin = getMinOpen(sfMin, shunFirstA, shunFirstC);
                } else {
                    sfMin = getMin(sfMin, shunFirstA, shunFirstB, shunFirstC);
                }
            }
            InstantF instantF = new InstantF();
            instantF.setaValue(sAValue);
            instantF.setbValue(sBValue);
            instantF.setcValue(sCValue);
            instantF.setMax(sfMax);
            instantF.setMin(sfMin);
            List<List<Float>> rmsData = waveData.getRmsData();
            for (int k = 0; k < rmsData.size(); k++) {
                float x = rmsData.get(k).get(0);
                float rmsFirstA = 0f, rmsFirstB = 0f, rmsFirstC = 0f;
                //根据相别来确认标题的名称
                for (int m = 0; m < waveData.getiPhasic(); m++) {
                    if (tmpWaveTitle.get(waveData.getiPhasic() * i + m + 1).substring(1).indexOf("A") > -1) {
                        float tmpRmsFirstA = rmsData.get(k).get(waveData.getiPhasic() * i + m + 1) * xishu;
                        rmsFirstA = tmpRmsFirstA;
                        rAValue.add(new ArrayList<Float>() {{
                            add(x);
                            add(tmpRmsFirstA);
                        }});
                    }
                    if (tmpWaveTitle.get(waveData.getiPhasic() * i + m + 1).substring(1).indexOf("B") > -1) {
                        float tmpRmsFirstB = rmsData.get(k).get(waveData.getiPhasic() * i + m + 1) * xishu;
                        rmsFirstB = tmpRmsFirstB;
                        rBValue.add(new ArrayList<Float>() {{
                            add(x);
                            add(tmpRmsFirstB);
                        }});
                    }
                    if (tmpWaveTitle.get(waveData.getiPhasic() * i + m + 1).substring(1).indexOf("C") > -1) {
                        float tmpRmsFirstC = rmsData.get(k).get(waveData.getiPhasic() * i + m + 1) * xishu;
                        rmsFirstC = tmpRmsFirstC;
                        rCValue.add(new ArrayList<Float>() {{
                            add(x);
                            add(tmpRmsFirstC);
                        }});
                    }
                }
                rfMax = getMax(sfMax, rmsFirstA, rmsFirstB, rmsFirstC);
                if (openTri) {
                    rfMin = getMinOpen(sfMin, rmsFirstA, rmsFirstC);
                } else {
                    rfMin = getMin(sfMin, rmsFirstA, rmsFirstB, rmsFirstC);
                }
            }
            RmsF rmsF = new RmsF();
            rmsF.setaValue(rAValue);
            rmsF.setbValue(rBValue);
            rmsF.setcValue(rCValue);
            rmsF.setMax(rfMax);
            rmsF.setMin(rfMin);

            waveDataDetail.setInstantF(instantF);
            waveDataDetail.setRmsF(rmsF);
            waveDataDetails.add(waveDataDetail);
        }
        /************  Modify by yexb -----end ***********/
        return waveDataDetails;
    }

    private Float getMinOpen(Float temp, float tempA, float tempB) {
        temp = temp < tempA ? temp : tempA;
        temp = temp < tempB ? temp : tempB;
        return temp;
    }


    private float getMin(float temp, float tempA, float tempB, float tempC) {
        temp = temp < tempA ? temp : tempA;
        temp = temp < tempB ? temp : tempB;
        temp = temp < tempC ? temp : tempC;
        return temp;
    }

    private float getMax(float temp, float tempA, float tempB, float tempC) {
        temp = temp > tempA ? temp : tempA;
        temp = temp > tempB ? temp : tempB;
        temp = temp > tempC ? temp : tempC;
        return temp;
    }

    @Override
    public String createRMSTitle(String title, WaveDataDetail waveDataDetail) throws Exception {
        HashMap<String, Object> datas = new HashMap<>();
        datas.put("title", title);
        return createRMSPic(datas, waveDataDetail);
    }

    @Override
    public String createRMS(WaveDataDetail waveDataDetail) throws Exception {
        HashMap<String, Object> datas = new HashMap<>();
        datas.put("title", "");
        return createRMSPic(datas, waveDataDetail);
    }


    private String createRMSPic(HashMap<String, Object> datas, WaveDataDetail waveDataDetail) throws Exception {
        datas.put("adata", JSONArray.fromObject(waveDataDetail.getRmsF().getaValue()).toString());
        datas.put("bdata", JSONArray.fromObject(waveDataDetail.getRmsF().getbValue()).toString());
        datas.put("cdata", JSONArray.fromObject(waveDataDetail.getRmsF().getcValue()).toString());
        datas.put("unit", waveDataDetail.getUnit());
        datas.put("a", waveDataDetail.getA());
        datas.put("b", waveDataDetail.getB());
        datas.put("c", waveDataDetail.getC());
        datas.put("colors", JSONArray.fromObject(waveDataDetail.getColors()).toString());
        datas.put("cshow", Boolean.toString(!waveDataDetail.getOpen()));
        String option = "";
        if (appConfig.getSystemFlag().equals("PQS9200")) {
            option = FreemarkerUtil.generateString("rms.ftl", "com/pqs9200/template", datas);
        } else {
            option = FreemarkerUtil.generateString("rms.ftl", "com/shining/cloud/template", datas);
        }
        return EchartsUtil.generateEchartsBase64(option, "6667");
    }

    @Override
    public String createShunTitle(String title, WaveDataDetail waveDataDetail) throws Exception {
        HashMap<String, Object> datas = new HashMap<>();
        datas.put("title", title);
        return createPic(datas, waveDataDetail);
    }

    @Override
    public String createShun(WaveDataDetail waveDataDetail) throws Exception {
        HashMap<String, Object> datas = new HashMap<>();
        datas.put("title", "");
        return createPic(datas, waveDataDetail);
    }

    private String createPic(HashMap<String, Object> datas, WaveDataDetail waveDataDetail) throws Exception {
        datas.put("adata", JSONArray.fromObject(waveDataDetail.getInstantF().getaValue()).toString());
        datas.put("bdata", JSONArray.fromObject(waveDataDetail.getInstantF().getbValue()).toString());
        datas.put("cdata", JSONArray.fromObject(waveDataDetail.getInstantF().getcValue()).toString());
        datas.put("unit", waveDataDetail.getUnit());
        datas.put("max", String.valueOf(waveDataDetail.getInstantF().getMax()));
        datas.put("min", String.valueOf(waveDataDetail.getInstantF().getMin()));
        datas.put("a", waveDataDetail.getA());
        datas.put("b", waveDataDetail.getB());
        datas.put("c", waveDataDetail.getC());
        datas.put("colors", JSONArray.fromObject(waveDataDetail.getColors()).toString());
        datas.put("cshow", Boolean.toString(!waveDataDetail.getOpen()));
        String option = "";
        if (appConfig.getSystemFlag().equals("PQS9200")) {
            option = FreemarkerUtil.generateString("wave.ftl", "com/pqs9200/template", datas);
        } else {
            option = FreemarkerUtil.generateString("wave.ftl", "com/shining/cloud/template", datas);
        }
        return EchartsUtil.generateEchartsBase64(option, "6667");
    }

    private WaveSvg createRmsSPic(String title, WaveDataDetail waveDataDetail) throws Exception {
        WaveSvg waveSvg = new WaveSvg();
        waveSvg.setDataA(JSONArray.fromObject(waveDataDetail.getRmsF().getaValue().toString()));
        waveSvg.setDataB(JSONArray.fromObject(waveDataDetail.getRmsF().getbValue().toString()));
        waveSvg.setDataC(JSONArray.fromObject(waveDataDetail.getRmsF().getcValue().toString()));
        waveSvg.setTitle(title);
        waveSvg.setUnit(waveDataDetail.getUnit());
        return waveSvg;
    }

    private WaveSvg createShunPic(String title, WaveDataDetail waveDataDetail) throws Exception {
        WaveSvg waveSvg = new WaveSvg();
        waveSvg.setDataA(JSONArray.fromObject(waveDataDetail.getInstantF().getaValue().toString()));
        waveSvg.setDataB(JSONArray.fromObject(waveDataDetail.getInstantF().getbValue().toString()));
        waveSvg.setDataC(JSONArray.fromObject(waveDataDetail.getInstantF().getcValue().toString()));
        waveSvg.setMax(waveDataDetail.getInstantF().getMax());
        waveSvg.setMin(waveDataDetail.getInstantF().getMin());
        waveSvg.setTitle(title);
        waveSvg.setUnit(waveDataDetail.getUnit());
        return waveSvg;
    }

    @Override
    public WaveSvg createShunS(WaveDataDetail waveDataDetail) throws Exception {
        return createShunPic("", waveDataDetail);
    }

    @Override
    public WaveSvg createRMSS( WaveDataDetail waveDataDetail) throws Exception {

        return createRmsSPic("", waveDataDetail);
    }

    @Override
    public WaveSvg createShunSTitle(String title, WaveDataDetail waveDataDetail) throws Exception {

        return createShunPic(title, waveDataDetail);
    }

    @Override
    public WaveSvg createRMSSTitle(String title, WaveDataDetail waveDataDetail) throws Exception {

        return createRmsSPic(title, waveDataDetail);
    }
}
