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

public class YthScannerOnlineTruckInvoiceDal extends OnlineTruckInvoiceDalImpl{
	private YthScannerShipmentDBHelper helper;
	public YthScannerOnlineTruckInvoiceDal(Context context) {
		helper = new YthScannerShipmentDBHelper(context);
	}
	@Override
	public void insertOnlineInvoiceList(List<YthOrderBean> orderBeans) {
		if(orderBeans == null) return;
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" (id,order_no,client_user,busi_unit,take_addr,product_name,plan_num,pack_num,isZhang,remark,online_status) values (?,?,?,?,?,?,?,?,?,?,?)";
		for (YthOrderBean b : orderBeans) {
			db.execSQL(sql, new Object[]{b.getId(),b.getOrderId(),b.getClientUser(),b.getBusiUnit(),b.getTakeAddr(),b.getProductName(),b.getPlanNum(),b.getPack_num(),b.getIszhang(),b.getRemark(),b.getStatus()});
		}
		db.close();
	}

	@Override
	public void insertOnlineInvoice(YthOrderBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" (id,order_no,client_user,busi_unit,take_addr,product_name,plan_num,pack_num,isZhang,remark,online_status) values (?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getOrderId(),b.getClientUser(),b.getBusiUnit(),b.getTakeAddr(),b.getProductName(),b.getPlanNum(),b.getPack_num(),b.getIszhang(),b.getRemark(),b.getStatus()});
		db.close();		
	}

	@Override
	public void deleteOrderBeanById(long id) {
		SQLiteDatabase db = helper.getWritableDatabase();
//		String delLabelSql = "delete from " + YthScannerShipmentDBHelper.tb_invoice +" where transmode=? and transid=?";
//		db.execSQL(delLabelSql, new Object[]{3,id});
		
//		String delDriver = "delete from " + YthScannerShipmentDBHelper.tb_driver_info +" where order_id=?";
//		db.execSQL(delDriver, new Object[]{id});
		
		String sql = "delete from " + YthScannerShipmentDBHelper.tb_online_truck_invoice  + " where id=?";
		db.execSQL(sql, new Object[]{id});
		db.close();
	}

	@Override
	public void deleteAllOrderBean() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delLabelSql = "delete from " + YthScannerShipmentDBHelper.tb_invoice +" where transmode=?";
		db.execSQL(delLabelSql, new Object[]{3});
		
		String delDriver = "delete from " + YthScannerShipmentDBHelper.tb_driver_info ;
		db.execSQL(delDriver);
		
		String sql = "delete from " + YthScannerShipmentDBHelper.tb_online_truck_invoice;
		db.execSQL(sql);
		db.close();		
	}

	@Override
	public ShipmentOnlineOrderBean queryYthOrderById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		ShipmentOnlineOrderBean b = null;
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getShipmentOnlineOrderBean(c);
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public void updateOnlineInvoiceStatusById(int error_code, int native_status, long id) {
//		LogUtil.e(TConfig.Echo, "更新状态信息：error_code:" + error_code +"  native_status:" + native_status +"   id:" + id);
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" set native_status=?,error_code=? where id=?";
		db.execSQL(sql, new Object[]{native_status,error_code,id});
		db.close();
	}

	@Override
	public List<ShipmentOnlineOrderBean> queryAllYthOrderASC() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<ShipmentOnlineOrderBean> list = new ArrayList<>();
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" order by id asc";
		Cursor c = db.rawQuery(sql,null);
		if (c.moveToNext()) {
			list.add(getShipmentOnlineOrderBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public List<ShipmentOnlineOrderBean> queryAllYthOrderDESC() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<ShipmentOnlineOrderBean> list = new ArrayList<>();
		String sql = "select * from " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" order by id desc";
		Cursor c = db.rawQuery(sql,null);
		if (c.moveToNext()) {
			list.add(getShipmentOnlineOrderBean(c));
		}
		c.close();
		db.close();
		return list;
	}
	
	@Override
	public boolean insertHasExist(long id) {
		boolean b = false;
		SQLiteDatabase db = helper.getReadableDatabase();
//		LogUtil.e(TConfig.Echo, "insertHasExist " + id);
		String sql = "select count(id) from " + YthScannerShipmentDBHelper.tb_online_truck_invoice +" where id=?";
		Cursor c = db.rawQuery(sql, new String[] {String.valueOf(id)});
		if (c.moveToNext()) {
			int count = c.getInt(0);
//			LogUtil.d(TConfig.Echo, "hasExistLabel count:" + count +"  labelId:" + labelId);
			if (count > 0) {
				b = true;
			}
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public ShipmentOnlineOrderBean getShipmentOnlineOrderBean(Cursor c) {
		ShipmentOnlineOrderBean b = new ShipmentOnlineOrderBean();
		b.setId(c.getLong(c.getColumnIndex("id")));
		b.setOrderId(c.getString(c.getColumnIndex("order_no")));
		b.setClientUser(c.getString(c.getColumnIndex("client_user")));
		b.setBusiUnit(c.getString(c.getColumnIndex("busi_unit")));
		b.setTakeAddr(c.getString(c.getColumnIndex("take_addr")));
		b.setProductName(c.getString(c.getColumnIndex("product_name")));
		b.setPlanNum(c.getString(c.getColumnIndex("plan_num")));
		b.setPack_num(c.getString(c.getColumnIndex("pack_num")));
		b.setIszhang(c.getString(c.getColumnIndex("isZhang")));
		b.setRemark(c.getString(c.getColumnIndex("remark")));
		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;
	}
	

}
