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

public class YthPunitOnlineTruckInvoiceDal extends OnlineTruckInvoiceDalImpl{
	private YthPunitShipmentDBHelper helper;
	public YthPunitOnlineTruckInvoiceDal(Context context) {
		helper = new YthPunitShipmentDBHelper(context);
	}
	@Override
	public void insertLogisticsOrderInfoBean(LogisticsOrderInfoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthPunitShipmentDBHelper.tb_online_truck_invoice+" (id,order_no,unit_name,neiqin_id,sales_id,submit_time,audit_time,guozhang_time,online_status) values (?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getOrder_no(),b.getUnit_name(),b.getNeiqin_id(),b.getSales_id(),b.getSubmit_time(),b.getAudit_time(),b.getGuozhang_time(),b.getStatus()});
		db.close();
	}

	@Override
	public void deleteOrderBeanById(long id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.tb_online_truck_invoice  + " where id=?";
		db.execSQL(sql, new Object[]{id});
		db.close();
	}


	@Override
	public ShipmentOnlineOrderBean queryYthOrderById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		ShipmentOnlineOrderBean b = null;
		String sql = "select * from " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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.setBusiUnit(c.getString(c.getColumnIndex("unit_name")));
		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;
	}
	

}
