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

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.WarehouseBean;
import cn.net.caoyao.cpzshandset.db.CommonDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.WarehouseDalImpl;

public class CommonWarehouseDal extends WarehouseDalImpl {
	private CommonDBHelper helper;
	public CommonWarehouseDal(Context context) {
		helper = new CommonDBHelper(context);
	}

	/**
	 * 根据操作类型，查询对应的出库，入库，返库信息
	 * 
	 * @param option_type：操作类型
	 *            入库：（HandsetOperationConstant.OPERATION_IN_WAREHOUSE_CONSTANT ）
	 *            出库：（HandsetOperationConstant.OPERATION_OUT_WAREHOUSE_CONSTANT）
	 *            返库：（HandsetOperationConstant.OPERATION_BACK_WAREHOUSE_CONSTANT）
	 * @return 按照id降序排列 
	 */
	@Override
	public List<WarehouseBean> queryWarehouseByOptionType(int option_type) {
		List<WarehouseBean> wList = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonDBHelper.tb_warehouse_option + " where option_type=? order by id desc";
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(option_type) });
		while (c.moveToNext()) {
			wList.add(getWarehouseBean(c));
		}
		c.close();
		db.close();
		return wList;
	}

	/**
	 * 根据操作类型，查询对应的出库，入库，返库信息
	 * 
	 * @param option_type：操作类型
	 *            入库：（HandsetOperationConstant.OPERATION_IN_WAREHOUSE_CONSTANT ）
	 *            出库：（HandsetOperationConstant.OPERATION_OUT_WAREHOUSE_CONSTANT
	 *            ）
	 *            返库：（HandsetOperationConstant.OPERATION_BACK_WAREHOUSE_CONSTANT
	 *            ）
	 * @return 按照id升序排列
	 */
	@Override
	public List<WarehouseBean> queryWarehouseByOptionTypeASC(int option_type) {
		List<WarehouseBean> wList = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonDBHelper.tb_warehouse_option + " where option_type=? order by id asc";
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(option_type) });
		while (c.moveToNext()) {
			wList.add(getWarehouseBean(c));
		}
		c.close();
		db.close();
		return wList;
	}

	/**
	 * 根据操作类型和id，查询对应的出库，入库，返库信息
	 * 
	 * @param option_type：操作类型
	 *            入库：（HandsetOperationConstant.OPERATION_IN_WAREHOUSE_CONSTANT ）
	 *            出库：（HandsetOperationConstant.OPERATION_OUT_WAREHOUSE_CONSTANT
	 *            ）
	 *            返库：（HandsetOperationConstant.OPERATION_BACK_WAREHOUSE_CONSTANT
	 *            ）
	 * @return
	 */
	@Override
	public WarehouseBean queryWarehouseById(int option_type, int id) {
		WarehouseBean b = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonDBHelper.tb_warehouse_option + " where option_type=? and id=?";
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(option_type), String.valueOf(id)});
		if (c.moveToNext()) {
			b = getWarehouseBean(c);
		}
		c.close();
		db.close();
		return b;
	}

	/**
	 * 删除出入库信息
	 * 
	 * @param warehouseBean
	 */
	@Override
	public void deleteWarehouse(WarehouseBean warehouseBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + CommonDBHelper.tb_warehouse_option + " where option_type=? and id=? ";
		db.execSQL(delSql, new Object[] { warehouseBean.getOption_type(), warehouseBean.getId() });
		String delLabelSql = "delete from " + CommonDBHelper.tb_single_label_option
				+ " where option_type=? and option_id=?";
		db.execSQL(delLabelSql, new Object[] { warehouseBean.getOption_type(), warehouseBean.getId() });
		db.close();
	}

	/**
	 * 修改出库的状态信息
	 * 
	 * @param warehouseBean
	 */
	@Override
	public void updateWarehouseErrorStatus(int status, int error_code, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonDBHelper.tb_warehouse_option + " set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[] { status, error_code, id });
		db.close();
	}

	/**
	 * 修改出库信息
	 * 
	 * @param warehouseBean
	 */
	@Override
	public void updateWarehouseInfo(WarehouseBean wB) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonDBHelper.tb_warehouse_option
				+ " set describe=?,option_mode=?,sale_area_code=?,status=?,error_code=? where id=? and option_type=?";
		db.execSQL(updateSql, new Object[] { wB.getDescribe(), wB.getOption_mode(),wB.getSale_area_code(), wB.getStatus(), wB.getError_code(),
				wB.getId(), wB.getOption_type() });
		db.close();
	}

	@Override
	public WarehouseBean getWarehouseBean(Cursor c) {
		WarehouseBean b = new WarehouseBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setDescribe(c.getString(c.getColumnIndex("describe")));
		b.setOption_type(c.getInt(c.getColumnIndex("option_type")));
		b.setOption_mode(c.getInt(c.getColumnIndex("option_mode"))); 
		b.setSale_area_code(c.getInt(c.getColumnIndex("sale_area_code")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		b.setCreateDate(c.getString(c.getColumnIndex("createDate")));
		return b;
	}

	/**
	 * 插入出入库信息
	 * @param warehouseBean
	 * @return
	 */
	@Override
	public int insertWarehouseInfo(WarehouseBean wB) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + CommonDBHelper.tb_warehouse_option
				+ " (describe,option_type,option_mode,sale_area_code,status,error_code) values(?,?,?,?,?,?)";
		db.execSQL(insertSql, new Object[] { wB.getDescribe(), wB.getOption_type(), wB.getOption_mode(),wB.getSale_area_code(), wB.getStatus(),
				wB.getError_code() });
		String querySql = "select last_insert_rowid() from " + CommonDBHelper.tb_warehouse_option;

		// String querySql = "select @@identity";
		Cursor cursor = db.rawQuery(querySql, null);
		if (cursor.moveToNext()) {
			key = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return key;
	}

}
