package com.wasu.cs.module;

import com.wasu.module.db.DBManage;

import java.util.List;

import basic.db.model.DBProgramChildFavorite;

public class ChildFavModule {
	private static ChildFavModule mInstance;
	private ChildFavModule(){
		
	}
	public static ChildFavModule getInstance() {
		if (mInstance == null) {
			synchronized (ChildFavModule.class) {
				if (mInstance == null) {
					mInstance = new ChildFavModule();
				}
			}
		}
		return mInstance;
	}
	public DBProgramChildFavorite readFavorite(String programId) {
		List<DBProgramChildFavorite> mFavoriteList=null;
		DBProgramChildFavorite mFavorite=null;
		try {
			mFavorite = DBManage.queryBy(DBProgramChildFavorite.class, "programId", programId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mFavorite;
	}
	/**
	 * 读取数据库中的所有收藏数据
	 * @return 异常返回null
	 * **/
	public List<DBProgramChildFavorite> readFavorite() {
		try {
			return DBManage.queryAndOrder(DBProgramChildFavorite.class, "savefavoritetime", false);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 保存需要收藏的数据
	 * @param mFavorite
	 * @return true：成功     false：失败
	 * **/
	public boolean saveFavorite(DBProgramChildFavorite mFavorite){
		try {
			mFavorite.insertOrUpdate();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据影片id删除收藏的数据
	 * @param programId 影片id
	 * @return true：成功   false：失败
	 * **/
	public boolean deleteFavorite(String programId){
		try {
			DBManage.deleteBy(DBProgramChildFavorite.class, "programId",programId);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteAllFavorite(){
		try {
			DBManage.clearTable(DBProgramChildFavorite.class);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据影片id判断当前影片是否收藏
	 * @param programId 影片id
	 * @return true：已收藏    false：未收藏
	 * **/
	public boolean hasFavorite(String programId){
		DBProgramChildFavorite exist = readFavorite(programId);
		if (exist == null) {
			return false;
		} else {
			return exist.programId != 0;
		}
	}
}
