package com.shining.cloud.serviceImpl.statistics;

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

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.njcn.mapper.configuration.LineMapper;
import com.njcn.pojo.commons.EventInfoDetail;
import com.njcn.pojo.configuration.Line;
import com.shining.cloud.enums.information.TypeCodeEnum;
import com.shining.cloud.mapper.information.SteadyUrlMapper;
import com.shining.cloud.mapper.statistics.StatisticsMapper;
import com.shining.cloud.pojo.information.SteadyUrl;
import com.shining.cloud.pojo.statistics.Statistics;
import com.shining.cloud.pojo.statistics.StatisticsData;
import com.shining.cloud.pojo.statistics.StatisticsDetail;
import com.shining.cloud.pojo.statistics.StatisticsEvent;
import com.shining.cloud.service.statistics.StatisticsService;

/**
 * @description: 统计数据ServiceImpl
 * @author: gbl
 * @time: 2019-12-16 14:13:17
 **/
@Service
public class StatisticsServiceImpl implements StatisticsService {
	@Autowired
	private StatisticsMapper statisticsMapper;
	@Autowired
	private LineMapper lineMapper;
	@Resource
	private SteadyUrlMapper steadyUrlMapper;
	
	@Override
	public List<StatisticsData> queryData(int lineIndex,Date startTime,Date endTime, int page, int num){
		List<StatisticsData> result = new ArrayList<>();
		PageHelper.offsetPage((page-1)*num, num);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        
//        Example example = new Example(Statistics.class);
//        example.createCriteria().andEqualTo("lineIndex", lineIndex);
//        example.setOrderByClause("timeid desc");
        List<Statistics> tmp = statisticsMapper.queryList(lineIndex, sdf.format(startTime), sdf.format(endTime));
        if(tmp.size()>0){
        	for(Statistics statistics : tmp){
        		StatisticsData data = new StatisticsData();
        		Line line = lineMapper.selectByPrimaryKey((long) lineIndex);
        		data.setLineName(line.getName());
        		data.setTime(statistics.getTimeID().getTime());
        		String describe = "";
        		String describe1 = "";
        		if(statistics.getEventCount() != null && statistics.getEventCount()>0){
        			describe = describe + "暂降次数:"+statistics.getEventCount().toString()+"次;";
        		}
        		if(statistics.getFreqDevRate() != null && statistics.getFreqDevRate()>0 && statistics.getFreqDevRate()<100){
        			describe1 = describe1 + "频率偏差合格率" + statistics.getFreqDevRate().toString() + "%;";
        		}
        		if(statistics.getVoltageDevRate() != null && statistics.getVoltageDevRate()>0 && statistics.getVoltageDevRate()<100){
        			describe1 = describe1 + "电压偏差合格率" + statistics.getVoltageDevRate().toString() + "%;";
        		}
        		if(statistics.getUBalanceRate() != null && statistics.getUBalanceRate()>0 && statistics.getUBalanceRate()<100){
        			describe1 = describe1 + "电压不平衡合格率" + statistics.getUBalanceRate().toString() + "%;";
        		}
        		if(statistics.getFlickerRate() != null && statistics.getFlickerRate()>0 && statistics.getFlickerRate()<100){
        			describe1 = describe1 + "闪变合格率" + statistics.getFlickerRate().toString() + "%;";
        		}
        		if(statistics.getUAberranceRate() != null && statistics.getUAberranceRate()>0 && statistics.getUAberranceRate()<100){
        			describe1 = describe1 + "电压谐波畸变率合格率" + statistics.getUAberranceRate().toString() + "%;";
        		}
        		if(statistics.getUHarm2Rate() != null && statistics.getUHarm2Rate()>0 && statistics.getUHarm2Rate()<100){
        			describe1 = describe1 + "2次电压谐波含有率合格率" + statistics.getUHarm2Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm3Rate() != null && statistics.getUHarm3Rate()>0 && statistics.getUHarm3Rate()<100){
        			describe1 = describe1 + "3次电压谐波含有率合格率" + statistics.getUHarm3Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm4Rate() != null && statistics.getUHarm4Rate()>0 && statistics.getUHarm4Rate()<100){
        			describe1 = describe1 + "4次电压谐波含有率合格率" + statistics.getUHarm4Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm5Rate() != null && statistics.getUHarm5Rate()>0 && statistics.getUHarm5Rate()<100){
        			describe1 = describe1 + "5次电压谐波含有率合格率" + statistics.getUHarm5Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm6Rate() != null && statistics.getUHarm6Rate()>0 && statistics.getUHarm6Rate()<100){
        			describe1 = describe1 + "6次电压谐波含有率合格率" + statistics.getUHarm6Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm7Rate() != null && statistics.getUHarm7Rate()>0 && statistics.getUHarm7Rate()<100){
        			describe1 = describe1 + "7次电压谐波含有率合格率" + statistics.getUHarm7Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm8Rate() != null && statistics.getUHarm8Rate()>0 && statistics.getUHarm8Rate()<100){
        			describe1 = describe1 + "8次电压谐波含有率合格率" + statistics.getUHarm8Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm9Rate() != null && statistics.getUHarm9Rate()>0 && statistics.getUHarm9Rate()<100){
        			describe1 = describe1 + "9次电压谐波含有率合格率" + statistics.getUHarm9Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm10Rate() != null && statistics.getUHarm10Rate()>0 && statistics.getUHarm10Rate()<100){
        			describe1 = describe1 + "10次电压谐波含有率合格率" + statistics.getUHarm10Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm11Rate() != null && statistics.getUHarm11Rate()>0 && statistics.getUHarm11Rate()<100){
        			describe1 = describe1 + "11次电压谐波含有率合格率" + statistics.getUHarm11Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm12Rate() != null && statistics.getUHarm12Rate()>0 && statistics.getUHarm12Rate()<100){
        			describe1 = describe1 + "12次电压谐波含有率合格率" + statistics.getUHarm12Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm13Rate() != null && statistics.getUHarm13Rate()>0 && statistics.getUHarm13Rate()<100){
        			describe1 = describe1 + "13次电压谐波含有率合格率" + statistics.getUHarm13Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm14Rate() != null && statistics.getUHarm14Rate()>0 && statistics.getUHarm14Rate()<100){
        			describe1 = describe1 + "14次电压谐波含有率合格率" + statistics.getUHarm14Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm15Rate() != null && statistics.getUHarm15Rate()>0 && statistics.getUHarm15Rate()<100){
        			describe1 = describe1 + "15次电压谐波含有率合格率" + statistics.getUHarm15Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm16Rate() != null && statistics.getUHarm16Rate()>0 && statistics.getUHarm16Rate()<100){
        			describe1 = describe1 + "16次电压谐波含有率合格率" + statistics.getUHarm16Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm17Rate() != null && statistics.getUHarm17Rate()>0 && statistics.getUHarm17Rate()<100){
        			describe1 = describe1 + "17次电压谐波含有率合格率" + statistics.getUHarm17Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm18Rate() != null && statistics.getUHarm18Rate()>0 && statistics.getUHarm18Rate()<100){
        			describe1 = describe1 + "18次电压谐波含有率合格率" + statistics.getUHarm18Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm19Rate() != null && statistics.getUHarm19Rate()>0 && statistics.getUHarm19Rate()<100){
        			describe1 = describe1 + "19次电压谐波含有率合格率" + statistics.getUHarm19Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm20Rate() != null && statistics.getUHarm20Rate()>0 && statistics.getUHarm20Rate()<100){
        			describe1 = describe1 + "20次电压谐波含有率合格率" + statistics.getUHarm20Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm21Rate() != null && statistics.getUHarm21Rate()>0 && statistics.getUHarm21Rate()<100){
        			describe1 = describe1 + "21次电压谐波含有率合格率" + statistics.getUHarm21Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm22Rate() != null && statistics.getUHarm22Rate()>0 && statistics.getUHarm22Rate()<100){
        			describe1 = describe1 + "22次电压谐波含有率合格率" + statistics.getUHarm22Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm23Rate() != null && statistics.getUHarm23Rate()>0 && statistics.getUHarm23Rate()<100){
        			describe1 = describe1 + "23次电压谐波含有率合格率" + statistics.getUHarm23Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm24Rate() != null && statistics.getUHarm24Rate()>0 && statistics.getUHarm24Rate()<100){
        			describe1 = describe1 + "24次电压谐波含有率合格率" + statistics.getUHarm24Rate().toString() + "%;";
        		}
        		if(statistics.getUHarm25Rate() != null && statistics.getUHarm25Rate()>0 && statistics.getUHarm25Rate()<100){
        			describe1 = describe1 + "25次电压谐波含有率合格率" + statistics.getUHarm25Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm2Rate() != null && statistics.getIHarm2Rate()>0 && statistics.getIHarm2Rate()<100){
        			describe1 = describe1 + "2次电流谐波幅值合格率" + statistics.getIHarm2Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm3Rate() != null && statistics.getIHarm3Rate()>0 && statistics.getIHarm3Rate()<100){
        			describe1 = describe1 + "3次电流谐波幅值合格率" + statistics.getIHarm3Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm4Rate() != null && statistics.getIHarm4Rate()>0 && statistics.getIHarm4Rate()<100){
        			describe1 = describe1 + "4次电流谐波幅值合格率" + statistics.getIHarm4Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm5Rate() != null && statistics.getIHarm5Rate()>0 && statistics.getIHarm5Rate()<100){
        			describe1 = describe1 + "5次电流谐波幅值合格率" + statistics.getIHarm5Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm6Rate() != null && statistics.getIHarm6Rate()>0 && statistics.getIHarm6Rate()<100){
        			describe1 = describe1 + "6次电流谐波幅值合格率" + statistics.getIHarm6Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm7Rate() != null && statistics.getIHarm7Rate()>0 && statistics.getIHarm7Rate()<100){
        			describe1 = describe1 + "7次电流谐波幅值合格率" + statistics.getIHarm7Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm8Rate() != null && statistics.getIHarm8Rate()>0 && statistics.getIHarm8Rate()<100){
        			describe1 = describe1 + "8次电流谐波幅值合格率" + statistics.getIHarm8Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm9Rate() != null && statistics.getIHarm9Rate()>0 && statistics.getIHarm9Rate()<100){
        			describe1 = describe1 + "9次电流谐波幅值合格率" + statistics.getIHarm9Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm10Rate() != null && statistics.getIHarm10Rate()>0 && statistics.getIHarm10Rate()<100){
        			describe1 = describe1 + "10次电流谐波幅值合格率" + statistics.getIHarm10Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm11Rate() != null && statistics.getIHarm11Rate()>0 && statistics.getIHarm11Rate()<100){
        			describe1 = describe1 + "11次电流谐波幅值合格率" + statistics.getIHarm11Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm12Rate() != null && statistics.getIHarm12Rate()>0 && statistics.getIHarm12Rate()<100){
        			describe1 = describe1 + "12次电流谐波幅值合格率" + statistics.getIHarm12Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm13Rate() != null && statistics.getIHarm13Rate()>0 && statistics.getIHarm13Rate()<100){
        			describe1 = describe1 + "13次电流谐波幅值合格率" + statistics.getIHarm13Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm14Rate() != null && statistics.getIHarm14Rate()>0 && statistics.getIHarm14Rate()<100){
        			describe1 = describe1 + "14次电流谐波幅值合格率" + statistics.getIHarm14Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm15Rate() != null && statistics.getIHarm15Rate()>0 && statistics.getIHarm15Rate()<100){
        			describe1 = describe1 + "15次电流谐波幅值合格率" + statistics.getIHarm15Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm16Rate() != null && statistics.getIHarm16Rate()>0 && statistics.getIHarm16Rate()<100){
        			describe1 = describe1 + "16次电流谐波幅值合格率" + statistics.getIHarm16Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm17Rate() != null && statistics.getIHarm17Rate()>0 && statistics.getIHarm17Rate()<100){
        			describe1 = describe1 + "17次电流谐波幅值合格率" + statistics.getIHarm17Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm18Rate() != null && statistics.getIHarm18Rate()>0 && statistics.getIHarm18Rate()<100){
        			describe1 = describe1 + "18次电流谐波幅值合格率" + statistics.getIHarm18Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm19Rate() != null && statistics.getIHarm19Rate()>0 && statistics.getIHarm19Rate()<100){
        			describe1 = describe1 + "19次电流谐波幅值合格率" + statistics.getIHarm19Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm20Rate() != null && statistics.getIHarm20Rate()>0 && statistics.getIHarm20Rate()<100){
        			describe1 = describe1 + "20次电流谐波幅值合格率" + statistics.getIHarm20Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm21Rate() != null && statistics.getIHarm21Rate()>0 && statistics.getIHarm21Rate()<100){
        			describe1 = describe1 + "21次电流谐波幅值合格率" + statistics.getIHarm21Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm22Rate() != null && statistics.getIHarm22Rate()>0 && statistics.getIHarm22Rate()<100){
        			describe1 = describe1 + "22次电流谐波幅值合格率" + statistics.getIHarm22Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm23Rate() != null && statistics.getIHarm23Rate()>0 && statistics.getIHarm23Rate()<100){
        			describe1 = describe1 + "23次电流谐波幅值合格率" + statistics.getIHarm23Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm24Rate() != null && statistics.getIHarm24Rate()>0 && statistics.getIHarm24Rate()<100){
        			describe1 = describe1 + "24次电流谐波幅值合格率" + statistics.getIHarm24Rate().toString() + "%;";
        		}
        		if(statistics.getIHarm25Rate() != null && statistics.getIHarm25Rate()>0 && statistics.getIHarm25Rate()<100){
        			describe1 = describe1 + "25次电流谐波幅值合格率" + statistics.getIHarm25Rate().toString() + "%;";
        		}
        		boolean flag=false;
        		if(describe.length() > 0){
            		describe = describe.substring(0, describe.length()-1);
            		data.setEventDescribe(describe);
            		flag=true;
            	}
        		if(describe1.length() > 0){
        			describe1 = "越限详情:" + describe1;
        			describe1 = describe1.substring(0, describe1.length()-1);
        			data.setSteadyDescribe(describe1);
        			flag = true;
        		}
        		if(flag){
        			result.add(data);
        		}
        	}
        }
		return result;
	}
	
