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

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.PackPackageInfoBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.db.CommonEggDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.PackPackageDalImpl;

public class CommonEggPackPackageDal extends PackPackageDalImpl {
	private CommonEggDBHelper helper;

	public CommonEggPackPackageDal(Context context) {
		helper = new CommonEggDBHelper(context);
	}

	@Override
	public int insertPackPackage(PackPackageInfoBean b) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql1 = "select id from "+ CommonEggDBHelper.tb_pack_package_info + " where package_label_id=? and option_id=? and option_type=?";
		Cursor c = db.rawQuery(sql1, new String[]{String.valueOf(b.getPackage_label_id()),String.valueOf(b.getOption_id()),String.valueOf(b.getOption_type())});
		if (c.moveToNext()) {
			key = c.getInt(0);
		}
		c.close();
		if (key > 0) {
			return key;
		}
		String sql = "insert into " + CommonEggDBHelper.tb_pack_package_info
				+ " (package_label_id,option_id,option_type) values (?,?,?)";
		db.execSQL(sql, new Object[] { b.getPackage_label_id(), b.getOption_id(), b.getOption_type() });
		String querySql = "select last_insert_rowid() from " + CommonEggDBHelper.tb_pack_package_info;

		// String querySql = "select @@identity";
		Cursor cursor = db.rawQuery(querySql, null);
		if (cursor.moveToNext()) {
			key = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return key;
	}

	@Override
	public List<PackPackageInfoBean> queryAllPackPackageDESC(int option_id, int option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<PackPackageInfoBean> list = new ArrayList<>();
		String sql = "select * from " + CommonEggDBHelper.tb_pack_package_info +" where option_id=? and option_type=? order by id desc";
		Cursor c = db.rawQuery(sql, new String[] {String.valueOf(option_id),String.valueOf(option_type)});
		while (c.moveToNext()) {
			list.add(getPackPackageInfoBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public List<PackPackageInfoBean> queryAllPackPackageASC(int option_id, int option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<PackPackageInfoBean> list = new ArrayList<>();
		String sql = "select * from " + CommonEggDBHelper.tb_pack_package_info +" where option_id=? and option_type=? order by id asc";
		Cursor c = db.rawQuery(sql, new String[] {String.valueOf(option_id),String.valueOf(option_type)});
		while (c.moveToNext()) {
			list.add(getPackPackageInfoBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public int queryPackageCountByOption(int option_id, int option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		int count = 0;
		String sql = "select count(id) from " + CommonEggDBHelper.tb_pack_package_info +" where option_id=? and option_type=?";
		Cursor c = db.rawQuery(sql, new String[] {String.valueOf(option_id),String.valueOf(option_type)});
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		c.close();
		db.close();
		return count;
	}
	@Override
	public PackPackageInfoBean queryPackPackageById(int id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		PackPackageInfoBean b = null;
		String sql = "select * from " + CommonEggDBHelper.tb_pack_package_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getPackPackageInfoBean(c);
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public void deletePackPackageById(int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delLabelSql = "delete from " + CommonEggDBHelper.tb_pack_sub_label_info +" where package_id=?";
		db.execSQL(delLabelSql, new Object[]{id});
		String delPackageSql = "delete from " + CommonEggDBHelper.tb_pack_package_info +" where id=?";
		db.execSQL(delPackageSql,new Object[]{id});
		db.close();
		

	}

	@Override
	public void deletePackPackageByOptionId(int option_id, int option_type) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delLabelSql = "delete from " + CommonEggDBHelper.tb_pack_sub_label_info +" where option_id=? and option_type=?";
		db.execSQL(delLabelSql, new Object[]{option_id,option_type});
		String delPackageSql = "delete from " + CommonEggDBHelper.tb_pack_package_info +" where option_id=? and option_type=?";
		db.execSQL(delPackageSql, new Object[]{option_id,option_type});
		db.close();
	}
	
	@Override
	public void updatePackPackageCountById(int id, int count) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update "+ CommonEggDBHelper.tb_pack_package_info +" set count=? where id=?";
		db.execSQL(sql, new Object[]{count,id});
		db.close();
	}

	@Override
	public void updatePackPackageSubmitStatus(int id, int status, int error_code) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update "+ CommonEggDBHelper.tb_pack_package_info +" set status=?,error_code=? where id=?";
		db.execSQL(sql, new Object[]{status,error_code,id});
		db.close();
		
	}

	@Override
	public boolean hasExisPackageInOtherPack(String packageLabel, int option_type, int option_id,
			int other_option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) from " + CommonEggDBHelper.tb_pack_package_info + " where (package_label_id=? and option_type=? and option_id<>?) or (package_label_id=? and option_type=?)";
		Cursor c = db.rawQuery(sql, new String[]{packageLabel,String.valueOf(option_type),String.valueOf(option_id),packageLabel,String.valueOf(other_option_type)});
		if (c.moveToNext()) {
			int count = c.getInt(0);
//			LogUtil.d(TConfig.Echo, "单标：count" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		c.close();
		db.close();
		return isExist;
	}

	@Override
	public boolean hasExisPackageInOtherActivate(String packageLabel) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) as count from " + CommonEggDBHelper.tb_single_label_option
				+ " where label_id=? AND option_type = ?";
		Cursor cursor = db.rawQuery(sql, new String[] { packageLabel, String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption()) });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
//			LogUtil.d(TConfig.Echo, "单标：count" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		cursor.close();
		//count(id) as count
		String batchSql = "select count(id) as count from " + CommonEggDBHelper.tb_batch_label_option
				+ " where start_label_id<=? AND end_label_id>=? AND option_type=?";

		Cursor c2 = db.rawQuery(batchSql, new String[] { packageLabel, packageLabel, String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption())  });
		if (c2.moveToNext()) {
			int count = c2.getInt(0);
//			LogUtil.d(TConfig.Echo, "批量：count" + count);
			if (count > 0) {
				isExist = true;
			}
		}

		c2.close();
		db.close();
		return isExist;
	}
	@Override
	public PackPackageInfoBean getPackPackageInfoBean(Cursor c) {
		PackPackageInfoBean b = new PackPackageInfoBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setPackage_label_id(c.getLong(c.getColumnIndex("package_label_id")));
		b.setOption_id(c.getInt(c.getColumnIndex("option_id")));
		b.setOption_type(c.getInt(c.getColumnIndex("option_type")));
		b.setCount(c.getInt(c.getColumnIndex("count")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		return b;
	}


}
