package com.pqs9200.serviceimpl.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

import com.njcn.mapper.data.GridTopMapper;
import com.njcn.mapper.data.LineMapsMapper;
import com.njcn.pojo.commons.AreaGeneralData;
import com.njcn.pojo.commons.device.DeviceValue;
import com.njcn.pojo.data.GridTop;
import com.njcn.pojo.data.LineMaps;
import com.njcn.utils.UserUtil;
import com.pqs9200.mapper.weeklyreport.GetAreaPointInfoMapper;
import com.pqs9200.pojo.commons.device.DevMapInfo;
import com.pqs9200.service.data.IOverviewServer;

@Service
public class OverViewServiceImpl implements IOverviewServer {

	@Autowired
	private LineMapsMapper lineMapsMapper;
	@Autowired
	private GridTopMapper gridTopMapper;
	@Autowired
	private GetAreaPointInfoMapper areaPointInfoMapper;
	@Autowired
	private UserUtil userUtil;
	
	@Override
	public List<LineMaps> getLineMaps() {
		List<LineMaps> vdLists=new ArrayList<LineMaps>();
		List<Integer> lineList = userUtil.listLine();
		if(!lineList.isEmpty()){
			List<Integer> lineIndexsTemp = new ArrayList<>(lineList);
            if (lineIndexsTemp.size() > 100) {
                int size = lineIndexsTemp.size() / 100 + 1;
                for (int i = 0; i < size; i++) {
                    if (lineIndexsTemp.size() > 100) {
                        List<Integer> tempLineIndexs = lineIndexsTemp.subList(0, 100);
                        List<LineMaps> tmplist = lineMapsMapper.queryLineMaps(tempLineIndexs);//返回的监测点的地理信息
                        vdLists.addAll(tmplist);
                        tempLineIndexs.clear();
                    } else {
                    	List<LineMaps> tmplist = lineMapsMapper.queryLineMaps(lineIndexsTemp);//返回的监测点的地理信息
                        vdLists.addAll(tmplist);
                    }
                }
            } else {
                vdLists = lineMapsMapper.queryLineMaps(lineIndexsTemp);//返回的监测点的地理信息
            }
		}
		return vdLists;
	}
	
	@Override
	public DevMapInfo getDevMap() {
		DevMapInfo result = new DevMapInfo();
		List<LineMaps> vdLists=new ArrayList<>();
		List<DeviceValue> devcount = new ArrayList<>();
		
		List<AreaGeneralData> areaGeneral = userUtil.getAreaGeneralRun();
		int total = 0,ontotal=0,offtotal=0;
		for (AreaGeneralData areaGeneralData : areaGeneral) {
			int oncount=0;
			int offcount=0;
			DeviceValue tmp = new DeviceValue();
			tmp.setName(areaGeneralData.getName());
			
			List<Integer> indexs = areaGeneralData.getLineIndexs();
			tmp.setAmounts(String.valueOf(indexs.size()));
			
//			for(Integer index : indexs){
//				LineMaps linemap = new LineMaps();
//				linemap.setLineindex(index);
//				linemap=lineMapsMapper.selectOne(linemap);
//				if(null==linemap){
//					continue;
//				}
//				vdLists.add(linemap);
//				if (Integer.valueOf("0").equals(linemap.getStatus())) {
//					offcount++;
//		        } else {
//		        	oncount++;
//		        }
//			}
			if(indexs.size() == 0){
				continue;
			}
			List<LineMaps> tmplist = new ArrayList<>();
			tmplist = areaPointInfoMapper.getMapsBylines(indexs);
			vdLists.addAll(tmplist);
			for(LineMaps tmap : tmplist){
				if(Integer.valueOf("0").equals(tmap.getStatus())){
					offcount++;
				}else{
					oncount++;
				}
			}
			tmp.setOncount(oncount);
			tmp.setOffcount(offcount);
			devcount.add(tmp);
			total = total + indexs.size();
			ontotal = ontotal + oncount;
			offtotal = offtotal + offcount;
		}
		
		Collections.sort(devcount, new Comparator<DeviceValue>(){
            /*
             * int compare(Person p1, Person p2) 返回一个基本类型的整型，
             * 返回负数表示：p1 小于p2，
             * 返回0 表示：p1和p2相等，
             * 返回正数表示：p1大于p2
             */
            public int compare(DeviceValue p1, DeviceValue p2) {
                //按照Person的年龄进行升序排列
                if(Integer.parseInt(p2.getAmounts()) > Integer.parseInt(p1.getAmounts())){
                    return 1;
                }
                if(Integer.parseInt(p2.getAmounts()) == Integer.parseInt(p1.getAmounts())){
                    return 0;
                }
                return -1;
            }
        });

		DeviceValue all = new DeviceValue();
		all.setName("总计");
		all.setAmounts(String.valueOf(total));
		all.setOncount(ontotal);
		all.setOffcount(offtotal);
		devcount.add(0,all);
		
		result.setMaps(vdLists);
		result.setOnline(devcount);
		return result;
	}
	
	@Override
	public  List<GridTop> getGridTop() {
		List<GridTop> vdLists;
		vdLists=gridTopMapper.queryGridTop();//返回的网络拓扑图信息
		return vdLists;
	}
	
	@Override
	public List<LineMaps> queryLineMapByAssID(String eventass_index) {
		List<LineMaps> vdLists;
		vdLists=lineMapsMapper.queryLineMapByAssID(eventass_index);//返回的归一化的地理信息
		return vdLists;
	}
}
