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

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.PackBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.db.LiqueurDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.PackDalImpl;

public class LiqueurPackDal extends PackDalImpl {
	private LiqueurDBHelper helper;

	public LiqueurPackDal(Context context) {
		helper = new LiqueurDBHelper(context);
	}

	/**
	 * 插入打包信息
	 * 
	 * @param packBean
	 * @return
	 */
	@Override
	public int insertPack(PackBean packBean) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + LiqueurDBHelper.tb_pack_info
				+ " (describe,count,package_label_id,option_mode,status,error_code) values (?,?,?,?,?,?)";
		db.execSQL(insertSql, new Object[] { packBean.getDescribe(),packBean.getCount(),packBean.getPackage_label_id(),packBean.getOption_mode(),packBean.getStatus(),packBean.getError_code() });
		String querySql = "select last_insert_rowid() from " + LiqueurDBHelper.tb_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;
	}

	/**
	 * 查询所有的打包列表，降序排列
	 * @return
	 */
	@Override
	public List<PackBean> queryAllPackList() {
		List<PackBean> beans = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurDBHelper.tb_pack_info+" order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			beans.add(getPackBean(c));
		}
		c.close();
		db.close();
		return beans;
	}
	/**
	 * 查询所有的打包列表，升序排列
	 * @return
	 */
	@Override
	public List<PackBean> queryAllPackListASC() {
		List<PackBean> beans = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurDBHelper.tb_pack_info+" order by id asc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			beans.add(getPackBean(c));
		}
		c.close();
		db.close();
		return beans;
	}

	@Override
	public PackBean getPackBean(Cursor c) {
		PackBean bean = new PackBean();
		bean.setId(c.getInt(c.getColumnIndex("id")));
		bean.setDescribe(c.getString(c.getColumnIndex("describe")));
		bean.setCount(c.getInt(c.getColumnIndex("count")));
		bean.setPackage_label_id(c.getString(c.getColumnIndex("package_label_id")));
		bean.setOption_mode(c.getInt(c.getColumnIndex("option_mode")));
		bean.setStatus(c.getInt(c.getColumnIndex("status")));
		bean.setError_code(c.getInt(c.getColumnIndex("error_code")));
		bean.setCreateDate(c.getString(c.getColumnIndex("createDate")));
		return bean;
	}

	/**
	 * 通过打包id，删除打包信息
	 */
	@Override
	public void deletePackInfoById(PackBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + LiqueurDBHelper.tb_pack_info+" where id=?";
		db.execSQL(delSql, new Object[]{b.getId()});
		if (b.getOption_mode() == Config.SINGLE_OPTION_LABEL_TYPE) {
			String delLabelSql="delete from " + LiqueurDBHelper.tb_single_label_option +" where option_type=? AND option_id=?";
			db.execSQL(delLabelSql, new Object[]{HandsetOperation.OPERATION_PACK_CONSTANT.getOption(),b.getId()});
		} else if (b.getOption_mode() == Config.BATCH_OPTION_LABEL_TYPE) {
			String delLabelSql="delete from " + LiqueurDBHelper.tb_batch_label_option +" where option_type=? AND option_id=?";
			db.execSQL(delLabelSql, new Object[]{HandsetOperation.OPERATION_PACK_CONSTANT.getOption(),b.getId()});
		}
		db.close();
	}

	/**
	 * 修改打包信息的状态
	 * @param status
	 * @param errorCode
	 * @param id
	 */
	@Override
	public void updatePackStatusById(int status, int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + LiqueurDBHelper.tb_pack_info+" set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{status,errorCode,id});
		db.close();
	}

	/**
	 * 已经作为包标签存在
	 * @param packBean
	 * @param labelId
	 * @return
	 */
	@Override
	public boolean isPackageId(String labelId) {
		boolean isPackage = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "select count(id) as count from " + LiqueurDBHelper.tb_pack_info +" where package_label_id=?";
		Cursor c = db.rawQuery(querySql, new String[]{labelId});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isPackage = true;
			}
		}
		c.close();
		db.close();
		return isPackage;
	}

	/**
	 * 通过打包id，更新打包信息
	 * @param packBean
	 */
	@Override
	public void updatePackById(PackBean packBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + LiqueurDBHelper.tb_pack_info+" set describe=?,count=?,package_label_id=?,option_mode=? where id=?";
		db.execSQL(updateSql, new Object[]{packBean.getDescribe(),packBean.getCount(),packBean.getPackage_label_id(),packBean.getOption_mode(),packBean.getId()});
		db.close();
		
	}

	/**
	 * 通过打包id查询打包信息
	 */
	@Override
	public PackBean queryPackById(int id) {
		PackBean b = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurDBHelper.tb_pack_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getPackBean(c);
		}
		c.close();
		db.close();
		return b;
	}

}
