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

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.SaleBean;
import cn.net.caoyao.cpzshandset.common.config.HandsetOperation;
import cn.net.caoyao.cpzshandset.db.CommonEggDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.SalesDalImpl;

public class CommonEggSalesDal extends SalesDalImpl{
	private CommonEggDBHelper helper;
	public CommonEggSalesDal(Context context) {
		helper = new CommonEggDBHelper(context);
	}
	/**
	 * 插入销售信息
	 * @param saleBean
	 * @return
	 */
	@Override
	public int insertSalesInfo(SaleBean saleBean) {
		int key = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + CommonEggDBHelper.tb_sales_info +" (describe,sales_area,option_mode,status,error_code) values (?,?,?,?,?)";
		db.execSQL(insertSql, new Object[]{saleBean.getDescribe(),saleBean.getSales_area(),saleBean.getOption_mode(),saleBean.getStatus(),saleBean.getError_code()});
		String querySql = "select last_insert_rowid() from " + CommonEggDBHelper.tb_sales_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;
	}
	/**
	 * 修改销售信息
	 * @param saleBean
	 */
	@Override
	public void updateSalesInfo(SaleBean saleBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonEggDBHelper.tb_sales_info+" set describe=?,sales_area=?,option_mode=?,status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{saleBean.getDescribe(),saleBean.getSales_area(),saleBean.getOption_mode(),saleBean.getStatus(),saleBean.getError_code(),saleBean.getId()});
		db.close();
		
	}
	/**
	 * 修改销售信息的提交
	 * @param status
	 * @param errorCode
	 * @param id
	 */
	@Override
	public void updateSalesSubmitStatus(int status, int errorCode, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " + CommonEggDBHelper.tb_sales_info +" set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{status,errorCode,id});
		db.close();
		
	}
	/**
	 * 删除销售信息
	 * @param saleBean
	 */
	@Override
	public void deleteSalesById(SaleBean saleBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + CommonEggDBHelper.tb_sales_info +" where id=?";
		db.execSQL(delSql, new Object[]{saleBean.getId()});
		String delLabel = "delete from " + CommonEggDBHelper.tb_single_label_option +" where option_type=? and option_id=?";
		db.execSQL(delLabel, new Object[]{HandsetOperation.OPERATION_SALE_CONSTANT.getOption(),saleBean.getId()});
		db.close();
	}
	/**
	 * 查询所有的销售信息
	 * @return 降序排列
	 */
	@Override
	public List<SaleBean> queryAllSalesList() {
		List<SaleBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String rawSql = "select * from " + CommonEggDBHelper.tb_sales_info +" order by id desc";
		Cursor c = db.rawQuery(rawSql, null);
		while (c.moveToNext()) {
			l.add(getSalesBean(c));
		}
		c.close();
		db.close();
		return l;
	}
	/**
	 * 查询所有的销售信息   
	 * @return  升序排列
	 */
	@Override
	public List<SaleBean> queryAllSalesListASC() {
		List<SaleBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String rawSql = "select * from " + CommonEggDBHelper.tb_sales_info +" order by id asc";
		Cursor c = db.rawQuery(rawSql, null);
		while (c.moveToNext()) {
			l.add(getSalesBean(c));
		}
		c.close();
		db.close();
		return l;
	}

	@Override
	public SaleBean getSalesBean(Cursor c) {
		SaleBean b = new SaleBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setDescribe(c.getString(c.getColumnIndex("describe")));
		b.setSales_area(c.getInt(c.getColumnIndex("sales_area")));
		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("createDate")));
		return b;
	}
	/**
	 * 通过销售id查询销售信息
	 * @param id
	 * @return
	 */
	@Override
	public SaleBean querySalesById(int id) {
		SaleBean b = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String rawSql = "select * from " + CommonEggDBHelper.tb_sales_info +" where id=?";
		Cursor c = db.rawQuery(rawSql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getSalesBean(c);
		}
		c.close();
		db.close();
		return b;
	}

}
