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

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.SeedsDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.ProductDalImpl;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class SeedsProductDal extends ProductDalImpl{
	private SeedsDBHelper helper;
	public SeedsProductDal(Context context) {
		helper = new SeedsDBHelper(context);
	}
	/**
	 * 插入	
	 */
	@Override
	public void insertProductList(List<Product> products) {
		
		SQLiteDatabase db = helper.getWritableDatabase();
		if (products != null) {
			for (int i = 0; i < products.size(); i++) {
				Product p = products.get(i);
				String sql = "insert into " + SeedsDBHelper.tb_product_info + "(product_id,name,nid) values(?,?,?)";
				db.execSQL(sql, new Object[] { p.getId(), p.getName(), p.getNid() });
			}
		}
		db.close();
	}
	/**
	 * 删除表中数据
	 * 
	 */
	
	@Override
	public void deleteAllProduct() {
		SQLiteDatabase db = helper.getWritableDatabase();
		if (db.isOpen()) {
			try {
				db.delete(SeedsDBHelper.tb_product_info, null, null);				
				db.delete(SeedsDBHelper.tb_land_info, null, null);	
				db.delete(SeedsDBHelper.tb_zhengshu_info, null, null);					
				
			} catch (Exception e) {
				e.printStackTrace();
			
			} finally {
				db.close();
			}
		}		
	}
	/**
	 * 获取查询结果
	 * 
	 * @param msg
	 * @return
	 */
	@Override
	public List<Product> getPSearch(String msg) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<Product> productInfos = new ArrayList<Product>();
		String sql = "SELECT * FROM " + SeedsDBHelper.tb_product_info +" WHERE product_id LIKE '%"+msg+"%' OR name LIKE '%"+msg+"%' OR nid LIKE '%"+msg+"%'";
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			Product pInfo = getProduct(cursor);
			productInfos.add(pInfo);
		}

		cursor.close();
		db.close();
		return productInfos;
	}

	@Override
	public Product queryProductById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Product> queryAllProductInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Product getProduct(Cursor c) {
		// TODO Auto-generated method stub
		return null;
	}

	

}
