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.LandDal;
import cn.net.cpzslibs.prot.handset.bean.Prot10065LandBean;

public class SeedsLandDal implements LandDal{
	
	private SeedsDBHelper helper;

	public SeedsLandDal(Context context) {
		helper = new SeedsDBHelper(context);
	}

	@Override
	public void insertLandList(List<Prot10065LandBean> products) {

		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + SeedsDBHelper.tb_land_info
				+ " (land_id,land_name,create_date) values(?,?,?)";
		String land_id, land_name, create_date;
		if (db.isOpen()) {
			db.beginTransaction();
			for (Prot10065LandBean m : products) {
				land_id = m.getId();
				land_name = m.getName();
				create_date = m.getCreate_time();
				if (land_id != null && !"".equals(land_id.trim())) {
					db.execSQL(sql, new String[] { land_id, land_name,
							create_date });

				}
			}
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		}
	}

	@Override
	public void deleteAllProduct() {
		// TODO Auto-generated method stub
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + SeedsDBHelper.tb_land_info;
		db.execSQL(delSql);
		db.close();

	}

	@Override
	public List<Map<String, String>> findLand() {
		String landinfo = SeedsDBHelper.tb_land_info;
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select land_id ,land_name , create_date from " + landinfo;

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

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

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

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

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

				map.put("land_id", land_id);
				map.put("land_name", land_name);
				map.put("create_date", create_date);

				list.add(map);
			}

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

			return list;
		}
		return list;
	}

}
