package com.pqs9000.serviceimpl.data;

import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.njcn.pojo.data.AssesData;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njcn.mapper.configuration.OverLimitMapper;
import com.njcn.mapper.data.AssesMapper;
import com.njcn.pojo.commons.AreaGeneralData;
import com.njcn.pojo.configuration.OverLimit;
import com.njcn.pojo.data.Asses;
import com.njcn.pojo.data.PqsComasses;
import com.njcn.utils.ComAssesUtil;
import com.njcn.utils.FloatUtils;
import com.njcn.utils.UserUtil;
import com.pqs9000.pojo.common.AreaPowerQualityData;
import com.pqs9000.pojo.common.PowerQualityData;
import com.pqs9000.service.data.AssesService;

/**
 * @author hongawen
 * @version 1.0
 * @Date 2018/6/12 16:13
 */
@Service
public class AssesServiceImpl implements AssesService {

	@Autowired
	private UserUtil userUtil;

	@Autowired
	private AssesMapper assesMapper;

	@Autowired
	private ComAssesUtil comAssesUtil;

	@Autowired
	private OverLimitMapper overlimitMapper;

	/**
	 * 概览--》缩略图电能质量--》电能质量指标
	 * @param startTime 起始时间
	 * @param endTime 截止时间
	 */
	@Override
	public PowerQualityData getPowerQualityVol(Date startTime, Date endTime, String overview) {
		List<AreaGeneralData> areaGeneralDatas =userUtil.getAreaGeneralRun();
		if(CollectionUtils.isEmpty(areaGeneralDatas)){
			return  null;
		}
		PowerQualityData powerQualityData=getPowerQualityVolAreas(startTime,endTime,areaGeneralDatas,overview);
		return powerQualityData;
	}

	/**
	 *区域统计--》电能质量指标
	 * @param startTime 起始时间
	 * @param endTime 截止时间
	 * @param area 区域
	 */
	@Override
	public PowerQualityData getPowerQualityVolArea(Date startTime, Date endTime, String area) {
		PowerQualityData powerQualityData;
		List<AreaGeneralData> areaGeneralDatasTemp=userUtil.getAreaDataByInfoRun(area);
		if(CollectionUtils.isEmpty(areaGeneralDatasTemp)){
			return  null;
		}
		powerQualityData=getPowerQualityVolAreas(startTime,endTime,areaGeneralDatasTemp,"");
		return powerQualityData;
	}

