package cn.net.caoyao.cpzshandset.service.thread.impl.ythPunit;

import java.io.IOException;
import java.net.SocketTimeoutException;

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20032ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20046ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20046ReqYthOrder;

public class YthPunitReqOrderByIdRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private Prot20032ReqBean reqBean;
	public YthPunitReqOrderByIdRunable(Handler mHandler, long deveicedId,Prot20032ReqBean reqBean) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.reqBean = reqBean;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				Prot20046ReqYthOrder yOrder = new Prot20046ReqYthOrder();
				yOrder.reqOrderInfoByOrderId(create, reqBean);
				Prot20046ResBean resBean =  yOrder.getResBean();
				
				Message msg = mHandler.obtainMessage(RequestConstant.PROT_REQ_YTH_ORDER_INFO_SUCCESS);
				msg.obj = resBean;
				mHandler.sendMessage(msg);
				
			} else {
//				mHandler.sendEmptyMessage(ThreadConstant.)
				Message loginFailMsg = mHandler.obtainMessage();
				loginFailMsg.obj = create.getErrMsg();
				loginFailMsg.arg1 = create.getiErrCode();
				loginFailMsg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				mHandler.sendMessage(loginFailMsg);
			}
		}catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} finally{
			new Prot02Logout().dealLogout(create);
		}
	}
}
