package elect;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import elect.ValidationCodeGenerater;
import elect.MD5Digest;
import elect.SocketConnection;

public class YDLWTestClient {
	public static void main(String[] args) {
		YDLWTestClient cl = new YDLWTestClient();
		String now = System.currentTimeMillis() + "";//请求时间
		//查询
		cl.test(YDLWTestClient.generateRequest("gz711", "35170030",now,"888888", "4601", "080003004771250#999999"));
		//缴费申请,客户号#账单日期#缴费月数#总电费#客户名称#操作员号
		//cl.test(YDLWTestClient.generateRequest("gz711", "35170030",now,"888888", "4801", "0800050029253040#999999#1#20000#epaylinks#gz711#00134840401234"));
		
	
	}
	public static String asciiToString(String value)
	
    {
        StringBuffer sbu = new StringBuffer();
        String[] chars = value.split(",");
        for (int i = 0; i < chars.length; i++) {
            sbu.append((char) Integer.parseInt(chars[i]));
        }
        return sbu.toString();
    }

	
	/**
	 * 获取签名串
	 */
	public String getSign(String storeId,String terminalId,String randomSignNum,String password){
		String sign = MD5Digest.getMD5().getMD5ofStr(
				storeId 
				+ terminalId 
				+ randomSignNum
				+ MD5Digest.getMD5().getMD5ofStr(password).toLowerCase()).toLowerCase();
		return sign;
	}
	
	/**
	 * 获取签名串
	 */
	public String getRandomSignNum(){
		return new Date().getTime() + "";
	}
	
	/**
	 * 生成请求指令: 总数据长度(3)+1008+业务数据长度(3)+商户编号|终端编号|签名随机数|签名值|请求码|请求数据+检验码(2)
	 * 
	 * @param storeId
	 *            商户编号
	 * @param terminalId
	 *            终端编号
	 * @param hexPassword
	 *            MD5加密后的密码
	 * @param requestCode
	 *            请求码
	 * @param requestContent
	 *            请求数据
	 * @return
	 */
	public static String generateRequest(String storeId, String terminalId,String now,
			String hexPassword, String requestCode,String requestContent) {
		if (storeId == null || terminalId == null || requestCode == null
				|| requestContent == null) {
			System.out.println("storeId == null || terminalId == null || "
					+ "requestCode == null|| requestContent == null");
		}
		String separator = "|";
		//long now = System.currentTimeMillis();
		StringBuffer sb = new StringBuffer();
		sb.append(storeId).append(separator);
		// 终端编号
		sb.append(terminalId).append(separator);
		// 签名随机数
		sb.append(now).append(separator);
		// 签名值: Md5(商户编号+终端编号+请求时间+md5(密码))
		sb.append(
				MD5Digest.getMD5().getMD5ofStr(
						storeId + terminalId + now
								+ MD5Digest.getMD5().getMD5ofStr(hexPassword).toLowerCase())
						.toLowerCase()).append(separator);
		// 请求码
		sb.append(requestCode).append(separator);
		// 请求数据
		sb.append(requestContent);
		sb.insert(0, convert(sb.length()));
		sb.insert(0, "1008"); // 1008 标识入口:IE版调用取1008
		//System.out.println(sb.toString());
		byte[] buf = ValidationCodeGenerater.generateValidatitonCode(sb.toString().getBytes());
		System.out.println(new String(buf));
		return new String(buf);
	}

	private static String convert(int number) {
		final int length = 3;
		StringBuffer sb = new StringBuffer();
		sb.append(number);
		while (sb.length() < length) {
			sb.insert(0, "0");
		}
		return sb.toString();
	}
	
	public void test(String sendData){
		SocketConnection client = SocketConnection.getConnection();

		boolean isConnect = client.connect("172.20.4.114",8887,1000*60);//测试地址
		if (isConnect == false) {
			System.out.println("客户端连接服务器失败");
			return;
		}
		try {
			byte[] contents = sendData.getBytes("GBK");
			if(!client.write(contents)){
				System.out.println("客户端:发送数据失败！");
				return;
			}
			byte[] backDatas= client.read();
			if(backDatas ==null || backDatas.length <= 0){
				System.out.println("客户端返回数据为空！");
			}else{
				System.out.println("客户端：接收服务器的数据："+new String(backDatas,"GBK"));			
			}
		
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}finally{
			client.disconnect();
		}
	}
	
}