	/**
	 * 抽取概览和区域统计模块的共同代码
	 * @param startTime 起始时间
	 * @param endTime 截止时间
	 * @param areas 区域
	 */
	private PowerQualityData getPowerQualityVolAreas(Date startTime, Date endTime, List<AreaGeneralData> areas,String overview){
		PowerQualityData powerQualityData=new PowerQualityData();
		List<AreaPowerQualityData> synData=new ArrayList<>();
		List<AreaPowerQualityData> vuDevs=new ArrayList<>();
		List<AreaPowerQualityData> freqDev=new ArrayList<>();
		List<AreaPowerQualityData> dataPlt=new ArrayList<>();
		List<AreaPowerQualityData> vunbalanceCp95=new ArrayList<>();
		List<AreaPowerQualityData> vThdCp95=new ArrayList<>();
		for (AreaGeneralData areaGeneralData:areas){
		    if(CollectionUtils.isEmpty(areaGeneralData.getLineIndexs())){
				//综合评分
				AreaPowerQualityData areaPowerQualityData5=new AreaPowerQualityData();
				areaPowerQualityData5.setDeptsIndex(areaGeneralData.getDeptIndex());
				areaPowerQualityData5.setName(areaGeneralData.getName());
				//获取综合评分
				areaPowerQualityData5.setData(3.1415f);
				//获取等级
				areaPowerQualityData5.setLevel("/");
				areaPowerQualityData5.setMonitor(0);
				synData.add(areaPowerQualityData5);
                if(StringUtils.isBlank(overview)){
					AreaPowerQualityData areaPowerQualityData=new AreaPowerQualityData();
					areaPowerQualityData.setDeptsIndex(areaGeneralData.getDeptIndex());
					areaPowerQualityData.setName(areaGeneralData.getName());
					//获取时间范围内该区域的电压偏差
					areaPowerQualityData.setData(3.1415f);
					areaPowerQualityData.setLevel("/");
					areaPowerQualityData.setMonitor(0);
					vuDevs.add(areaPowerQualityData);
                    AreaPowerQualityData areaPowerQualityData1=new AreaPowerQualityData();
					areaPowerQualityData1.setDeptsIndex(areaGeneralData.getDeptIndex());
                    areaPowerQualityData1.setName(areaGeneralData.getName());
                    areaPowerQualityData1.setData(3.1415f);
                    areaPowerQualityData1.setLevel("/");
                    areaPowerQualityData1.setMonitor(0);
                    freqDev.add(areaPowerQualityData1);
                    AreaPowerQualityData areaPowerQualityData2=new AreaPowerQualityData();
					areaPowerQualityData2.setDeptsIndex(areaGeneralData.getDeptIndex());
                    areaPowerQualityData2.setName(areaGeneralData.getName());
                    areaPowerQualityData2.setData(3.1415f);
                    areaPowerQualityData2.setLevel("/");
                    areaPowerQualityData2.setMonitor(0);
                    dataPlt.add(areaPowerQualityData2);
                    AreaPowerQualityData areaPowerQualityData3=new AreaPowerQualityData();
					areaPowerQualityData3.setDeptsIndex(areaGeneralData.getDeptIndex());
                    areaPowerQualityData3.setName(areaGeneralData.getName());
                    areaPowerQualityData3.setData(3.1415f);
                    areaPowerQualityData3.setLevel("/");
                    areaPowerQualityData3.setMonitor(0);
                    vThdCp95.add(areaPowerQualityData3);
                    AreaPowerQualityData areaPowerQualityData4=new AreaPowerQualityData();
					areaPowerQualityData4.setDeptsIndex(areaGeneralData.getDeptIndex());
                    areaPowerQualityData4.setName(areaGeneralData.getName());
                    areaPowerQualityData4.setData(3.1415f);
                    areaPowerQualityData4.setLevel("/");
                    areaPowerQualityData4.setMonitor(0);
                    vunbalanceCp95.add(areaPowerQualityData4);
                }
            }else{
                AreaPowerQualityData areaPowerQualityData=new AreaPowerQualityData();
				areaPowerQualityData.setDeptsIndex(areaGeneralData.getDeptIndex());
                areaPowerQualityData.setName(areaGeneralData.getName());
                //获取时间范围内该区域的电压偏差
                List<Integer> lineIds=areaGeneralData.getLineIndexs();
                Asses asses=getVolValue(startTime,endTime,lineIds);
                if(null==asses){
					//综合评分
					AreaPowerQualityData areaPowerQualityData5=new AreaPowerQualityData();
					areaPowerQualityData5.setName(areaGeneralData.getName());
					//获取综合评分
					float synDatas=getSynData(lineIds,startTime,endTime);
					areaPowerQualityData5.setData(synDatas);
					//获取等级
					areaPowerQualityData5.setLevel(getLevel(synDatas));
					areaPowerQualityData5.setMonitor(lineIds.size());
					synData.add(areaPowerQualityData5);

                    if(StringUtils.isBlank(overview)){
						areaPowerQualityData.setData(3.14159f);
						areaPowerQualityData.setLevel("（暂无数据）");
						areaPowerQualityData.setMonitor(lineIds.size());
						vuDevs.add(areaPowerQualityData);
                        AreaPowerQualityData areaPowerQualityData1=new AreaPowerQualityData();
						areaPowerQualityData1.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData1.setName(areaGeneralData.getName());
                        areaPowerQualityData1.setData(3.14159f);
                        areaPowerQualityData1.setLevel("（暂无数据）");
                        areaPowerQualityData1.setMonitor(lineIds.size());
                        freqDev.add(areaPowerQualityData1);
                        AreaPowerQualityData areaPowerQualityData2=new AreaPowerQualityData();
						areaPowerQualityData2.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData2.setName(areaGeneralData.getName());
                        areaPowerQualityData2.setData(3.14159f);
                        areaPowerQualityData2.setLevel("（暂无数据）");
                        areaPowerQualityData2.setMonitor(lineIds.size());
                        dataPlt.add(areaPowerQualityData2);
                        AreaPowerQualityData areaPowerQualityData3=new AreaPowerQualityData();
						areaPowerQualityData3.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData3.setName(areaGeneralData.getName());
                        areaPowerQualityData3.setData(3.14159f);
                        areaPowerQualityData3.setLevel("（暂无数据）");
                        areaPowerQualityData3.setMonitor(lineIds.size());
                        vThdCp95.add(areaPowerQualityData3);
                        AreaPowerQualityData areaPowerQualityData4=new AreaPowerQualityData();
						areaPowerQualityData4.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData4.setName(areaGeneralData.getName());
                        areaPowerQualityData4.setData(3.14159f);
                        areaPowerQualityData4.setLevel("（暂无数据）");
                        areaPowerQualityData4.setMonitor(lineIds.size());
                        vunbalanceCp95.add(areaPowerQualityData4);
                    }
                }else{
					//综合评分
					AreaPowerQualityData areaPowerQualityData5=new AreaPowerQualityData();
					areaPowerQualityData5.setDeptsIndex(areaGeneralData.getDeptIndex());
					areaPowerQualityData5.setName(areaGeneralData.getName());
					//获取综合评分
					float synDatas=getSynData(areaGeneralData.getLineIndexs(),startTime,endTime);
					areaPowerQualityData5.setData(synDatas);
					//获取等级
					areaPowerQualityData5.setLevel(getLevel(synDatas));
					areaPowerQualityData5.setMonitor(lineIds.size());
					synData.add(areaPowerQualityData5);
                    if(StringUtils.isBlank(overview)){
						areaPowerQualityData.setData(FloatUtils.get2Float(asses.getVuDev()));
						areaPowerQualityData.setLevel(getLevel(asses.getVuDev(),0f,2.5f,5f,7.5f,10f,12.5f,15f,17.5f));
						areaPowerQualityData.setMonitor(lineIds.size());
						vuDevs.add(areaPowerQualityData);
                        AreaPowerQualityData areaPowerQualityData1=new AreaPowerQualityData();
						areaPowerQualityData1.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData1.setName(areaGeneralData.getName());
                        areaPowerQualityData1.setData(FloatUtils.get2Float(asses.getFreqDev()));
                        areaPowerQualityData1.setLevel(getLevel(asses.getFreqDev(),0f,0.05f,0.1f,0.15f,0.2f,0.25f,0.30f,0.35f));
                        areaPowerQualityData1.setMonitor(lineIds.size());
                        freqDev.add(areaPowerQualityData1);
                        AreaPowerQualityData areaPowerQualityData2=new AreaPowerQualityData();
						areaPowerQualityData2.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData2.setName(areaGeneralData.getName());
                        areaPowerQualityData2.setData(FloatUtils.get2Float(asses.getDataPlt()));
                        areaPowerQualityData2.setLevel(getLevel(asses.getDataPlt(),0f,0.15f,0.3f,0.45f,0.6f,0.75f,0.90f,1.05f));
                        areaPowerQualityData2.setMonitor(lineIds.size());
                        dataPlt.add(areaPowerQualityData2);
                        AreaPowerQualityData areaPowerQualityData3=new AreaPowerQualityData();
						areaPowerQualityData3.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData3.setName(areaGeneralData.getName());
                        areaPowerQualityData3.setData(FloatUtils.get2Float(asses.getvThdCp95()));
                        areaPowerQualityData3.setLevel(getLevel(asses.getvThdCp95(),0f,0.5f,1f,1.5f,2f,2.5f,3f,3.5f));
                        areaPowerQualityData3.setMonitor(lineIds.size());
                        vThdCp95.add(areaPowerQualityData3);
                        AreaPowerQualityData areaPowerQualityData4=new AreaPowerQualityData();
						areaPowerQualityData4.setDeptsIndex(areaGeneralData.getDeptIndex());
                        areaPowerQualityData4.setName(areaGeneralData.getName());
                        areaPowerQualityData4.setData(FloatUtils.get2Float(asses.getVunbalanceCp95()));
                        areaPowerQualityData4.setLevel(getLevel(asses.getVunbalanceCp95(),0f,0.5f,1f,1.5f,2f,2.5f,3f,3.5f));
                        areaPowerQualityData4.setMonitor(lineIds.size());
                        vunbalanceCp95.add(areaPowerQualityData4);
                    }
                }

            }
		}
		powerQualityData.setVuDevs(vuDevs);
		powerQualityData.setDataPlt(dataPlt);
		powerQualityData.setFreqDev(freqDev);
		powerQualityData.setvThdCp95(vThdCp95);
		powerQualityData.setSynData(synData);
		powerQualityData.setVunbalanceCp95(vunbalanceCp95);
		return powerQualityData;
	}

