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

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

import android.os.Handler;
import android.os.Message;
import cn.net.caoyao.cpzshandset.bean.DriverInfoBean;
import cn.net.caoyao.cpzshandset.bean.InvoiceBean;
import cn.net.caoyao.cpzshandset.bean.ShipmentOnlineOrderBean;
import cn.net.caoyao.cpzshandset.bean.ShipmentUploadRecordBean;
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.DriverInfoBll;
import cn.net.caoyao.cpzshandset.service.bll.InvoiceBll;
import cn.net.caoyao.cpzshandset.service.bll.ShipmentUploadStatusBll;
import cn.net.cpzslibs.prot.Prot02Logout;
import cn.net.cpzslibs.prot.SocketCreate;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20025ReqBean;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20030ReqBean;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20030ResBean;
import cn.net.cpzslibs.prot.handset.newProt.Prot20025SubmitHandsetParameter;
import cn.net.cpzslibs.prot.handset.newProt.Prot20030SubmitYthScannerOrder;

public class YthScannerSubmitInvoiceRunable implements Runnable {
	private Handler mHandler;
	private long deveicedId; 
	private DriverInfoBll driverInfoBll;
	private InvoiceBll invoiceBll;
	private DriverInfoBean driverInfoBean;
	private Prot20025ReqBean reqBean;
	private String account;
	private ShipmentUploadStatusBll uploadStatusBll;
	private ShipmentOnlineOrderBean orderBean;
	public YthScannerSubmitInvoiceRunable(Handler mHandler, String account,long deveicedId,DriverInfoBean driverInfoBean,DriverInfoBll driverInfoBll,InvoiceBll invoiceBll,Prot20025ReqBean reqBean,ShipmentOnlineOrderBean orderBean,ShipmentUploadStatusBll uploadStatusBll) {
		this.mHandler = mHandler;
		this.deveicedId = deveicedId;
		this.invoiceBll = invoiceBll;
		this.driverInfoBll = driverInfoBll;
		this.driverInfoBean = driverInfoBean;
		this.reqBean = reqBean;
		this.account = account;
		this.uploadStatusBll = uploadStatusBll;
		this.orderBean = orderBean;
//		LogUtil.d(TConfig.Echo, "上传的JSON数据：" + reqBean.toString());
	}

