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

public class YthPunitShipmentProductDal extends ShipmentProductDalImpl{
	private YthPunitShipmentDBHelper helper;
	public YthPunitShipmentProductDal(Context context) {
		helper = new YthPunitShipmentDBHelper(context);
	}

	@Override
	public void insertShipmentProductBean(LogisticsProductInfo b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthPunitShipmentDBHelper.tb_shipment_product_info + " (id,diver_id,repertory_id,item_num,huowu_name,out_order_no,xiangmu_no,weight,price,settle_price,online_status,guozhang_time,isguozhang,isshenhe) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getDriver_id(),b.getRepertory_id(),b.getItem_num(),b.getHuowu_name(),b.getOut_order_no(),b.getXiangmu_no(),b.getWeight(),b.getPrice(),b.getSettle_price(),b.getStatus(),b.getGuozhang_time(),b.getIsguozhang(),b.getIsshenhe()});
		db.close();
	}

	@Override
	public void insertLogisticsProductList(List<LogisticsProductInfo> list,long order_id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthPunitShipmentDBHelper.tb_shipment_product_info + " (id,order_id,diver_id,repertory_id,item_num,huowu_name,out_order_no,xiangmu_no,weight,price,settle_price,online_status,guozhang_time,isguozhang,isshenhe) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		for (LogisticsProductInfo b : list) {
			db.execSQL(sql, new Object[]{b.getId(),order_id,b.getDriver_id(),b.getRepertory_id(),b.getItem_num(),b.getHuowu_name(),b.getOut_order_no(),b.getXiangmu_no(),b.getWeight(),b.getPrice(),b.getSettle_price(),b.getStatus(),b.getGuozhang_time(),b.getIsguozhang(),b.getIsshenhe()});
		}
		db.close();
	}

	@Override
	public ShipmentProductBean queryShipmentProductBeanById(long id) {
		ShipmentProductBean b = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getShipmentProductBean(c);
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public List<ShipmentProductBean> queryShipmentProductByDriverIdAndRepertoryId(long driver_id,long repertory_id) {
		List<ShipmentProductBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=? and repertory_id=? order by id asc";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(driver_id),String.valueOf(repertory_id)});
		while (c.moveToNext()) {
			ShipmentProductBean b = getShipmentProductBean(c);
//			b.setOrder_id(order_id);s
			list.add(b);
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public void updateShipmentProductBeanStatusById(int status, int error_code, long id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + YthPunitShipmentDBHelper.tb_shipment_product_info +" set native_status=?,error_code=? where id=?";
		db.execSQL(sql, new Object[]{status,error_code,id});
		db.close();
	}
	@Override
	public void deleteShipmentProductBeanById(ShipmentProductBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delLabel = "delete from " + YthPunitShipmentDBHelper.tb_invoice +" where product_id=?";
		db.execSQL(delLabel, new Object[]{b.getId()});
		
		String updateProductStatus = "update " + YthPunitShipmentDBHelper.tb_shipment_product_info +" set native_status=? where id=?";
		db.execSQL(updateProductStatus, new Object[] {Config.SUBMIT_STATUS_SUBMIT_SUCCESS,b.getId()});
		
//		String delProduct = "delete from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where id=?";
//		db.execSQL(delProduct, new Object[]{b.getId()});
		
		String sql = "select count(id) from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=? AND native_status<>? AND repertory_id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(b.getDriver_id()),String.valueOf(Config.SUBMIT_STATUS_SUBMIT_SUCCESS),String.valueOf(b.getRepertory_id())});
		int p_count_in_driver = 0;
		if (c.moveToNext()) {
			p_count_in_driver = c.getInt(0);
		}
		c.close();
		
		String sqlLabelCount = "select count(id) from " + YthPunitShipmentDBHelper.tb_invoice +" where driver_id=?";
		Cursor cL = db.rawQuery(sqlLabelCount, new String[]{String.valueOf(b.getDriver_id())});
		int label_count_in_driver = 0;
		if (cL.moveToNext()) {
			label_count_in_driver = cL.getInt(0);
		}
		cL.close();
		
		if (p_count_in_driver <=0 && label_count_in_driver <= 0) {
			String delProduct = "delete from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=?";
			db.execSQL(delProduct, new Object[]{b.getDriver_id()});
			
			String delDriver = "delete from " + YthPunitShipmentDBHelper.tb_driver_info +" where id=?";
			db.execSQL(delDriver,new Object[]{b.getDriver_id()});
		}
		
		String orderSql = "select count(id) from " + YthPunitShipmentDBHelper.tb_driver_info +" where order_id=?";
		Cursor c1 = db.rawQuery(orderSql, new String[]{String.valueOf(b.getOrder_id())});
		int driver_count_in_order = 0;
		if (c1.moveToNext()) {
			driver_count_in_order = c1.getInt(0);
		}
		c1.close();
		
		if (driver_count_in_order <= 0) {
			String delOrder = "delete from " + YthPunitShipmentDBHelper.tb_online_truck_invoice +" where id=?";
			db.execSQL(delOrder, new Object[]{b.getOrder_id()});
		}
		
		db.close();
	}

/*	@Override
	public void deleteShipmentProductBeanById(ShipmentProductBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delLabel = "delete from " + YthPunitShipmentDBHelper.tb_invoice +" where product_id=?";
		db.execSQL(delLabel, new Object[]{b.getId()});
		
		String delProduct = "delete from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where id=?";
		db.execSQL(delProduct, new Object[]{b.getId()});
		
		String sql = "select count(id) from " + YthPunitShipmentDBHelper.tb_shipment_product_info +" where diver_id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(b.getDriver_id())});
		int p_count_in_driver = 0;
		if (c.moveToNext()) {
			p_count_in_driver = c.getInt(0);
		}
		c.close();
		
		if (p_count_in_driver <=0) {
			String delDriver = "delete from " + YthPunitShipmentDBHelper.tb_driver_info +" where id=?";
			db.execSQL(delDriver,new Object[]{b.getDriver_id()});
		}
		
		String orderSql = "select count(id) from " + YthPunitShipmentDBHelper.tb_driver_info +" where order_id=?";
		Cursor c1 = db.rawQuery(orderSql, new String[]{String.valueOf(b.getOrder_id())});
		int driver_count_in_order = 0;
		if (c1.moveToNext()) {
			driver_count_in_order = c.getInt(0);
		}
		c1.close();
		
		if (driver_count_in_order <= 0) {
			String delOrder = "delete from " + YthPunitShipmentDBHelper.tb_online_truck_invoice +" where id=?";
			db.execSQL(orderSql, new Object[]{b.getOrder_id()});
		}
		
		db.close();
	}*/


	@Override
	public ShipmentProductBean getShipmentProductBean(Cursor c) {
		ShipmentProductBean b = new ShipmentProductBean();
		b.setId(c.getLong(c.getColumnIndex("id")));
		b.setOrder_id(c.getLong(c.getColumnIndex("order_id")));
		b.setDriver_id(c.getLong(c.getColumnIndex("diver_id")));
		b.setRepertory_id(c.getLong(c.getColumnIndex("repertory_id")));
		b.setItem_num(c.getString(c.getColumnIndex("item_num")));
		b.setHuowu_name(c.getString(c.getColumnIndex("huowu_name")));
		b.setOut_order_no(c.getString(c.getColumnIndex("out_order_no")));
		b.setXiangmu_no(c.getString(c.getColumnIndex("xiangmu_no")));
		b.setWeight(c.getString(c.getColumnIndex("weight")));
		b.setPrice(c.getString(c.getColumnIndex("price")));
		b.setSettle_price(c.getString(c.getColumnIndex("settle_price")));
		b.setOnline_status(c.getInt(c.getColumnIndex("online_status")));
		b.setGuozhang_time(c.getString(c.getColumnIndex("guozhang_time")));
		b.setIsguozhang(c.getInt(c.getColumnIndex("isguozhang")));
		b.setIsshenhe(c.getInt(c.getColumnIndex("isshenhe")));
		b.setNative_status(c.getInt(c.getColumnIndex("native_status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		return b;
	}

}
