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

import java.util.ArrayList;
import java.util.List;

import com.bytebeats.py4j.Py4j;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import cn.net.caoyao.cpzshandset.db.CommonDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.ProductDalImpl;
import cn.net.cpzslibs.prot.handset.bean.Product;

public class CommonProductDal extends ProductDalImpl{
	private CommonDBHelper helper;

	public CommonProductDal(Context context){
		helper = new CommonDBHelper(context);
	}

	/**
	 * 插入产品列表到数据库
	 * 
	 * @param products
	 */
	public void insertProductList(List<Product> products) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + CommonDBHelper.tb_product_info + "(id,name,namePY,nid) values(?,?,?,?)";
		Py4j py4j = new Py4j();
		if (products != null) {
			for (int i = 0; i < products.size(); i++) {
				Product p = products.get(i);
				String namePY = py4j.getPreLetterPY(p.getName()).toUpperCase();
				db.execSQL(sql, new Object[] { p.getId(), p.getName(),namePY, p.getNid() });
			}
		}
		py4j = null;
		db.close();
	}

	/**
	 * 删除所有的产品信息
	 */
	public void deleteAllProduct() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + CommonDBHelper.tb_product_info;
		db.execSQL(delSql);
		db.close();
		
	}

	/**
	 * 获取查询结果
	 * 
	 * @param msg
	 * @return
	 */
	public List<Product> getPSearch(String msg) {
		msg = msg.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		List<Product> productInfos = new ArrayList<Product>();
		String sql = "SELECT * FROM " + CommonDBHelper.tb_product_info +" WHERE id LIKE '%"+msg+"%' OR name LIKE '%"+msg+"%' OR namePY 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;
	}

	/**
	 * 通过产品id获取产品信息
	 * 
	 * @param id
	 * @return
	 */
	public Product queryProductById(String id) {
		Product p = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonDBHelper.tb_product_info + " where id=?";
		Cursor c = db.rawQuery(sql, new String[] { id });
		if (c.moveToNext()) {
			p = getProduct(c);
		}
		c.close();
		db.close();
		return p;
	}

	/**
	 * 获取所有的产品列表信息
	 * 
	 * @return
	 */
	public List<Product> queryAllProductInfo() {
		List<Product> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + CommonDBHelper.tb_product_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getProduct(c));

		}
		c.close();
		db.close();
		return l;
	}

	public Product getProduct(Cursor c) {
		Product p = new Product();
		p.setId(c.getString(c.getColumnIndex("id")));
		p.setName(c.getString(c.getColumnIndex("name")));
		p.setNid(c.getString(c.getColumnIndex("nid")));
//		LogUtil.d(TConfig.Echo, "name:" + p.getName() +"   namePY:" + c.getString(c.getColumnIndex("namePY")));
		return p;
	}


}
