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.SingleLabelOptionBean;
import cn.net.caoyao.cpzshandset.db.SheepExDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.SingleLabelDalImpl;
import cn.net.cpzslibs.prot.handset.bean.req.Prot20002ReqBatchBean;

public class SheepExSingleLabelDal extends SingleLabelDalImpl{
	private SheepExDBHelper helper;

	public SheepExSingleLabelDal(Context context) {
		helper = new SheepExDBHelper(context);
	}
	
	/**
	 * 插入标签到单标库中
	 * @param b
	 */
	public void insertLabel(SingleLabelOptionBean b){
//		LogUtil.d(TConfig.Echo, "插入数据：" + b.toString());
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + SheepExDBHelper.tb_single_label_option+" (label_id,option_type,option_id) values(?,?,?)";
//		db.execSQL(sql, new Object[]{Long.parseLong(b.getLabel_id()),b.getOption_type(),b.getOption_id()});
		db.execSQL(sql, new Object[]{b.getLabel_id(),b.getOption_type(),b.getOption_id()});
		db.close();
	}
	/**
	 * 通过操作类型和操作id删除标签
	 * @param option_type
	 * @param option_id
	 */
	public void deleteLabelById(int option_type, int option_id){
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + SheepExDBHelper.tb_single_label_option+" where option_type=? AND option_id=?";
		db.execSQL(sql, new Object[]{option_type,option_id});
		db.close();
	}
	
	/**
	 * 通过操作类型和操作id和labelId删除标签
	 * @param option_type
	 * @param option_id
	 */
	public void deleteLabelByLabelId(SingleLabelOptionBean b){
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + SheepExDBHelper.tb_single_label_option+" where option_type=? AND option_id=? AND label_id=?";
		db.execSQL(sql, new Object[]{b.getOption_type(),b.getOption_id(),b.getLabel_id()});
		db.close();
	}