	@Override
	public void run() {
		SocketCreate create = new SocketCreate(Config.ip, Config.port, String.valueOf(deveicedId));
		try {
			create.createLoginCpzsHandset(deveicedId);
			if (create.isResult()) {
				submitHandsetParameter(create);
				submitTruckInvoice(create);
			} else {
				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 submitHandsetParameter(SocketCreate create) throws IOException{
		LogUtil.d(TConfig.Echo, "reqBean:" +reqBean.toString());
		Prot20025SubmitHandsetParameter submitHandsetParameter = new Prot20025SubmitHandsetParameter();
		submitHandsetParameter.submitHandsetParameter(create, reqBean);
	}

	private void submitTruckInvoice(SocketCreate create) throws IOException{
		Prot20030SubmitYthScannerOrder submitSAInvoice = new Prot20030SubmitYthScannerOrder();
		Prot20030ReqBean prot20030Req = getProt20030ReqBean(driverInfoBean);
		int submitCount = prot20030Req.getLabel_id().size();
//		LogUtil.e(TConfig.Echo, "上传的信息：" + prot20030Req.toString());
		submitSAInvoice.submitYthScannerOrder(create, prot20030Req);
		Prot20030ResBean resBean = submitSAInvoice.getResBean();
		//{"count":10,"errcode":10024,"exception":2}
		//{"count":0,"errcode":0,"exception":0}
		//{"count":6,"errcode":10025,"exception":1,"list":[{"label_id":"1000000000167831"},{"label_id":"1000000000174191"},{"label_id":"1000000000180415"},{"label_id":"1000000000195990"},{"label_id":"1000000000208008"},{"label_id":"1000000000211387"}]}
//		LogUtil.e(TConfig.Echo, "t提交订单返回结果：" + resBean.toString());
		if (resBean.getResult() == 0) {
//			driverInfoBll.deleteTruckInvoiceById(driverInfoBean);
			switch(resBean.getException()){
			case 0:
//				driverInfoBll.deleteOrderBeanById(driverInfoBean.getId());
				
				insertUploadSuccessInfo(driverInfoBean,submitCount);
				driverInfoBll.deleteDriverInfoById(driverInfoBean);
				Message message = mHandler.obtainMessage(RequestConstant.PROT_SUBMIT_INVOICE_10051_SUCCESS);
				message.arg1 = submitCount;
				mHandler.sendMessage(message);
				break;
			case 1:
				//重复发货
//				driverInfoBll.updateOnlineInvoiceStatusById(resBean.getErrcode(), Config.SUBMIT_STATUS_SUBMIT_FAIL, driverInfoBean.getId());
				driverInfoBll.updateDriverStatusById(resBean.getErrcode(), Config.SUBMIT_STATUS_SUBMIT_FAIL, driverInfoBean.getId());
				if (resBean.getList() != null) {
					invoiceBll.updateLabelStatusRepeatByLabelIdList(resBean.getList());
				}
				Message repeatMsg = mHandler.obtainMessage(RequestConstant.PROT_SUBMIT_INVOICE_10051_REPEAT_FAIL);
				repeatMsg.obj = resBean;
				mHandler.sendMessage(repeatMsg);
				break;
			case 2:
				//发货数目超过司机的指定数据
//				driverInfoBll.updateOnlineInvoiceStatusById(resBean.getErrcode(), Config.SUBMIT_STATUS_SUBMIT_FAIL, driverInfoBean.getId());
				driverInfoBll.updateDriverStatusById(resBean.getErrcode(), Config.SUBMIT_STATUS_SUBMIT_FAIL, driverInfoBean.getId());
				Message overCountMsg = mHandler.obtainMessage(RequestConstant.PROT_SUBMIT_INVOICE_10051_OVER_COUNT_FAIL);
				overCountMsg.obj = resBean;
				mHandler.sendMessage(overCountMsg);
				break;
			}
			
		} else {
//			driverInfoBll.updateOnlineInvoiceStatusById(resBean.getErrcode(), Config.SUBMIT_STATUS_SUBMIT_FAIL, driverInfoBean.getId());
			driverInfoBll.updateDriverStatusById(resBean.getErrcode(), Config.SUBMIT_STATUS_SUBMIT_FAIL, driverInfoBean.getId());
			Message msg = mHandler.obtainMessage(RequestConstant.PROT_SUBMIT_INVOICE_10051_FAIL);
			msg.obj = resBean;
			mHandler.sendMessage(msg);
		}
		
	}



	private void insertUploadSuccessInfo(DriverInfoBean dB, int submitCount) {
		ShipmentUploadRecordBean rB = new ShipmentUploadRecordBean();
		rB.setDriver_id(dB.getId());
		rB.setOrder_id(dB.getOrder_id());
		rB.setDriver_name(dB.getDriver_name());
		rB.setDriver_tel(dB.getDriver_phone());
		rB.setCar_no(dB.getCar_no());
		if (orderBean != null) {
			rB.setOrder_no(orderBean.getOrderId());
			rB.setClient_user(orderBean.getClientUser());
			rB.setBusi_unit(orderBean.getBusiUnit());
			rB.setProduct_name(orderBean.getProductName());
		}
		rB.setLabel_count(submitCount);
		uploadStatusBll.insertUploadStatus(rB);
	}

	private Prot20030ReqBean getProt20030ReqBean(DriverInfoBean truckInvoice){
		Prot20030ReqBean reqBean = new Prot20030ReqBean();
		reqBean.setAccount(account);
		reqBean.setId(String.valueOf(truckInvoice.getId()));
		reqBean.setLabel_id(getTruckLabelList(truckInvoice));
		return reqBean;
	}

	private List<String> getTruckLabelList(DriverInfoBean truckInvoice) {
		List<String> labels = new ArrayList<String>();
//		List<InvoiceBean> invoices = invoiceBll.queryTruckInvoicesBylId(truckInvoice);
		List<InvoiceBean> invoices = invoiceBll.queryOnlineTruckInvoicesBylId(truckInvoice.getId());
		for (InvoiceBean invoice : invoices) {
			labels.add(invoice.getLabelid());
		}
		return labels;
	}

}
