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

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.db.CommonEggDBHelper;
import cn.net.caoyao.cpzshandset.db.LiqueurDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.BatchLabelDalImpl;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20002ReqBatchBean;

public class CommonEggBatchLabelDal extends BatchLabelDalImpl{
	private CommonEggDBHelper helper;

	public CommonEggBatchLabelDal(Context context) {
		helper = new CommonEggDBHelper(context);
	}
	
	/**
	 * 根据批量数据Id修改批量数据
	 * @param id
	 * @param reqBatchBean
	 */
	public void updateBatchLabelById(int id, Prot20002ReqBatchBean reqBatchBean){
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update "+CommonEggDBHelper.tb_batch_label_option +" set start_label_id=?,end_label_id=? where id=?";
		db.execSQL(updateSql, new Object[]{reqBatchBean.getStart(),reqBatchBean.getEnd(),id});
		db.close();
	}
	/**
	 * 删除所有的批量数据
	 */
	public void deleteAllBatch(){
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " +  CommonEggDBHelper.tb_batch_label_option;
		db.execSQL(delSql);
		db.close();
	}
	/**
	 * 删除某种操作的所有批量数据
	 */
	public void deleteBatchByType(int option_type){
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " +  CommonEggDBHelper.tb_batch_label_option +" where option_type = ?";
		db.execSQL(delSql,new Object[]{option_type});
		db.close();
	}
	
	/**
	 * 根据id和操作类型删除某条批量操作的数据
	 */
	public void deleteBatchById(int option_type,int option_id){
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " +  CommonEggDBHelper.tb_batch_label_option +" where option_type = ? AND option_id = ?";
		db.execSQL(delSql,new Object[]{option_type, option_id});
		db.close();
	}
	
	/**
	 * 插入批量激活信息到数据库
	 * @param option_type
	 * @param option_id
	 * @param reqBatchBean
	 * @return
	 */
	public int insertBatch(int option_type,int option_id, Prot20002ReqBatchBean reqBatchBean){
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		if (hasExistOptionBatchInfo(db,option_type, option_id)) {
			String updateSql = "update "+CommonEggDBHelper.tb_batch_label_option +" set start_label_id=?,end_label_id=? where option_type=? AND option_id=?";
			db.execSQL(updateSql,new Object[]{reqBatchBean.getStart(),reqBatchBean.getEnd(),option_type,option_id});
		} else {
			String insertSql = "insert into " + CommonEggDBHelper.tb_batch_label_option+" (start_label_id,end_label_id,option_type,option_id) values(?,?,?,?)";
			db.execSQL(insertSql,new Object[]{reqBatchBean.getStart(),reqBatchBean.getEnd(),option_type,option_id});
		}
		
		String rawSql = "select last_insert_rowid() from " + CommonEggDBHelper.tb_batch_label_option;
		
		
		Cursor cursor = db.rawQuery(rawSql, null);
		if (cursor.moveToNext()) {
			key = cursor.getInt(0);
			LogUtil.e(TConfig.Echo, "key:"+key);
		}
		cursor.close();
		db.close();
		return key;
	}
	
	public boolean hasExistOptionBatchInfo(SQLiteDatabase db,int option_type, int option_id){
		boolean isExist = false;
		String rawSql = "select count(id) as count from " + CommonEggDBHelper.tb_batch_label_option +" where option_type=? AND option_id=?";
		Cursor c = db.rawQuery(rawSql, new String[]{String.valueOf(option_type),String.valueOf(option_id)});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		
		c.close();
//		db.close();
		return isExist;
	}
	/**
	 * 根据操作类型和操作id查询批量数据的个数
	 * @param option_type
	 * @param option_id
	 * @return
	 */
	public int queryBatchCountById(int option_type,int option_id){
		int count = 0;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonEggDBHelper.tb_batch_label_option +" where option_type=? AND option_id = ?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(option_type),String.valueOf(option_id)});
		if (cursor.moveToNext()) {
			long start = cursor.getLong(cursor.getColumnIndex("start_label_id"))/10000;
			long end = cursor.getLong(cursor.getColumnIndex("end_label_id"))/10000;
			count = (int) (end - start + 1);
		}
		cursor.close();
		db.close();
		return count;
	}

	/**
	 * 根据操作类型和操作id查询批量数据
	 * @param option_type
	 * @param option_id
	 * @return
	 */
	public Prot20002ReqBatchBean queryBatchBeanById(int option_type, int option_id) {
		Prot20002ReqBatchBean batchBean = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonEggDBHelper.tb_batch_label_option +" where option_type=? AND option_id = ?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(option_type),String.valueOf(option_id)});
		if (cursor.moveToNext()) {
			batchBean = getBatchBean(cursor);
		}
		cursor.close();
		db.close();
		return batchBean;
	}


