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

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.common.config.Config;
import cn.net.caoyao.cpzshandset.common.util.CacheGet;
import cn.net.caoyao.cpzshandset.db.LiqueurSFYDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.WarehouseDalImpl;

public class LiqueurSFYWarehouseDal extends WarehouseDalImpl {
	private LiqueurSFYDBHelper helper;
	private String account = "";
	public LiqueurSFYWarehouseDal(Context context) {
		helper = new LiqueurSFYDBHelper(context);
		CacheGet cacheG = new CacheGet();
		account = cacheG.getCacheStringG(context, Config.configSPName, Config.liqueurSFYLoginAccount);
	}

	/**
	 * 根据操作类型，查询对应的出库，入库，返库信息
	 * 
	 * @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 " + LiqueurSFYDBHelper.tb_warehouse_option + " where option_type=? and account=? order by id desc";
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(option_type),account });
		while (c.moveToNext()) {
			wList.add(getWarehouseBean(c));
		}
		c.close();
		db.close();
//		LogUtil.e(TConfig.Echo, account + "wList:" + wList.toString());
		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 " + LiqueurSFYDBHelper.tb_warehouse_option + " where option_type=? and account=? order by id asc";
		Cursor c = db.rawQuery(sql, new String[] { String.valueOf(option_type) ,account});
		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 " + LiqueurSFYDBHelper.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 " + LiqueurSFYDBHelper.tb_warehouse_option + " where option_type=? and id=? ";
		db.execSQL(delSql, new Object[] { warehouseBean.getOption_type(), warehouseBean.getId() });
		String delLabelSql = "delete from " + LiqueurSFYDBHelper.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 " + LiqueurSFYDBHelper.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 " + LiqueurSFYDBHelper.tb_warehouse_option
				+ " set to_sale_account=?,dealer_id=?,product_id=?,plan_num=?,describe=?,option_mode=?,sale_area_code=?,status=?,error_code=? where id=? and option_type=?";
		db.execSQL(updateSql, new Object[] { wB.getTo_sale_user_account(),wB.getDealer_id(),wB.getProduct_id(),wB.getPlan_num(),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.setPlan_num(c.getInt(c.getColumnIndex("plan_num")));
		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")));
		b.setProduct_id(c.getString(c.getColumnIndex("product_id")));
		b.setDealer_id(c.getLong(c.getColumnIndex("dealer_id")));
		b.setTo_sale_user_account(c.getString(c.getColumnIndex("to_sale_account")));
		return b;
	}

	/**
	 * 插入出入库信息
	 * @param warehouseBean
	 * @return
	 */
	@Override
	public int insertWarehouseInfo(WarehouseBean wB) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + LiqueurSFYDBHelper.tb_warehouse_option
				+ " (to_sale_account,dealer_id,product_id,account,plan_num,describe,option_type,option_mode,sale_area_code,status,error_code) values(?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(insertSql, new Object[] { wB.getTo_sale_user_account(),wB.getDealer_id(),wB.getProduct_id(),account,wB.getPlan_num(),wB.getDescribe(), wB.getOption_type(), wB.getOption_mode(),wB.getSale_area_code(), wB.getStatus(),
				wB.getError_code() });
		String querySql = "select last_insert_rowid() from " + LiqueurSFYDBHelper.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;
	}

}
