package com.iamberry.wechat.tools;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class GetWxOrderno {

    private static final Logger log = Logger.getLogger(GetWxOrderno.class);
	/**
	 * description:获取预支付id
	 */
	public static Object getPayNo(String url, String xmlParam) {
		try {

			String jsonStr = HttpClient431Util.doPostContent(xmlParam, "application/json", url);
			// 判断
			if (jsonStr.indexOf("FAIL") != -1) {
				WxPrepayIdErrorResult errorResult = (WxPrepayIdErrorResult) MessageUtil.xmlToBean(jsonStr,
						WxPrepayIdErrorResult.class);
				return errorResult;
			}
			Map<String, String> map = doXMLParse(jsonStr);
			String prepay_id = (String) map.get("prepay_id");
			return prepay_id;
		} catch (Exception e1) {
			log.error(e1.getMessage());
		}
		return null;
	}

	/**
	 * description:获取扫码支付连接
	 * 
	 * @param url
	 * @param xmlParam
	 * @return
	 * @author ex_yangxiaoyi
	 * @see
	 */
	public static String getCodeUrl(String url, String xmlParam) {
		String code_url = null;
		try {
			String jsonStr = HttpClient431Util.doPostContent(xmlParam, "JSON", url);
			if (jsonStr.indexOf("FAIL") != -1) {
				return code_url;
			}
			Map<String, String> map = doXMLParse(jsonStr);
			code_url = (String) map.get("code_url");
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
		return code_url;
	}

	/**
	 * 解析xml,返回第一级元素键值对。
	 * 
	 * @param strxml
	 * @return
	 * @throws JDOMException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> doXMLParse(String strxml) throws Exception {
		if (null == strxml || "".equals(strxml)) {
			return null;
		}

		Map<String, String> m = new HashMap<String, String>();
		InputStream in = string2Inputstream(strxml);
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(in);
		Element root = doc.getRootElement();
		List<Element> list = root.getChildren();
		Iterator<Element> it = list.iterator();
		while (it.hasNext()) {
			Element e = (Element) it.next();
			String k = e.getName();
			String v = "";
			List<Element> children = e.getChildren();
			if (children.isEmpty()) {
				v = e.getTextNormalize();
			} else {
				v = getChildrenText(children);
			}

			m.put(k, v);
		}

		// 关闭io
		in.close();

		return m;
	}

	/**
	 * 获取子结点的xml
	 * 
	 * @param children
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public static String getChildrenText(List<Element> children) {
		StringBuffer sb = new StringBuffer();
		if (!children.isEmpty()) {
			Iterator<Element> it = children.iterator();
			while (it.hasNext()) {
				Element e = (Element) it.next();
				String name = e.getName();
				String value = e.getTextNormalize();
				List<Element> list = e.getChildren();
				sb.append("<" + name + ">");
				if (!list.isEmpty()) {
					sb.append(getChildrenText(list));
				}
				sb.append(value);
				sb.append("</" + name + ">");
			}
		}

		return sb.toString();
	}

	public static InputStream string2Inputstream(String str) {
		return new ByteArrayInputStream(str.getBytes());
	}

}