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

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.bean.ReturnedGoodsBean;
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.ReturnedGoodsDalImpl;

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

	@Override
	public int insertReturnedGoods(ReturnedGoodsBean bean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		int id = -1;
		String querySql = "select id from " + LiqueurSFYDBHelper.tb_returned_goods_info+" where account=?";
		Cursor cursor = db.rawQuery(querySql, new String[]{bean.getAccount()});
		if (cursor.moveToNext()) {
			id = cursor.getInt(0);
//			LogUtil.e(TConfig.Echo, "key:"+key);
		}
		cursor.close();
		if (id <=0) {
			String insertSql = "insert into " + LiqueurSFYDBHelper.tb_returned_goods_info +" (account,status) values (?,?)";
			db.execSQL(insertSql, new Object[]{bean.getAccount(),String.valueOf(Config.SUBMIT_STATUS_NOT_SUBMIT)});
			String queryKeySql = "select last_insert_rowid() from " + LiqueurSFYDBHelper.tb_returned_goods_info;
			
//			String querySql = "select @@identity";
			Cursor c = db.rawQuery(queryKeySql, null);
			if (c.moveToNext()) {
				id = c.getInt(0);
//				LogUtil.e(TConfig.Echo, "key:"+key);
			}
			c.close();
		}
		
		db.close();
		return id;
	}

	@Override
	public void updateReturnedGoodsSubmitStatus(int status, int error_code, int id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String updateSql = "update " +LiqueurSFYDBHelper.tb_returned_goods_info+" set status=?,error_code=? where id=?";
		db.execSQL(updateSql, new Object[]{status,error_code,id});
		db.close();
	}

	@Override
	public ReturnedGoodsBean queryReturnedGoodsById(int id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		ReturnedGoodsBean b = null;
		String sql = "select * from " + LiqueurSFYDBHelper.tb_returned_goods_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			b = getReturnedGoods(c);
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public ReturnedGoodsBean getReturnedGoods(Cursor c) {
		ReturnedGoodsBean b = new ReturnedGoodsBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setAccount(c.getString(c.getColumnIndex("account")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		return b;
	}

	@Override
	public int insertReturnedGoodsByAccount() {
		SQLiteDatabase db = helper.getWritableDatabase();
		int id = -1;
		String querySql = "select id from " + LiqueurSFYDBHelper.tb_returned_goods_info+" where account=?";
		Cursor cursor = db.rawQuery(querySql, new String[]{account});
		if (cursor.moveToNext()) {
			id = cursor.getInt(0);
//			LogUtil.e(TConfig.Echo, "key:"+key);
		}
		cursor.close();
		if (id <=0) {
			String insertSql = "insert into " + LiqueurSFYDBHelper.tb_returned_goods_info +" (account,status) values (?,?)";
			db.execSQL(insertSql, new Object[]{account,String.valueOf(Config.SUBMIT_STATUS_NOT_SUBMIT)});
			String queryKeySql = "select last_insert_rowid() from " + LiqueurSFYDBHelper.tb_returned_goods_info;
			
//			String querySql = "select @@identity";
			Cursor c = db.rawQuery(queryKeySql, null);
			if (c.moveToNext()) {
				id = c.getInt(0);
//				LogUtil.e(TConfig.Echo, "key:"+key);
			}
			c.close();
		}
		
		db.close();
		return id;
	}

}
