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

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.db.CommonFarmersHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.FarmersDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.FarmerBean;

public class CommonFarmersFarmersDal extends FarmersDalImpl{
	private CommonFarmersHelper helper;

	public CommonFarmersFarmersDal(Context context){
		
		helper = new CommonFarmersHelper(context);
		
	}
	/**
	 * 插入产品列表到数据库
	 * @param products
	 */
	public void insertFarmersList(List<FarmerBean> products) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + CommonFarmersHelper.tb_farmers_info + "(farmer_id,farmer_name) values(?,?)";
		if (products != null) {
			for (int i = 0; i < products.size(); i++) {
				FarmerBean p = products.get(i);
				db.execSQL(sql, new Object[] { p.getId(), p.getName()});
			}
		}
		db.close();
	}
	/**
	 * 删除所有的产品信息
	 */
	public void deleteAllFarmers() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + CommonFarmersHelper.tb_farmers_info;
		db.execSQL(delSql);
		db.close();
		
	}
	
	/**
	 * 获取所有的产品列表信息
	 * 
	 * @return
	 */
	public List<FarmerBean> queryAllFarmersInfo() {
		List<FarmerBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonFarmersHelper.tb_farmers_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getFarmers(c));

		}
		c.close();
		db.close();
		return l;
	}
	
	public FarmerBean getFarmers(Cursor c) {
		FarmerBean p = new FarmerBean();
		p.setId(Integer.parseInt(c.getString(c.getColumnIndex("farmer_id"))));
		p.setName(c.getString(c.getColumnIndex("farmer_name")));
		
//		LogUtil.d(TConfig.Echo, "name:" + p.getName() +"   namePY:" + c.getString(c.getColumnIndex("namePY")));
		return p;
	}
}
