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

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

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.ShipmentOnlineOrderBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
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.InvoiceBll;
import cn.net.caoyao.cpzshandset.service.bll.OnlineTruckInvoiceBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20031ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20031ReqYthOrderList;

public class YthScannerReqOrderListRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId;
	private OnlineTruckInvoiceBll truckInvoiceBll;
	private InvoiceBll invoiceBll;

	public YthScannerReqOrderListRunable(Handler mHandler, long deveicedId, OnlineTruckInvoiceBll truckInvoiceBll,
			InvoiceBll invoiceBll) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.truckInvoiceBll = truckInvoiceBll;
		this.invoiceBll = invoiceBll;
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				Prot20031ReqYthOrderList yOrder = new Prot20031ReqYthOrderList();
				yOrder.reqYthOrderList(create);
				Prot20031ResBean orderBean = yOrder.getResBean();
				LogUtil.e(TConfig.Echo, "请求未发货列表成功：" + orderBean.toString());
				if (orderBean.getResult() == 0) {
					insertTruckInvoice(orderBean);
				}

				Message msg = mHandler.obtainMessage(RequestConstant.PROT_REQ_YTH_ORDER_INFO_SUCCESS);
				msg.obj = orderBean;
				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);
		}
	}

	private void insertTruckInvoice(Prot20031ResBean orderBean) {
		if (orderBean.getList() != null && orderBean.getList().size() > 0) {
			List<ShipmentOnlineOrderBean> tBeans = truckInvoiceBll.queryAllYthOrderASC();
			for (ShipmentOnlineOrderBean b : tBeans) {
				int labelCount  = invoiceBll.queryOnlineTruckLabelCountByLId(b.getId());
				if (labelCount <= 0) {
					truckInvoiceBll.deleteOrderBeanById(b.getId());
				}
			}

			truckInvoiceBll.insertOnlineInvoiceList(orderBean.getList());
		}
	}

}
