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

public class CommonFarmersActivateWithPackDal  extends ActivateWithPackDalImpl{
	private CommonFarmersHelper helper;
	public CommonFarmersActivateWithPackDal(Context context) {
		helper = new CommonFarmersHelper(context);
	}
	@Override
	public int insertActivatePack(ActivateBean activateBean) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();//select @@identity
		String sql = "insert into " + CommonFarmersHelper.tb_activate_pack_info
				+ " (product_id,product_date,product_batch_number,producer,option_mode,package_label_id,count,status,error_code) values (?,?,?,?,?,?,?,?,?)";
		db.execSQL(sql,
				new Object[] {activateBean.getProduct_id(),activateBean.getProduct_date(),activateBean.getProduct_batch_number(),activateBean.getProducer(),activateBean.getOption_mode(),activateBean.getPackage_label_id(),activateBean.getPackCount(),activateBean.getStatus(),activateBean.getError_code()});
//		String querySql = "SELECT MAX(id) from " + DBOpenHelper.tb_train_invoice;//返回当表中
		String querySql = "select last_insert_rowid() from " + CommonFarmersHelper.tb_activate_pack_info;
		
//		String querySql = "select @@identity";
		Cursor cursor = db.rawQuery(querySql, null);
		if (cursor.moveToNext()) {
			key = cursor.getInt(0);
//			LogUtil.e(TConfig.Echo, "key:"+key);
		}
		cursor.close();
		db.close();
		return key;
	}
	@Override
	public List<ActivateBean> queryAllActivatePackList() {
		List<ActivateBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " +CommonFarmersHelper.tb_activate_pack_info +" order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getActivatePackBean(c));
		}
		c.close();
		db.close();
		return l;
	}
	@Override
	public List<ActivateBean> queryAllActivatePackListByASC() {
		List<ActivateBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " +CommonFarmersHelper.tb_activate_pack_info +" order by id asc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getActivatePackBean(c));
		}
		c.close();
		db.close();
		return l;
	}
	@Override
	public ActivateBean getActivatePackBean(Cursor c) {
		ActivateBean b = new ActivateBean();
		b.setType(1);
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setProduct_id(c.getString(c.getColumnIndex("product_id")));
		b.setProduct_date(c.getString(c.getColumnIndex("product_date")));
		b.setProduct_batch_number(c.getString(c.getColumnIndex("product_batch_number")));
		b.setProducer(c.getString(c.getColumnIndex("producer")));
		b.setOption_mode(c.getInt(c.getColumnIndex("option_mode")));
		b.setPackage_label_id(c.getString(c.getColumnIndex("package_label_id")));
		b.setPackCount(c.getInt(c.getColumnIndex("count")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		b.setCreateDate(c.getString(c.getColumnIndex("error_code")));
		return b;
	}
	@Override
	public ActivateBean queryActivatePackBeanById(int id) {
		ActivateBean aBean = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonFarmersHelper.tb_activate_pack_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			aBean = getActivatePackBean(c);
		}
		c.close();
		db.close();
		return aBean;
	}
	@Override
	public void updateActivatePack(ActivateBean activateBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
//		String updateSql = "update " + CommonDBHelper.tb_activate_pack_info + " set product_id=?,product_date=?,product_batch_number=?,producer=?,group_number=?,option_mode=?,package_label_id=?,count=?,status=?,error_code=? where id=?";
//		db.execSQL(updateSql, new Object[]{activateBean.getProduct_id(),activateBean.getProduct_date(),activateBean.getProduct_batch_number(),activateBean.getProducer(),activateBean.getGroup_number_id(),activateBean.getOption_mode(),activateBean.getPackage_label_id(),activateBean.getPackCount(),activateBean.getStatus(),activateBean.getStatus(),activateBean.getId()});
		String updateSql = "update " + CommonFarmersHelper.tb_activate_pack_info + " set product_id=?,product_date=?,product_batch_number=?,producer=?,option_mode=?,package_label_id=?,count=?,status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{activateBean.getProduct_id(),activateBean.getProduct_date(),activateBean.getProduct_batch_number(),activateBean.getProducer(),activateBean.getOption_mode(),activateBean.getPackage_label_id(),activateBean.getPackCount(),activateBean.getStatus(),activateBean.getStatus(),activateBean.getId()});
		db.close();
		
	}
	@Override
	public void delActivatePackById(ActivateBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from "+ CommonFarmersHelper.tb_activate_pack_info +" where id=?";
		db.execSQL(delSql, new Object[]{b.getId()});
		
		String delPack = "delete from " + CommonFarmersHelper.tb_pack_package_info +" where option_id=? and option_type=?";
		db.execSQL(delPack, new Object[]{b.getId(),HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption()});
		
		String delSubSql = "delete from " + CommonFarmersHelper.tb_pack_sub_label_info +" where option_id=? and option_type=?";
		db.execSQL(delSubSql, new Object[]{b.getId(),HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption()});
//		if (b.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
//			String delLabelSql="delete from " + CommonDBHelper.tb_single_label_option +" where option_type=? AND option_id=?";
//			db.execSQL(delLabelSql, new Object[]{HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption(),b.getId()});
//		} else if (b.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
//			String delLabelSql="delete from " + CommonDBHelper.tb_batch_label_option +" where option_type=? AND option_id=?";
//			db.execSQL(delLabelSql, new Object[]{HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption(),b.getId()});
//		}
		
		db.close();
	}
	@Override
	public void updateActivatePackErrorById(int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonFarmersHelper.tb_activate_pack_info + " set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{Config.SUBMIT_STATUS_SUBMIT_FAIL,errorCode,id});
		db.close();
		
	}
	@Override
	public void updateActivatePackErrorStatusById(int status, int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonFarmersHelper.tb_activate_pack_info + " set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{status,errorCode,id});
		db.close();		
	}
	@Override
	public boolean isSubLabelInOtherPack(String labelId) {
		boolean isExit = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select count(id) from " +  CommonFarmersHelper.tb_single_label_option +" where label_id=? and (option_type=? or option_type=?)";
		Cursor c = db.rawQuery(sql, new String[]{labelId,String.valueOf(HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption()),String.valueOf(HandsetOperation.OPERATION_PACK_CONSTANT.getOption())});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExit = true;
			}
		}
		c.close();
		db.close();
		return isExit;
	}
	@Override
	public boolean packageIdIsExitInOtherActivate(String labelId, int id, boolean isCreate) {
		boolean isExit = false;
		//在其他激活单中作为label存在
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select count(id) from " +  CommonFarmersHelper.tb_single_label_option +" where label_id=? and (option_type=? or option_type=?)";
		Cursor c = db.rawQuery(sql, new String[]{labelId,String.valueOf(HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption()),String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption())});