	@Override
	public StatisticsDetail queryDetail(int lineIndex,Date timeID){
		StatisticsDetail result = new StatisticsDetail();
		
		//获取监测点完整名称
		String name = statisticsMapper.getTotalName(lineIndex);
		result.setName(name);
		
		//获取稳态消息详情
		String steadydescribe = "";
		List<SteadyUrl> ass = new ArrayList<>();
		ass = steadyUrlMapper.queryList(lineIndex, timeID);
		if(ass != null && ass.size()>0){
			for(int n=0;n<ass.size();n++){
				String typename = TypeCodeEnum.getMsgByCode(ass.get(n).getTypeCode());
				steadydescribe = steadydescribe + typename + ass.get(n).getLimitNum() + "次,";
			}
		}
    	if(steadydescribe.length() > 0){
    		steadydescribe = steadydescribe.substring(0, steadydescribe.length()-1);
        	result.setSteadyInfo(steadydescribe);
    	}
    	
    	//获取暂态消息详情
    	List<StatisticsEvent> eventInfo = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	List<EventInfoDetail> tmp = statisticsMapper.eventDetailList(lineIndex, sdf.format(timeID));
    	if(tmp.size() > 0 ){
    		for(EventInfoDetail event : tmp){
    			StatisticsEvent eventdata = new StatisticsEvent();
    			String describe = "";
    			describe = describe + event.getLineName() + "于" + sdf1.format(event.getTimeID())
    				+ "." + String.valueOf(event.getMs()) + "发生暂降事件,特征幅值:" + event.getEventValue() + "%,持续时间:" 
    				+ event.getPersistTime() +"s";
    			eventdata.setDescribe(describe);
    			eventdata.setEventdetail_index(event.getEventDetailIndex());
    			eventInfo.add(eventdata);
    		}
    		result.setEventInfo(eventInfo);
    	}
		
		return result;
	}
}