package com.bcxin.ins.third.gzx.huatai;


import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class AppInterfaceTest {
	
	// 测试配置信息 & 很重要
	public final static String ENCRYPT_VAL = "123456@HT"; // MD5加密的值
	public final static String CHANNELCODE = "100000"; // 渠道编码
	
	/**
	 * 
	 * @param toURL
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public static String requestService(String toURL,String data) throws Exception {
		StringBuffer bs = new StringBuffer();
			URL url = new URL(toURL);
			HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
			urlcon.setRequestMethod("POST");
			urlcon.setUseCaches(false);
			urlcon.setConnectTimeout(30000);
			urlcon.setReadTimeout(30000);
			urlcon.setDoInput(true);
			urlcon.setDoOutput(true);
			urlcon.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			OutputStream out = urlcon.getOutputStream();
			out.write(data.getBytes("UTF-8"));
			out.flush();
			out.close();
			urlcon.connect();
			InputStream is = urlcon.getInputStream();
			BufferedReader buffer = new BufferedReader(
					new InputStreamReader(is,"UTF-8"));

			String l = null;
			while ((l = buffer.readLine()) != null) {
				bs.append(l);
			}
		return bs.toString();
	}
	

	public void testPolicyJsonBean() throws Exception{ // 投保用例
		// ********************** 测试须知，根据不同的环境 使用 配置信息、报文、地址 *****************************
		
		// 测试报文
		//String insureJson = "{\"agreementObject\":{\"policyDeductible\":\"请自定义免赔额信息。\"},\"appntObject\":{\"appIDType\":\"01\",\"appName\":\"投保人姓名\",\"appNumber\":\"41272419880920597X\",\"appTelNumber\":\"15810395740\",\"appType\":\"1\",\"isTaxInvoice\":\"0\"},\"channelObject\":{\"bizCode\":\"101\",\"channelCode\":\"100000\",\"channelName\":\"测试渠道\",\"createTime\":\"2016-08-04 11:26:38\",\"orderId\":\"000000000000001\"},\"definedSafeObj\":{\"isDefinedSafe\":\"1\",\"rdrDTOs\":[{\"itemId\":1,\"rdrAmount\":\"100000.00\",\"rdrCode\":\"01\",\"rdrName\":\"自定义责任1\",\"rdrPremium\":\"18.00\"},{\"itemId\":1,\"rdrAmount\":\"50000.00\",\"rdrCode\":\"02\",\"rdrName\":\"自定义责任2\",\"rdrPremium\":\"0\",\"rdrRemark\":\"自定义责任2备注信息\"}]},\"insuranceObject\":{\"amount\":\"100000.00\",\"amtCur\":\"01\",\"copy\":\"1\",\"effectiveTime\":\"2016-11-03 11:26:27\",\"insuranceCode\":\"在这儿配置产品编码\",\"insuranceName\":\"在这儿配置产品名称\",\"plan\":\"A\",\"premium\":\"18.00\",\"prmCur\":\"01\",\"terminalTime\":\"2016-09-04 11:26:27\"},\"insuredObject\":[{\"insuredIDType\":\"01\",\"insuredName\":\"被保人名字\",\"insuredNumber\":\"41272419880920597X\",\"insuredTelNumber\":\"13880160953\",\"insuredType\":\"1\",\"isLegal\":\"1\"}],\"payObject\":{\"isSinglePay\":\"0\"},\"productDiffObject\":{}}";
		String insureJson="{\"agreementObject\":{\"policyDeductible\":\"6000元\"},\"appntObject\":{\"appIDType\":\"01\",\"appName\":\"张三\",\"appNumber\":\"340881198910020112\",\"appTelNumber\":\"13818181818\",\"appType\":\"2\",\"isTaxInvoice\":\"1\",\"appBirthday\":\"1989-10-02\",\"appEmail\":\"123@qq.com\",\"appGender\":\"0\",\"appAddr\":\"地址信息\",\"appContact\":\"联系人名字\",\"taxCertifi\":\"税务登记证号\",\"depositBank\":\"开户银行\",\"bankAccount\":\"银行账户\"},\"channelObject\":{\"bizCode\":\"101\",\"channelCode\":\"100000\",\"channelName\":\"测试频道\",\"createTime\":\"2016-01-27 00:00:00\",\"orderId\":\"1000000002\"},\"definedSafeObj\":{\"isDefinedSafe\":\"1\",\"rdrDTOs\":[{\"itemId\":1,\"rdrAmount\":\"6000\",\"rdrCode\":\"01\",\"rdrName\":\"意外伤害\",\"rdrPremium\":\"100\"},{\"itemId\":1,\"rdrAmount\":\"6000\",\"rdrCode\":\"02\",\"rdrName\":\"意外伤害\",\"rdrPremium\":\"100\"}]},\"insuranceObject\":{\"insuranceCode\":\"2502\",\"insuranceName\":\"公众责任险\",\"amount\":\"12000.0\",\"plan\":\"A\",\"prmCur\":\"01\",\"premium\":\"30.0\",\"amtCur\":\"01\",\"copy\":\"1\",\"effectiveTime\":\"2016-01-27 00:00:00\",\"terminalTime\":\"2016-1-29 23:59:59\"},\"insuredObject\":[{\"insuredIDType\":\"01\",\"insuredName\":\"xx1\",\"insuredNumber\":\"340881198910020112\",\"insuredTelNumber\":\"18721662166\",\"insuredType\":\"2\",\"isLegal\":\"1\"}],\"payObject\":{\"isSinglePay\":\"0\"},\"productDiffObject\":{\"reMark\":\"备注信息\"}}";
//		json={\"agreementObject\":{\"policyDeductible\":\"6000元\"},\"appntObject\":{\"appIDType\":\"01\",\"appName\":\"张三\",\"appNumber\":\"340881198910020112\",\"appTelNumber\":\"13818181818\",\"appType\":\"2\",\"isTaxInvoice\":\"1\",\"appBirthday\":\"1989-10-02\",\"appEmail\":\"123@qq.com\",\"appGender\":\"0\",\"appAddr\":\"地址信息\",\"appContact\":\"联系人名字\",\"taxCertifi\":\"税务登记证号\",\"depositBank\":\"开户银行\",\"bankAccount\":\"银行账户\"},\"channelObject\":{\"bizCode\":\"101\",\"channelCode\":\"100000\",\"channelName\":\"测试频道\",\"createTime\":\"2016-01-27 00:00:00\",\"orderId\":\"1000000002\"},\"definedSafeObj\":{\"isDefinedSafe\":\"1\",\"rdrDTOs\":[{\"itemId\":1,\"rdrAmount\":\"6000\",\"rdrCode\":\"01\",\"rdrName\":\"意外伤害\",\"rdrPremium\":\"100\"},{\"itemId\":1,\"rdrAmount\":\"6000\",\"rdrCode\":\"02\",\"rdrName\":\"意外伤害\",\"rdrPremium\":\"100\"}]},\"insuranceObject\":{\"insuranceCode\":\"2502\",\"insuranceName\":\"公众责任险\",\"amount\":\"12000.0\",\"plan\":\"A\",\"prmCur\":\"01\",\"premium\":\"30.0\",\"amtCur\":\"01\",\"copy\":\"1\",\"effectiveTime\":\"2016-01-27 00:00:00\",\"terminalTime\":\"2016-1-29 23:59:59\"},\"insuredObject\":[{\"insuredIDType\":\"01\",\"insuredName\":\"xx1\",\"insuredNumber\":\"340881198910020112\",\"insuredTelNumber\":\"18721662166\",\"insuredType\":\"2\",\"isLegal\":\"1\"}],\"payObject\":{\"isSinglePay\":\"0\"},\"productDiffObject\":{\"reMark\":\"备注信息\"}}
		//String  insureJson="{\"channelObject\":{\"bizCode\":\"101\",\"channelCode\":\"100000\",\"channelName\":\"测试频道\",\"orderId\":\"1000000002\",\"createTime\":\"2016-01-27 00:00:00\"},\"insuranceObject\":{\"insuranceCode\":\"4604\",\"insuranceName\":\"旅行险\",\"plan\":\"A\",\"prmCur\":\"01\",\"premium\":\"30.0\",\"amtCur\":\"01\",\"amount\":\"12000.0\",\"rate\":\"1.3\",\"effectiveTime\":\"2016-01-27 00:00:00\",\"terminalTime\":\"2016-1-29 23:59:59\",\"copy\":\"1\",\"docType\":\"PC401440\",\"docSN\":\"xxxxxxx\"},\"appntObject\":{\"appName\":\"张三\",\"appType\":\"2\",\"appBirthday\":\"1989-10-02\",\"appEmail\":\"123@qq.com\",\"appGender\":\"0\",\"appIDType\":\"01\",\"appNumber\":\"340881198910020112\",\"appTelNumber\":\"13818181818\",\"appAddr\":\"地址信息\",\"appContact\":\"联系人名字\",\"isTaxInvoice\":\"1\",\"taxCertifi\":\"税务登记证号\",\"depositBank\":\"开户银行\",\"bankAccount\":\"银行账户\"},\"insuredObject\":{\"insuredName\":\"xx1\",\"insuredType\":\"2\",\"insuredBirthday\":\"1989-10-02\",\"insuredEmail\":\"111@qq.com\",\"insuredGender\":\"0\",\"insuredIDType\":\"01\",\"insuredNumber\":\"340881198910020112\",\"insuredTelNumber\":\"18721662166\",\"insuredAddress\":\"被保险人地址\",\"relationship\":\"5\",\"isLegal\":\"0\",\"benefitDTOs\":[{\"benefitName\":\"xx1\",\"benefitRelation\":\"10\",\"benefitListNumber\":\"1\",\"share\":\"50\",\"benefitIDType\":\"01\",\"benefitNumber\":\"429006199203045612\"},{\"benefitName\":\"xx1\",\"benefitRelation\":\"10\",\"benefitListNumber\":\"2\",\"share\":\"50\",\"benefitIDType\":\"01\",\"benefitNumber\":\"429006199203045612\"}]},\"definedSafeObj\":{\"isDefinedSafe\":\"1\",\"rdrDTOs\":[{\"rdrCode\":\"01\",\"rdrName\":\"意外伤害\",\"rdrAmount\":\"6000\",\"rdrPremium\":\"100\",\"rdrDeductible\":\"5000元\",\"rdrRemark\":\"意外医疗费用\"},{\"rdrCode\":\"02\",\"rdrName\":\"意外伤害\",\"rdrAmount\":\"6000\",\"rdrPremium\":\"100\",\"rdrDeductible\":\"5000元\",\"rdrRemark\":\"意外医疗费用\"}]},\"payObject\":{\"isSinglePay\":\"1\",\"payMode\":\"16\",\"payDate\":\"2016-01-27 00:00:00\",\"payBankNo\":\"WXS010343324401160000007\"},\"agreementObject\":{\"policyDeductible\":\"6000元\",\"policySpec\":\"特别约定\"},\"productDiffObject\":{\"reMark\":\"备注信息\"}}";
		// 针对url传递特殊字符转译问题
		insureJson = insureJson.replaceAll("%","%25");
		
		String signature = MD5Util.MD5Encode(insureJson + ENCRYPT_VAL, "utf-8"); // 加密
		
		// 测试地址
	    String jsonStr = AppInterfaceTest.requestService("http://202.108.103.171:7777/service_platform/InsureInterface", "json="+insureJson+"&channelCode="+CHANNELCODE+"&signature="+signature);
		System.out.println("投保返回JSON串="+jsonStr);
	}
}
