package com.gbicc.bll.uitl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.gbicc.bll.doubt.bean.DouMap;
import com.opensymphony.oscache.util.StringUtil;

public class XmlUtil {
	
	public void copyAndUpdateXml(String srcPath,String destPath,List<DouMap> colorList,List<DouMap> dataList,String link) 
		throws JDOMException, IOException{
		
		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		if(classLoader == null){
			classLoader = XmlUtil.class.getClassLoader();
		}
		
		URL url = classLoader.getResource(srcPath);
		SAXBuilder build = new SAXBuilder();
		Document doc = build.build(url);
		
		Element root = doc.getRootElement();
		
		Element colorRange = root.getChild("colorRange");
		List colors = colorRange.getChildren("color");
		Iterator color = colors.iterator();
		int sign = 1;
		while(color.hasNext()){
			Element colorElement = (Element) color.next();
			int i = 0;
			for(i = 0; i<colorList.size(); i++){
				DouMap douMap = colorList.get(i);
				String colorGroup = douMap.getColorGroup();
				if(colorGroup.equals(sign + "")){
					colorElement.getAttribute("minValue").setValue(douMap.getMinRiskValue());
					colorElement.getAttribute("maxValue").setValue(douMap.getMaxRiskValue());	
					
					break;
				}
			}
			sign++;
		}
		
		String riskValue = "";
		
		Element data = root.getChild("data");
		List entityList = data.getChildren("entity");
		Iterator  entity = entityList.iterator();
		
		String orgCode  = "";
		while(entity.hasNext()){
			Element entityElement = (Element)entity.next();
			String idValue = entityElement.getAttribute("id").getValue();
			int i = 0;
			for(i=0; i<dataList.size(); i++){
				DouMap douMap = dataList.get(i);
				orgCode = douMap.getOrgCode();
				if(douMap.getEntityId().equalsIgnoreCase(idValue)){
					
					riskValue = douMap.getRiskValue();
					riskValue = riskValue==null?"":riskValue;
					if(riskValue.startsWith(".")){
						riskValue = "0"+riskValue;
					}
					
					entityElement.getAttribute("value").setValue(douMap.getRiskValue());
					entityElement.getAttribute("toolText").setValue(douMap.getOrgName() + ",疑点数" + douMap.getDouSum()+",风险值"+riskValue+",排名"+douMap.getRankValue());
					

					
					if(!StringUtil.isEmpty(link)){

						if(link.indexOf('?')>0){
							orgCode = "&orgCode=" + orgCode;
						} else {
							orgCode = "?orgCode=" + orgCode;
						}
						
						Attribute attribute = new Attribute("link","" + link+orgCode);
						entityElement.setAttribute(attribute);
						//colorElement.getAttribute("link").setValue("n-" + link);	
					}
					//link='n-/audit/pages/doubt/ydcx_ydtj.html'/
					break;
				}
			}
		}
		
		Element markers = root.getChild("markers");
		Element application = markers.getChild("application");
		List markerList = application.getChildren("marker");
		Iterator  marker = markerList.iterator();
		while(marker.hasNext()){
			Element markerElement = (Element)marker.next();
			String idValue = markerElement.getAttribute("id").getValue();
			int i = 0;
			for(i=0; i<dataList.size(); i++){
				DouMap douMap = dataList.get(i);
				
				orgCode = douMap.getOrgCode();
				
				if(douMap.getEntityId().equalsIgnoreCase(idValue)){
					

					riskValue = douMap.getRiskValue();
					riskValue = riskValue==null?"":riskValue;
					if(riskValue.startsWith(".")){
						riskValue = "0"+riskValue;
					}
					
					
					markerElement.getAttribute("toolText").setValue(douMap.getOrgName() + ",疑点数" + douMap.getDouSum()+",风险值"+riskValue+",排名"+douMap.getRankValue());
					

					if(!StringUtil.isEmpty(link)){

						if(link.indexOf('?')>0){
							orgCode = "&orgCode=" + orgCode;
						} else {
							orgCode = "?orgCode=" + orgCode;
						}
						
						Attribute attribute = new Attribute("link","" + link + orgCode);
						markerElement.setAttribute(attribute);
						//colorElement.getAttribute("link").setValue("n-" + link);	
					}
					//link='n-/audit/pages/doubt/ydcx_ydtj.html'/
					break;
				}
			}
		}
		
		Format format = Format.getPrettyFormat();
		format.setIndent("    ");  // 设置xml文件的缩进为4个空格
		XMLOutputter Outputter = new XMLOutputter(format.setEncoding("gb2312"));

		FileOutputStream os = new FileOutputStream(destPath);
		Outputter.output(doc, os);
		os.close();
	}
	
}
