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

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.YthPunitShipmentDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.InvoiceDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.Prot20017LabelBean;

public class YthPunitInvoiceDal extends InvoiceDalImpl{
	private SQLiteDatabase db;
	private YthPunitShipmentDBHelper helper;

	public YthPunitInvoiceDal(Context context) {
		helper = new YthPunitShipmentDBHelper(context);
	}

//	id INTEGER PRIMARY KEY AUTOINCREMENT, 
//	transmode INTEGER default 4,
//	driver_id bigint,product_id bigint,elemtype INTEGER default 2,labelid VARCHAR(20),labelStatus INTEGER default 0 ,timestamp VARCHAR(20),package_id varchar(32),createDate DATE DEFAULT (date('now','localtime')))";
	/**
	 * 插入订单信息
	 * 
	 * @param invoice
	 */
	public void insertInvoice(InvoiceBean invoice) {
		db = helper.getWritableDatabase();
		String sql = "insert into " + YthPunitShipmentDBHelper.tb_invoice
				+ " (transmode,driver_id,product_id,elemtype,labelid,timestamp,package_id) values(?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { invoice.getTransmode(), invoice.getDriver_id(),invoice.getTrans_product_id(), invoice.getElemtype(),
				invoice.getLabelid(), invoice.getTimestamp(),invoice.getPackage_id() });
		closeDB(db, null);
	}
	
	//UPDATE test SET status=(-1)*status

	

	/**
	 * 删除所有运单信息
	 * 
	 * @param id
	 */
	public void deleteInvoice() {
		db = helper.getWritableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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;
	}
	

	public InvoiceBean getInvoice(Cursor cursor) {
		InvoiceBean invoice = new InvoiceBean();
		invoice.setId(cursor.getInt(cursor.getColumnIndex("id")));
		invoice.setTransmode(cursor.getInt(cursor.getColumnIndex("transmode")));
		invoice.setDriver_id(cursor.getInt(cursor.getColumnIndex("driver_id")));
		invoice.setTrans_product_id(cursor.getLong(cursor.getColumnIndex("product_id")));
		invoice.setElemtype(cursor.getInt(cursor.getColumnIndex("elemtype")));
		invoice.setLabelid(cursor.getString(cursor.getColumnIndex("labelid")));
		invoice.setTimestamp(cursor.getString(cursor.getColumnIndex("timestamp")));
		invoice.setLabelStatus(cursor.getInt(cursor.getColumnIndex("labelStatus")));
		return invoice;
	}

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

	@Override
	public void updateLabelStatusByLabelId(String labelId, int labelStatus) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + YthPunitShipmentDBHelper.tb_invoice + " set labelStatus=? where labelid=?";
		db.execSQL(sql, new Object[] {labelStatus,labelId});
		db.close();
	}

	@Override
	public void updateLabelStatusByLabelIdList(List<Prot20017LabelBean> labels, int labelStatus) {
//		LogUtil.e(TConfig.Echo, "更新标签状态：" + labels.toString() +"\nlabelStatus:" + labelStatus);
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + YthPunitShipmentDBHelper.tb_invoice + " set labelStatus=? where labelid=?";
		for (Prot20017LabelBean labelId : labels) {
			db.execSQL(sql, new Object[] {labelStatus,labelId.getLabel_id()});
		}
		db.close();		
	}
	/**
	 * 根据运单id，删除运单信息
	 * 
	 * @param id
	 */
	public void deleteInvoiceById(int id) {
		db = helper.getReadableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.tb_invoice + " where id=?";
		db.execSQL(sql, new Object[] { id });
		closeDB(db, null);
	}

	@Override
	public List<InvoiceBean> queryInvoiceListByShipmentProductId(long driver_id, long product_id) {
		db = helper.getReadableDatabase();
		String sql = "select * from " + YthPunitShipmentDBHelper.tb_invoice
				+ " where driver_id=? AND product_id=? order by id desc";
		Cursor cursor = db.rawQuery(sql,
				new String[] { String.valueOf(driver_id), String.valueOf(product_id)});
		List<InvoiceBean> invoices = new ArrayList<InvoiceBean>();
		while (cursor.moveToNext()) {
			InvoiceBean invoice = getInvoice(cursor);
			invoices.add(invoice);
		}
		closeDB(db, cursor);
		return invoices;
	}

	@Override
	public void deleteInvoiceByShipmentIdAndLabel(long driver_id, long product_id, String labelId) {
		db = helper.getReadableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.tb_invoice + " where  driver_id=? AND product_id=? AND labelid=?";
		db.execSQL(sql, new Object[] { driver_id,product_id,labelId});
		closeDB(db, null);
		
	}

	@Override
	public int queryLabelCountByShipmentProductId(long driver_id, long product_id, int elemtype) {
		int count = 0;
		db = helper.getReadableDatabase();
		String sql = "select count(*) as count from " + YthPunitShipmentDBHelper.tb_invoice
				+ " where driver_id=? AND product_id=?";
		Cursor cursor = db.rawQuery(sql,
				new String[] { String.valueOf(driver_id), String.valueOf(product_id) });
		if (cursor.moveToNext()) {
			count = cursor.getInt(0);
		}
		closeDB(db, cursor);
		return count;
	}

	@Override
	public void deleteInvoiceByShipmentProductId(long driver_id, long product_id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.tb_invoice +" where driver_id=? AND product_id=?";
		db.execSQL(sql, new Object[] {driver_id,product_id});
		closeDB(db, null);
	}
}