	//获取综合得分等级
	private String getLevel(float synDatas) {
	    if(synDatas==3.14159f){
	        return "（暂无数据）";
        }
		if(synDatas==3.1415f){
			return "/";
		}
		if(0<=synDatas&&synDatas<2){
			return "极差";
		}else if(2<=synDatas&&synDatas<3){
			return "较差";
		}else if(3<=synDatas&&synDatas<4){
    	 	return "合格";
		}else if(4<=synDatas&&synDatas<4.5){
           return "良好";
		}else{
          return  "优质";
		}
	}

	/**
	 * 根据监测点集合，返回对应的综合电能质量评估
	 * @param lineIndexs 监测点集合
	 * @param startTime 起始时间
	 * @param endTime 截止时间
	 */
	private float getSynData(List<Integer> lineIndexs, Date startTime, Date endTime) {
		List<PqsComasses> pqsComasses=new ArrayList<>();
		List<Integer> lineTemps=new ArrayList<>(lineIndexs);
		if(CollectionUtils.isNotEmpty(lineTemps)){
			if(lineTemps.size()>1000){
				int size=lineTemps.size();
				if(lineTemps.size()>1000){
					for(int i=1;i<=size;i++){
						List<Integer> temp=lineTemps.subList(0,1000);
						List<PqsComasses> tempComass=assesMapper.getSynData(temp,startTime,endTime);
						pqsComasses.addAll(tempComass);
						temp.clear();
					}
				}else{
					List<PqsComasses> tempComass=assesMapper.getSynData(lineTemps,startTime,endTime);
					pqsComasses.addAll(tempComass);
				}
			}else{
				List<PqsComasses> tempComass=assesMapper.getSynData(lineTemps,startTime,endTime);
				pqsComasses.addAll(tempComass);
			}

		}else{
			return 3.1415f;//无监测点前台统一替换为“/”
		}
		if(CollectionUtils.isEmpty(pqsComasses)){
			return 3.14159f;//有监测点，但是无数据的处理
		}
		float synData=comAssesUtil.getAllComAss(pqsComasses);
		return synData;
	}

