package cn.net.caoyao.cpzshandset.service.bll;

import java.util.List;

import android.content.Context;
import cn.net.caoyao.cpzshandset.bean.InvoiceBean;
import cn.net.caoyao.cpzshandset.bean.TrainInvoiceBean;
import cn.net.caoyao.cpzshandset.bean.TruckInvoiceBean;
import cn.net.caoyao.cpzshandset.common.config.ConfigDBCreateService;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;
import cn.net.caoyao.cpzshandset.db.presenter.InvoiceDal;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20017LabelBean;

public class InvoiceBll {
	private InvoiceDal dal;

	public InvoiceBll(Context context) {
		ConfigDBCreateService service = new ConfigDBCreateService();
//		dal = new YthInvoiceDal(context);
		dal = service.createInvoiceDal(new CacheGet(), context);
	}

	/**
	 * 插入订单信息
	 * 
	 * @param InvoiceBean
	 */
	public void insertInvoice(InvoiceBean InvoiceBean) {
		if(dal == null) return ;
		dal.insertInvoice(InvoiceBean);
	}

	/**
	 * 根据发运方式、发运单id 查询对应的托盘列表
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTrainInvoices(TrainInvoiceBean InvoiceBean) {
		if(dal == null) return null;
		return dal.queryInvoiceByTransmode(1, InvoiceBean.getId());
	}
	
	/**
	 * 根据发运方式、发运单id 查询对应的托盘列表
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTruckInvoices(TruckInvoiceBean InvoiceBean) {
		if(dal == null) return null;
		return dal.queryInvoiceByTransmode(2, InvoiceBean.getId());
	}
	
	/**
	 * 根据发运方式、发运单id 查询对应的托盘列表
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryOnlineTruckInvoicesBylId(long id) {
		if(dal == null) return null;
		return dal.queryInvoiceByTransmodeAndLId(3, id);
	}
	
	/**
	 * 根据发运方式、发运单id 查询对应的托盘列表
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTrayInvoices(int transmode, int transid) {
		if(dal == null) return null;
		return dal.queryInvoices(transmode, transid, 1);
	}
	/**
	 * 通过订单号和发货方式查询订单信息
	 * @param transmode
	 * @param transid
	 * @return
	 */
	public List<InvoiceBean> queryInvoiceByTransmode(int transmode, int transid){
		if(dal == null) return null;
		return dal.queryInvoiceByTransmode(transmode, transid);
	}

