package cn.net.caoyao.cpzshandset.db.dal.yth;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.bean.InvoiceBean;
import cn.net.caoyao.cpzshandset.db.YthDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.InvoiceDalImpl;

public class YthInvoiceDal extends InvoiceDalImpl{
	private SQLiteDatabase db;
	private YthDBHelper helper;

	public YthInvoiceDal(Context context) {
		helper = new YthDBHelper(context);
	}

	/**
	 * 插入订单信息
	 * 
	 * @param invoice
	 */
	public void insertInvoice(InvoiceBean invoice) {
		db = helper.getWritableDatabase();
		String sql = "insert into " + YthDBHelper.tb_invoice
				+ " (transmode,transid,elemtype,labelid,timestamp,package_id) values(?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { invoice.getTransmode(), invoice.getTransid(), invoice.getElemtype(),
				invoice.getLabelid(), invoice.getTimestamp(),invoice.getPackage_id() });
		closeDB(db, null);
	}
	
//	select status*-1 as status from test

	/**
	 * 根据发运方式、发运单id、类型 获取运单列表
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public List<InvoiceBean> queryInvoices(int transmode, int transid, int elemtype) {
		db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_invoice
				+ " where transmode=? AND transid=? AND elemtype=? order by id desc";
		Cursor cursor = db.rawQuery(sql,
				new String[] { String.valueOf(transmode), String.valueOf(transid), String.valueOf(elemtype) });
		List<InvoiceBean> invoices = new ArrayList<InvoiceBean>();
		while (cursor.moveToNext()) {
			InvoiceBean invoice = getInvoice(cursor);
			invoices.add(invoice);
		}
		closeDB(db, cursor);
		return invoices;
	}
	/**
	 * 通过订单号和发货方式查询订单信息
	 * @param transmode
	 * @param transid
	 * @return
	 */
	public List<InvoiceBean> queryInvoiceByTransmode(int transmode, int transid){
		db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_invoice
				+ " where transmode=? AND transid=? order by id desc";
		Cursor cursor = db.rawQuery(sql,
				new String[] { String.valueOf(transmode), String.valueOf(transid)});
		List<InvoiceBean> invoices = new ArrayList<InvoiceBean>();
		while (cursor.moveToNext()) {
			InvoiceBean invoice = getInvoice(cursor);
			invoices.add(invoice);
		}
		closeDB(db, cursor);
		return invoices;
	}
	
	//UPDATE test SET status=(-1)*status
	

	/**
	 * 根据发运模式，统计表签数量
	 * @param transmode
	 * @param transid
	 * @param elemtype
	 * @return
	 */
	public int queryLabelCountByMode(int transmode, int transid, int elemtype) {
		int count = 0;
		db = helper.getReadableDatabase();
		String sql = "select count(*) as count from " + YthDBHelper.tb_invoice
				+ " where transmode=? AND transid=? AND elemtype=?";
		Cursor cursor = db.rawQuery(sql,
				new String[] { String.valueOf(transmode), String.valueOf(transid), String.valueOf(elemtype) });
		if (cursor.moveToNext()) {
			count = cursor.getInt(0);
		}
		closeDB(db, cursor);
		return count;
	}
	
	/**
	 * 根据发运方式、发运单id、类型、标签id 删除运单信息
	 * 
	 * @param transmode
	 *            1:火车 2:汽车
	 * @param transid
	 * @param elemtype
	 *            1:托盘包含的标签 2:单个产品的标签
	 * @return
	 */
	public void deleteInvoiceSingle(int transmode, int transid, int elemtype, String labelid) {
		db = helper.getReadableDatabase();
		String sql = "delete from " + YthDBHelper.tb_invoice
				+ " where transmode=? AND transid=? AND elemtype=? AND labelid=?";
		db.execSQL(sql, new Object[] { transmode, transid, elemtype, labelid });
		closeDB(db, null);
	}

	/**
	 * 根据运单id，删除运单信息
	 * 
	 * @param id
	 */
	public void deleteInvoiceById(int id) {
		db = helper.getReadableDatabase();
		String sql = "delete from " + YthDBHelper.tb_invoice + " where id=?";
		db.execSQL(sql, new Object[] { id });
		closeDB(db, null);
	}

	/**
	 * 删除所有运单信息
	 * 
	 * @param id
	 */
	public void deleteInvoice() {
		db = helper.getReadableDatabase();
		String sql = "delete from " + YthDBHelper.tb_invoice;
		db.execSQL(sql);
		closeDB(db, null);
	}

	/**
	 * 检测运单表中是否已经存在该标签
	 * 
	 * @param labelId
	 * @return
	 */
	public boolean hasExistLabel(String labelId) {
		db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(*) as count from " + YthDBHelper.tb_invoice + " where labelid=? ";
		Cursor cursor = db.rawQuery(sql, new String[] { labelId });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
//			LogUtil.d(TConfig.Echo, "hasExistLabel count:" + count +"  labelId:" + labelId);
			if (count > 0) {
				isExist = true;
			}
		}
		
		closeDB(db, cursor);
		return isExist;
	}
	
	/**
	 * 检测运单表中是否已经存在该package_id
	 * 
	 * @param labelId
	 * @return
	 */
	public boolean hasExistPackageId(String package_id) {
		db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(*) as count from " + YthDBHelper.tb_invoice + " where package_id=? ";
		Cursor cursor = db.rawQuery(sql, new String[] { package_id });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
//			LogUtil.d(TConfig.Echo, "hasExistLabel count:" + count +"  labelId:" + labelId);
			if (count > 0) {
				isExist = true;
			}
		}
		
		closeDB(db, cursor);
		return isExist;
	}

	public InvoiceBean getInvoice(Cursor cursor) {
		InvoiceBean invoice = new InvoiceBean();
		invoice.setId(cursor.getInt(cursor.getColumnIndex("id")));
		invoice.setTransmode(cursor.getInt(cursor.getColumnIndex("transmode")));
		invoice.setTransid(cursor.getInt(cursor.getColumnIndex("transid")));
		invoice.setElemtype(cursor.getInt(cursor.getColumnIndex("elemtype")));
		invoice.setLabelid(cursor.getString(cursor.getColumnIndex("labelid")));
		invoice.setTimestamp(cursor.getString(cursor.getColumnIndex("timestamp")));
		return invoice;
	}

	private void closeDB(SQLiteDatabase db, Cursor cursor) {
		if (cursor != null) {
			cursor.close();
		}
		if (db != null) {
			db.close();
		}
	}


}