	/**
	 * 获取电压偏差值对应的等级
	 * @param value 电压偏差值
	 */
	private String getLevel(float value,float f1,float f2,float f3,float f4,float f5,float f6,float f7,float f8) {
        String level;
		if(f1<=value&&value<=f2){
			level="特质";
		}else if(f2<value&&value<=f3){
			level="优质";
		}else if(f3<value&&value<=f4){
			level="优良";
		}else if(f4<value&&value<=f5){
			level="良好";
		}else if(f5<value&&value<=f6){
			level="轻度污染";
		}else if(f6<value&&value<=f7){
			level="中度污染";
		}else if(f7<value&&value<=f8){
			level="重度污染";
		}else{
			level="极度污染";
		}
		return level;
	}

	/**
	 * 获取时间范围内，对应监测点的电压偏差
	 * @param startTime 起始时间
	 * @param endTime  截止时间
	 * @param lineIds  监测点
	 */
	private Asses getVolValue(Date startTime, Date endTime, List<Integer> lineIds) {
		Asses asses=null;
		List<Integer> temps=new ArrayList<>(lineIds);
		//分片查询数据
		if(temps.size()>1000){
			int times=temps.size()+1;
			for (int i=0;i<times;i++){
				if(temps.size()>1000){
					List<Integer>  tempIds=temps.subList(0,1000);
					Asses valueTemp=assesMapper.getVol(startTime,endTime,tempIds);
					asses=mergeAsses(asses,valueTemp);
					tempIds.clear();
				}else{
					Asses valueTemp=assesMapper.getVol(startTime,endTime,temps);
					asses=mergeAsses(asses,valueTemp);
				}
			}
		}else{
					Asses valueTemp=assesMapper.getVol(startTime,endTime,temps);
					asses=mergeAsses(asses,valueTemp);
		}
		return asses;
	}

	/**
	 * 合并平均值
	 */
	private Asses mergeAsses(Asses asses, Asses valueTemp) {
		if(null==valueTemp){
			return asses;
		}
		if(null==asses){
            asses=valueTemp;
        }else{
            DecimalFormat df = new DecimalFormat(".00");
            asses.setDataPlt(Float.valueOf(df.format(asses.getDataPlt()+valueTemp.getDataPlt())));
            asses.setFreqDev(Float.valueOf(df.format(asses.getFreqDev()+valueTemp.getFreqDev())));
            asses.setvThdCp95(Float.valueOf(df.format(asses.getvThdCp95()+valueTemp.getvThdCp95())));
            asses.setVuDev(Float.valueOf(df.format(asses.getVuDev()+valueTemp.getVuDev())));
            asses.setVunbalanceCp95(Float.valueOf(df.format(asses.getVunbalanceCp95()+valueTemp.getVunbalanceCp95())));
        }
		return asses;
	}

