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.ActivateBean;
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.ActivateDalImpl;

public class LiqueurActivateDal extends ActivateDalImpl{
	private LiqueurDBHelper helper;
	public LiqueurActivateDal(Context context) {
		helper = new LiqueurDBHelper(context);
	}
	/**
	 * 生成激活信息
	 * 
	 * @param trainInvoice
	 */
	public int insertActivate(ActivateBean activateBean) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();//select @@identity
		String sql = "insert into " + LiqueurDBHelper.tb_activate_info
				+ " (product_id,product_date,product_batch_number,producer,group_number,option_mode,status,error_code) values (?,?,?,?,?,?,?,?)";
		db.execSQL(sql,
				new Object[] {activateBean.getProduct_id(),activateBean.getProduct_date(),activateBean.getProduct_batch_number(),activateBean.getProducer(),activateBean.getGroup_number_id(),activateBean.getOption_mode(),activateBean.getStatus(),activateBean.getError_code()});
//		String querySql = "SELECT MAX(id) from " + DBOpenHelper.tb_train_invoice;//返回当表中
		String querySql = "select last_insert_rowid() from " + LiqueurDBHelper.tb_activate_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
	 */
	public List<ActivateBean> queryAllActivateList(){
		List<ActivateBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " +LiqueurDBHelper.tb_activate_info +" order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getActivateBean(c,db));
		}
		c.close();
		db.close();
		return l;
	}
	
	public ActivateBean getActivateBean(Cursor c,SQLiteDatabase db) {
		ActivateBean b = new ActivateBean();
		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.setGroup_number_id(c.getInt(c.getColumnIndex("group_number")));
		b.setOption_mode(c.getInt(c.getColumnIndex("option_mode")));
		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;
	}
	/**
	 * 通过激活历史的id查询激活信息
	 */
	@Override
	public ActivateBean queryActivateBeanById(int id) {
		ActivateBean aBean = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurDBHelper.tb_activate_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			aBean = getActivateBean(c, db);
		}
		c.close();
		db.close();
		return aBean;
	}
	@Override
	public void updateActivate(ActivateBean activateBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + LiqueurDBHelper.tb_activate_info + " set product_id=?,product_date=?,product_batch_number=?,producer=?,group_number=?,option_mode=?,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.getStatus(),activateBean.getStatus(),activateBean.getId()});
		db.close();
	}
	@Override
	public void delActivateById(ActivateBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from "+ LiqueurDBHelper.tb_activate_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_ACTIVATE_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_ACTIVATE_CONSTANT.getOption(),b.getId()});
		}
		
		db.close();
	}
	@Override
	public void updateActivateErrorById(int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + LiqueurDBHelper.tb_activate_info + " set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{Config.SUBMIT_STATUS_SUBMIT_FAIL,errorCode,id});
		db.close();
	}

	@Override
	public void updateActivateErrorStatusById(int status,int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + LiqueurDBHelper.tb_activate_info + " set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{status,errorCode,id});
		db.close();
	}
	@Override
	public List<ActivateBean> queryAllActivateListByASC() {
		List<ActivateBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " +LiqueurDBHelper.tb_activate_info +" order by id ASC";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getActivateBean(c,db));
		}
		c.close();
		db.close();
		return l;
	}
}
