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

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


/** 
 * @author  作者 李扬: 
 * @date 创建时间：2017-4-26 上午10:30:33 
 * @version 1.0 
 * @parameter 
 * @since  
 * @return  
 */
public class ChinatexWarehouseDal extends WarehouseDalImpl{
	private ChinatexDBHelper helper;
	public ChinatexWarehouseDal(Context context) {
		helper = new ChinatexDBHelper(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 " + ChinatexDBHelper.tb_opt_info + " 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 " + ChinatexDBHelper.tb_opt_info + " 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 " + ChinatexDBHelper.tb_opt_info + " 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 " + ChinatexDBHelper.tb_opt_info + " where option_type=? and id=? ";
		db.execSQL(delSql, new Object[] { warehouseBean.getOption_type(), warehouseBean.getId() });
		String delLabelSql = "delete from " + ChinatexDBHelper.tb_opt_num
				+ " 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 " + ChinatexDBHelper.tb_opt_info + " 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 " + ChinatexDBHelper.tb_opt_info
				+ " 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();
	}
	/**
	 * 插入出入库信息并返回单号
	 * @param warehouseBean
	 * @return 
	 * id：表ID,主键自增
	 * describe：描述
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）是返库还是出库还是入库
	 * option_mode：0：单标激活 1：批量激活
	 * plan_number：计划数量
	 * actual_number:实际数量
	 * daler_id：分销商id （初版暂时未用）
	 * daler_name：分销商名（初版暂时未用）
	 * sale_area_code:销售区域
	 * out_warehouse_type：出库类型 0  普通出 1托盘出
	 * status：提交状态  0：未提交  1：提交失败
	 * error_code：服务器返回的错误码
	 * createDate：创建时间
	 * @param db
	 */
	 
	@Override
	public int insertWarehouseInfo(WarehouseBean wB) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + ChinatexDBHelper.tb_opt_info
				+ " (describe,option_type,option_mode,sale_area_code,status,error_code,plan_number,out_warehouse_type) values(?,?,?,?,?,?,?,?)";
		db.execSQL(insertSql, new Object[] { wB.getDescribe(), wB.getOption_type(), wB.getOption_mode(),wB.getSale_area_code(), wB.getStatus(),
				wB.getError_code(),wB.getPlanNumber(),wB.getOutWarehouseType()});
		String querySql = "select last_insert_rowid() from " + ChinatexDBHelper.tb_opt_info;

		//  String querySql = "select @@identity";
		Cursor cursor = db.rawQuery(querySql, null);
		if (cursor.moveToNext()) {
			key = cursor.getInt(0);
		}
		cursor.close();
		db.close();
		return key;
	}
	@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")));
		b.setPlanNumber(c.getString(c.getColumnIndex("plan_number")));
		b.setOutWarehouseType(c.getInt(c.getColumnIndex("out_warehouse_type")));
		
		return b;
	}
}
