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.DriverInfoBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.db.YthPunitShipmentDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.DriverInfoDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.LogisticsDriverInfoBean;

public class YthPunitDriverInfoDal extends DriverInfoDalImpl{
	private YthPunitShipmentDBHelper helper;
	public YthPunitDriverInfoDal(Context context) {
		helper = new YthPunitShipmentDBHelper(context);
	}

	@Override
	public void insertLogisticsDriverInfo(LogisticsDriverInfoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthPunitShipmentDBHelper.tb_driver_info +" (id,order_id,driver_name,driver_phone,car_no,driver_licenseNo,unit_name,car_kg,huo_kg,total_money,dest_tel,sales_name,dest_addr) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getOrder_id(),b.getDriver_name(),b.getDriver_tel(),b.getCar_no(),b.getDriver_no(),b.getUnit_name(),b.getCar_kg(),b.getHuo_kg(),b.getTotal_money(),b.getDest_tel(),b.getSales_name(),b.getDest_addr()});
		db.close();
	}

	@Override
	public List<DriverInfoBean> queryAllDriverInfoDESC() {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.tb_driver_info;
		db.execSQL(sql);
		db.close();
	}

	@Override
	public void deleteDriverInfoByOrderId(long orderId) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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 "+ YthPunitShipmentDBHelper.tb_driver_info +" where id=?";
		db.execSQL(sql,new Object[] {b.getId()});
		
		String delPSql = "delete from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=?";
		db.execSQL(delPSql, new Object[]{b.getId()});

		String querySql = "select count(id) from " +  YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.tb_online_truck_invoice +" where id=?";
				db.execSQL(delSql, new Object[] {String.valueOf(b.getOrder_id())});
			}
		}
		
		c.close();
		//删除对应的标签
		String delLabel = "delete from " + YthPunitShipmentDBHelper.tb_invoice +" where driver_id=? and transmode=?";
		db.execSQL(delLabel,new Object[] {b.getId(),4});
		
		db.close();
	}

	@Override
	public int countDriverByOrderId(long orderId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "select count(id) from " +  YthPunitShipmentDBHelper.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 " +  YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.tb_driver_info +" set native_status=?,error_code=? where id=?";
		db.execSQL(sql, new Object[] {native_status,error_code,id});
		db.close();
		
	}
	
	@Override
	public int queryLabelCountByDriverId(long driver_id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		int count = 0;
		String sql = "select count(id) from " + YthPunitShipmentDBHelper.tb_invoice +" where driver_id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(driver_id)});
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		c.close();
		db.close();
		return count;
	}
	
	@Override
	public int queryProductSubmitErrorByDriverId(DriverInfoBean b) {
		SQLiteDatabase db = helper.getReadableDatabase();
		int count = 0;
		String sql = "select count(id) from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=? and native_status=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(b.getId()),String.valueOf(Config.SUBMIT_STATUS_SUBMIT_FAIL)});
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		c.close();
		db.close();
		return count;
	}

	@Override
	public int queryProductSubmitErrorByDriverIdAndRepId(DriverInfoBean b, long repertory_id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		int count = 0;
		String sql = "select count(id) from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=? and repertory_id=? and native_status=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(b.getId()),String.valueOf(repertory_id),String.valueOf(Config.SUBMIT_STATUS_SUBMIT_FAIL)});
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		c.close();
		db.close();
		return count;
	}
	@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.setUnit_name(c.getString(c.getColumnIndex("unit_name")));
		b.setCar_kg(c.getString(c.getColumnIndex("car_kg")));
		b.setHuo_kg(c.getString(c.getColumnIndex("huo_kg")));
		b.setDest_addr(c.getString(c.getColumnIndex("dest_addr")));
		b.setTotal_money(c.getString(c.getColumnIndex("total_money")));
		b.setDest_tel(c.getString(c.getColumnIndex("dest_tel")));
		b.setSales_name(c.getString(c.getColumnIndex("sales_name")));
		
		b.setNative_status(c.getInt(c.getColumnIndex("native_status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		return b;
	}



}
