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.UnpackBean;
import cn.net.caoyao.cpzshandset.db.CommonFarmersHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.UnPackDalImpl;

public class CommonFarmersUnPackDal extends UnPackDalImpl {
	private CommonFarmersHelper helper;

	public CommonFarmersUnPackDal (Context context) {
		helper = new CommonFarmersHelper(context);
	}
	
	/**
	 * 拆包列表中已经存在该标签
	 * @param labelId
	 * @return
	 */
	@Override
	public boolean hasExistUnpackLabel(String labelId){
		boolean b = false;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select count(id) as count from " + CommonFarmersHelper.tb_unpack_info +" where label_id=?";
		Cursor c = db.rawQuery(sql, new String[]{labelId});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				b = true;
			}
		}
		c.close();
		db.close();
		return b;
	}

	/**
	 * 查询所有的拆包信息，并按照降序的顺序排序
	 */
	@Override
	public List<UnpackBean> queryAllUnPackList() {
		List<UnpackBean> uList = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonFarmersHelper.tb_unpack_info + " order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			uList.add(getUnPackBean(c));
		}
		c.close();
		db.close();
		return uList;
	}

	/**
	 * 查询所有的拆包信息，并按照降序的升序排序
	 */
	@Override
	public List<UnpackBean> queryAllUnPackListASC() {
		List<UnpackBean> uList = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonFarmersHelper.tb_unpack_info + " order by id asc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			uList.add(getUnPackBean(c));
		}
		c.close();
		db.close();
		return uList;
	}

	@Override
	public UnpackBean getUnPackBean(Cursor c) {
		UnpackBean uBean = new UnpackBean();
		uBean.setId(c.getInt(c.getColumnIndex("id")));
		uBean.setLabel_id(c.getString(c.getColumnIndex("label_id")));
		uBean.setStatus(c.getInt(c.getColumnIndex("status")));
		uBean.setError_code(c.getInt(c.getColumnIndex("error_code")));
		uBean.setCreateDate(c.getString(c.getColumnIndex("createDate")));
		return uBean;
	}

	/**
	 * 根据拆包id，修改提交状态
	 * 
	 * @param status
	 * @param errorCode
	 * @param id
	 */
	@Override
	public void updateUnpackStatusById(int status, int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonFarmersHelper.tb_unpack_info + " set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[] { status, errorCode, id });
		db.close();
	}

	/**
	 * 通过激活id删除拆包信息
	 * 
	 * @param uBean
	 */
	@Override
	public void deleteUnpackById(UnpackBean uBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + CommonFarmersHelper.tb_unpack_info + " where id=?";
		db.execSQL(delSql, new Object[] { uBean.getId() });
		db.close();
	}

	/**
	 * 插入拆包信息
	 * 
	 * @param uBean
	 * @return
	 */
	@Override
	public int insertUnpack(UnpackBean uBean) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String inserSql = "insert into " + CommonFarmersHelper.tb_unpack_info
				+ " (label_id,status,error_code) values (?,?,?)";
		db.execSQL(inserSql, new Object[] { uBean.getLabel_id(), uBean.getStatus(), uBean.getError_code() });
		String querySql = "select last_insert_rowid() from " + CommonFarmersHelper.tb_unpack_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;
	}

}