	@Override
	public PowerQualityData qualityAssess(Date startTime, Date endTime, Long lineIndex){
		PowerQualityData powerQualityData = new PowerQualityData();

		List<AreaPowerQualityData> syn=new ArrayList<>();
		List<AreaPowerQualityData> vuDevs=new ArrayList<>();
		List<AreaPowerQualityData> freqDev=new ArrayList<>();
		List<AreaPowerQualityData> dataPlt=new ArrayList<>();
		List<AreaPowerQualityData> vunbalanceCp95=new ArrayList<>();
		List<AreaPowerQualityData> vThdCp95=new ArrayList<>();

		AreaPowerQualityData areaPowerQualityData0=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData1=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData2=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData3=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData4=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData5=new AreaPowerQualityData();
		
		List<Integer> indexs = new ArrayList<>();
		indexs.add(lineIndex.intValue());
		
		//获取综合评分
		float synDatas=getSynData(indexs,startTime,endTime);
		areaPowerQualityData0.setData(synDatas);
		areaPowerQualityData0.setLevel(getLevel(synDatas));
		syn.add(areaPowerQualityData0);
		
		Asses asses=getVolValue(startTime,endTime,indexs);
		
		if(asses==null){
			areaPowerQualityData1.setData(3.14159f);
			areaPowerQualityData1.setLevel("（暂无数据）");
			vuDevs.add(areaPowerQualityData1);
			
            areaPowerQualityData2.setData(3.14159f);
            areaPowerQualityData2.setLevel("（暂无数据）");
            freqDev.add(areaPowerQualityData2);
                    
            areaPowerQualityData3.setData(3.14159f);
            areaPowerQualityData3.setLevel("（暂无数据）");
            dataPlt.add(areaPowerQualityData3);
                    
            areaPowerQualityData4.setData(3.14159f);
            areaPowerQualityData4.setLevel("（暂无数据）");
            vThdCp95.add(areaPowerQualityData4);
                    
            areaPowerQualityData5.setData(3.14159f);
            areaPowerQualityData5.setLevel("（暂无数据）");
            vunbalanceCp95.add(areaPowerQualityData5);
		}else{
			OverLimit overlimit = new OverLimit();
			overlimit = overlimitMapper.selectByPrimaryKey(lineIndex);
			float vu = overlimit.getVoltageDev()/4;
			float fre = overlimit.getFreqDev()/4;
			float plt = overlimit.getFlicker()/4;
			float cp = overlimit.getUaberrance()/4;
			float vb = overlimit.getUbalance()/4;
			
			areaPowerQualityData1.setData(FloatUtils.get2Float(asses.getVuDev()));
			areaPowerQualityData1.setLevel(getLevel(asses.getVuDev(),0f,vu,vu*2,vu*3,vu*4,vu*5,vu*6,vu*7));
			vuDevs.add(areaPowerQualityData1);
			
			areaPowerQualityData2.setData(FloatUtils.get2Float(asses.getFreqDev()));
			areaPowerQualityData2.setLevel(getLevel(asses.getFreqDev(),0f,fre,fre*2,fre*3,fre*4,fre*5,fre*6,fre*7));
			freqDev.add(areaPowerQualityData2);
			
			areaPowerQualityData3.setData(FloatUtils.get2Float(asses.getDataPlt()));
			areaPowerQualityData3.setLevel(getLevel(asses.getDataPlt(),0f,plt,plt*2,plt*3,plt*4,plt*5,plt*6,plt*7));
			dataPlt.add(areaPowerQualityData3);
			
			areaPowerQualityData4.setData(FloatUtils.get2Float(asses.getvThdCp95()));
			areaPowerQualityData4.setLevel(getLevel(asses.getvThdCp95(),0f,cp,cp*2,cp*3,cp*4,cp*5,cp*6,cp*7));
			vThdCp95.add(areaPowerQualityData4);
			
			areaPowerQualityData5.setData(FloatUtils.get2Float(asses.getVunbalanceCp95()));
			areaPowerQualityData5.setLevel(getLevel(asses.getVunbalanceCp95(),0f,vb,vb*2,vb*3,vb*4,vb*5,vb*6,vb*7));
			vunbalanceCp95.add(areaPowerQualityData5);

		}
		
		powerQualityData.setSyn(syn);
		powerQualityData.setVuDevs(vuDevs);
		powerQualityData.setDataPlt(dataPlt);
		powerQualityData.setFreqDev(freqDev);
		powerQualityData.setvThdCp95(vThdCp95);
		powerQualityData.setVunbalanceCp95(vunbalanceCp95);
		return powerQualityData;
	}
	
