package com.pqs9300.controller.report;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.njcn.enums.LogTypeEnum;
import com.njcn.mapper.configuration.OverLimitMapper;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.RedisDB;
import com.njcn.pojo.commons.device.MachineAccount;
import com.njcn.pojo.configuration.OverLimit;
import com.njcn.pojo.configuration.RptPloy;
import com.njcn.service.configuration.LineService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.AppConfig;
import com.njcn.utils.COverlimit;
import com.njcn.utils.InstantiateUtil;
import com.njcn.utils.PubUtils;
import com.njcn.utils.redis.JedisManager;
import com.pqs9300.pojo.report.Condition;
import com.pqs9300.pojo.report.FpyDataByType;
import com.pqs9300.pojo.report.OverLimitInfo;
import com.pqs9300.pojo.report.Pass;
import com.pqs9300.pojo.report.PloySetting;
import com.pqs9300.pojo.report.ReportTarget;
import com.pqs9300.pojo.report.ReportValue;
import com.pqs9300.pojo.report.enumReport.EnumPass;
import com.pqs9300.service.report.ExcelReportService;
import com.pqs9300.service.report.GetDistortionDataService;
import com.pqs9300.service.report.GetFlickerDataService;
import com.pqs9300.service.report.GetFpyService;
import com.pqs9300.service.report.GetFrequencyDataService;
import com.pqs9300.service.report.GetICurrentService;
import com.pqs9300.service.report.GetOverLimitDataService;
import com.pqs9300.service.report.GetPowerDataService;
import com.pqs9300.service.report.GetThreephaseDataService;
import com.pqs9300.service.report.GetVdeviationDataService;
import com.pqs9300.service.report.GetVirtualDataService;
import com.pqs9300.service.report.GetVoltageRateService;
import com.pqs9300.util.ExcelUtil;
import com.pqs9300.util.TransformTime;

import net.sf.json.JSONObject;

@RestController
public class ExcelReportController {
    private static final Logger logger = LoggerFactory.getLogger(ExcelReportController.class);

    @Autowired
    private GetVirtualDataService getVirtualDataService;
    @Autowired
    private GetPowerDataService getPowerDataService;
    @Autowired
    private GetOverLimitDataService getOverLimitDataService;
    @Autowired
    private GetFlickerDataService getFlickerDataService;
    @Autowired
    private GetDistortionDataService getDistortionDataService;
    @Autowired
    private GetVdeviationDataService getVdeviationDataService;
    @Autowired
    private GetFrequencyDataService getFrequencyDataService;
    @Autowired
    private GetThreephaseDataService getThreephaseDataService;
    @Autowired
    private GetICurrentService getICurrentService;
    @Autowired
    private GetVoltageRateService getVoltageRateService;
    @Autowired
    private ExcelReportService excelReportService;
    @Autowired
    private GetFpyService getFpyService;
    @Autowired
    private AppConfig appConfig;
    @Autowired
    private UserLogDetailService userLog;
    @Autowired
    private COverlimit cOverlimit;
    @Autowired
    private LineService lineService;
    @Autowired
    OverLimitMapper overLimitMapper;
    @Autowired
    private JedisManager jedisManager;

    private InstantiateUtil<String> instantiateUtil;
    private InstantiateUtil<List<ReportTarget>> listInstantiateUtil;
    private InstantiateUtil<List<PloySetting>> listPloysetting;
    private InstantiateUtil<List<FpyDataByType>> listFpydata;
    private InstantiateUtil<OverLimitInfo> listOverlimit;
    private InstantiateUtil<Condition> conditionInstantiateUtil;
    //选中的模板信息
    private byte[] ployGuid;//##为默认模板(展示所有指标)
    //模板设置
    private byte[] ploy;

    //限值
    private byte[] overlimit;
    //缓存查询条件
    private byte[] condi;
    //Excel实例对象
    Map<String, ExcelUtil> excelUtilMap;
    //非谐波数据
    private byte[] virtual;
    private byte[] power;
    private byte[] flicker;
    private byte[] distortion;
    private byte[] vdeviation;
    private byte[] frequency;
    private byte[] threephase;
    //谐波电压数据
    private byte[] voltageRate;
    //谐波电流数据
    private byte[] iCurrent;
    //合格率数据
    private byte[] fpy;