//		String sql = "select count(id) from " +  CommonDBHelper.tb_single_label_option +" where (label_id=? and option_type=?) or (label_id=? and option_type=? and option_id<>?)";
//		Cursor c = db.rawQuery(sql, new String[]{labelId,String.valueOf(HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption()),String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption())});
		
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExit = true;
			}
		}
		c.close();
		
		String batchSql = "select count(id) from " + CommonFarmersHelper.tb_batch_label_option +" where start_label_id<= ? and end_label_id>=? and option_type=?";
		Cursor bC = db.rawQuery(batchSql, new String[]{labelId,labelId,String.valueOf(HandsetOperation.OPERATION_ACTIVATE_CONSTANT.getOption())});
		if (bC.moveToNext()) {
			int count = bC.getInt(0);
			if (count > 0) {
				isExit = true;
			}
		}
		bC.close();
		
		Cursor c1 = null;
		//在其他激活并打包的订单中存在
		if (isCreate) {
			String pSql = "select count(id) from " + CommonFarmersHelper.tb_activate_pack_info +" where package_label_id=?";
			c1 = db.rawQuery(pSql, new String[] {labelId});
		} else {
			String pSql = "select count(id) from " + CommonFarmersHelper.tb_activate_pack_info +" where package_label_id=? and id<>?";
			c1 = db.rawQuery(pSql, new String[] {labelId, String.valueOf(id)});
		}
		
		if (c1.moveToNext()) {
			int count = c1.getInt(0);
			if (count > 0) {
				isExit = true;
			}
		}
		c1.close();
		
		db.close();
		return isExit;
	}
	@Override
	public boolean isPackageIdInOtherPack(String labelId,int id) {
		boolean isExist = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select count(id) from " + CommonFarmersHelper.tb_activate_pack_info +" where package_label_id=? and id<>?";
		Cursor c = db.rawQuery(sql, new String[] {labelId,String.valueOf(id)});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		c.close();
		
		String sql1 = "select count(id) from " + CommonFarmersHelper.tb_pack_info +" where package_label_id=?";
		Cursor c1 = db.rawQuery(sql1, new String[] {labelId});
		if (c1.moveToNext()) {
			int count = c1.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		c1.close();
		db.close();
		return isExist;
	}
	@Override
	public boolean isPackageIdInAWithPack(String labelId) {
		boolean isExist = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select count(id) from " + CommonFarmersHelper.tb_pack_package_info +" where package_label_id=? and option_type=?";
		Cursor c = db.rawQuery(sql, new String[] {labelId, String.valueOf(HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption())});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		c.close();
		
		String subSql = "select count(id) from " + CommonFarmersHelper.tb_pack_sub_label_info+" where label_id=? and option_type=?";
		Cursor subC = db.rawQuery(subSql, new String[] {labelId, String.valueOf(HandsetOperation.OPERATION_ACTIVATE_WITH_PACK_CONSTANT.getOption())});
		if (subC.moveToNext()) {
			int count = subC.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		subC.close();
		
		db.close();
		return isExist;
	}
	@Override
	public boolean isExitPackageIdInAWithPack(String startLabel, String endLabel) {
		boolean isExist = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select count(id) from " + CommonFarmersHelper.tb_pack_package_info +" where package_label_id>=? AND package_label_id<=?";
		Cursor c = db.rawQuery(sql, new String[] {startLabel,endLabel});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		c.close();
		
		String subSql = "select count(id) from " + CommonFarmersHelper.tb_pack_sub_label_info+" where label_id>=? and label_id<=?";
		Cursor subC = db.rawQuery(subSql, new String[] {startLabel,endLabel});
		if (subC.moveToNext()) {
			int count = subC.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		subC.close();
		
		db.close();
		return isExist;
	}
	
}