	/**
	 * 根据操作类型和操作id，查询所有的label信息
	 * @param option_type
	 * @param option_id
	 * @return
	 */
	public List<SingleLabelOptionBean> queryAllLabelById(int option_type, int option_id) {
		List<SingleLabelOptionBean> labels = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + SheepExDBHelper.tb_single_label_option
				+ " where option_type = ? AND option_id=? order by id desc";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(option_type), String.valueOf(option_id)});
		while (c.moveToNext()) {
			labels.add(getLabelBean(c));
		}
		c.close();
		
		//所有单标的
		/*List<SingleLabelOptionBean> labels2 = new ArrayList<>();
		String sql2 = "select * from " + SheepExDBHelper.tb_single_label_option;
		Cursor c2 = db.rawQuery(sql2, null);
		while (c2.moveToNext()) {
			labels2.add(getLabelBean(c2));
		}
		LogUtil.i(TConfig.Echo, labels2.size() + "  查询所有数据：" + labels2.toString());
		c2.close();
		LogUtil.i(TConfig.Echo, labels2.size() + "  查询所有数据：" + labels2.toString());
		
		List<Prot20002ReqBatchBean> labels3 = new ArrayList<>();
		String sql3 = "select * from " + SheepExDBHelper.tb_batch_label_option;
		Cursor c3 = db.rawQuery(sql3, null);
		while (c3.moveToNext()) {
//			labels2.add(getLabelBean(c2));
			labels3.add(getBatchBean(c3));
		}
		LogUtil.i(TConfig.Echo, labels2.size() + labels3.size()+"  查询批量所有数据：" + labels3.toString());
		c3.close();*/
		
		db.close();
		return labels;
	}
	
	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;
	}

	public SingleLabelOptionBean getLabelBean(Cursor c) {
		
//		LogUtil.i(TConfig.Echo,c.getType(c.getColumnIndex("label_id"))+" String:  "+c.getString(c.getColumnIndex("label_id")) +" int值"+c.getInt(c.getColumnIndex("label_id"))+ "  long值：" + c.getLong(c.getColumnIndex("label_id")));
		SingleLabelOptionBean b = new SingleLabelOptionBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setLabel_id(c.getString(c.getColumnIndex("label_id")));
		b.setOption_id(c.getInt(c.getColumnIndex("option_id")));
		b.setOption_type(c.getInt(c.getColumnIndex("option_type")));
		b.setLabelStatus(c.getInt(c.getColumnIndex("labelStatus")));
		
		return b;
	}

	/**
	 * 检测某个标签是否已经存在在激活表中
	 * 
	 * @param labelId
	 * @return
	 */
	public boolean hasExistSingleLabel(String labelId, int option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) as count from " + SheepExDBHelper.tb_single_label_option
				+ " where label_id=? AND option_type = ?";
		Cursor cursor = db.rawQuery(sql, new String[] { labelId, String.valueOf(option_type) });
		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 " + SheepExDBHelper.tb_batch_label_option
				+ " where start_label_id<=? AND end_label_id>=? AND option_type=?";

		Cursor c2 = db.rawQuery(batchSql, new String[] { labelId, labelId, String.valueOf(option_type) });
		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;
	}

	/**
	 * 某个操作的单标已经达到最大限制
	 * 
	 * @param option_type
	 * @param option_id
	 * @return
	 */
	public boolean isMaxLimitCount(int option_type, int option_id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) as count from " + SheepExDBHelper.tb_single_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()) {
			int count = cursor.getInt(0);
			if (count >= 2000) {
				isExist = true;
			}
		}
		cursor.close();
		db.close();
		return isExist;

	}

	/**
	 * 根据操作类型和编号查询标签个数
	 * 
	 * @param option_type
	 * @param option_id
	 * @return
	 */
	public int queryLabelCountById(int option_type, int option_id) {
		SQLiteDatabase db = helper.getReadableDatabase();
//		boolean isExist = false;
		String sql = "select count(id) as count from " + SheepExDBHelper.tb_single_label_option
				+ " where option_type=? AND option_id = ?";
		Cursor cursor = db.rawQuery(sql, new String[] { String.valueOf(option_type), String.valueOf(option_id) });
		int count = 0;
		if (cursor.moveToNext()) {
			count = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return count;
	}

	/**
	 * 查询某个操作下的所有单标数据
	 */
	@Override
	public List<SingleLabelOptionBean> queryAllLabelByOptionType(int option_type) {
		List<SingleLabelOptionBean> labels = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + SheepExDBHelper.tb_single_label_option
				+ " where option_type = ? order by id desc";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(option_type)});
		while (c.moveToNext()) {
			labels.add(getLabelBean(c));
		}
		c.close();
		db.close();
		return labels;
	}

	@Override
	public boolean hasExistSingleLabelInAId(String labelId, int option_type, int id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) as count from " + SheepExDBHelper.tb_single_label_option
				+ " where label_id=? AND option_type = ? AND option_id = ?";
		Cursor cursor = db.rawQuery(sql, new String[] { labelId, String.valueOf(option_type),String.valueOf(id) });
		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 " + SheepExDBHelper.tb_batch_label_option
				+ " where start_label_id<=? AND end_label_id>=? AND option_type=? AND option_id = ?";

		Cursor c2 = db.rawQuery(batchSql, new String[] { labelId, labelId, String.valueOf(option_type),String.valueOf(id) });
		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 List<String> queryAllLabelStrById(int option_type, int option_id) {
		List<String> labels = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select label_id from " + SheepExDBHelper.tb_single_label_option
				+ " where option_type = ? AND option_id=? order by id desc";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(option_type), String.valueOf(option_id)});
		while (c.moveToNext()) {
			labels.add(c.getString(0));
		}
		c.close();
		db.close();
		return labels;
	}

	@Override
	public void updateLableStatusById(List<String> labelId, int option_type, int option_id,int labelStatus) {
//		LogUtil.e(TConfig.Echo, "option_type:" + option_type +"  option_id:" + option_id +"  labelStatus:" + labelStatus+"  labelId:" + labelId.toString());
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + SheepExDBHelper.tb_single_label_option+" set labelStatus=? where label_id=? AND option_type=? AND option_id=?";
		for (String label : labelId) {
			db.execSQL(updateSql, new Object[]{labelStatus,label,option_type,option_id});
		}
		db.close();
	}

}