    public ExcelReportController() {
        instantiateUtil = new InstantiateUtil<>();
        listInstantiateUtil = new InstantiateUtil<>();
        listPloysetting = new InstantiateUtil<>();
        listFpydata = new InstantiateUtil<>();
        listOverlimit = new InstantiateUtil<>();
        conditionInstantiateUtil = new InstantiateUtil<>();
        ployGuid = instantiateUtil.instantiate("ployGuid");
        ploy = instantiateUtil.instantiate("ploy");
        overlimit = instantiateUtil.instantiate("overlimit");
        condi = instantiateUtil.instantiate("condi");
        virtual = instantiateUtil.instantiate("virtual");
        power = instantiateUtil.instantiate("power");
        flicker = instantiateUtil.instantiate("flicker");
        distortion = instantiateUtil.instantiate("distortion");
        vdeviation = instantiateUtil.instantiate("vdeviation");
        frequency = instantiateUtil.instantiate("frequency");
        threephase = instantiateUtil.instantiate("threephase");
        voltageRate = instantiateUtil.instantiate("voltageRate");
        iCurrent = instantiateUtil.instantiate("iCurrent");
        fpy = instantiateUtil.instantiate("fpy");
        excelUtilMap = new HashMap<>();
    }

    //获取模板配置信息
    @PostMapping("getPloyData")
    public HttpResult getPloyData() {
        HttpResult httpResult = new HttpResult();
        List<RptPloy> list;

        try {
            list = excelReportService.getPloyData();
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("模板获取失败.");
            return httpResult;
        }

        httpResult.setCode(200);
        httpResult.setMessage("模板获取成功.");
        httpResult.setBody(list);
        return httpResult;
    }

    //设置选中的模板
    @PostMapping("setPloy")
    @ResponseBody
    public HttpResult setPloy(String guid) {
        HttpResult httpResult = new HttpResult();
        String userIndex = TokenManager.getUserId();
        byte[] value = instantiateUtil.instantiate(guid);
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, ployGuid, value, RedisDB.SHORT_TIME);

        if (guid.equals("##")) {
            httpResult.setCode(200);
            httpResult.setBody(null);
            return httpResult;
        }

