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.TruckInvoiceBean;
import cn.net.caoyao.cpzshandset.db.YthDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.TruckInvoiceDalImpl;

public class YthTruckInvoiceDal extends TruckInvoiceDalImpl{
	private SQLiteDatabase db;
	private YthDBHelper helper;
	public YthTruckInvoiceDal(Context context) {
		helper = new YthDBHelper(context);
	}
	/**
	 * 插入汽车运单信息
	 * @param invoice
	 */
	public int insertTruckInvoice(TruckInvoiceBean invoice){
		int key = -1;
		db = helper.getWritableDatabase();
		String sql = "insert into " + YthDBHelper.tb_truck_invoice +" (truckno,drivername,driverphone,orderno,startingstation,terminalstation,consigneeId,timestamp,status,labelMode,expand1,expand2) values (?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{invoice.getTruckno(),invoice.getDrivername(),invoice.getDriverphone(),invoice.getOrderno(),invoice.getStartingstation(),invoice.getTerminalstation(),invoice.getConsigneeId(),invoice.getTimestamp(),invoice.getStatus(),invoice.getLabelMode(),invoice.getExpand1(),invoice.getExpand2()});
//		String querySql = "SELECT MAX(id) from " + YthDBHelper.tb_truck_invoice;
		String querySql = "select last_insert_rowid() from "+YthDBHelper.tb_truck_invoice;
		Cursor cursor = db.rawQuery(querySql, null);
		if (cursor.moveToNext()) {
//			LogUtil.e(TConfig.Echo, "key:"+cursor.getInt(0));
			key = cursor.getInt(0);
		}
		closeDB(db,cursor);
		return key;
	}
	
