package com.gh.gamecenter.db;

import android.content.Context;

import com.gh.gamecenter.db.info.FilterInfo;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

public class FilterDao {
	
	private DatabaseHelper helper;
	private Dao<FilterInfo, String> dao;

	public FilterDao(Context context) {
		try {
			helper = DatabaseHelper.getHelper(context);
			dao = helper.getDao(FilterInfo.class);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	public boolean isFilter(String packageName){
		try {
			FilterInfo filterEntity = dao.queryForId(packageName);
			if (filterEntity != null) {
				return true;
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 添加一个过滤包名
	 */
	public void add(FilterInfo entity) {
		try {
			dao.create(entity);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
	
	/**
	 * 添加一堆过滤包名
	 */
	public void addAll(List<FilterInfo> list) {
		try {
			dao.create(list);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 删除一个过滤包名
	 */
	public void delete(String packageName) {
		try {
			dao.deleteById(packageName);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 获取所有的过滤包名
	 */
	public List<FilterInfo> getAll() {
		try {
			return dao.queryForAll();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取表中数据总数
	 */
	public long getCount() {
		try {
			return dao.countOf();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return 0;
	}

}
