package com.data.cache;

import com.data.bean.BaseBean;
import com.data.bean.GroupBean;
import com.data.bean.GroupMemberBean;
import com.data.bean.GroupPlayBean;

public class GroupCache extends BaseCache{
	static GroupCache				inst;
	
	protected GroupCache() {
		super("group:", "group1_db11");
		this.readTime = 0;
	}
	
	@Override
	protected BaseBean newBean() {
		return new GroupBean();
	}

	private static GroupCache me() {
		if (inst == null) {
			inst = new GroupCache();
		}
		return inst;
	}
	
	public static void clearData() {
		me().clearExpireData();
	}

	public static GroupBean getGroup(int id) {
		return me().getBean(id);
	}

	public static GroupBean getGroup(String key) {
		return me().getBean(key);
	}
	
	public static GroupMemberBean getMember(int gid, int uid) {
		GroupBean gb = getGroup(gid);
		if (gb != null) {
			synchronized (gb) {
				if (gb.memberCache == null) {
					gb.memberCache = new GroupMemberCache(gid);
				}
			}
			return gb.memberCache.getBean(uid);
		}
		return null;
	}
	
	public static GroupPlayBean getPlay(int gid, int pid) {
		GroupBean gb = getGroup(gid);
		if (gb != null) {
			synchronized (gb) {
				if (gb.playCache == null) {
					gb.playCache = new GroupPlayCache(gid,gb.pay_type);
				}
			}
			return gb.playCache.getBean(pid);
		}
		return null;
	}
	
	public static String genGroupsKey(int uid) {
		return "groups:" + uid;
	}
	
	public static String genKey(int gid) {
		return "group:" + gid;
	}
	
	public static String genRewardKey(int gid,int pid) {
		return "g{" + gid+"}:reward:"+pid;
	}

	public static String genXiPaiRewardKey(int gid,int pid) {
		return "g{" + gid+"}:xipai_reward:"+pid;
	}

	public static String genAnChouRewardKey(int gid,int pid) {
		return "g{" + gid+"}:anchou_reward:"+pid;
	}
	
	public static String genRoomsKey(int gid) {
		return "g{" + gid+"}:rooms";
	}
	
	public static String genPidsKey(int gid) {
		return "g{" + gid + "}:pids";
	}
	
	public static String genPlayKey(int gid,int pid) {
		return "g{" + gid + "}:play:" + pid;
	}
	
	public static String genBanKey(int gid,int uid) {
		return "g{" + gid+"}:ban:"+uid;
	}
	
	public static String genMemberListKey(int gid,int uid) {
		return "g{"+gid+"}:member_list:"+uid;
	}
	
	public static String genParListKey(int gid,int uid) {
		return "g{"+gid+"}:par_list:"+uid;
	}
	
	public static String genJoinsKey(int gid) {
		return "gmjs_"+gid;
	}
	
	public static String genMailKey(int gid,int uid) {
		return "g{"+gid+"}:mail:"+uid;
	}
	
	public static String genMailTipKey(int gid) {
		return "g{"+gid+"}:mail_tip";
	}
}
