package com.evt.mgr.handler;

import com.data.cache.GroupMemberCache;
import com.evt.mgr.IHandler;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.StringUtil;

public class HandlerOver implements IHandler {

	public static final int _SHARE_NUM = 20;

	private static void share(String id) throws Exception {
		if (!Redis.use("group1_db1").sismember("shares", id)) {
			return;
		}
		String sql = "SELECT succ,playtimes FROM shares WHERE uid = " + id;
		ITArray resultArray = DataBase.use().executeQueryByTArray(sql);
		if (resultArray.size() == 0) {
			return;
		}
		ITObject dbData = resultArray.getTObject(0);
		if (dbData.getInt("succ") != 0) {
			Redis.use("group1_db1").srem("shares", id);
			return;
		}
		int playTimes = dbData.getInt("playtimes") + 1;
		dbData.putInt("playtimes", playTimes);
		if (playTimes >= _SHARE_NUM) {
			dbData.putInt("succ", 1);
			Redis.use("group1_db1").srem("shares", id);
		}
		DataBase.use().update("shares", dbData, "uid=" + id);

	}

	@Override
	public int process(int uid,ITObject param) throws Exception {
		int is_rec = param.getInt("is_rec");
		if(is_rec == 1) {
			String roomid =param.getString("roomid");
			String rec_key =param.getString("rec_key");
			int gid =param.containsKey("gid")?param.getInt("gid"):0;
			int time =param.getInt("time");
			int par = 0;
			if(gid>0) {
				String gm_key = GroupMemberCache.genKey(gid, uid);
				String parentId = Redis.use("group1_db10").hget(gm_key, "parentId");
				par = StringUtil.isEmpty(parentId)?0:Integer.parseInt(parentId);
			}
			String sql =String.format("insert into room_rec_log(roomid,gid,uid,rec_key,time,parentId) values('%s',%s,%s,'%s',%s,%s)",
					roomid,gid,uid,rec_key,time,par);
			DataBase.use().executeUpdate(sql);
		}
		
		share(uid+"");
		return 0;
	}

	@Override
	public int processGroup(int groupId, ITObject param) throws Exception {
		return 0;
	}

}