	/**
	 * 检测某个标签段是否已经存在在激活表中
	 * 
	 * @param labelId
	 * @return
	 */
	public boolean hasExistBatchLabel(String startLabel, String endLabel, int option_type,int optionId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) as count from " + CommonEggDBHelper.tb_single_label_option
				+ " where label_id>=? AND label_id<=? AND option_type = ?";
		Cursor cursor = db.rawQuery(sql, new String[] { startLabel, endLabel, String.valueOf(option_type) });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
			LogUtil.d(TConfig.Echo, "单标存在：" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		cursor.close();
		long start = Long.parseLong(startLabel);
		long end = Long.parseLong(endLabel);
		String condition1 = "SELECT COUNT(id) as count FROM " + CommonEggDBHelper.tb_batch_label_option
				+ " where option_type=" + option_type + " AND start_label_id >=" + start + " AND start_label_id<=" + end +" AND option_id <> "+optionId;//
		String condition2 = " UNION ALL SELECT COUNT(id) as count FROM " + CommonEggDBHelper.tb_batch_label_option
				+ " where option_type=" + option_type + " AND start_label_id <=" + start + " AND end_label_id>=" + start+" AND option_id <> "+optionId;//
		String condition3 = " UNION ALL SELECT COUNT(id) as count FROM " + CommonEggDBHelper.tb_batch_label_option
				+ " where option_type=" + option_type + " AND start_label_id <=" + start + " AND end_label_id>=" + end+" AND option_id <> "+optionId;//
		String condition4 = " UNION ALL SELECT COUNT(id)as count FROM " + CommonEggDBHelper.tb_batch_label_option
				+ " where option_type=" + option_type + " AND start_label_id >=" + start + " AND end_label_id<=" + end+" AND option_id <> "+optionId;//
		String sql2 = condition1 + condition2 + condition3 + condition4;
		Cursor c2 = db.rawQuery(sql2, null);

		while (c2.moveToNext()) {
			int count = c2.getInt(0);
			if (count > 0) {
				LogUtil.d(TConfig.Echo, "批量存在：" + count);
				isExist = true;
			}
		}

		c2.close();
		db.close();
		return isExist;
	}
	
	public Prot20002ReqBatchBean getBatchBean(Cursor cursor) {
		Prot20002ReqBatchBean b = new Prot20002ReqBatchBean();
		b.setStart(cursor.getString(cursor.getColumnIndex("start_label_id")));
		b.setEnd(cursor.getString(cursor.getColumnIndex("end_label_id")));
		return b;
	}

	@Override
	public boolean hasExistBatchLabelInTwoType(String startLabel, String endLabel, int current_option_type, int other_option_type,
			int optionId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) as count from " + LiqueurDBHelper.tb_single_label_option
				+ " where label_id>=? AND label_id<=? AND (option_type = ? OR option_type = ?)";
		Cursor cursor = db.rawQuery(sql, new String[] { startLabel, endLabel, String.valueOf(current_option_type), String.valueOf(other_option_type) });
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
			LogUtil.d(TConfig.Echo, "单标存在：" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		cursor.close();
		long start = Long.parseLong(startLabel);
		long end = Long.parseLong(endLabel);
//		String condition1 = "SELECT COUNT(id) as count FROM " + LiqueurDBHelper.tb_batch_label_option
//				+ " where (option_type=" + option_type1 + " OR option_type=" + option_type2+")" + " AND start_label_id >=" + start + " AND start_label_id<=" + end +" AND option_id <> "+optionId;//
//		String condition2 = " UNION ALL SELECT COUNT(id) as count FROM " + LiqueurDBHelper.tb_batch_label_option
//				+ " where (option_type=" + option_type1 + " OR option_type=" + option_type2+")" + " AND start_label_id <=" + start + " AND end_label_id>=" + start+" AND option_id <> "+optionId;//
//		String condition3 = " UNION ALL SELECT COUNT(id) as count FROM " + LiqueurDBHelper.tb_batch_label_option
//				+ " where (option_type=" + option_type1 + " OR option_type=" + option_type2+")" + " AND start_label_id <=" + start + " AND end_label_id>=" + end+" AND option_id <> "+optionId;//
//		String condition4 = " UNION ALL SELECT COUNT(id)as count FROM " + LiqueurDBHelper.tb_batch_label_option
//				+ " where (option_type=" + option_type1 + " OR option_type=" + option_type2+")" + " AND start_label_id >=" + start + " AND end_label_id<=" + end+" AND option_id <> "+optionId;//
//		String sql2 = condition1 + condition2 + condition3 + condition4;
		String sql2 = "SELECT COUNT(id) as count FROM " + LiqueurDBHelper.tb_batch_label_option +" WHERE ((start_label_id >= " + start + " AND start_label_id<=" + end+") "+
				" OR (start_label_id <=" + start + " AND end_label_id>=" + start +")" +
				" OR (start_label_id <=" + start + " AND end_label_id>=" + end +")" +
				" OR (start_label_id >=" + start + " AND end_label_id<=" + end + "))"+
				" AND ((option_type = " + current_option_type + " AND option_id <> "+optionId +") OR (option_type = " + other_option_type + "))" ;
		
		Cursor c2 = db.rawQuery(sql2, null);

		while (c2.moveToNext()) {
			int count = c2.getInt(0);
			if (count > 0) {
				LogUtil.d(TConfig.Echo, "当前激活列表中批量存在：" + count);
				isExist = true;
			}
		}

		c2.close();
		
		
		db.close();
		
		return isExist;
	}

}
