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

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.DriverInfoBean;
import cn.net.caoyao.cpzshandset.db.YthScannerShipmentDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.DriverInfoDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.YthDriverInfoBean;

public class YthScannerDriverInfoDal extends DriverInfoDalImpl{
	private YthScannerShipmentDBHelper helper;
	public YthScannerDriverInfoDal(Context context) {
		helper = new YthScannerShipmentDBHelper(context);
	}

	@Override
	public void insertDriverInfoList(List<YthDriverInfoBean> beans) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthScannerShipmentDBHelper.tb_driver_info +" (id,order_id,driver_name,driver_phone,car_no,driver_licenseNo,out_orderId,num,pack_num,online_status) values (?,?,?,?,?,?,?,?,?,?)";
		for (YthDriverInfoBean b : beans) {
			db.execSQL(sql, new Object[]{b.getId(),b.getOrder_id(),b.getDriver_name(),b.getDriver_phone(),b.getCar_no(),b.getDriver_licenseNo(),b.getOut_orderId(),b.getNum(),b.getPack_num(),b.getStatus()});
		}
		db.close();
	}

	@Override
	public void insertDriverInfo(YthDriverInfoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthScannerShipmentDBHelper.tb_driver_info +" (id,order_id,driver_name,driver_phone,car_no,driver_licenseNo,out_orderId,num,pack_num,online_status) values (?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getOrder_id(),b.getDriver_name(),b.getDriver_phone(),b.getCar_no(),b.getDriver_licenseNo(),b.getOut_orderId(),b.getNum(),b.getPack_num(),b.getStatus()});
		db.close();		
	}

	@Override
	public List<DriverInfoBean> queryAllDriverInfoDESC() {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_driver_info +" order by id desc";
		List<DriverInfoBean> list = new ArrayList<>();
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			list.add(getDriverInfo(c));
		}
		c.close();
		db.close();
		return list;
	}
	
	@Override
	public List<DriverInfoBean> queryAllDriverInfoASC() {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_driver_info +" order by id asc";
		List<DriverInfoBean> list = new ArrayList<>();
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			list.add(getDriverInfo(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public List<DriverInfoBean> queryDriverInfoByOrderId(long orderId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_driver_info +" where order_id=?";
		List<DriverInfoBean> list = new ArrayList<>();
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(orderId)});
		while (c.moveToNext()) {
			list.add(getDriverInfo(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public void deleteAllDriverInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthScannerShipmentDBHelper.tb_driver_info;
		db.execSQL(sql);
		db.close();
	}

	@Override
	public void deleteDriverInfoByOrderId(long orderId) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthScannerShipmentDBHelper.tb_driver_info +" where order_id=?";
		db.execSQL(sql,new Object[]{orderId});
		db.close();		
	}

	@Override
	public DriverInfoBean queryDriverInfoById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_driver_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		DriverInfoBean b = null;
		if (c.moveToNext()) {
			b = getDriverInfo(c);
		}
		c.close();
		db.close();
		return b;
	}
	
	@Override
	public void deleteDriverInfoById(DriverInfoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from "+ YthScannerShipmentDBHelper.tb_driver_info +" where id=?";
		db.execSQL(sql,new Object[] {b.getId()});

		String querySql = "select count(id) from " +  YthScannerShipmentDBHelper.tb_driver_info +" where order_id=?";
		Cursor c = db.rawQuery(querySql, new String[] {String.valueOf(b.getOrder_id())});
		if (c.moveToNext()) {
			int count = c.getInt(0);
//			LogUtil.d(TConfig.Echo, "hasExistLabel count:" + count +"  labelId:" + labelId);
			if (count <= 0) {
				//删除对应的本地订单
				String delSql = "delete from " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" where id=?";
				db.execSQL(delSql, new Object[] {String.valueOf(b.getOrder_id())});
			}
		}
		
		c.close();
		//删除对应的标签
		String delLabel = "delete from " + YthScannerShipmentDBHelper.tb_invoice +" where transid=? and transmode=?";
		db.execSQL(delLabel,new Object[] {b.getId(),3});
		
		db.close();
	}

	@Override
	public int countDriverByOrderId(long orderId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "select count(id) from " +  YthScannerShipmentDBHelper.tb_driver_info +" where order_id=?";
		Cursor c = db.rawQuery(querySql, new String[] {String.valueOf(orderId)});
		int count = 0;
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		return count;
	}
	
	@Override
	public boolean insertHasExist(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean b = false;
		String querySql = "select count(id) from " +  YthScannerShipmentDBHelper.tb_driver_info +" where id=?";
		Cursor c = db.rawQuery(querySql, new String[] {String.valueOf(id)});
		int count = 0;
		if (c.moveToNext()) {
			count = c.getInt(0);
			if (count > 0) {
				b = true;
			}
		} 
		c.close();
		db.close();
		return b;
	}
	
	@Override
	public void updateDriverStatusById(int error_code, int native_status, long id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + YthScannerShipmentDBHelper.tb_driver_info +" set native_status=?,error_code=? where id=?";
		db.execSQL(sql, new Object[] {native_status,error_code,id});
		db.close();
		
	}
	
	@Override
	public DriverInfoBean getDriverInfo(Cursor c) {
		DriverInfoBean b = new DriverInfoBean();
		b.setId(c.getLong(c.getColumnIndex("id")));
		b.setOrder_id(c.getLong(c.getColumnIndex("order_id")));
		b.setDriver_name(c.getString(c.getColumnIndex("driver_name")));
		b.setDriver_phone(c.getString(c.getColumnIndex("driver_phone")));
		b.setCar_no(c.getString(c.getColumnIndex("car_no")));
		b.setDriver_licenseNo(c.getString(c.getColumnIndex("driver_licenseNo")));
		b.setOut_orderId(c.getString(c.getColumnIndex("out_orderId")));
		b.setNum(c.getString(c.getColumnIndex("num")));
		b.setPack_num(c.getString(c.getColumnIndex("pack_num")));
		b.setOnline_status(c.getInt(c.getColumnIndex("online_status")));
		b.setNative_status(c.getInt(c.getColumnIndex("native_status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		return b;
	}

	


}
