package com.offcn.TestUnti;

import java.util.HashMap;

import com.offcn.interfaces.API;


/**
 * 反射工具类
 * 
 * @author yaoshuai
 *
 */
public class Reflect_api {

	public Class<?> ReflecClass(HashMap<String, Object> data) {
		String ClassName = Reflect_api.getRefleserviceUrlxClassName(data);
		Class<?> c = null;
		int count=1;
		while(true){
			try {
				c = Class.forName(ClassName);
				break;
			}catch (ClassNotFoundException e) {
				String serviceUrl = data.get("serviceUrl").toString();
				String CalssName_new = serviceUrl.split("/")[serviceUrl.split("/").length - 1];
				String system = MapUtil.getValue("system", data);
				String classRoute = ReadProperties.GetPropertyByKey(system);
				ClassName=classRoute+"process."+CalssName_new;
//				String system = MapUtil.getValue("system", data);
//				String classRoute = ReadProperties.GetPropertyByKey(system);
				count++;
//				if(!classRoute.equals("com.offcn.api.xyzb.liucheng")){
////					String serviceUrl = data.get("serviceUrl").toString();
////					ClassName = serviceUrl.split("/")[serviceUrl.split("/").length - 1];
//					String classRoute1 = ReadProperties.GetPropertyByKey("gmysx");
//					ClassName=ClassName.replaceAll(classRoute, classRoute1);
//				}
			} 
			if(count==3){
				Log.logError("Reflect_api,ReflexClass Error，Classname=" + ClassName);
				break;
			}
		}
		return c;
	}
	public Object ReflecObj(Class<?> c){
		Object obj = null;
		try{
			obj = c.newInstance();
		}catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return obj;
	}

	// 获得数据中serviceUrl所对应的类名
	private static String getRefleserviceUrlxClassName(
			HashMap<String, Object> data) {
		String serviceUrl = data.get("serviceUrl").toString();
		String CalssName = serviceUrl.split("/")[serviceUrl.split("/").length - 1];

		String system = MapUtil.getValue("system", data);
		
		String classRoute = ReadProperties.GetPropertyByKey(system);
//		if ("/order/insert".equals(serviceUrl)) {
//		if (serviceUrl.indexOf("/order/insert")!=-1) {
//			return classRoute+CalssName + "_dd";
//		}

		//xuwen-添加对商户（sh）的支持
//		if("zy".equals(system) || "sh".equals(system) || "dk".equals(system) ){
//			String classReflectName = ReadProperties.GetPropertyByClassName(serviceUrl);
//			if (	classReflectName!=null 	) {
//				return classRoute+ classReflectName;
//			}
//		}
		if("xyzb".equals(system)){
			String classReflectName = ReadProperties.GetPropertyByClassName(serviceUrl,system);
			if (	classReflectName!=null 	) {
				return classRoute+ classReflectName;
			}
		}
		return classRoute+CalssName;
	}
}