	@Override
	public PowerQualityData qualityAssessTB(Date startTime, Date endTime, Date laststartTime, Date lastendTime, Long lineIndex){
		PowerQualityData powerQualityData = new PowerQualityData();
		
		List<AreaPowerQualityData> syn=new ArrayList<>();
		List<AreaPowerQualityData> vuDevs=new ArrayList<>();
		List<AreaPowerQualityData> freqDev=new ArrayList<>();
		List<AreaPowerQualityData> dataPlt=new ArrayList<>();
		List<AreaPowerQualityData> vunbalanceCp95=new ArrayList<>();
		List<AreaPowerQualityData> vThdCp95=new ArrayList<>();

		AreaPowerQualityData areaPowerQualityData0=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData1=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData2=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData3=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData4=new AreaPowerQualityData();
		AreaPowerQualityData areaPowerQualityData5=new AreaPowerQualityData();
		
		List<Integer> indexs = new ArrayList<>();
		indexs.add(lineIndex.intValue());

		//获取综合评分
		float synDatas=getSynData(indexs,startTime,endTime);
		areaPowerQualityData0.setData(synDatas);
		areaPowerQualityData0.setLevel(getLevel(synDatas));
		syn.add(areaPowerQualityData0);
		
		Asses asses=getVolValue(startTime,endTime,indexs);

		OverLimit overlimit = new OverLimit();
		overlimit = overlimitMapper.selectByPrimaryKey(lineIndex);
		float vu = overlimit.getVoltageDev()/4;
		float fre = overlimit.getFreqDev()/4;
		float plt = overlimit.getFlicker()/4;
		float cp = overlimit.getUaberrance()/4;
		float vb = overlimit.getUbalance()/4;
		
		if(asses==null){
			areaPowerQualityData1.setData(3.14159f);
			areaPowerQualityData1.setLevel("（暂无数据）");
			vuDevs.add(areaPowerQualityData1);
			
            areaPowerQualityData2.setData(3.14159f);
            areaPowerQualityData2.setLevel("（暂无数据）");
            freqDev.add(areaPowerQualityData2);
            
            areaPowerQualityData3.setData(3.14159f);
            areaPowerQualityData3.setLevel("（暂无数据）");
            dataPlt.add(areaPowerQualityData3);
            
            areaPowerQualityData4.setData(3.14159f);
            areaPowerQualityData4.setLevel("（暂无数据）");
            vThdCp95.add(areaPowerQualityData4);
            
            areaPowerQualityData5.setData(3.14159f);
            areaPowerQualityData5.setLevel("（暂无数据）");
            vunbalanceCp95.add(areaPowerQualityData5);
		}else{
			areaPowerQualityData1.setData(FloatUtils.get2Float(asses.getVuDev()));
			areaPowerQualityData1.setLevel(getLevel(asses.getVuDev(),0f,vu,vu*2,vu*3,vu*4,vu*5,vu*6,vu*7));
			vuDevs.add(areaPowerQualityData1);
			
			areaPowerQualityData2.setData(FloatUtils.get2Float(asses.getFreqDev()));
			areaPowerQualityData2.setLevel(getLevel(asses.getFreqDev(),0f,fre,fre*2,fre*3,fre*4,fre*5,fre*6,fre*7));
			freqDev.add(areaPowerQualityData2);
			
			areaPowerQualityData3.setData(FloatUtils.get2Float(asses.getDataPlt()));
			areaPowerQualityData3.setLevel(getLevel(asses.getDataPlt(),0f,plt,plt*2,plt*3,plt*4,plt*5,plt*6,plt*7));
			dataPlt.add(areaPowerQualityData3);
			
			areaPowerQualityData4.setData(FloatUtils.get2Float(asses.getvThdCp95()));
			areaPowerQualityData4.setLevel(getLevel(asses.getvThdCp95(),0f,cp,cp*2,cp*3,cp*4,cp*5,cp*6,cp*7));
			vThdCp95.add(areaPowerQualityData4);
			
			areaPowerQualityData5.setData(FloatUtils.get2Float(asses.getVunbalanceCp95()));
			areaPowerQualityData5.setLevel(getLevel(asses.getVunbalanceCp95(),0f,vb,vb*2,vb*3,vb*4,vb*5,vb*6,vb*7));
			vunbalanceCp95.add(areaPowerQualityData5);
		}
		AreaPowerQualityData lastPowerQualityData0=new AreaPowerQualityData();
		AreaPowerQualityData lastPowerQualityData1=new AreaPowerQualityData();
		AreaPowerQualityData lastPowerQualityData2=new AreaPowerQualityData();
		AreaPowerQualityData lastPowerQualityData3=new AreaPowerQualityData();
		AreaPowerQualityData lastPowerQualityData4=new AreaPowerQualityData();
		AreaPowerQualityData lastPowerQualityData5=new AreaPowerQualityData();

		//获取综合评分
		float lastsynDatas=getSynData(indexs,laststartTime,lastendTime);
		lastPowerQualityData0.setData(lastsynDatas);
		lastPowerQualityData0.setLevel(getLevel(lastsynDatas));
		syn.add(lastPowerQualityData0);
		
		Asses lastasses=getVolValue(laststartTime,lastendTime,indexs);
		
		if(lastasses==null){
			lastPowerQualityData1.setData(3.14159f);
			lastPowerQualityData1.setLevel("（暂无数据）");
			vuDevs.add(lastPowerQualityData1);
			
			lastPowerQualityData2.setData(3.14159f);
			lastPowerQualityData2.setLevel("（暂无数据）");
            freqDev.add(lastPowerQualityData2);
                    
            lastPowerQualityData3.setData(3.14159f);
            lastPowerQualityData3.setLevel("（暂无数据）");
            dataPlt.add(lastPowerQualityData3);
                    
            lastPowerQualityData4.setData(3.14159f);
            lastPowerQualityData4.setLevel("（暂无数据）");
            vThdCp95.add(lastPowerQualityData4);
                    
            lastPowerQualityData5.setData(3.14159f);
            lastPowerQualityData5.setLevel("（暂无数据）");
            vunbalanceCp95.add(lastPowerQualityData5);
		}else{
			lastPowerQualityData1.setData(FloatUtils.get2Float(lastasses.getVuDev()));
			lastPowerQualityData1.setLevel(getLevel(lastasses.getVuDev(),0f,vu,vu*2,vu*3,vu*4,vu*5,vu*6,vu*7));
			vuDevs.add(lastPowerQualityData1);
			
			lastPowerQualityData2.setData(FloatUtils.get2Float(lastasses.getFreqDev()));
			lastPowerQualityData2.setLevel(getLevel(lastasses.getFreqDev(),0f,fre,fre*2,fre*3,fre*4,fre*5,fre*6,fre*7));
			freqDev.add(lastPowerQualityData2);
			
			lastPowerQualityData3.setData(FloatUtils.get2Float(lastasses.getDataPlt()));
			lastPowerQualityData3.setLevel(getLevel(lastasses.getDataPlt(),0f,plt,plt*2,plt*3,plt*4,plt*5,plt*6,plt*7));
			dataPlt.add(lastPowerQualityData3);
			
			lastPowerQualityData4.setData(FloatUtils.get2Float(lastasses.getvThdCp95()));
			lastPowerQualityData4.setLevel(getLevel(lastasses.getvThdCp95(),0f,cp,cp*2,cp*3,cp*4,cp*5,cp*6,cp*7));
			vThdCp95.add(lastPowerQualityData4);
			
			lastPowerQualityData5.setData(FloatUtils.get2Float(lastasses.getVunbalanceCp95()));
			lastPowerQualityData5.setLevel(getLevel(lastasses.getVunbalanceCp95(),0f,vb,vb*2,vb*3,vb*4,vb*5,vb*6,vb*7));
			vunbalanceCp95.add(lastPowerQualityData5);
		}
		powerQualityData.setSyn(syn);
		powerQualityData.setVuDevs(vuDevs);
		powerQualityData.setDataPlt(dataPlt);
		powerQualityData.setFreqDev(freqDev);
		powerQualityData.setvThdCp95(vThdCp95);
		powerQualityData.setVunbalanceCp95(vunbalanceCp95);
		return powerQualityData;
	}