	/**
	 * 插入时检测已经数据库中已经存在该订单
	 * @param trainInvoice
	 * @return
	 */
	public boolean insertHasExist(TruckInvoiceBean truckInvoice){
		boolean isExist = false;
		db = helper.getReadableDatabase();
		String sql = "select count(*) as count from " + YthDBHelper.tb_truck_invoice +" where truckno=? and orderno=? and status=?";
		Cursor queryCursor = db.rawQuery(sql, new String[]{truckInvoice.getTruckno(), truckInvoice.getOrderno(), String.valueOf(truckInvoice.getStatus())});
		if (queryCursor.moveToNext()) {
			int count = queryCursor.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		closeDB(db, queryCursor);
		return isExist;
	}
	
	/**
	 * 修改当前订单单个标签的上传类型
	 * @param invoice
	 * @return
	 */
	public TruckInvoiceBean updateTruckLabelMode(TruckInvoiceBean invoice){
		TruckInvoiceBean tInvoice = null;
		db = helper.getWritableDatabase();
		String updateSql = "UPDATE " + YthDBHelper.tb_truck_invoice +" SET labelMode=(-1)*labelMode WHERE id=?";
		db.execSQL(updateSql, new Object[]{invoice.getId()});
		String querySql = "SELECT * FROM " + YthDBHelper.tb_truck_invoice +" WHERE id=?";
		Cursor cursor = db.rawQuery(querySql, new String[]{String.valueOf(invoice.getId())});
		if (cursor.moveToNext()) {
			tInvoice = getTruckInvoice(cursor);
		}
		closeDB(db, cursor);
		return tInvoice;
	}
	/**
	 * 修改时检测数据库中已经存在该订单
	 * @param trainInvoice
	 * @return
	 */
	public boolean updateHasExist(TruckInvoiceBean truckInvoice){
		boolean isExist = false;
		db = helper.getReadableDatabase();
		String sql = "select count(*) as count from " + YthDBHelper.tb_truck_invoice +" where truckno=? and orderno=? and id!=?";
		Cursor queryCursor = db.rawQuery(sql, new String[]{truckInvoice.getTruckno(), truckInvoice.getOrderno(), String.valueOf(truckInvoice.getId())});
		if (queryCursor.moveToNext()) {
			int count = queryCursor.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		closeDB(db, queryCursor);
		return isExist;
	}
	/**
	 * 修改汽车运单信息
	 * @param invoice
	 */
	public void updateTruckInvoice(TruckInvoiceBean invoice){
		db = helper.getWritableDatabase();
		String sql = "update "+YthDBHelper.tb_truck_invoice+" set truckno=?,drivername=?,driverphone=?,orderno=?,startingstation=?,terminalstation=?,consigneeId=?,timestamp=?,status=?,labelMode=?,expand1=?,expand2=? where id=?";
		db.execSQL(sql, new Object[]{invoice.getTruckno(),invoice.getDrivername(),invoice.getDriverphone(),invoice.getOrderno(),invoice.getStartingstation(),invoice.getTerminalstation(),invoice.getConsigneeId(),invoice.getTimestamp(),invoice.getStatus(),invoice.getLabelMode(),invoice.getExpand1(),invoice.getExpand2(),invoice.getId()});
		closeDB(db, null);
	}
	/**
	 * 根据修单号，修改汽车运单信息
	 * @param status 0:有效   1:无效
	 * @param id
	 */
	public void updateTruckInvoiceStatus(int status,int errorCode, int id){
		db = helper.getWritableDatabase();
		String sql = "update " + YthDBHelper.tb_truck_invoice +" set status=?,error_code=? where id=?";
		db.execSQL(sql, new Object[]{status,errorCode,id});
		closeDB(db, null);
	}
	/**
	 * 通过运单ID，查询运单信息
	 * @param id
	 * @return
	 */
	public TruckInvoiceBean queryTruckInvoiceById(int id){
		db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_truck_invoice+" where id=?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id)});
		TruckInvoiceBean invoice = null;
		if (cursor.moveToNext()) {
			invoice = getTruckInvoice(cursor);
		}
		closeDB(db, cursor);
		return invoice;
	}
	/**
	 * 通过运单状态，查询运单信息
	 * @param status 0:有效  1:无效
	 * @return
	 */
	public List<TruckInvoiceBean> queryAllTruckInvoices(){
		db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_truck_invoice+" order by id desc";
		Cursor cursor = db.rawQuery(sql, null);
		List<TruckInvoiceBean> invoices = new ArrayList<TruckInvoiceBean>();
		while (cursor.moveToNext()) {
			TruckInvoiceBean invoice = getTruckInvoice(cursor);
			invoices.add(invoice);
		}
		closeDB(db, cursor);
		return invoices;
	}
	
	/**
	 * 通过运单状态，查询运单信息
	 * @param status 0:有效  1:无效
	 * @return
	 */
	public List<TruckInvoiceBean> queryAllTruckInvoicesASC(){
		db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_truck_invoice+" order by id ASC";
		Cursor cursor = db.rawQuery(sql, null);
		List<TruckInvoiceBean> invoices = new ArrayList<TruckInvoiceBean>();
		while (cursor.moveToNext()) {
			TruckInvoiceBean invoice = getTruckInvoice(cursor);
			invoices.add(invoice);
		}
		closeDB(db, cursor);
		return invoices;
	}
	
	
	/**
	 * 根据运单状态，删除运单
	 * @param status
	 */
	public void deleteTruckInvoice(int status){
		db = helper.getWritableDatabase();
		String sql = "delete from " + YthDBHelper.tb_truck_invoice +" where status=?";
		db.execSQL(sql, new Object[]{status});
		closeDB(db, null);
	}
	
	
	/**
	 * 删除所有的汽车发货无效订单
	 */
	public void deleteTruckInvoiceById(TruckInvoiceBean invoice){
		db = helper.getWritableDatabase();
		String delInvoiceSql = "delete from " + YthDBHelper.tb_invoice +" where transmode=2 and transid=?";
		db.execSQL(delInvoiceSql, new Object[]{invoice.getId()});
		
		String delTruckSql = "delete from " + YthDBHelper.tb_truck_invoice +" where id=?";
		db.execSQL(delTruckSql, new Object[]{invoice.getId()});
		
		closeDB(db, null);
	}
	
	public TruckInvoiceBean getTruckInvoice(Cursor cursor) {
		TruckInvoiceBean invoice = new TruckInvoiceBean();
		invoice.setId(cursor.getInt(cursor.getColumnIndex("id")));
		invoice.setTruckno(cursor.getString(cursor.getColumnIndex("truckno")));
		invoice.setDrivername(cursor.getString(cursor.getColumnIndex("drivername")));
		invoice.setDriverphone(cursor.getString(cursor.getColumnIndex("driverphone")));
		invoice.setOrderno(cursor.getString(cursor.getColumnIndex("orderno")));
		invoice.setStartingstation(cursor.getInt(cursor.getColumnIndex("startingstation")));
		invoice.setTerminalstation(cursor.getInt(cursor.getColumnIndex("terminalstation")));
		invoice.setConsigneeId(cursor.getInt(cursor.getColumnIndex("consigneeId")));
		invoice.setTimestamp(cursor.getString(cursor.getColumnIndex("timestamp")));
		invoice.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
		invoice.setError_code(cursor.getInt(cursor.getColumnIndex("error_code")));
		invoice.setLabelMode(cursor.getInt(cursor.getColumnIndex("labelMode")));
		invoice.setExpand1(cursor.getString(cursor.getColumnIndex("expand1")));
		invoice.setExpand2(cursor.getString(cursor.getColumnIndex("expand2")));
		return invoice;
	}
	private void closeDB(SQLiteDatabase db, Cursor cursor) {
		if (cursor != null) {
			cursor.close();
		}
		if (db != null) {
			db.close();
		}
	}

}
