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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.CertificateDal;
import cn.net.cpzslibs.prot.handset.bean.Prot10064ZhengShuBean;

public class SeedsCertificateDal implements CertificateDal {
	private SeedsDBHelper helper;
	
	public SeedsCertificateDal(Context context) {
		helper = new SeedsDBHelper(context);
	}
	/**
	 * 插入证书列表
	 * @param products
	 */
	@Override
	public void insertCertificateList(List<Prot10064ZhengShuBean> products) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into "
				+ SeedsDBHelper.tb_zhengshu_info
				+ " (zhengshu_id,zhengshu_name,create_date,update_date) values(?,?,?,?)";
		String zhengshu_id, zhengshu_name, create_date, update_date;
		if (db.isOpen()) {
			db.beginTransaction();
			for (Prot10064ZhengShuBean m : products) {
				zhengshu_id = m.getId();
				zhengshu_name = m.getName();
				create_date = m.getCreate_date();
				update_date = m.getUpdate_date();
				if (zhengshu_id != null && !"".equals(zhengshu_id.trim())) {
					db.execSQL(sql, new String[] { zhengshu_id, zhengshu_name,
							create_date, update_date });

				}
			}
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		}
	}
	/**
	 * 删除证书列表
	 * 
	 */
	@Override
	public void deleteAllProduct() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + SeedsDBHelper.tb_zhengshu_info;
		db.execSQL(delSql);
		db.close();

	}
	/**
	 * 查询证书列表
	 * @return List<Map<String, String>>
	 */
	@Override
	public List<Map<String, String>> findCertificate() {
		String zhengShuInfo = SeedsDBHelper.tb_zhengshu_info;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select zhengshu_id ,zhengshu_name , create_date ,update_date  from "
				+ zhengShuInfo;

		if (db.isOpen()) {
			Cursor cursor = db.rawQuery(sql, null);
			Map<String, String> map = null;

			while (cursor.moveToNext()) {
				map = new HashMap<String, String>();

				String zhengshu_id = cursor.getString(cursor
						.getColumnIndex("zhengshu_id"));

				String zhengshu_name = cursor.getString(cursor
						.getColumnIndex("zhengshu_name"));

				String create_date = cursor.getString(cursor
						.getColumnIndex("create_date"));
				String update_date = cursor.getString(cursor
						.getColumnIndex("update_date"));
				

				map.put("zhengshu_id", zhengshu_id);
				map.put("zhengshu_name", zhengshu_name);
				map.put("create_date", create_date);
				map.put("update_date", update_date);
				list.add(map);
			}

			cursor.close();
			db.close();

			return list;
		}
		return list;
	}

}