	@Override
	public List<AssesData>  getAssesTableData(Date startTime, Date endTime, String area) {
		List<AreaGeneralData> areaGeneralDataList = userUtil.getAreaGeneralRun(null,area,null);
		List<Integer> lineIndexs = areaGeneralDataList.stream().flatMap(item->item.getLineIndexs().stream()).collect(Collectors.toList());
		if (org.springframework.util.CollectionUtils.isEmpty(lineIndexs)) {
			return null;
		}
		List<AssesData> integralityTable = getIntegralityTable(startTime, endTime, lineIndexs);
		if(CollUtil.isNotEmpty(integralityTable)){
			Map<String, Float> assesSynData = getAssesSynData(startTime, endTime, lineIndexs);
			for (AssesData assesData : integralityTable) {
				if(assesSynData.containsKey(assesData.getLineId())){
					assesData.setSynData(assesSynData.get(assesData.getLineId()));
				}else{
					assesData.setSynData(0f);
				}
			}
		}
		return integralityTable;
	}

	/**
	 * 分片处理获取
	 *
	 * @param startTime  起始时间
	 * @param endTime    截止时间
	 * @param lineIndexs 监测点索引
	 */
	private List<AssesData> getIntegralityTable(Date startTime, Date endTime, List<Integer> lineIndexs) {
		List<AssesData> intergralityTables = new ArrayList<>();
		//分片1000个监测点
		if (lineIndexs.size() > 1000) {
			int times = lineIndexs.size() / 1000 + 1;
			for (int i = 1; i <= times; i++) {
				if (lineIndexs.size() > 1000) {
					List<Integer> tempIndexs = lineIndexs.subList(0, 1000);
					List<AssesData> tempTables = assesMapper.getLineVol(tempIndexs, startTime, endTime);
					intergralityTables.addAll(tempTables);
					tempIndexs.clear();
				} else {
					List<AssesData> tempTables = assesMapper.getLineVol(lineIndexs, startTime, endTime);
					intergralityTables.addAll(tempTables);
				}
			}
		} else {
			intergralityTables = assesMapper.getLineVol(lineIndexs, startTime, endTime);
		}
		return intergralityTables;
	}

