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

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.db.SheepExDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.SheepDealerDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.DealerBean;

public class SheepExDealerDal extends SheepDealerDalImpl{
	private SheepExDBHelper helper;
	public SheepExDealerDal(Context context) {
		helper = new SheepExDBHelper(context);
	}

	@Override
	public void insertDealer(DealerBean b) {
		if(b == null) return;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "INSERT INTO " + SheepExDBHelper.tb_sheep_dealer_info+"(id,province,market,name,phone,code,remark,createTime,area_code,status) VALUES (?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(insertSql,new Object[]{b.getId(),b.getProvince(),b.getMarket(),b.getName(),b.getPhone(),b.getCode(),b.getRemark(),b.getCreatetime(),b.getArea_code(),b.getStatus()});
		db.close();
	}

	@Override
	public void insertDealerList(List<DealerBean> dealers) {
		if(dealers == null) return;
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "INSERT INTO " + SheepExDBHelper.tb_sheep_dealer_info+"(id,province,market,name,phone,code,remark,createTime,area_code,status) VALUES (?,?,?,?,?,?,?,?,?,?)";
		for (DealerBean b : dealers) {
			db.execSQL(insertSql,new Object[]{b.getId(),b.getProvince(),b.getMarket(),b.getName(),b.getPhone(),b.getCode(),b.getRemark(),b.getCreatetime(),b.getArea_code(),b.getStatus()});
		}
		db.close();
	}

	@Override
	public void deleteAllDealer() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "DELETE FROM " + SheepExDBHelper.tb_sheep_dealer_info;
		db.execSQL(delSql);
		db.close();
	}

	@Override
	public DealerBean queryDealerByCode(String code) {
//		LogUtil.e(TConfig.Echo, "查询条件：" + code);
		code = code.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + SheepExDBHelper.tb_sheep_dealer_info+" WHERE code=?";
		Cursor c = db.rawQuery(querySql, new String[]{code});
		DealerBean b = null;
		if (c.moveToNext()) {
			b = getDealerBean(c);
		}
		c.close();
		
		
//		List<DealerBean> list = new ArrayList<>();
//		String querySql1 = "SELECT * FROM " + SheepExDBHelper.tb_sheep_dealer_info;
//		Cursor c1 = db.rawQuery(querySql1, null);
//		while (c1.moveToNext()) {
//			list.add(getDealerBean(c1));
//		}
//		c1.close();
//		LogUtil.e(TConfig.Echo, "所有经销商数据：" + list.toString());
		
		db.close();
		return b;
	}
	@Override
	public DealerBean queryDealerById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + SheepExDBHelper.tb_sheep_dealer_info+" WHERE id=?";
		Cursor c = db.rawQuery(querySql, new String[]{String.valueOf(id)});
		DealerBean b = null;
		if (c.moveToNext()) {
			b = getDealerBean(c);
		}
		return b;
	}

	@Override
	public DealerBean getDealerBean(Cursor c) {
		DealerBean b = new DealerBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setProvince(c.getString(c.getColumnIndex("province")));
		b.setMarket(c.getString(c.getColumnIndex("market")));
		b.setName(c.getString(c.getColumnIndex("name")));
		b.setPhone(c.getString(c.getColumnIndex("phone")));
		b.setCode(c.getString(c.getColumnIndex("code")));
		b.setRemark(c.getString(c.getColumnIndex("remark")));
		b.setCreatetime(c.getString(c.getColumnIndex("createTime")));
		b.setArea_code(c.getInt(c.getColumnIndex("area_code")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		return b;
	}


}
