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

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.PackSubLabelInfoBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.db.CommonFarmersHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.PackSubLabelDalImpl;

public class CommonFarmersPackSubLabelDal extends PackSubLabelDalImpl{
	private CommonFarmersHelper helper;
	
	public CommonFarmersPackSubLabelDal(Context context) {
		helper = new CommonFarmersHelper(context);
	}

	@Override
	public void insertPackSubLabel(PackSubLabelInfoBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + CommonFarmersHelper.tb_pack_sub_label_info+" (label_id,package_id,option_id,option_type) values (?,?,?,?)";
		db.execSQL(sql, new Object[] {b.getLabel_id(),b.getPackage_id(),b.getOption_id(),b.getOption_type()});
		db.close();
	}

	@Override
	public List<PackSubLabelInfoBean> queryAllSubLabelByPackageId(int packageId,int option_id,int option_type) {
		List<PackSubLabelInfoBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "select * from " + CommonFarmersHelper.tb_pack_sub_label_info +" where package_id=? and option_id=? and option_type=? order by id desc";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(packageId),String.valueOf(option_id),String.valueOf(option_type)});
		while (c.moveToNext()) {
			list.add(getSubLabel(c));
		}
		c.close();
		db.close();
		return list;
	}
	@Override
	public List<String> queryAllSubLabelStrByPackageId(int packageId, int option_id, int option_type) {
		List<String> list = new ArrayList<>();
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "select label_id from " + CommonFarmersHelper.tb_pack_sub_label_info +" where package_id=? and option_id=? and option_type=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(packageId),String.valueOf(option_id),String.valueOf(option_type)});
		while (c.moveToNext()) {
			list.add(c.getString(0));
		}
		c.close();
		db.close();
		return list;
	}
	@Override
	public void deletePackLabelById(int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + CommonFarmersHelper.tb_pack_sub_label_info +" where id=?";
		db.execSQL(sql, new Object[]{id});
		db.close();
	}

	@Override
	public boolean hasExistActivateLabel(String labelId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		//在激活打包表中存在
		String sql = "select count(id) from " + CommonFarmersHelper.tb_pack_sub_label_info + " where label_id=?";
		Cursor c = db.rawQuery(sql, new String[]{labelId});
		if (c.moveToNext()) {
			int count = c.getInt(0);
//			LogUtil.d(TConfig.Echo, "单标：count" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		c.close();
		
		//存在于单标激活中
		String aSingleSql = "select count(id) from " + CommonFarmersHelper.tb_single_label_option +" where label_id=? and option_type=?";
		Cursor cSingle = db.rawQuery(aSingleSql, new String[]{labelId,String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption())} );
		if (cSingle.moveToNext()) {
			int count = cSingle.getInt(0);
//			LogUtil.d(TConfig.Echo, "单标：count" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		cSingle.close();
		
		String aBatchSql = "select count(id) as count from " + CommonFarmersHelper.tb_batch_label_option
				+ " where start_label_id<=? AND end_label_id>=? AND option_type=?";
		Cursor cBatch = db.rawQuery(aBatchSql, new String[]{labelId,labelId,String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption())});
		if (cBatch.moveToNext()) {
			int count = cBatch.getInt(0);
//			LogUtil.d(TConfig.Echo, "单标：count" + count);
			if (count > 0) {
				isExist = true;
			}
		}
		cBatch.close();
		db.close();
		return isExist;
	}

	@Override
	public boolean hasExistSubPackLabel(String labelId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) from " +CommonFarmersHelper.tb_pack_sub_label_info+" where label_id=?";
		Cursor c = db.rawQuery(sql, new String[]{labelId});
		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 hasExistSubPackLabelInPackageId(String labelId, int packageId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(id) from " +CommonFarmersHelper.tb_pack_sub_label_info+" where label_id=? and package_id=?";
		Cursor c = db.rawQuery(sql, new String[]{labelId,String.valueOf(packageId)});
		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 void updateLabelStatus(String labelId, int option_id, int option_type, int status) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + CommonFarmersHelper.tb_pack_sub_label_info +" set labelStatus=? where label_id=? and option_id=? and option_type=?";
		db.execSQL(sql, new Object[]{status,labelId,option_id,option_type});
		db.close();
	}

	@Override
	public void updateLabelListStatus(List<String> labels,int package_id, int option_id, int option_type, int status) {
//		LogUtil.e(TConfig.Echo, "labels:" + labels + " option_type:" + option_type +"  status:" + status);
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "update " + CommonFarmersHelper.tb_pack_sub_label_info +" set labelStatus=? where label_id=? and option_id=? and option_type=? and package_id=? ";
		for (String labelId : labels) {
//			LogUtil.e(TConfig.Echo, "labelis:"+labelId +"  status:" + status +"  option_id:" + option_id +" option_type:"+option_type);
			db.execSQL(sql, new Object[]{status,labelId,option_id,option_type,package_id});
		}
		db.close();
	}

	@Override
	public int queryLabelCountByOptionId(int option_id, int option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		int count = 0;
		String sql = "select count(id) from " + CommonFarmersHelper.tb_pack_sub_label_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 int queryLabelCountByPackageId(int packageId) {
		SQLiteDatabase db = helper.getReadableDatabase();
		int count = 0;
		String sql = "select count(id) from " + CommonFarmersHelper.tb_pack_sub_label_info+" where package_id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(packageId)});
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		c.close();
		db.close();
		return count;
	}
	@Override
	public PackSubLabelInfoBean getSubLabel(Cursor c) {
		PackSubLabelInfoBean b = new PackSubLabelInfoBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setLabel_id(c.getLong(c.getColumnIndex("label_id")));
		b.setPackage_id(c.getInt(c.getColumnIndex("package_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;
	}

	

}
