package com.gh.gamecenter.db;

import android.content.Context;

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

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

public class DataCollectionDao {

	private DatabaseHelper helper;
	private Dao<DataCollectionInfo, String> dao;

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

	/**
	 * 查找一个数据
	 */
	public List<DataCollectionInfo> findByType(String type) {
		try {
			return dao.queryForEq("type", type);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 添加一个数据
	 */
	public void add(DataCollectionInfo entity) {
		try {
			dao.create(entity);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除一个数据
	 */
	public void delete(String id) {
		try {
			dao.deleteById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除一组数据
	 */
	public void delete(List<String> ids) {
		try {
			dao.deleteIds(ids);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据id获取某一个数据
	 */
	public DataCollectionInfo find(String id) {
		try {
			return dao.queryForId(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取所有的数据
	 */
	public List<DataCollectionInfo> getAll() {
		try {
			return dao.queryForAll();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取点击数据
	 */
	public List<DataCollectionInfo> getClickData() {
		try {
			return dao.queryForEq("type", "click-item");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 更新数据
	 */
	public void update(DataCollectionInfo entity) {
		try {
			dao.update(entity);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