	/**
	 * 分片处理获取
	 *
	 * @param startTime  起始时间
	 * @param endTime    截止时间
	 * @param lineIndexs 监测点索引
	 */
	private Map<String,Float> getAssesSynData(Date startTime, Date endTime, List<Integer> lineIndexs) {
		Map<String,Float> map=new HashMap<>();
		List<PqsComasses> intergralityTables = new ArrayList<>();
		//分片1000个监测点
		if (lineIndexs.size() > 1000) {
			int times = lineIndexs.size() / 1000 + 1;
			for (int i = 1; i <= times; i++) {
				if (lineIndexs.size() > 1000) {
					List<Integer> tempIndexs = lineIndexs.subList(0, 1000);
					List<PqsComasses> tempTables = assesMapper.getSynData(tempIndexs, startTime, endTime);
					intergralityTables.addAll(tempTables);
					tempIndexs.clear();
				} else {
					List<PqsComasses> tempTables = assesMapper.getSynData(lineIndexs, startTime, endTime);
					intergralityTables.addAll(tempTables);
				}
			}
		} else {
			intergralityTables = assesMapper.getSynData(lineIndexs, startTime, endTime);
		}
		if(CollUtil.isNotEmpty(intergralityTables)){
			for (int i = 0; i < intergralityTables.size(); i++) {
				PqsComasses tempPqs=intergralityTables.get(i);
				//组合二维数组
				float f1[][]={{tempPqs.getFreqDev1(),tempPqs.getFreqDev2(),tempPqs.getFreqDev3(),tempPqs.getFreqDev4(),tempPqs.getFreqDev5()}
						,{tempPqs.getVThd1(),tempPqs.getVThd2(),tempPqs.getVThd3(),tempPqs.getVThd4(),tempPqs.getVThd5(),}
						,{tempPqs.getDataPst1(),tempPqs.getDataPst2(),tempPqs.getDataPst3(),tempPqs.getDataPst4(),tempPqs.getDataPst5()}
						,{tempPqs.getVuDev1(),tempPqs.getVuDev2(),tempPqs.getVuDev3(),tempPqs.getVuDev4(),tempPqs.getVuDev5(),}
						,{tempPqs.getVUnbalance1(),tempPqs.getVUnbalance2(),tempPqs.getVUnbalance3(),tempPqs.getVUnbalance4(),tempPqs.getVUnbalance5(),}
						,{tempPqs.getEvent1(),tempPqs.getEvent2(),tempPqs.getEvent3(),tempPqs.getEvent4(),tempPqs.getEvent5(),}};
				//获取该值返回的数据
				float temp=comAssesUtil.GetComAsses(f1);
				map.put(tempPqs.getLineid(),temp);
			}
		}
		return map;
	}
}
