package com.ibeetl.admin.flow.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

public class IopUtil {

	private static final Logger logger = LogManager.getLogger(IopUtil.class);
	
	@Value("${iop_center}")
	private String iopCenter;
	// @Value("${center.iop_center}")
	// private static String iopcenter;

	// private static String iopcenter="http://100.0.5.3:9990/ecb/ecb";

	/**
	 * 订单号规则
	 * 
	 * @param photono
	 * @return
	 * @throws Exception
	 */
	public static String AssemorderID(String photono) throws Exception {
		try {
			if (StringUtils.isEmpty(photono)) {
				throw new Exception("Parameter<trans_type> is NULL!");
			}
			String timelong = System.currentTimeMillis() + "";
			return timelong + photono;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 将实体转换成xml并发送请求
	 * 
	 * @param header
	 * @param body
	 * @param footer
	 * @return
	 * @throws Exception
	 */
	public static <T> String sendinter(XmlHeader header, T body,
			XmlFooter footer,String optype) throws Exception {
		long starttime = System.currentTimeMillis();
		String CHANNEL_CODE = null;
		String PWD = null;
		String resultInfo = null;
		try {
			if (header == null || body == null || footer == null) {
				throw new Exception(
						"Parameter<header or body or footer> is NULL!");
			}

			String inXmlMsg = XmlUtil.BeantoXML(header, body, footer);
			System.out.println("inXmlMsg = " + inXmlMsg);
			if (StringUtils.isEmpty(inXmlMsg)) {
				throw new Exception("Generated  data  is NULL!");
			}

			CHANNEL_CODE = header.getCHANNEL_CODE();
			PWD = header.getPWD();
			if (StringUtils.isEmpty(PWD) || StringUtils.isEmpty(CHANNEL_CODE)) {
				throw new Exception("PWD or CHANNEL_CODE is NULL!");
			}
			HttpData httpData = new HttpData();
			httpData.setMethod("POST");
			// TODO写到配置文件
			httpData.setUrl(optype);
			inXmlMsg = HttpClientSt.formatXml(inXmlMsg, CHANNEL_CODE, PWD);
			System.out.println("---- inXmlMsg ----" + inXmlMsg);
			httpData.setData(inXmlMsg);
			resultInfo = HttpClientSt.getInfo(httpData);
			resultInfo = HttpClientSt.parseX(resultInfo, PWD);
			System.out.println("2 = " + resultInfo);
			resultInfo = XmlUtil.xml2JSON(resultInfo).toString();
			long endtime = System.currentTimeMillis();
			logger.info("此次请求共花费：" + (endtime - starttime) / 1000 + "秒");
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return resultInfo;
	}
	// /**
	// * 发送请求并将结果转成JSOn
	// * @param <T>
	// * @param header
	// * @param body
	// * @param footer
	// * @return
	// * @throws Exception
	// */
	// public static <T> JSONObject dealresult(XmlHeader header,T body,XmlFooter
	// footer) throws Exception{
	// JSONObject json= new JSONObject();
	// try {
	// if (body==null||header==null||footer==null) {
	// throw new Exception("Parameter<open or  result> is NULL!");
	// }
	// String result=sendinter(header, body, footer);
	// json=XmlUtil.xml2JSON(result);
	// logger.info("请求接口返回的数据："+json);
	// } catch (Exception e) {
	// e.printStackTrace();
	// throw e;
	// }
	// return json;
	// }

}
