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

import java.io.IOException;
import java.net.BindException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import android.os.Handler;
import android.os.Message;

import cn.net.caoyao.cpzshandset.bean.SaleBean;
import cn.net.caoyao.cpzshandset.bean.UploadBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.common.config.RequestConstant;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.service.bll.BatchLabelBll;
import cn.net.caoyao.cpzshandset.service.bll.DealerOutBll;
import cn.net.caoyao.cpzshandset.service.bll.UploadStatusBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.bean.res.ProtStatusResBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20002ReqBatchBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20027ReqBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20076SubmitDealerOut;

public class CommonSubmitSingleBatchDealerOutThread implements Runnable {
	private long deviceId;
	private DealerOutBll bll;
	private BatchLabelBll batchBll;
	private UploadStatusBll uploadStatusBll;
	private SaleBean saleBean;
	private Prot20002ReqBatchBean batchBean;
	private Handler mHandler;
	private int option_type = HandsetOperation.OPERATION_DEALER_OUT_CONSTANT.getOption();

	public CommonSubmitSingleBatchDealerOutThread(Handler mHandler, long deviceId, DealerOutBll bll,
			BatchLabelBll batchBll, UploadStatusBll uploadStatusBll, SaleBean saleBean,
			Prot20002ReqBatchBean batchBean) {
		this.mHandler = mHandler;
		this.deviceId = deviceId;
		this.bll = bll;
		this.uploadStatusBll = uploadStatusBll;
		this.saleBean = saleBean;
		this.batchBean = batchBean;
		this.batchBll = batchBll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, "");
		try {
			LogUtil.d(TConfig.Echo, "登录");
			create.createLoginCpzsHandset(deviceId);
			if (create.isResult()) {
				submitDealerOutInfo(create);
			} else {
				Message msg = new Message();
				msg.what = RequestConstant.REQUEST_PROT01_LOGIN_FAIL;
				msg.arg1 = create.getiErrCode();
				msg.obj = create.getErrMsg();
				mHandler.sendMessage(msg);
			}

		} catch (BindException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (ConnectException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (SocketTimeoutException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL_SOCKET_TIMEOUT);
		} catch (UnknownHostException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} catch (IOException e) {
			e.printStackTrace();
			mHandler.sendEmptyMessage(RequestConstant.REQUEST_FAIL);
		} finally {
			new Prot02Logout().dealLogout(create);
		}
	}

	private void submitDealerOutInfo(SocketCreate create) throws IOException, SocketTimeoutException {
		Prot20076SubmitDealerOut activate = new Prot20076SubmitDealerOut();
		activate.submitDealerOut(create, getReqBean());
		ProtStatusResBean resBean = activate.getResBean();
		if (resBean == null) {
			mHandler.sendEmptyMessage(RequestConstant.SUBMIT_ACTIVATE_WITHOUT_RES);
			return;
		}
		

		if (resBean.getResult() == 0) {
			// 上传成功，插入记录到提交状态表
			uploadStatusBll.insertUploadStatus(getUploadBean(saleBean));
			bll.deleteSalesById(saleBean);
		} else {
			batchBll.insertBatch(option_type, saleBean.getId(), batchBean);
			bll.updateSalesSubmitStatus(Config.SUBMIT_STATUS_SUBMIT_FAIL, resBean.getErrcode(), saleBean.getId());
		}
		Message msg = mHandler.obtainMessage(RequestConstant.SUBMIT_ACTIVATE_WITH_RES);
		msg.obj = resBean;
		msg.what = RequestConstant.SUBMIT_ACTIVATE_WITH_RES;
		mHandler.sendMessage(msg);

	}

	private UploadBean getUploadBean(SaleBean saleBean) {
		UploadBean b = new UploadBean();
		b.setLabel_count(batchBll.queryBatchCountById(option_type, saleBean.getId()));
		b.setOption_type(option_type);
		return b;
	}

	private Prot20027ReqBean getReqBean() {
		Prot20027ReqBean reqBean = new Prot20027ReqBean();
		reqBean.setOpt(1);//1是出库 2是返库
		reqBean.setFlag(saleBean.getOption_mode());
		reqBean.setArea_id(saleBean.getSales_area());
		reqBean.setDealer_id((int)saleBean.getDealer_id());
		reqBean.setActual_num(saleBean.getPlan_num());
		reqBean.setRemark(saleBean.getDescribe());
		reqBean.setBatcher(batchBean);
		reqBean.setCreate_time(saleBean.getCreateDate());
		LogUtil.i(TConfig.Echo, "提交的数据：" + reqBean.toString());
		return reqBean;
	}

}
