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

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.ExchangeCargoBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.db.SheepExDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.ExchangeCargoDalImpl;

public class SheepExchangeCargoDal extends ExchangeCargoDalImpl {
	private SheepExDBHelper helper;

	public SheepExchangeCargoDal(Context context) {
		helper = new SheepExDBHelper(context);
	}

	@Override
	public int insertExchangeCargo(ExchangeCargoBean b) {
		int key = 0;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "INSERT INTO " + SheepExDBHelper.tb_sheep_exchange_cargo_option
				+ " (product_id,from_dealer_id,to_dealer_id,to_dealer_sale_area,exchange_count,option_mode) VALUES (?,?,?,?,?,?)";
		db.execSQL(insertSql, new Object[] { b.getProduct_id(), b.getFrom_dealer_id(), b.getTo_dealer_id(),b.getTo_dealer_sale_area(),
				b.getExchange_count(), b.getOption_mode() });
		String querySql = "select last_insert_rowid() from " + SheepExDBHelper.tb_sheep_exchange_cargo_option;

		Cursor cursor = db.rawQuery(querySql, null);
		if (cursor.moveToNext()) {
			key = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return key;
	}

	@Override
	public void updateExchangeCargoById(ExchangeCargoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "UPDATE " + SheepExDBHelper.tb_sheep_exchange_cargo_option +" SET product_id=?,from_dealer_id=?,to_dealer_id=?,to_dealer_sale_area=?,exchange_count=?,status=?,error_code=? WHERE id=?";
		db.execSQL(updateSql, new Object[]{b.getProduct_id(),b.getFrom_dealer_id(),b.getTo_dealer_id(),b.getTo_dealer_sale_area(),b.getExchange_count(),b.getStatus(),b.getError_code(),b.getId()});
		db.close();
	}
	@Override
	public void updateExchangeCargoStatusById(int status, int error_code, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "UPDATE " +SheepExDBHelper.tb_sheep_exchange_cargo_option+" SET status=?,error_code=? WHERE id=?";
		db.execSQL(updateSql, new Object[]{status,error_code,id});
		db.close();
	}

	@Override
	public void deleteExchangeCargoById(ExchangeCargoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "DELETE FROM " + SheepExDBHelper.tb_sheep_exchange_cargo_option+" WHERE id=?";
		db.execSQL(delSql, new Object[]{b.getId()});
		if (b.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
			 String delSingleSql = "delete from " + SheepExDBHelper.tb_single_label_option +" where option_type=? AND option_id=?";
			 db.execSQL(delSingleSql,new Object[]{HandsetOperation.OPERATION_EXCHANGE_CARGO_CONSTANT.getOption(),b.getId()});
		} else {
			String delLabelSql="delete from " + SheepExDBHelper.tb_batch_label_option +" where option_type=? AND option_id=?";
			db.execSQL(delLabelSql, new Object[]{HandsetOperation.OPERATION_EXCHANGE_CARGO_CONSTANT.getOption(),b.getId()});
		}
		db.close();
	}

	@Override
	public List<ExchangeCargoBean> queryAllExchangeCargoDesc() {
		List<ExchangeCargoBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + SheepExDBHelper.tb_sheep_exchange_cargo_option +" ORDER BY id DESC";
		Cursor c = db.rawQuery(querySql, null);
		while (c.moveToNext()) {
			list.add(getExchangeCargo(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public List<ExchangeCargoBean> queryAllExchangeCargoAsc() {
		List<ExchangeCargoBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + SheepExDBHelper.tb_sheep_exchange_cargo_option +" ORDER BY id ASC";
		Cursor c = db.rawQuery(querySql, null);
		while (c.moveToNext()) {
			list.add(getExchangeCargo(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public ExchangeCargoBean queryExchangeCargoById(int id) {
		ExchangeCargoBean b = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + SheepExDBHelper.tb_sheep_exchange_cargo_option +" WHERE id=?";
		Cursor c = db.rawQuery(querySql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getExchangeCargo(c);
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public ExchangeCargoBean getExchangeCargo(Cursor c) {
		ExchangeCargoBean b = new ExchangeCargoBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setProduct_id(c.getColumnIndex("product_id"));
		b.setFrom_dealer_id(c.getInt(c.getColumnIndex("from_dealer_id")));
		b.setTo_dealer_id(c.getInt(c.getColumnIndex("to_dealer_id")));
		b.setTo_dealer_sale_area(c.getInt(c.getColumnIndex("to_dealer_sale_area")));
		b.setExchange_count(c.getInt(c.getColumnIndex("exchange_count")));
		b.setOption_mode(c.getInt(c.getColumnIndex("option_mode")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		b.setCreateDate(c.getString(c.getColumnIndex("createDate")));
		return b;
	}


}