	/**
	 * 根据运单id，查询火车发运的托盘信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTrainTrayInvoices(TrainInvoiceBean trainInvoiceBean) {
		if(dal == null) return null;
		return queryTrayInvoices(1, trainInvoiceBean.getId());
	}
	/**
	 * 根据运单id，统计发运单中的托盘个数
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public int queryTrayCount(int transid,int transmode) {
		if(dal == null) return 0;
		return dal.queryLabelCountByMode(transmode, transid,1);
	}
	/**
	 * 根据运单id，统计火车发运的托盘信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public int queryTrainTrayCount(TrainInvoiceBean trainInvoiceBean) {
		if(dal == null) return 0;
		return dal.queryLabelCountByMode(1, trainInvoiceBean.getId(),1);
	}
	/**
	 * 根据运单id，统计火车发运的标签信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public int queryTrainLabelCount(TrainInvoiceBean trainInvoiceBean) {
		if(dal == null) return 0;
		return dal.queryLabelCountByMode(1, trainInvoiceBean.getId(),2);
	}
	
	/**
	 * 根据运单id，统计货车发运的托盘信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public int queryTruckTrayCount(TruckInvoiceBean trainInvoiceBean) {
		if(dal == null) return 0;
		return dal.queryLabelCountByMode(2, trainInvoiceBean.getId(),1);
	}
	
	/**
	 * 根据运单id，统计货车发运的标签信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public int queryTruckLabelCount(TruckInvoiceBean trainInvoiceBean) {
		if(dal == null) return 0;
		return dal.queryLabelCountByMode(2, trainInvoiceBean.getId(),2);
	}
	
	/**
	 * 根据运单id，统计货车发运的标签信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public int queryOnlineTruckLabelCountByLId(long transId) {
		if(dal == null) return 0;
		return dal.queryLabelCountByModeLId(3, transId,2);
	}

	/**
	 * 根据运单id，查询汽车发运的托盘信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTruckTrayInvoices(TruckInvoiceBean truckInvoiceBean) {
		if(dal == null) return null;
		return queryTrayInvoices(2, truckInvoiceBean.getId());
	}

	/**
	 * 根据火车运单号，查询汽车发运的单个标签信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTrainLabelInvoices(TrainInvoiceBean trainInvoiceBean) {
		if(dal == null) return null;
		return querySingleLabelInvoices(1, trainInvoiceBean.getId());
	}

	/**
	 * 根据汽车运单号，查询汽车发运的单个标签信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryTruckLabelInvoices(TruckInvoiceBean truckInvoiceBean) {
		if(dal == null) return null;
		return querySingleLabelInvoices(2, truckInvoiceBean.getId());
	}

	/**
	 * 根据发运方式、发运单id 查询所有的单个产品发货单列表
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> querySingleLabelInvoices(int transmode, int transid) {
		if(dal == null) return null;
		return dal.queryInvoices(transmode, transid, 2);
	}

	/**
	 * 根据运单id，删除运单信息
	 * 
	 * @param id
	 */
	public void deleteInvoiceById(InvoiceBean InvoiceBean) {
		if(dal == null) return;
		dal.deleteInvoiceById(InvoiceBean.getId());
	}

	/**
	 * 检测运单表中是否已经存在该标签
	 * 
	 * @param labelId
	 * @return
	 */
	public boolean hasExistLabel(String labelId) {
		if(dal == null) return false;
		return dal.hasExistLabel(labelId);
	}
	/**
	 * 检测运单表中是否已经存在该标签
	 * 
	 * @param package_id
	 * @return
	 */
	public boolean hasExistPackageId(String package_id) {
		return dal.hasExistPackageId(package_id);
	}
	
	public void updateLabelStatusByLabelId(String labelId, int labelStatus){
		if (dal == null) return;
		dal.updateLabelStatusByLabelId(labelId, labelStatus);
	}
	
	public void updateLabelStatusByLabelIdList(List<Prot20017LabelBean> labels, int labelStatus) {
		if (dal == null || labels == null) return;
		dal.updateLabelStatusByLabelIdList(labels, labelStatus);
	}
	
	/**
	 * 更新标签的状态为重复发货标签
	 * @param labels
	 */
	public void updateLabelStatusRepeatByLabelIdList(List<Prot20017LabelBean> labels) {
		if (dal == null || labels == null) return;
		dal.updateLabelStatusByLabelIdList(labels, 1);
	}
	
	
	public List<InvoiceBean> queryInvoiceListByShipmentProductId(long driver_id, long product_id) {
		if(dal == null) return null;
		return dal.queryInvoiceListByShipmentProductId(driver_id, product_id);
	}

	
	public void deleteInvoiceByShipmentIdAndLabel(long driver_id, long product_id, String labelId) {
		if(dal == null) return;
		dal.deleteInvoiceByShipmentIdAndLabel(driver_id, product_id, labelId);
		
	}

	
	public int queryLabelCountByShipmentProductId(long driver_id, long product_id, int elemtype) {
		if(dal == null) return -1;
		return dal.queryLabelCountByShipmentProductId(driver_id, product_id, elemtype);
	}
	
	public void deleteInvoiceByShipmentProductId(long driver_id, long product_id) {
		if(dal == null) return;
		dal.deleteInvoiceByShipmentProductId(driver_id, product_id);
	}
}
