package com.njcn.serviceimpl.user;

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

import javax.annotation.Resource;

import com.njcn.serviceimpl.weeklyreport.WeeklyReportDataServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.njcn.mapper.configuration.GDInforMapper;
import com.njcn.mapper.user.TfLgTreeMapper;
import com.njcn.pojo.user.TfLgTree;
import com.njcn.service.user.TfLgTreeService;
import com.njcn.utils.UserUtil;
import com.njcn.utils.AppConfig;

@Service
public class TfLgTreeServiceImpl implements TfLgTreeService {
	private static final Logger logger = LoggerFactory.getLogger(TfLgTreeServiceImpl.class);
	
	@Autowired
	private AppConfig appConfig;
	
	@Autowired
	private UserUtil userUtil;
	
	@Resource
	private TfLgTreeMapper tfLgMapper;
	
	@Resource
	private GDInforMapper gdInforMapper;
	
	@Override
	public List<TfLgTree> getTfLgTree() {
		String dky="设备树";

		int pIdNumber=Integer.parseInt(appConfig.getPidNumber());
		
		List<TfLgTree> list = new ArrayList<TfLgTree>();

		List<Integer> index= userUtil.allGD();
		
 		List<Integer> suIndex= userUtil.listSu();//获取变电站Id
		
		List<TfLgTree> gdInfo=tfLgMapper.getGdInfo(index);
		
		if (gdInfo == null) {
			TfLgTree dto = new TfLgTree();
			dto.setId(100);
			dto.setNodeType(0);
			dto.setpId(0);
			dto.setPpId(0);
			dto.setName(dky);
			list.add(dto);
			return list;
		}
		
		List<TfLgTree> subInfo=tfLgMapper.getSubInfo(index);//获取变电站
		if (subInfo == null) {
			TfLgTree dt = new TfLgTree();
			dt.setId(100);
			dt.setNodeType(0);
			dt.setpId(0);
			dt.setPpId(0);
			dt.setName(dky);
			list.add(dt);

			Map<Integer, Integer> value = new HashMap<Integer, Integer>();
			int p = pIdNumber;
			for (int i=0;i<gdInfo.size();i++) {
				TfLgTree dto = new TfLgTree();
				dto.setId(++p);
				dto.setName(gdInfo.get(i).getName());
				dto.setpId(100);
				dto.setPpId(100);
				dto.setNodeIndex(gdInfo.get(i).getNodeIndex());
				dto.setNodeType(1);
				list.add(dto);
				value.put(gdInfo.get(i).getNodeIndex(), p);
			}

			return list;
		}
        
		List<TfLgTree> transInfo=tfLgMapper.getTransInfo(suIndex);//获取变压器
		if (transInfo == null) {
			TfLgTree dt = new TfLgTree();
			dt.setId(100);
			dt.setNodeType(0);
			dt.setpId(0);
			dt.setPpId(0);
			dt.setName(dky);
			list.add(dt);

			Map<Integer, Integer> value1 = new HashMap<Integer, Integer>();
			int p1 = pIdNumber*2;
			for (int i=0;i<subInfo.size();i++) {
				TfLgTree dto = new TfLgTree();
				dto.setId(++p1);
				dto.setName(subInfo.get(i).getName());
				dto.setpId(100);
				dto.setPpId(100);
				dto.setNodeIndex(subInfo.get(i).getNodeIndex());
				dto.setNodeType(1);
				list.add(dto);
				value1.put(gdInfo.get(i).getNodeIndex(), p1);
			}

			return list;
		}

		TfLgTree dt = new TfLgTree();
		dt.setId(100);
		dt.setNodeType(0);
		dt.setpId(0);
		dt.setPpId(0);
		dt.setName("变压器策略");
		list.add(dt);
        
		//供电公司
		Map<Integer, Integer> value = new HashMap<Integer, Integer>();
		int p = pIdNumber;
		for (int i=0;i<gdInfo.size();i++) {
			TfLgTree dto = new TfLgTree();
			dto.setId(++p);
			dto.setName(gdInfo.get(i).getName());
			dto.setpId(100);
			dto.setPpId(100);
			dto.setNodeIndex(gdInfo.get(i).getNodeIndex());
			dto.setNodeType(1);
			list.add(dto);
			value.put(gdInfo.get(i).getNodeIndex(), p);
		}
        
		//变电站
		Map<Integer, Integer> value1 = new HashMap<Integer, Integer>();
		int p1 = pIdNumber*2;
		for (int i=0;i<subInfo.size();i++) {
			TfLgTree dto = new TfLgTree();
			dto.setId(++p1);
			dto.setName(subInfo.get(i).getName());
			dto.setpId(value.get(subInfo.get(i).getpId()));
			dto.setPpId(subInfo.get(i).getpId());
			dto.setNodeIndex(subInfo.get(i).getNodeIndex());
			dto.setNodeType(2);
			list.add(dto);
			value1.put(subInfo.get(i).getNodeIndex(), p1);
		}
		
		//变压器
		Map<String, Integer> value2 = new HashMap<String, Integer>();
		int p2 = pIdNumber*3;
		for (int i=0;i<transInfo.size();i++) {
			 TfLgTree dto = new TfLgTree();
			 dto.setId(++p2);
			 dto.setName(transInfo.get(i).getName());
			 if(value1.get(transInfo.get(i).getpId())==null){
				 dto.setpId(100);
			 }else{
				 dto.setpId(value1.get(transInfo.get(i).getpId())); 
			 }
			 dto.setPpId(transInfo.get(i).getpId());
			 dto.setRdIndex(transInfo.get(i).getRdIndex());
			 dto.setNodeType(2);
			 list.add(dto);
			 value2.put(transInfo.get(i).getRdIndex(), p2);
		}
		return list;
	}
	
	
	
}