        try {
            List<PloySetting> list = excelReportService.setPloy(guid);
            httpResult.setBody(list);
            byte[] valueset = listPloysetting.instantiate(list);
            jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, ploy, valueset, RedisDB.SHORT_TIME);
        } catch (Exception e) {
            httpResult.setCode(500);
            return httpResult;
        }

        httpResult.setCode(200);
        return httpResult;
    }

    @PostMapping(value = "/createExcel")
    public HttpResult createExcel(String pointInfo) {
        HttpResult httpResult = new HttpResult();
        JSONObject jsonObject = JSONObject.fromObject(pointInfo);
        JSONObject json = (JSONObject) jsonObject.get("body");
        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);

        ExcelUtil excelUtil = new ExcelUtil();
        excelUtil.createWorkBook();
        excelUtil.createSheet("非谐波");
        excelUtil.createSheet("谐波电压");
        excelUtil.createSheet("谐波电流");
        excelUtil.createSheet("合格率");
        excelUtil.setTableName(jsonObject.get("Name").toString());
        OverLimitInfo overLimitIn = listOverlimit.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, overlimit));
        excelUtil.setOverLimitInfo(overLimitIn);

        for (XSSFSheet sheet : excelUtil.getListSheet()) {
            excelUtil.createHeader(jsonObject, json, sheet);
        }

        String strPloyGuid = (instantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, ployGuid)) == null ? "##" : instantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, ployGuid)));
        List<PloySetting> listPloy = instantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, ploy));
        List<ReportTarget> listVirtual = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, virtual));
        List<ReportTarget> listPower = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, power));
        List<ReportTarget> listFlicker = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, flicker));
        List<ReportTarget> listDistortion = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, distortion));
        List<ReportTarget> listVdeviation = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, vdeviation));
        List<ReportTarget> listFrequency = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, frequency));
        List<ReportTarget> listThreephase = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, threephase));
        List<ReportTarget> listVoltageRate = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, voltageRate));
        List<ReportTarget> listICurrent = listInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, iCurrent));
        List<FpyDataByType> listFpy = listFpydata.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, fpy));

        //非谐波电流
        excelUtil.writeNonHarmonics(excelUtil.getListSheet().get(0), listPloy, strPloyGuid, listVirtual, listPower, listFlicker, listDistortion, listVdeviation, listFrequency, listThreephase);
        //谐波电压
        excelUtil.writeHarmonics(excelUtil.getListSheet().get(1), listPloy, strPloyGuid, listVoltageRate, true);
        //谐波电流
        excelUtil.writeHarmonics(excelUtil.getListSheet().get(2), listPloy, strPloyGuid, listICurrent, false);
        //合格率
        excelUtil.writeFpyData(excelUtil.getListSheet().get(3), listPloy, strPloyGuid, listFpy);

        excelUtilMap.put(userIndex, excelUtil);

        httpResult.setCode(200);

        return httpResult;
    }

    @GetMapping(value = "/exportExcel")
    public void exportExcel(HttpServletResponse response, HttpServletRequest request) {
        String userIndex = TokenManager.getUserId();
        ExcelUtil excelUtil = excelUtilMap.get(userIndex);
        excelUtil.exportFile(excelUtil.getWorkbook(), response, request);
    }

    private void splitTime(String strTime, List<TransformTime> list) throws Exception {
        if (strTime.indexOf(" 至 ") == -1) {
            return;
        }

        String strArray[] = strTime.split(" 至 ");
        TransformTime TransformTime = new TransformTime(strArray[0], strArray[1]);
        TransformTime.string2Date();

        list.add(TransformTime);
    }

    private void transCondition(Condition condition, JSONObject jsonObject) throws Exception { //异常抛给上层处理
        String startTime = jsonObject.get("startTime").toString();
        String endTime = jsonObject.get("endTime").toString();
        String first = jsonObject.get("first").toString();
        String second = jsonObject.get("second").toString();
        String thrid = jsonObject.get("thrid").toString();
        String state = jsonObject.get("state").toString();
        Integer pointIndex = Integer.valueOf(jsonObject.get("pointIndex").toString());
        String mode = jsonObject.get("mode").toString();

        TransformTime TransformTime = new TransformTime(startTime, endTime);
        TransformTime.string2Date();
        condition.setStart(TransformTime.getStart());
        condition.setEnd(TransformTime.getEnd());
        condition.setPointIndex(pointIndex);
        condition.setB();
        condition.setMode(mode);

        if (state.equals("true")) {
            splitTime(first, condition.getExceptTime());
            splitTime(second, condition.getExceptTime());
            splitTime(thrid, condition.getExceptTime());
        }
    }

    @PostMapping(value = "/getOverLimitData")
    public HttpResult getOverLimitData(String timeObj, HttpServletRequest request) {
        HttpResult httpResult = new HttpResult();
        JSONObject jsonObject = JSONObject.fromObject(timeObj);
        Condition condition = new Condition(appConfig);

        try {
            transCondition(condition, jsonObject);
        } catch (Exception e) {
            condition = null;
            logger.error("getLimitData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            userLog.getLog("查询excel报表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }
        condition.setB(false);
        try {
            OverLimit over = overLimitMapper.selectByPrimaryKey(condition.getPointIndex().longValue());
            MachineAccount machineAccount = lineService.getLineName(condition.getPointIndex());

            if (over == null) {
                cOverlimit.addOverLimit(condition.getPointIndex(), machineAccount.getScale());
            }
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("监测点限值无数据.");
            userLog.getLog("查询excel报表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }

        OverLimitInfo overLimitInfo = getOverLimitDataService.getOverLimitData(condition);
        overLimitInfo.setMode(condition.getMode());

        List<String> list;
        switch (condition.getMode()) {
            case "0":
                list = new ArrayList<>();
                list.add("A");
                list.add("B");
                list.add("C");
                overLimitInfo.setList(list);
                break;
            case "1":
                list = new ArrayList<>();
                list.add("AB");
                list.add("BC");
                list.add("CA");
                overLimitInfo.setList(list);
                break;
            case "2":
                list = new ArrayList<>();
                list.add("AB");
                list.add("BC");
                list.add("-");
                overLimitInfo.setList(list);
                break;
        }


        condition.setCount(overLimitInfo.getCount());
        condition.setPltCount(overLimitInfo.getPltCount());
        condition.setPstCount(overLimitInfo.getPstCount());

        if (0 == overLimitInfo.getOverLimitRate().size()) {
            httpResult.setCode(500);
            httpResult.setMessage("监测点限值无数据.");
            userLog.getLog("查询excel报表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            return httpResult;
        }

        httpResult.setCode(200);
        httpResult.setBody(overLimitInfo);
        userLog.getLog("查询excel报表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, overlimit, listOverlimit.instantiate(overLimitInfo), RedisDB.SHORT_TIME);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, condi, conditionInstantiateUtil.instantiate(condition), RedisDB.SHORT_TIME);

        return httpResult;
    }

    private void transformData(List<ReportTarget> list, List<ReportValue> listValue, List<Pass> listOverlimit, boolean... b) {
        int offset = b[0] ? 3 : 1;
        offset = (b.length == 2 && !b[1]) ? 4 : offset;
        int i = 0;

        for (int index = 0; index < listValue.size(); index += offset) {
            ReportTarget reportTarget = new ReportTarget();
            List<ReportValue> list1 = new ArrayList<>();
            list1.addAll(listValue.subList(index, index + offset));
            reportTarget.setList(list1);
            Float overLimit = listOverlimit.get(i).getOverLimit();
            reportTarget.setOverLimit(overLimit);

            switch (listOverlimit.get(i).getCode()) {
                case 1:
                    for (int k = 0; k < reportTarget.getList().size(); k++) {
                        if (null == reportTarget.getList().get(k).getFmaxValue()) {
                            break;
                        } else if (reportTarget.getList().get(k).getFmaxValue().floatValue() > overLimit.floatValue()) {
                            reportTarget.setPass(EnumPass.NOPASS.getCode());
                            break;
                        } else {
                            reportTarget.setPass(EnumPass.PASS.getCode());
                        }
                    }
                    break;
                case 2:
                    for (int k = 0; k < reportTarget.getList().size(); k++) {
                        if (null == reportTarget.getList().get(k).getMinValue()) {
                            break;
                        } else if (reportTarget.getList().get(k).getMinValue().floatValue() > overLimit.floatValue()) {
                            reportTarget.setPass(EnumPass.NOPASS.getCode());
                            break;
                        } else {
                            reportTarget.setPass(EnumPass.PASS.getCode());
                        }
                    }
                    break;
                case 3:
                    for (int k = 0; k < reportTarget.getList().size(); k++) {
                        if (null == reportTarget.getList().get(k).getMeanValue()) {
                            break;
                        } else if (reportTarget.getList().get(k).getMeanValue().floatValue() > overLimit.floatValue()) {
                            reportTarget.setPass(EnumPass.NOPASS.getCode());
                            break;
                        } else {
                            reportTarget.setPass(EnumPass.PASS.getCode());
                        }
                    }
                    break;
                case 4:
                    for (int k = 0; k < reportTarget.getList().size(); k++) {
                        if (null == reportTarget.getList().get(k).getCp95Value()) {
                            break;
                        } else if (reportTarget.getList().get(k).getCp95Value().floatValue() > overLimit.floatValue()) {
                            reportTarget.setPass(EnumPass.NOPASS.getCode());
                            break;
                        } else {
                            reportTarget.setPass(EnumPass.PASS.getCode());
                        }
                    }
                    break;
                default:
                    break;
            }

            i++;
            list.add(reportTarget);
        }
    }

    @PostMapping(value = "/getVirtualData")
    public HttpResult getVirtualData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();

        if (condition == null) {
            logger.error("getVirtualData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listVirtual;

        try {
            listVirtual = getVirtualDataService.getVirtualData(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("有效值获取失败");
            return httpResult;
        }


        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            Pass pass = new Pass(null);
            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listVirtual, list, true);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, virtual, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("有效值获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getPowerData")
    public HttpResult getPowerData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();

        if (condition == null) {
            logger.error("getPowerData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listPower;

        try {
            listPower = getPowerDataService.getPowerData(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("功率获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 4; i++) { //3-->4
            Pass pass = new Pass(null);
            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listPower, list, true, false);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, power, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("功率获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getflickerData")
    public HttpResult getFlickerData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getflickerData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listFlicker;

        try {
            listFlicker = getFlickerDataService.getFlickerData(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("电压闪变获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 2; i++) {
            Pass pass;

            if (i == 1) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getFlicker(), EnumPass.MAX.getCode());
            } else {
                pass = new Pass(null);
            }

            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listFlicker, list, true);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, flicker, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("电压闪变获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getdistortionData")
    public HttpResult getDistortionData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getDistortionData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listDistortion;

        try {
            listDistortion = getDistortionDataService.getDistortionData(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("畸变率获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 2; i++) {
            Pass pass;

            if (i == 0) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getUaberrance(), EnumPass.CP95.getCode());
            } else {
                pass = new Pass(null);
            }

            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listDistortion, list, true);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, distortion, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("畸变率获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getVdeviationData")
    public HttpResult getVdeviationData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getVdeviationData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listVdeviation;

        try {
            listVdeviation = getVdeviationDataService.getVdeviation(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("电压偏差获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 2; i++) {
            Pass pass;
            pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getVoltageDev(), EnumPass.MAX.getCode());
            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listVdeviation, list, true);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, vdeviation, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("电压偏差获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getFrequencyData")
    public HttpResult getFrequencyData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getFrequencyData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listFrequency;

        try {
            listFrequency = getFrequencyDataService.getFrequencyData(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("频率获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 2; i++) {
            Pass pass;

            if (i == 1) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getFreqDev(), EnumPass.MAX.getCode());
            } else {
                pass = new Pass(null);
            }

            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listFrequency, list, false);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, frequency, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("频率获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getThreephaseData")
    public HttpResult getThreephaseData() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getThreephaseData()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listThreephase;

        try {
            listThreephase = getThreephaseDataService.getThreephase(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("三相电压不平衡度获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 8; i++) {
            Pass pass;

            if (i == 0) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getUbalance(), EnumPass.CP95.getCode());
            } else {
                pass = new Pass(null);
            }

            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listThreephase, list, false);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, threephase, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("三相电压不平衡度获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getICurrent")
    public HttpResult getICurrent() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getICurrent()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }

        List<ReportValue> listICurrent;

        try {
            listICurrent = getICurrentService.getICurrent(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("谐波电流数据获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 50; i++) {
            Pass pass;

            if (i < overLimitInfo.getOverLimitRate().get(0).getIHarmList().size() + 1 && i > 0) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getIHarmList().get(i - 1), EnumPass.CP95.getCode());
            } else {
                pass = new Pass(null);
            }

            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listICurrent, list, true);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, iCurrent, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("谐波电流数据获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getVoltageRate")
    public HttpResult getVoltageRate() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();
        OverLimitInfo overLimitInfo = getOverlimitInfo();

        if (condition == null) {
            logger.error("getVoltageRate()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }
        condition.setB(false);
        List<ReportValue> listVoltageRate;

        try {
            listVoltageRate = getVoltageRateService.getVoltageRate(condition);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("谐波电压数据获取失败");
            return httpResult;
        }

        List<Pass> list = new ArrayList<>();

        for (int i = 0; i < 51; i++) {
            Pass pass;

            if (i == 0) {
                pass = new Pass(null);
            } else if (i == 50) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getUaberrance(), EnumPass.MAX.getCode());
            } else if (i < overLimitInfo.getOverLimitRate().get(0).getVHarmList().size() + 1) {
                pass = new Pass(overLimitInfo.getOverLimitRate().get(0).getVHarmList().get(i - 1), EnumPass.CP95.getCode());
            } else {
                pass = new Pass(null);
            }

            list.add(pass);
        }

        List<ReportTarget> list1 = new ArrayList<>();
        transformData(list1, listVoltageRate, list, true);

        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, voltageRate, listInstantiateUtil.instantiate(list1), RedisDB.SHORT_TIME);

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("谐波电压数据获取成功.");

        return httpResult;
    }

    @PostMapping(value = "/getFpy")
    public HttpResult getFpy() {
        HttpResult httpResult = new HttpResult();
        Condition condition = getCondition();

        if (condition == null) {
            logger.error("getFpy()方法时间转换失败.");
            httpResult.setCode(500);
            httpResult.setMessage("解析前台传递的时间有误");
            return httpResult;
        }
        condition.setB(false);
        List<FpyDataByType> list1 = new ArrayList<>();

        try {
            list1 = getFpyService.getFpy(condition);
            String userIndex = TokenManager.getUserId();
            byte[] userIndexs = instantiateUtil.instantiate(userIndex);
            jedisManager.hset(RedisDB.SHORT_TIME_QUERY, userIndexs, fpy, listFpydata.instantiate(list1), RedisDB.SHORT_TIME);
        } catch (Exception e) {
            httpResult.setCode(500);
            httpResult.setMessage("合格率获取失败");
            return httpResult;
        }

        httpResult.setCode(200);
        httpResult.setBody(list1);
        httpResult.setMessage("合格率获取成功.");

        return httpResult;
    }

    private Condition getCondition() {
        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        Condition condition = conditionInstantiateUtil.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, condi));
        return condition;
    }

    private OverLimitInfo getOverlimitInfo() {
        String userIndex = TokenManager.getUserId();
        byte[] userIndexs = instantiateUtil.instantiate(userIndex);
        OverLimitInfo overLimitInfo = listOverlimit.deInstantiate(jedisManager.hget(RedisDB.SHORT_TIME_QUERY, userIndexs, overlimit));
        return overLimitInfo;
    }
}
