package com.group.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;

import com.data.bean.AccountBean;
import com.data.bean.GameBean;
import com.data.bean.GroupBean;
import com.data.bean.GroupMemberBean;
import com.data.bean.GroupPlayBean;
import com.data.cache.AccountCache;
import com.data.cache.BaseCache;
import com.data.cache.GameCache;
import com.data.cache.GroupCache;
import com.data.cache.GroupMemberCache;
import com.data.util.ConsumeCode;
import com.data.util.CountUtil;
import com.data.util.ErrorCode;
import com.data.util.Utility;
import com.group.MainServer;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.plugin.redis.RedisLock;
import com.taurus.core.util.DateUtils;
import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;
import com.taurus.web.WebException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

public class GroupService {
	private final static Logger log;

	static {
		log = Logger.getLogger(GroupService.class);
	}
	/** 普通圈子3000 */
	public static final int TYPE_1_NUM = 3000000;
	/** 大联盟20000 */
	public static final int TYPE_2_NUM = 3000000;

	/** 删除玩家 */
	public static final int PERMISSION_DEL_MEMBER = 1;
	/** 添加玩家 */
	public static final int PERMISSION_ADD_MEMBER = 2;
	/** 增减体力值 */
	public static final int PERMISSION_HP_OPT = 4;
	/** 禁止娱乐 */
	public static final int PERMISSION_BAN = 8;
	/** 禁止同桌 */
	public static final int PERMISSION_BAN_DESKMATE = 16;

	public static final String redisdb = "group1_db9";

	private static ITObject getGroupData(int uid, int groupId, Jedis jedis11, Jedis jedis10) {
		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb == null)
			return null;
		GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
		if (gmb == null) {
			return null;
		}
		if (uid == gb.owner) {
			Redis.use("group1_db1").sadd("robot_group", Integer.toString(groupId));
		}
		ITObject obj = TObject.newInstance();
		obj.putInt("id", groupId);
		obj.putString("name", gb.name);
		int owner = gb.owner;
		if (gmb.lev == 1) {
			obj.putInt("owner", owner);
		} else {
			obj.putInt("owner", 0);
		}

		obj.putInt("type", gb.type);
		obj.putInt("parentId", gmb.parentId);
		obj.putInt("pay_type", gb.pay_type);
		obj.putInt("member_num", gb.gms);
		obj.putInt("option", gb.option);
		obj.putInt("show_num", gb.show_num);
		String grooms_key = GroupCache.genRoomsKey(groupId);
		Set<String> rooms = jedis11.zrangeByScore(grooms_key, 100000, 2000000);
		obj.putInt("room_num", rooms.size());
		obj.putInt("top_time", gmb.top_time);
		AccountBean acc = AccountCache.getAccount(owner);
		if (acc != null) {
			obj.putString("o_nick", acc.nick);
			obj.putString("o_portrait", acc.portrait);
		} else {
			obj.putString("o_nick", StringUtil.Empty);
			obj.putString("o_portrait", StringUtil.Empty);
		}
		// Redis.use("group1_db1").hset("alllook",uid+"","1");
		String fp = Redis.use("group1_db1").hget("alllook", uid + "");
		if (fp != null) {
			obj.putInt("lev", 1);
		} else {
			obj.putInt("lev", gmb.lev);
		}

		return obj;
	}

	/**
	 * 获取圈子列表
	 * 
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITArray getGroups(int uid) throws Exception {
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			ITArray list = TArray.newInstance();
			String key = GroupCache.genGroupsKey(uid);
			log.info("key:" + key);
			log.info("uid:" + uid);

			Set<String> groups = jedis11.zrevrange(key, 0, -1);
			for (String tem : groups) {
				ITObject obj = getGroupData(uid, Integer.parseInt(tem), jedis11, jedis10);
				if (obj == null) {
					jedis11.zrem(key, tem);
					continue;
				}
				list.addTObject(obj);
			}

			return list;
		} finally {
			jedis11.close();
			jedis10.close();
		}
	}

	/**
	 * 获取圈子预览
	 * 
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getPreview(int uid) throws Exception {
		String key = GroupCache.genGroupsKey(uid);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		try {
			Set<String> groups = jedis11.zrevrange(key, 0, -1);
			ITObject obj = TObject.newInstance();
			for (String tem : groups) {
				int groupId = Integer.parseInt(tem);

				GroupBean gb = GroupCache.getGroup(groupId);
				if (gb == null) {
					continue;
				}
				obj.putInt("id", gb.id);
				obj.putString("name", gb.name);
				obj.putInt("type", gb.type);
				obj.putInt("member_num", gb.gms);
				String grooms_key = GroupCache.genRoomsKey(groupId);
				Set<String> rooms = jedis11.zrangeByScore(grooms_key, 100000, 1000000);
				obj.putInt("room_num", rooms.size());
				obj.putBoolean("is_group", true);
				return obj;
			}
			obj.putBoolean("is_group", false);
			return obj;
		} finally {
			jedis11.close();
		}

	}

	private static final Map<String, String> groupToRedis(String groupId, String name, int owner, int type,
			int pay_type) {
		Map<String, String> redis_map = new HashMap<>();
		redis_map.put("id", groupId);
		redis_map.put("name", name);
		redis_map.put("owner", owner + "");
		redis_map.put("type", type + "");
		redis_map.put("pay_type", pay_type + "");
		redis_map.put("opt", "1");
		redis_map.put("ban", "0");
		redis_map.put("dissolve_opt", "1");
		redis_map.put("kick_opt", "1");
		redis_map.put("ban_apply", "0");
		redis_map.put("create_time", System.currentTimeMillis() / 1000 + "");
		redis_map.put("gms", "1");
		redis_map.put("option", "0");
		return redis_map;
	}

	private static final Map<String, String> memberToRedis(String groupId, int uid, int lev, int partnerLev,
			int parentId, long time) {
		Map<String, String> redis_map = new HashMap<>();
		redis_map.put("groupId", groupId);
		redis_map.put("uid", uid + "");
		redis_map.put("lev", lev + "");
		redis_map.put("opt", "1");
		redis_map.put("hp", "0");
		redis_map.put("ban", "0");
		redis_map.put("join_time", time + "");
		redis_map.put("top_time", "0");
		redis_map.put("partnerLev", partnerLev + "");
		redis_map.put("parentId", parentId + "");
		redis_map.put("hp_opt", "1");
		redis_map.put("permission", "0");
		return redis_map;
	}

	private static final Map<String, String> payToRedis(int groupId, int pid, int gameId, String name, int deskId,
			String config, String hpData, int hpOnOff, int hp_times, int reward, int rewardType, int rewardValueType,
			int xipai_reward, int xipai_rewardType, int xipai_rewardValueType, int robot_room) {
		Map<String, String> redis_map = new HashMap<>();
		redis_map.put("groupId", groupId + "");
		redis_map.put("id", pid + "");
		redis_map.put("name", name);
		redis_map.put("deskId", deskId + "");
		redis_map.put("gameId", gameId + "");
		redis_map.put("config", config);
		redis_map.put("hpData", hpData);
		redis_map.put("hpOnOff", hpOnOff + "");
		redis_map.put("reward", reward + "");
		redis_map.put("rewardType", rewardType + "");
		redis_map.put("rewardValueType", rewardValueType + "");
		redis_map.put("xipai_reward", xipai_reward + "");
		redis_map.put("xipai_rewardType", xipai_rewardType + "");
		redis_map.put("xipai_rewardValueType", xipai_rewardValueType + "");
		redis_map.put("hp_times", hp_times + "");
		redis_map.put("ban", "0");
		redis_map.put("robot_room", robot_room + "");
		return redis_map;
	}

//	private static final String DEF_PROMOTION_DATA = "[{\"num\":0,\"val\":0}]";

	/**
	 * 创建圈子
	 * 
	 * @param name
	 * @param owner
	 * @param type     1普通圈子 2大联盟
	 * @param pay_type 1 房主支付 2AA支付
	 * @return
	 * @throws Exception
	 */
	public static final ITObject createGroup(String name, int owner, int type, int pay_type, int num) throws Exception {
		log.info("createGroup name:" + name + " owner:" + owner + " type:" + type + " pay_type:" + pay_type + " num:"
				+ num);

		String groupId = null;
		Jedis jedis1 = Redis.use("group1_db1").getJedis();
		try {
			if (num > 0) {
				groupId = num + "";
				if (!jedis1.sismember("lh_ids", groupId)) {
					throw new WebException(ErrorCode.GROUP_NO_EXIST);
				}
			} else {
				groupId = jedis1.rpop("free_group");
				if (jedis1.sismember("lh_ids", groupId)) {
					throw new WebException(ErrorCode._FAILED);
				}
			}

			String sql = String.format("SELECT id FROM groups WHERE owner=%s", owner);
			ITArray arr = DataBase.use().executeQueryByTArray(sql);
			if (arr.size() > 0) {
				if (num <= 0) {
					jedis1.lpush("free_group", groupId);
				}
				throw new WebException(ErrorCode.GROUP_TYPE2_ONLY_1);
			}

		} finally {
			jedis1.close();
		}

		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		int gid = Integer.parseInt(groupId);
		String key = GroupCache.genKey(Integer.parseInt(groupId));
		RedisLock lock = null;
		try {
			lock = new RedisLock(key, jedis11);
			lock.lock();

			String sql = String.format("{call sp_create_group(%s,'%s',%s,%s,%s)}", groupId, name, owner, type,
					pay_type);
			Utility.evtdb(gid, 2, sql);

			Map<String, String> redis_map = groupToRedis(groupId, name, owner, type, pay_type);
			jedis11.hmset(key, redis_map);
			BaseCache.updateCacheVer(jedis11, key);
			long time = System.currentTimeMillis() / 1000;
			redis_map = memberToRedis(groupId, owner, 1, 0, 0, time);
			Jedis jedis10 = Redis.use("group1_db10").getJedis();
			try {
				String gm_key = GroupMemberCache.genKey(Integer.parseInt(groupId), owner);
				jedis10.hmset(gm_key, redis_map);
				BaseCache.updateCacheVer(jedis10, gm_key);
			} finally {
				jedis10.close();
			}

			ITObject info = TObject.newInstance();
			info.putInt("id", gid);
			info.putString("name", name);
			String gpids_key = GroupCache.genPidsKey(gid);
			Pipeline pipeline = jedis11.pipelined();
			pipeline.del(gpids_key);
			for (int i = 10; i < 210; ++i) {
				pipeline.zadd(gpids_key, time, i + "");
				time++;
			}
			pipeline.sync();

			String gs_key = GroupCache.genGroupsKey(owner);
			jedis11.zadd(gs_key, 0, groupId);
			return info;

		} catch (Exception ex) {
			Redis.use("group1_db1").lpush("free_group", groupId);
			if (ex instanceof WebException) {
				throw ex;
			} else {
				log.error(ex);
			}

		} finally {
			if (lock != null) {
				lock.unlock();
			}
		}
		return null;
	}

	/**
	 * 删除圈子
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final void delGroup(int groupId, int uid) throws Exception {
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String key = GroupCache.genKey(groupId);

		RedisLock lock = new RedisLock(key, jedis11);
		try {
			lock.lock();
			String opt = jedis11.hget(key, "opt");
			if (StringUtil.isEmpty(opt) || Integer.parseInt(opt) != 1) {
				throw new WebException(ErrorCode.GROUP_NO_EXIST);
			}
			String grooms_key = GroupCache.genRoomsKey(groupId);
			jedis11.zremrangeByScore(grooms_key, 0, 0);
			Set<String> rooms = jedis11.zrangeByScore(grooms_key, 100000, 1000000);
			if (rooms.size() > 0) {
				throw new WebException(ErrorCode.GROUP_EXIST_ROOMS);
			}

			jedis11.hset(key, "opt", "2");
			GroupPublisherService.delGroupEvt(groupId);
			jedis11.expire(key, 30);
			String gpids_key = GroupCache.genPidsKey(groupId);
			String gs_key = GroupCache.genGroupsKey(uid);
			jedis11.del(gpids_key);
			jedis11.zrem(gs_key, groupId + "");
			Set<String> play_list = jedis11.keys("g{" + groupId + "}:*");
			Pipeline pipeline = jedis11.pipelined();
			for (String p_key : play_list) {
				pipeline.expire(p_key, 30);
			}
			pipeline.sync();
			Set<String> members = jedis10.keys("g{" + groupId + "}:*");
			pipeline = jedis10.pipelined();
			for (String m_key : members) {
				pipeline.expire(m_key, 30);
			}
			pipeline.sync();

			String sql = String.format("{call sp_del_group(%s)}", groupId);
			Utility.evtdb(groupId, 2, sql);
		} finally {
			lock.unlock();
			jedis10.close();
		}
	}

	/**
	 * 退出圈子
	 * 
	 * @param groupId
	 * @param uid
	 * @throws Exception
	 */
	public static final void exitGroup(int groupId, int uid) throws Exception {
		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb.exit_opt == 1) {
			throw new WebException(ErrorCode.GROUP_TYPE2_NOT_EXIT);
		}

		if ((gb.option & 2) == 0) {
			throw new WebException(ErrorCode.GROUP_TYPE2_NOT_EXIT);
		}

		delMember(groupId, gb.owner, uid);
	}

	/**
	 * 申请加入牌友圈
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final int applyGroup(int groupId, int uid) throws Exception {
		String joins_key = GroupCache.genJoinsKey(groupId);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		RedisLock lock = new RedisLock(joins_key, jedis11);
		try {
			lock.lock();
			GroupBean gb = GroupCache.getGroup(groupId);
			if (gb == null) {
				throw new WebException(ErrorCode.GROUP_NO_EXIST);
			}
			if (gb.ban_apply == 2) {
				return ErrorCode.GROUP_BAN_APPLY;
			}
			int type = gb.type;
			int gms = gb.gms;
			if (type == 1 && gms >= TYPE_1_NUM) {
				return ErrorCode.GROUP_MEMBER_IS_FULL;
			}
			if (type == 2 && gms >= TYPE_2_NUM) {
				return ErrorCode.GROUP_MEMBER_IS_FULL;
			}
			String tag_key = GroupMemberCache.genKey(groupId, uid);
			String member_opt = Redis.use("group1_db10").hget(tag_key, "opt");
			if (StringUtil.isNotEmpty(member_opt) && member_opt.equals("1")) {
				return ErrorCode.GROUP_MEMBER_EXIST;
			}
			if (jedis11.sismember(joins_key, uid + "")) {
				return ErrorCode.GROUP_JOIN_EXIST;
			}
			jedis11.sadd(joins_key, uid + "");
			int joins = jedis11.scard(joins_key).intValue();
			GroupPublisherService.updateJoinsEvt(groupId, joins);
		} finally {
			lock.unlock();
		}
		return 0;
	}

	/**
	 * 验证加入牌友圈
	 * 
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @param allow
	 * @return
	 * @throws Exception
	 */
	public static final int verifyJoinGroup(int groupId, int uid, int tagId, boolean allow) throws Exception {
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String member_key = GroupMemberCache.genKey(groupId, tagId);
		RedisLock lock1 = new RedisLock(member_key, jedis10);
		String joins_key = GroupCache.genJoinsKey(groupId);
		RedisLock lock = new RedisLock(joins_key, jedis11);
		try {
			lock.lock();
			lock1.lock();
			long len = jedis11.srem(joins_key, tagId + "");
			String member_opt = jedis10.hget(member_key, "opt");
			if (StringUtil.isNotEmpty(member_opt) && member_opt.equals("1")) {
				return ErrorCode.GROUP_MEMBER_EXIST;
			}
			String g_key = GroupCache.genKey(groupId);
			List<String> list = jedis11.hmget(g_key, "type", "gms");
			int type = Integer.parseInt(list.get(0));
			int gms = Integer.parseInt(list.get(1));
			if (type == 1 && gms >= TYPE_1_NUM) {
				return ErrorCode.GROUP_MEMBER_IS_FULL;
			}
			if (type == 2 && gms >= TYPE_2_NUM) {
				return ErrorCode.GROUP_MEMBER_IS_FULL;
			}
			if (len > 0 && allow) {
				long time = System.currentTimeMillis() / 1000;
				String sql = String.format(
						"INSERT INTO group_member(uid,groupId,join_time,parentId) VALUES(%s,%s,%s,%s)", tagId, groupId,
						time, 0);
				Utility.evtdb(groupId, 1, sql);
				Map<String, String> redis_map = memberToRedis(groupId + "", tagId, 3, 0, 0, time);
				jedis10.hmset(member_key, redis_map);
				BaseCache.updateCacheVer(jedis10, member_key);
				jedis11.hincrBy(g_key, "gms", 1);
				BaseCache.updateCacheVer(jedis11, g_key);
				String key = GroupCache.genGroupsKey(tagId);
				jedis11.zadd(key, 0, groupId + "");
			}
			int joins = jedis11.scard(joins_key).intValue();
			GroupPublisherService.updateJoinsEvt(groupId, joins);
			return 0;
		} finally {
			lock.unlock();
			lock1.unlock();
		}
	}

	private static String getSvr(Jedis jedis11) {
		Set<String> svr_list = jedis11.zrange("mgr_group", 0, -1);
		if (svr_list.size() > 0) {
			for (String str : svr_list) {
				if (jedis11.exists(str)) {
					return str;
				}
			}
		}
		return null;
	}

	/**
	 * 进入圈子
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITObject enterGroup(int groupId, int uid) throws Exception {
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();

		try {
			String svr = getSvr(jedis11);
			if (StringUtil.isEmpty(svr)) {
				log.error("enterGroup fail groupId:" + groupId + " svr:" + svr + " uid:" + uid);
				throw new WebException(ErrorCode.NO_SERVICE);
			}
			String group_key = GroupCache.genKey(groupId);

			String host = jedis11.hget(svr, "host");
			ITObject data = TObject.newInstance();
			data.putString("host", host);
			String notice = Redis.use("group1_db11").hget(group_key, "notice");
			data.putString("notice", StringUtil.isEmpty(notice) ? StringUtil.Empty : notice);
			String hide_action = Redis.use("group1_db11").hget(group_key, "hide_action");
			data.putInt("hide_action", StringUtil.isEmpty(hide_action) ? 0 : Integer.parseInt(hide_action));
//			// 同步redis跟mysql的hp
//			String sql = String.format("select hp from group_member where groupId = %s and uid=%s", groupId, uid);
//			ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
//			if (tmplist.size() > 0) {
//				int hp = tmplist.getTObject(0).getInt("hp");
//				jedis10.hset("g{" + groupId + "}:m" + uid, "hp", hp + "");
//			}
			return data;
		} finally {
			jedis11.close();
			jedis10.close();
		}
	}

	/**
	 * 获得玩家信息
	 *
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getPlayerInfo(int uid) throws Exception {
		AccountBean acc = AccountCache.getAccount(uid);
		if (acc == null) {
			return null;
		}

		ITObject obj = TObject.newInstance();
		obj.putInt("uid", uid);
		obj.putString("nick", acc.nick);
		obj.putString("portrait", acc.portrait);
		return obj;
	}

	/**
	 * 添加成员
	 * 
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	public static final int addMember(int groupId, int uid, int tagId) throws Exception {
		log.info("addMember uid:" + uid + " tagId:" + tagId);
		GroupBean gb = GroupCache.getGroup(groupId);
		String g_key = gb.redis_key;
		int type = gb.type;
		int gms = gb.gms;
		if (type == 1 && gms >= TYPE_1_NUM) {
			return ErrorCode.GROUP_MEMBER_IS_FULL;
		}
		if (type == 2 && gms >= TYPE_2_NUM) {
			return ErrorCode.GROUP_MEMBER_IS_FULL;
		}
		String tag_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(tag_key, jedis10);
		RedisLock lock1 = null;
		try {
			lock.lock();
			String session_key = AccountCache.genKey(tagId);
			List<String> s_list = Redis.use("group1_db0").hmget(session_key, "id", "invitation");
			if (StringUtil.isEmpty(s_list.get(0))) {
				return ErrorCode._FAILED;
			}
			int invitation = 0;
			if (StringUtil.isNotEmpty(s_list.get(1))) {
				invitation = Integer.parseInt(s_list.get(1));
			}
			if (invitation == 0) {
				return ErrorCode.GROUP_CLOSE_INVITATION;
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			lock1 = new RedisLock(mng_key, jedis10);
			lock1.lock();
			GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);

			if (mng_bean == null) {
				return ErrorCode.GROUP_NOT_MEMBER;
			}

			int mgn_lev = mng_bean.lev;
			int mgn_partnerLev = mng_bean.partnerLev;
			if (mgn_lev == 3 && mgn_partnerLev == 0) {
				return ErrorCode.GROUP_NOT_PARTNER;
			}
			if (mgn_lev == 2) {
				if ((mng_bean.permission & PERMISSION_ADD_MEMBER) == 0) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
			String member_opt = jedis10.hget(tag_key, "opt");
			boolean exits = StringUtil.isNotEmpty(member_opt) && member_opt.equals("1");
			if (exits) {
				return ErrorCode.GROUP_MEMBER_EXIST;
			}
			Jedis jedis11 = Redis.use("group1_db11").getJedis();
			if (!exits) {
				String joins_key = GroupCache.genJoinsKey(groupId);
				log.info("addMember joins_key:" + joins_key);
				if (jedis11.sismember(joins_key, tagId + "")) {
					jedis11.srem(joins_key, tagId + "");
					int joins = jedis11.scard(joins_key).intValue();
					log.info("addMember joins:" + joins);
					GroupPublisherService.updateJoinsEvt(groupId, joins);
				}
			}

			try {
				int mgn_uid = mgn_lev == 3 ? uid : 0;
				long time = System.currentTimeMillis() / 1000;
//				String sql = String.format("{call sp_add_member(%s,%s,%s)}", groupId, mgn_uid, tagId);
//				Utility.evtdb(groupId, 2, sql);

//				INSERT INTO group_member(uid,groupId,join_time,parentId) VALUES(_tagId,_groupId,_time,_pid);
				String selectSql = String.format("select uid from group_member where uid=%s", tagId);
				ITArray tmplist = DataBase.use().executeQueryByTArray(selectSql);
				if (tmplist.size() > 0) {
					return ErrorCode.GROUP_MEMBER_EXIST;
				} else {
					String insertSql = String.format(
							"INSERT INTO group_member(uid,groupId,join_time,parentId) VALUES(%s,%s,%s,%s)", tagId,
							groupId, time, mgn_uid);
					Utility.evtdb(groupId, 1, insertSql);

				}

//				sp_add_member(groupId, mgn_uid, tagId);

				Map<String, String> redis_map = memberToRedis(groupId + "", tagId, 3, 0, mgn_uid, time);

				redis_map.put("queueid", mng_bean.queueid + "");
				// redis_map.put("seeid",mng_bean.seeid+"");

				jedis10.hmset(tag_key, redis_map);
				BaseCache.updateCacheVer(jedis10, tag_key);
				jedis11.hincrBy(g_key, "gms", 1);
				BaseCache.updateCacheVer(jedis11, g_key);
				String key = GroupCache.genGroupsKey(tagId);
				jedis11.zadd(key, 0, groupId + "");

				String group_ban = jedis10.hget(mng_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					String updateSql = String.format("update group_member set ban = %s where uid = %s AND groupId = %s",
							group_ban, tagId, groupId);
					Utility.evtdb(groupId, 1, updateSql);

					jedis10.hset(tag_key, "ban", group_ban);
					jedis10.hset(tag_key, "group_ban", group_ban);
					BaseCache.updateCacheVer(jedis10, tag_key);
				}

				String sql2 = String.format("update account set is_bangding =1 where id = %s", tagId);
				Utility.evtdb(groupId, 1, sql2);

			} finally {
				jedis11.close();
			}
			return 0;
		} finally {
			log.info("lock1 time:" + lock1);
			if (lock1 != null) {
				lock1.unlock(false);
			}
			log.info("lock time:" + lock);
			lock.unlock();
		}
	}

	public static final ITObject cancelParent(int groupId, int uid, int tagId) throws Exception {
		log.info("setParent uid:" + uid + " tagId:" + tagId);
		String tag_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(tag_key, jedis10);
		RedisLock lock1 = null;
		try {
			lock.lock();
			GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
			if (tag_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			lock1 = new RedisLock(mng_key, jedis10);
			lock1.lock();
			GroupMemberBean mng = GroupCache.getMember(groupId, uid);
			if (mng == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			int mgn_lev = mng.lev;
			int mgn_partner = mng.partnerLev;

			int member_lev = tag_bean.lev;
			if (member_lev < 3) {
				throw new WebException(ErrorCode.GROUP_TAG_ISMGR);
			}

			int member_parentId = tag_bean.parentId;
			if (member_parentId > 0 && uid != member_parentId) {
				throw new WebException(ErrorCode.GROUP_MEMBER_EXIST_PARTENER);
			}
			GroupBean gb = GroupCache.getGroup(groupId);
			int tag_partner_lev = 0;
			int parent = uid;

			String sql = String.format(
					"update group_member set parentId = %s,partnerLev=%s " + "WHERE uid = %s and groupId = %s", parent,
					tag_partner_lev, tagId, groupId);
			Utility.evtdb(groupId, 1, sql);
			GroupPublisherService.updateMemberEvt(groupId, tagId, 3, tag_partner_lev);
			jedis10.hset(tag_key, "parentId", parent + "");
			jedis10.hset(tag_key, "partnerLev", tag_partner_lev + "");
			BaseCache.updateCacheVer(jedis10, tag_key);
			if (mgn_lev == 3) {
				List<Integer> list = Utility.getMemberParents(jedis10, groupId, uid, true);
				for (Integer par : list) {
					String pl_key = GroupCache.genParListKey(groupId, par);
					jedis10.del(pl_key);
				}
			}

			ITObject resData = TObject.newInstance();
			resData.putInt("parentId", parent);
			resData.putInt("partnerLev", tag_partner_lev);

			return resData;
		} finally {

		}
	}

	/**
	 * 设置合伙人
	 * 
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	public static final ITObject setParent(int groupId, int uid, int tagId) throws Exception {
		log.info("setParent uid:" + uid + " tagId:" + tagId);
		String tag_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(tag_key, jedis10);
		RedisLock lock1 = null;
		try {
			lock.lock();
			GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
			if (tag_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			lock1 = new RedisLock(mng_key, jedis10);
			lock1.lock();
			GroupMemberBean mng = GroupCache.getMember(groupId, uid);
			if (mng == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			int mgn_lev = mng.lev;
			int mgn_partner = mng.partnerLev;
			if (mgn_lev == 3 && mgn_partner == 0) {
				throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
			}

			int member_lev = tag_bean.lev;
			if (member_lev < 3) {
				throw new WebException(ErrorCode.GROUP_TAG_ISMGR);
			}
			int member_partner = tag_bean.partnerLev;
			if (member_partner > 0) {
				throw new WebException(ErrorCode.GROUP_ALREADY_PARTNER);
			}
			int member_parentId = tag_bean.parentId;
			if (member_parentId > 0 && uid != member_parentId) {
				throw new WebException(ErrorCode.GROUP_MEMBER_EXIST_PARTENER);
			}
			GroupBean gb = GroupCache.getGroup(groupId);
			int tag_partner_lev = 1;
			int parent = uid;
			if (mgn_lev < 3) {
				parent = gb.owner;
			} else {
				tag_partner_lev = (mgn_partner + 1);
			}

			String sql = String.format(
					"update group_member set parentId = %s,partnerLev=%s " + "WHERE uid = %s and groupId = %s", parent,
					tag_partner_lev, tagId, groupId);
			Utility.evtdb(groupId, 1, sql);
			GroupPublisherService.updateMemberEvt(groupId, tagId, 3, tag_partner_lev);
			jedis10.hset(tag_key, "parentId", parent + "");
			jedis10.hset(tag_key, "partnerLev", tag_partner_lev + "");
			BaseCache.updateCacheVer(jedis10, tag_key);
			if (mgn_lev == 3) {
				List<Integer> list = Utility.getMemberParents(jedis10, groupId, uid, true);
				for (Integer par : list) {
					String pl_key = GroupCache.genParListKey(groupId, par);
					jedis10.sadd(pl_key, tagId + "");
				}
			}
			// int member_hp = Integer.parseInt(jedis10.hget(tag_key, "hp"));
			// String ph_key = String.format("g{%s}:par_hp:%s", groupId,tagId);
			// jedis10.incrBy(ph_key, member_hp);
			ITObject resData = TObject.newInstance();
			resData.putInt("parentId", parent);
			resData.putInt("partnerLev", tag_partner_lev);

			return resData;

		} finally {
			if (lock1 != null) {
				lock1.unlock(false);
			}
			lock.unlock();

		}
	}

	/**
	 * 设置备注值
	 * 
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @param score
	 * @return
	 * @throws Exception
	 */
	public static final void updateMemberScore(int groupId, int uid, int tagId, int score) throws Exception {
		log.info("updateMemberScore uid:" + uid + " tagId:" + tagId + " score:" + score);
		GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
		if (tag_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
		if (mng_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			if (mng_bean.lev >= 3) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, false);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			jedis10.hset(tag_bean.redis_key, "score", score + "");
			BaseCache.updateCacheVer(jedis10, GroupMemberCache.genKey(groupId, tagId));
		} finally {
			jedis10.close();
		}
	}

	public static final void updateMemberDiaokong(int groupId, int uid, int tagId, int diaokongid) throws Exception {

		// 判断uid是否存在
		GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
		if (tag_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
		if (mng_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		Jedis jedis1b = Redis.use("group1_db1").getJedis();
		try {
			String diaokong = jedis1b.hget("gods_value", Integer.toString(tagId));
			if (StringUtil.isNotEmpty(diaokong)) {
				jedis1b.hset("gods_value", Integer.toString(tagId), Integer.toString(diaokongid));
			} else {
				throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
			}
		} finally {
			jedis1b.close();
		}
	}

	/**
	 * 设置备注值
	 *
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @param queueid
	 * @return
	 * @throws Exception
	 */
	public static final void updateMemberQueue(int groupId, int uid, int tagId, int queueid) throws Exception {
		log.info("updateMemberQueue uid:" + uid + " tagId:" + tagId + " queue:" + queueid);
		GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
		if (tag_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
		if (mng_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		/*
		 * try { if (mng_bean.lev >= 3) { List<Integer> par_list =
		 * Utility.getMemberParents(jedis10, groupId, tagId, false); if (par_list ==
		 * null || !par_list.contains(uid)) { throw new
		 * WebException(ErrorCode.GROUP_NOT_PERMISSION); } }
		 * 
		 * jedis10.hset(tag_bean.redis_key, "queueid", queueid + "");
		 * BaseCache.updateCacheVer(jedis10, GroupMemberCache.genKey(groupId, tagId)); }
		 * finally { jedis10.close(); }
		 */

		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		// Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {

			jedis10.hset(tag_bean.redis_key, "queueid", queueid + "");
			BaseCache.updateCacheVer(jedis10, GroupMemberCache.genKey(groupId, tagId));

			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			RedisLock lock1 = new RedisLock(mng_key, jedis10);
			try {
				lock1.lock();
				int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
				if (mgn_lev == 2) {
					String permission = jedis10.hget(mng_key, "permission");
					if (StringUtil.isEmpty(permission) || (Integer.parseInt(permission) & PERMISSION_BAN) == 0) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				} else if (mgn_lev >= 3) {
					if (uid != tagId) {
						List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
						if (par_list == null || !par_list.contains(uid)) {
							throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
						}
						// log.info("uid:" + uid + " ban_member:" + tagId + " par_list:" + par_list);
					}
				}

				String group_ban = jedis10.hget(mng_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					throw new WebException(ErrorCode.GROUP_MEMBER_BAN);
				}
			} finally {
				lock1.unlock(false);
			}

			// log.info("uid:" + uid + " ban_member:" + tagId + " optType:" + optType + "
			// ban:" + ban);

			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql;
			// String _ban = ban + "";
			if (isPartner) {
				HashSet<Integer> hashSet = new HashSet<>();
				hashSet.add(tagId);
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				gm_key = GroupMemberCache.genKey(groupId, tagId);
				// jedis10.hset(gm_key, "ban", _ban);
				// jedis10.hset(gm_key, "group_ban", _ban);
				BaseCache.updateCacheVer(jedis10, gm_key);

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);

						// sql = String.format("update group_member set ban = %s where groupId = %s and
						// (parentId in(%s) or uid = %s)", _ban, groupId,child_list2,tmpTagId);
						// Utility.evtdb(groupId, 1, sql);

						sql = String.format(
								"select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");

								if (hashSet.contains(childId))
									continue;

								gm_key = GroupMemberCache.genKey(groupId, childId);
								jedis10.hset(gm_key, "queueid", queueid + "");
								// jedis10.hset(gm_key, "group_ban", _ban);
								BaseCache.updateCacheVer(jedis10, gm_key);

								hashSet.add(childId);
								arrayList.add(childId);
							}
						}
					}
				}
			} else {

				// sql = String.format("update group_member set ban = %s where uid = %s AND
				// groupId = %s", _ban, tagId,groupId);
				// Utility.evtdb(groupId, 1, sql);
				jedis10.hset(gm_key, "queueid", queueid + "");
				BaseCache.updateCacheVer(jedis10, gm_key);
			}
		} finally {
			lock.unlock();
		}
	}

	/**
	 * 设置权限看值
	 *
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @param seeid
	 * @return
	 * @throws Exception
	 */
	public static final void updateMemberSee(int groupId, int uid, int tagId, int seeid) throws Exception {
		log.info("updateMemberSee uid:" + uid + " tagId:" + tagId + " seeid:" + seeid);
		GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
		if (tag_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
		if (mng_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			if (mng_bean.lev >= 3) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, false);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			jedis10.hset(tag_bean.redis_key, "seeid", seeid + "");
			BaseCache.updateCacheVer(jedis10, GroupMemberCache.genKey(groupId, tagId));
		} finally {
			jedis10.close();
		}
	}

//	/**
//	 * 设置体力值
//	 * 
//	 * @param groupId
//	 * @param uid
//	 * @param tagId
//	 * @param hp
//	 * @return
//	 * @throws Exception
//	 */
//	public static final ITObject updateMemberHp(int groupId, int uid, int tagId, int hp, boolean self, int otherId)
//			throws Exception {
//
//		String tag_key = GroupMemberCache.genKey(groupId, tagId);
//		Jedis jedis10 = Redis.use("group1_db10").getJedis();
//		RedisLock lock = new RedisLock(tag_key, jedis10);
//		try {
//			lock.lock();
//			GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
//			if (tag_bean == null) {
//				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
//			}
//			GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
//			if (mng_bean == null) {
//				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
//			}
//			// 正在游戏不允许下分操作
//			if (hp < 0) {
//				if (GroupRoomService.checkRoom(groupId, uid)) {
//					throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
//				}
//				if (GroupRoomService.checkRoom(groupId, tagId)) {
//					throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
//				}
//			}
//
//			if (hp > 0) {
//				int pre_hp = Integer.parseInt(jedis10.hget(tag_key, "hp"));
//
//				if ((long) pre_hp + (long) hp > Integer.MAX_VALUE) {
//					throw new WebException(ErrorCode._FAILED);
//				}
//			}
//
//			int mgn_partner = mng_bean.partnerLev;
//			int mgn_lev = mng_bean.lev;
//			if (tag_bean.lev == 1) {
//				if (uid != tagId) {
//					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//				}
//			} else if (tag_bean.lev == 2) {
//				if (mgn_lev != 1) {
//					if (uid != tagId) {
//						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//					}
//				}
//			}
//
//			if (mgn_lev == 3 && mgn_partner == 0) {
//				throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
//			}
//			if (mgn_lev == 2) {
//				if ((mng_bean.permission & PERMISSION_HP_OPT) == 0) {
//					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//				}
//			}
//
//			if (self == false && mgn_lev != 1 && uid == tagId) {
//				throw new WebException(ErrorCode._FAILED);
//			}
//
//			if (mgn_lev == 3) {
//				if (!self) {
//					List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, false);
//					if (par_list == null || !par_list.contains(uid)) {
//						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//					}
//				} else {
//					List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
//					if (par_list == null || !par_list.contains(uid)) {
//						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//					}
//				}
//			}
//			String lua = MainServer.lua_map.get("mgr");
//			Object obj = jedis10.evalsha(lua, Arrays.asList(mng_bean.redis_key, tag_key),
//					Arrays.asList(hp + "", mgn_lev + ""));
//			if (obj == null) {
//				throw new WebException(ErrorCode._FAILED);
//			}
//			if (obj instanceof Long) {
//				Long code = (Long) obj;
//				if (code == 3) {
//					throw new WebException(ErrorCode.GROUP_TAG_MGR_HP);
//				} else if (code == 4) {
//					throw new WebException(ErrorCode.GROUP_TAG_NO_HP);
//				}
//			} else {
//				ArrayList<Long> arr = (ArrayList<Long>) obj;
//				ITObject resData = TObject.newInstance();
//				int mhp = arr.get(0).intValue();
//				int thp = arr.get(1).intValue();
//				resData.putInt("mhp", (int) mhp);
//				resData.putInt("hp", (int) thp);
//
////				String sql = String.format("{call sp_update_hp_mgr2(%s,%s,%s,%s,%s,%s,%s,%s)}", groupId, uid, hp, mhp,
////						thp, tagId, mgn_lev, otherId);
////				Utility.evtdb(groupId, 2, sql);
//
//				String sql = String.format("{call sp_update_hp_mgr2(%s,%s,%s,%s,%s,%s,%s,%s)}", groupId, uid, hp, mhp,
//						thp, tagId, mgn_lev, otherId);
//				Utility.evtdb(groupId, 2, sql);
//
//				if (!self)
//					log.info("updateMemberHp uid:" + uid + " tagId:" + tagId + " hp:" + hp / 1000);
//
//				// ------------------------邮件 -------------------
//				ITObject mail_data = TObject.newInstance();
//				mail_data.putInt("mgr_id", uid);
//				AccountBean acc = AccountCache.getAccount(uid);
//				mail_data.putString("nick", acc.nick);
//				mail_data.putString("headurl", acc.portrait);
//				mail_data.putInt("hp", hp);
//				mail_data.putInt("type", 1);
//				int time = (int) (System.currentTimeMillis() / 1000);
//				mail_data.putInt("time", time);
//				String mail_key = GroupCache.genMailKey(groupId, tagId);
//				jedis10.zadd(mail_key, time, mail_data.toJson());
//				String mail_tip_key = GroupCache.genMailTipKey(groupId);
//				jedis10.zadd(mail_tip_key, 1, tagId + "");
//				GroupPublisherService.updateMailTipEvt(groupId, tagId);
//				// -----------------------------------------------
//
//				String c_tem = hp > 0 ? "upper" : "sub";
//				if (mgn_lev < 3) {
//					String hp_mgr_key = String.format("g%s:hp_mgr:%s", groupId, c_tem);
//					String hp_mgr_id_key = String.format("g%s:hp_mgr:%s:u%s", groupId, c_tem, uid);
//					Jedis jedis9 = Redis.use(redisdb).getJedis();
//					try {
//						hp = Math.abs(hp);
//						CountUtil.countLogByDay(hp_mgr_key, hp, jedis9);
//						CountUtil.countLogByDay(hp_mgr_id_key, hp, jedis9);
//					} finally {
//						jedis9.close();
//					}
//				} else {
//					String hp_par_id_key = String.format("g%s:hp_par:%s:u%s", groupId, c_tem, uid);
//					Jedis jedis9 = Redis.use(redisdb).getJedis();
//					try {
//						hp = Math.abs(hp);
//						CountUtil.countLogByDay(hp_par_id_key, hp, jedis9);
//					} finally {
//						jedis9.close();
//					}
//				}
//				return resData;
//			}
//			return null;
//		} finally {
//			lock.unlock();
//
//		}
//	}
	/**
	 * 设置体力值
	 * 
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @param hp
	 * @return
	 * @throws Exception
	 */
	public static final ITObject updateMemberHp(int groupId, int uid, int tagId, int hp, boolean self, int otherId)
			throws Exception {

		String tag_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(tag_key, jedis10);
		try {
			lock.lock();
			GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
			if (tag_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
			if (mng_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			// 正在游戏不允许下分操作
			if (hp < 0) {
				if (GroupRoomService.checkRoom(groupId, uid)) {
					throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
				}
				if (GroupRoomService.checkRoom(groupId, tagId)) {
					throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
				}
			}

			if (hp > 0) {
				int pre_hp = Integer.parseInt(jedis10.hget(tag_key, "hp"));

				if ((long) pre_hp + (long) hp > Integer.MAX_VALUE) {
					throw new WebException(ErrorCode._FAILED);
				}
			}

			int mgn_partner = mng_bean.partnerLev;
			int mgn_lev = mng_bean.lev;
			if (tag_bean.lev == 1) {
				if (uid != tagId) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			} else if (tag_bean.lev == 2) {
				if (mgn_lev != 1) {
					if (uid != tagId) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				}
			}

			if (mgn_lev == 3 && mgn_partner == 0) {
				throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
			}
			if (mgn_lev == 2) {
				if ((mng_bean.permission & PERMISSION_HP_OPT) == 0) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			if (self == false && mgn_lev != 1 && uid == tagId) {
				throw new WebException(ErrorCode._FAILED);
			}

			if (mgn_lev == 3) {
				if (!self) {
					List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, false);
					if (par_list == null || !par_list.contains(uid)) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				} else {
					List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
					if (par_list == null || !par_list.contains(uid)) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				}
			}
			String lua = MainServer.lua_map.get("mgr");
			Object obj = jedis10.evalsha(lua, Arrays.asList(mng_bean.redis_key, tag_key),
					Arrays.asList(hp + "", mgn_lev + ""));
			if (obj == null) {
				throw new WebException(ErrorCode._FAILED);
			}
			if (obj instanceof Long) {
				Long code = (Long) obj;
				if (code == 3) {
					throw new WebException(ErrorCode.GROUP_TAG_MGR_HP);
				} else if (code == 4) {
					throw new WebException(ErrorCode.GROUP_TAG_NO_HP);
				}
			} else {
				ArrayList<Long> arr = (ArrayList<Long>) obj;
				ITObject resData = TObject.newInstance();
				int mhp = arr.get(0).intValue();
				int thp = arr.get(1).intValue();
				resData.putInt("mhp", (int) mhp);
				resData.putInt("hp", (int) thp);

//				String sql = String.format("{call sp_update_hp_mgr2(%s,%s,%s,%s,%s,%s,%s,%s)}", groupId, uid, hp, mhp,
//						thp, tagId, mgn_lev, otherId);
//				Utility.evtdb(groupId, 2, sql);

				String sql = "";

				if (!self)
					log.info("updateMemberHp uid:" + uid + " tagId:" + tagId + " hp:" + hp / 100);
				// 手动录入sql,不走存储过程
				int reason = 8;
				if (mgn_lev > 1) {
					if (mgn_lev == 2) {
						reason = hp < 0 ? ConsumeCode.HP_MGR_SUB : ConsumeCode.HP_MGR_UPPER;
					} else {
						reason = hp < 0 ? ConsumeCode.HP_PARTNER_SUB : ConsumeCode.HP_PARTNER_UPPER;
					}
					// 扣除操作者hp
					sql = String.format("UPDATE group_member SET hp=hp+%s WHERE  groupId = %s and uid=%s ", -hp,
							groupId, uid);
//					log.info("sql1:" + sql);
					DataBase.use().executeUpdate(sql);
					// 增加记录
					sql = String.format(
							"INSERT INTO group_hp_log(gid,uid,mgr_id,reason,hp,cur_hp,pid,roomid,time) "
									+ "VALUES(%s,%s,%s,%s,%s,%s,%s,'',%s)",
							groupId, uid, tagId, reason, -hp, mhp, 0, System.currentTimeMillis() / 1000);
//					log.info("操作者sql:" + sql);
					DataBase.use().executeUpdate(sql);

					// 扣除目标人物hp
					sql = String.format("UPDATE group_member SET hp=hp+%s WHERE  groupId = %s and uid=%s ", hp, groupId,
							tagId);
					DataBase.use().executeUpdate(sql);
					// 增加记录
					reason = hp < 0 ? ConsumeCode.HP_MGR_SUB : ConsumeCode.HP_MGR_UPPER;

					sql = String.format(
							"INSERT INTO group_hp_log(gid,uid,mgr_id,reason,hp,cur_hp,pid,roomid,time) "
									+ "VALUES(%s,%s,%s,%s,%s,%s,%s,'',%s)",
							groupId, tagId, uid, reason, hp, thp, 0, System.currentTimeMillis() / 1000);
//					log.info("目标人物sql:" + sql);
					DataBase.use().executeUpdate(sql);
				} else {
					reason = hp < 0 ? ConsumeCode.HP_MGR_SUB : ConsumeCode.HP_MGR_UPPER;

					// 扣除目标人物hp
					sql = String.format("UPDATE group_member SET hp=hp+%s WHERE groupId = %s and uid=%s", hp, groupId,
							tagId);
					DataBase.use().executeUpdate(sql);
					if (uid == tagId && otherId > 0) {
						// 扣除操作者hp
						sql = String.format("UPDATE group_member SET hp=hp+%s WHERE groupId = %s and uid=%s", -hp,
								groupId, uid);
						DataBase.use().executeUpdate(sql);
						// 增加记录
						sql = String.format(
								"INSERT INTO group_hp_log(gid,uid,mgr_id,reason,hp,cur_hp,pid,roomid,time) "
										+ "VALUES(%s,%s,%s,%s,%s,%s,%s,'',%s)",
								groupId, uid, otherId, reason, hp, thp, 0, System.currentTimeMillis() / 1000);
//						log.info("自身记录:" + sql);
						DataBase.use().executeUpdate(sql);
					} else {
						if (uid != tagId) {
							// 扣除操作者hp
							sql = String.format("UPDATE group_member SET hp=hp+%s WHERE groupId = %s and uid=%s", -hp,
									groupId, uid);
							DataBase.use().executeUpdate(sql);

						}
						// 增加记录
						sql = String.format(
								"INSERT INTO group_hp_log(gid,uid,mgr_id,reason,hp,cur_hp,pid,roomid,time) "
										+ "VALUES(%s,%s,%s,%s,%s,%s,%s,'',%s)",
								groupId, tagId, uid, reason, hp, thp, 0, System.currentTimeMillis() / 1000);
//						log.info("mengzhu自身记录:" + sql);
						DataBase.use().executeUpdate(sql);
					}

				}

				// ------------------------邮件 -------------------
				ITObject mail_data = TObject.newInstance();
				mail_data.putInt("mgr_id", uid);
				AccountBean acc = AccountCache.getAccount(uid);
				mail_data.putString("nick", acc.nick);
				mail_data.putString("headurl", acc.portrait);
				mail_data.putInt("hp", hp);
				mail_data.putInt("type", 1);
				int time = (int) (System.currentTimeMillis() / 1000);
				mail_data.putInt("time", time);
				String mail_key = GroupCache.genMailKey(groupId, tagId);
				jedis10.zadd(mail_key, time, mail_data.toJson());
				String mail_tip_key = GroupCache.genMailTipKey(groupId);
				jedis10.zadd(mail_tip_key, 1, tagId + "");
				GroupPublisherService.updateMailTipEvt(groupId, tagId);
				// -----------------------------------------------

				String c_tem = hp > 0 ? "upper" : "sub";
				if (mgn_lev < 3) {
					String hp_mgr_key = String.format("g%s:hp_mgr:%s", groupId, c_tem);
					String hp_mgr_id_key = String.format("g%s:hp_mgr:%s:u%s", groupId, c_tem, uid);
					Jedis jedis9 = Redis.use(redisdb).getJedis();
					try {
						hp = Math.abs(hp);
						CountUtil.countLogByDay(hp_mgr_key, hp, jedis9);
						CountUtil.countLogByDay(hp_mgr_id_key, hp, jedis9);
					} finally {
						jedis9.close();
					}
				} else {
					String hp_par_id_key = String.format("g%s:hp_par:%s:u%s", groupId, c_tem, uid);
					Jedis jedis9 = Redis.use(redisdb).getJedis();
					try {
						hp = Math.abs(hp);
						CountUtil.countLogByDay(hp_par_id_key, hp, jedis9);
					} finally {
						jedis9.close();
					}
				}
				return resData;
			}
			return null;
		} finally {
			lock.unlock();

		}
	}

	/**
	 * 获取奖励数据
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 */
	public static final ITObject getTakeInfo(int groupId, int uid, int tagId) throws Exception {
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			jedis10.close();
		}

		String rhp_key = String.format("g{%s}:m%s:reward_hp", groupId, tagId);
		String r_hp = Redis.use("group1_db10").get(rhp_key);
		int _r_hp = 0;
		if (StringUtil.isNotEmpty(r_hp)) {
			_r_hp = Integer.parseInt(r_hp);
		}

		long day_rewad = 0;
		long day_rewad_1 = 0;
		long day_rewad_2 = 0;
//		int all_day_reward = 0;
//		Jedis jedis9 = Redis.use(redisdb).getJedis();
//		try {
//			String key = String.format("g%s:hp_reward:m%s", groupId, tagId);
//			day_rewad = CountUtil.getCountLogByDay(key, jedis9);
//			String day_key_1 = key + ":d" + (DateUtils.getBeginDay() - 3600 * 24);
//			day_rewad_1 = CountUtil.getCountLog(day_key_1, 0, jedis9);
//			String day_key_2 = key + ":d" + (DateUtils.getBeginDay() - 3600 * 48);
//			day_rewad_2 = CountUtil.getCountLog(day_key_2, 0, jedis9);
//		} finally {
//			jedis9.close();
//		}
		Double todayReward = 0.0;
		GroupBean gb = GroupCache.getGroup(groupId);
		// 计算群主每日抽水
		String pumpwhere_sql = String.format(
				" WHERE gid = %s and uid=%s and reason = %s and time>=%s and time <=%s  AND info != '' ", groupId,
				gb.owner, ConsumeCode.HP_PARTNER_REWARD, DateUtils.getBeginDay() , DateUtils.getEndDay());
		String pumpsql = String.format("SELECT ifnull(sum(hp),0) as hp FROM group_hp_log  %s ", pumpwhere_sql);

		ITArray resultArray = DataBase.use().executeQueryByTArray(pumpsql);
		if (resultArray.size() == 0) {
			todayReward = 0.0;
		} else {
			ITObject dbData = resultArray.getTObject(0);
			Double pump = dbData.getDouble("hp");
			String xipaiwhere_sql = String.format(
					" WHERE gid = %s and  reason in(%s,%s) and time>=%s and time <=%s  AND info != '' ", groupId,
					ConsumeCode.HP_XIPAI_PUMP, ConsumeCode.HP_PARTNER_XIPAI_REWARD_BACK,
					DateUtils.getBeginDay() , DateUtils.getEndDay());
			String xipaisql = String.format("SELECT ifnull(sum(hp),0) as hp FROM group_hp_log  %s ", xipaiwhere_sql);
			ITArray xipaiArray = DataBase.use().executeQueryByTArray(xipaisql);

			if (xipaiArray.size() == 0) {
				// 获得赠送
				todayReward = pump;
			} else {
				ITObject xipaiData = xipaiArray.getTObject(0);
				// 获得赠送
				todayReward = pump - xipaiData.getDouble("hp");

			}

		}
		if (gb.owner == tagId) {
			String rewardsql = String.format(
					" SELECT -sum(win_score+round_reward) reward FROM `team_round_stat_day` WHERE  stat_day >= %s AND stat_day < %s and uid!=%s and parent_uid=%s group by stat_day order by stat_day desc",
					DateUtils.getBeginDay() - 3600 * 48, DateUtils.getEndDay(), gb.owner, gb.owner);

			log.info("rewardsql:" + rewardsql+"---todayReward:"+todayReward);
			ITArray rewardArray = DataBase.use().executeQueryByTArray(rewardsql);
			if (rewardArray.size() != 0) {
				ITObject rewardData = rewardArray.getTObject(0);

				day_rewad = (rewardData.getDouble("reward").longValue());
				if (day_rewad < todayReward) {
					day_rewad = todayReward.longValue();
				}
			}
			if (rewardArray.size() > 1) {
				ITObject rewardData = rewardArray.getTObject(1);
				day_rewad_1 = (rewardData.getDouble("reward").longValue());
			}
			if (rewardArray.size() > 2) {
				ITObject rewardData = rewardArray.getTObject(2);
				day_rewad_2 = (rewardData.getDouble("reward").longValue());
			}
		} else {
			String rewardsql = String.format(
					"SELECT SUM(h.hp) reward FROM group_hp_log h WHERE h.time >= %s AND h.time < %s AND h.reason = 12 and gid=%s and uid=%s GROUP BY FROM_UNIXTIME(h.time, '%%Y-%%m-%%d') order by h.time desc",
					DateUtils.getBeginDay() - 3600 * 48, DateUtils.getEndDay(), groupId, tagId);
			log.info("rewardsql:" + rewardsql);
			ITArray rewardArray = DataBase.use().executeQueryByTArray(rewardsql);
			if (rewardArray.size() != 0) {
				ITObject rewardData = rewardArray.getTObject(0);
				day_rewad = (rewardData.getDouble("reward").longValue());
			}
			if (rewardArray.size() > 1) {
				ITObject rewardData = rewardArray.getTObject(1);
				day_rewad_1 = (rewardData.getDouble("reward").longValue());
			}
			if (rewardArray.size() > 2) {
				ITObject rewardData = rewardArray.getTObject(2);
				day_rewad_2 = (rewardData.getDouble("reward").longValue());
			}
		}

		ITObject resData = TObject.newInstance();
		resData.putLong("total_hp", _r_hp);
		// resData.putInt("bank_hp", _b_hp);
		resData.putLong("day_rewad", day_rewad);

		resData.putLong("day_rewad_1", day_rewad_1);
		resData.putLong("day_rewad_2", day_rewad_2);
		log.info("resData:" + resData);
		return resData;
	}

	public static final ITObject getBankInfo(int groupId, int uid, int tagId) throws Exception {
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			jedis10.close();
		}

		String mng_key = GroupMemberCache.genKey(groupId, tagId);
		// String rhp_key = String.format("g{%s}:m%s:reward_hp", groupId, tagId);
		// String r_hp = Redis.use("group1_db10").get(rhp_key);
		String r_hp = Redis.use("group1_db10").hget(mng_key, "hp");
		long _r_hp = 0;
		if (StringUtil.isNotEmpty(r_hp)) {
			_r_hp = Long.parseLong(r_hp);
		}
		String b_hp = Redis.use("group1_db10").hget(mng_key, "bank_hp");
		long _b_hp = 0;
		if (StringUtil.isNotEmpty(b_hp)) {
			_b_hp = Long.parseLong(b_hp);
		}

		int day_rewad = 0;
		Jedis jedis9 = Redis.use(redisdb).getJedis();
		try {
			String key = String.format("g%s:hp_reward:m%s", groupId, tagId);
			day_rewad = CountUtil.getCountLogByDay(key, jedis9);
		} finally {
			jedis9.close();
		}
		ITObject resData = TObject.newInstance();
		resData.putLong("total_hp", _r_hp);
		resData.putLong("bank_hp", _b_hp);
		// resData.putInt("day_rewad", day_rewad);
		return resData;
	}

	/**
	 * 提取体力值
	 * 
	 * @param groupId
	 * @param uid
	 * @param hp
	 * @return
	 * @throws Exception
	 */
	public static final ITObject takeHp(int groupId, int uid, int tagId, long hp) throws Exception {
		log.info("takeHp uid:" + uid + " tagId:" + tagId + " hp:" + hp / 1000);
		if (hp <= 0) {
			throw new WebException(ErrorCode._FAILED);
		}

		/*
		 * if (hp > Integer.MAX_VALUE) { throw new
		 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
		 */

		String mng_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(mng_key, jedis10);
		try {
			lock.lock();

			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			GroupMemberBean mng_bean = GroupCache.getMember(groupId, tagId);
			if (mng_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			String key = String.format("g{%s}:m%s:reward_hp", groupId, tagId);
			String r_hp = jedis10.get(key);
			if (StringUtil.isEmpty(r_hp)) {
				throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
			}

			if (Long.parseLong(r_hp) < hp) {
				throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
			}

			long tag_hp = Long.parseLong(jedis10.hget(mng_key, "hp"));
			/*
			 * if (tag_hp + hp > Integer.MAX_VALUE) { throw new
			 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
			 */

			if (GroupRoomService.checkRoom(groupId, tagId)) {
				throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
			}

			String lua = MainServer.lua_map.get("take_hp");
			Object obj = jedis10.evalsha(lua, Arrays.asList(key, mng_key), Arrays.asList((int) hp + ""));
			if (obj == null) {
				throw new WebException(ErrorCode._FAILED);
			}
			if (obj instanceof Long) {
				Long code = (Long) obj;
				if (code == 3) {
					throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
				}
			} else {
				ArrayList<Long> arr = (ArrayList<Long>) obj;
				int _r_hp = arr.get(0).intValue();
				int mhp = arr.get(1).intValue();
//				String sql = String.format("{call sp_take_reward_hp(%s,%s,%s,%s)}", groupId, tagId, (int) hp, mhp);
//				Utility.evtdb(groupId, 2, sql);
				String sql = String.format(
						"update group_member set reward_hp = reward_hp-%s,hp=hp+%s where uid = %s AND groupId = %s", hp,
						hp, tagId, groupId);
				Utility.evtdb(groupId, 1, sql);
				log.info("提取sql:" + sql);
				sql = String.format(
						"INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time) "
								+ "VALUES(%s,%s,%s,%s,%s,%s,'',%s)",
						groupId, tagId, ConsumeCode.HP_TAKE_REWARD, hp, _r_hp, 0, System.currentTimeMillis() / 1000);
				Utility.evtdb(groupId, 1, sql);

				ITObject resData = TObject.newInstance();
				resData.putInt("hp", mhp);
				resData.putLong("r_hp", _r_hp);
				return resData;
			}
			return null;
		} finally {
			lock.unlock();

		}
	}

	/**
	 * 提取体力值
	 *
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	public static final void groupTakeHp(int groupId, int uid, int tagId) throws Exception {
		log.info("groupTakeHp uid:" + uid + " tagId:" + tagId);
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			String mng_key = GroupMemberCache.genKey(groupId, uid);
			RedisLock lock1 = new RedisLock(mng_key, jedis10);
			try {
				lock1.lock();
				int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
				if (mgn_lev != 1) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			} finally {
				lock1.unlock(false);
			}

			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql;
			if (isPartner) {
				HashSet<Integer> hashSet = new HashSet<>();
				hashSet.add(tagId);
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				String key = String.format("g{%s}:m%s:reward_hp", groupId, tagId);
				String r_hp = jedis10.get(key);
				if (!StringUtil.isEmpty(r_hp)) {
					try {
						long allRewardHp = Long.parseLong(r_hp);
						GroupService.takeHp(groupId, uid, tagId, allRewardHp);
					} catch (Exception e) {

					}
				}

				String tag_gm_key = GroupMemberCache.genKey(groupId, tagId);
				String b_hp = jedis10.hget(tag_gm_key, "bank_hp");
				if (!StringUtil.isEmpty(b_hp)) {
					try {
						long allBankHp = Long.parseLong(b_hp);
						GroupService.takeBankHp(groupId, uid, tagId, allBankHp);
					} catch (Exception e) {

					}
				}

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);

						sql = String.format(
								"select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");

								if (hashSet.contains(childId))
									continue;

								String child_key = String.format("g{%s}:m%s:reward_hp", groupId, childId);
								String child_hp = jedis10.get(child_key);
								if (!StringUtil.isEmpty(child_hp)) {
									try {
										long allRewardHp = Long.parseLong(child_hp);
										GroupService.takeHp(groupId, uid, childId, allRewardHp);
									} catch (Exception e) {

									}
								}

								String child_tag_gm_key = GroupMemberCache.genKey(groupId, childId);
								String child_b_hp = jedis10.hget(child_tag_gm_key, "bank_hp");
								if (!StringUtil.isEmpty(child_b_hp)) {
									try {
										long allBankHp = Long.parseLong(child_b_hp);
										GroupService.takeBankHp(groupId, uid, childId, allBankHp);
									} catch (Exception e) {

									}
								}

								hashSet.add(childId);
								arrayList.add(childId);
							}
						}
					}
				}
			} else {
				String key = String.format("g{%s}:m%s:reward_hp", groupId, tagId);
				String r_hp = jedis10.get(key);
				if (!StringUtil.isEmpty(r_hp)) {
					try {
						long allRewardHp = Long.parseLong(r_hp);
						GroupService.takeHp(groupId, uid, tagId, allRewardHp);
					} catch (Exception e) {

					}
				}

				String tag_gm_key = GroupMemberCache.genKey(groupId, tagId);
				String b_hp = jedis10.hget(tag_gm_key, "bank_hp");
				if (!StringUtil.isEmpty(b_hp)) {
					try {
						long allBankHp = Long.parseLong(b_hp);
						GroupService.takeBankHp(groupId, uid, tagId, allBankHp);
					} catch (Exception e) {

					}
				}
			}
		} finally {
			lock.unlock();
		}
	}

	public static final ITObject takeBankHp(int groupId, int uid, int tagId, long hp) throws Exception {
		log.info("takeBankHp uid:" + uid + " tagId:" + tagId + " hp:" + hp / 100);
		if (hp <= 0) {
			throw new WebException(ErrorCode._FAILED);
		}
		/*
		 * if (hp > Integer.MAX_VALUE) { throw new
		 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
		 */
		String mng_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(mng_key, jedis10);
		try {
			lock.lock();

			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			GroupMemberBean mng_bean = GroupCache.getMember(groupId, tagId);
			if (mng_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

//			String r_hp = jedis10.hget(mng_key, "bank_hp");
			String banksql = String.format("SELECT bank_hp from group_member where uid=%s and groupId=%s", tagId,
					groupId);
//			log.info("rewardsql:" + rewardsql);
			ITArray rewardArray = DataBase.use().executeQueryByTArray(banksql);
			if (rewardArray.size() > 0) {
				ITObject userData = rewardArray.getTObject(0);
				long rewardHp = userData.getLong("bank_hp");
				if (rewardHp <= 0) {
					throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
				}
			} else {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			long tag_hp = Long.parseLong(jedis10.hget(mng_key, "hp"));
			/*
			 * if (tag_hp + hp > Integer.MAX_VALUE) { throw new
			 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
			 */

			if (GroupRoomService.checkRoom(groupId, tagId)) {
				throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
			}

			String lua = MainServer.lua_map.get("bank_hp");
			Object obj = jedis10.evalsha(lua, Arrays.asList(mng_key, mng_key, "bank_hp"),
					Arrays.asList((int) hp + "", "0"));
			if (obj == null) {
				throw new WebException(ErrorCode._FAILED);
			}
			if (obj instanceof Long) {
				Long code = (Long) obj;
				if (code == 3) {
					throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
				}
			} else {
				ArrayList<Long> arr = (ArrayList<Long>) obj;
				long mhp = arr.get(0).longValue();
				long b_hp = arr.get(1).longValue();
//				String sql = String.format("{call sp_bank_hp(%s,%s,%s,%s)}", groupId, tagId, -(int) hp, mhp);
//				Utility.evtdb(groupId, 2, sql);

				String sql = String.format(
						"update group_member set bank_hp = bank_hp-%s,hp=hp+%s where uid = %s AND groupId = %s", hp, hp,
						tagId, groupId);
				Utility.evtdb(groupId, 1, sql);
				log.info("取出sql:" + sql);
				sql = String.format(
						"INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time) "
								+ "VALUES(%s,%s,%s,%s,%s,%s,'',%s)",
						groupId, uid, ConsumeCode.HP_TAKE_BANK, -hp, b_hp, 0, System.currentTimeMillis() / 1000);
				Utility.evtdb(groupId, 1, sql);
				ITObject resData = TObject.newInstance();
				resData.putLong("hp", mhp);
				resData.putLong("b_hp", b_hp);
				return resData;
			}
			return null;
		} finally {
			lock.unlock();
		}
	}

	public static final ITObject saveBankHp(int groupId, int uid, int tagId, long hp) throws Exception {
		log.info("saveBankHp uid:" + uid + " tagId:" + tagId + " hp:" + hp / 100);
		if (hp <= 0) {
			throw new WebException(ErrorCode._FAILED);
		}

		/*
		 * if (hp > Integer.MAX_VALUE) { throw new
		 * WebException(ErrorCode.GROUP_PARTNER_HP_THAN_LIMIET); }
		 */

		String mng_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(mng_key, jedis10);
		try {
			lock.lock();

			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			GroupMemberBean mng_bean = GroupCache.getMember(groupId, tagId);
			if (mng_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			String cur_hp = jedis10.hget(mng_key, "hp");
			if (StringUtil.isEmpty(cur_hp)) {
				throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
			}

			if (Long.parseLong(cur_hp) < hp) {
				throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
			}

			if (GroupRoomService.checkRoom(groupId, tagId)) {
				throw new WebException(ErrorCode.GROUP_DONOT_SUB_HP);
			}

			String lua = MainServer.lua_map.get("bank_hp");
			Object obj = jedis10.evalsha(lua, Arrays.asList(mng_key, mng_key, "bank_hp"), Arrays.asList(hp + "", "1"));
			if (obj == null) {
				throw new WebException(ErrorCode._FAILED);
			}
			if (obj instanceof Long) {
				Long code = (Long) obj;
				if (code == 3) {
					throw new WebException(ErrorCode.GROUP_REWARD_NO_HP);
				}
			} else {
				ArrayList<Long> arr = (ArrayList<Long>) obj;
				long mhp = arr.get(0).longValue();
				long b_hp = arr.get(1).longValue();
//				String sql = String.format("{call sp_bank_hp(%s,%s,%s,%s)}", groupId, tagId, hp, mhp);
//				Utility.evtdb(groupId, 2, sql);

				String sql = String.format(
						"update group_member set bank_hp =bank_hp+ %s,hp=hp-%s where uid = %s AND groupId = %s", hp, hp,
						tagId, groupId);
				Utility.evtdb(groupId, 1, sql);

				sql = String.format(
						"INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time) "
								+ "VALUES(%s,%s,%s,%s,%s,%s,'',%s)",
						groupId, uid, ConsumeCode.HP_SAVE_BANK, hp, b_hp, 0, System.currentTimeMillis() / 1000);
				Utility.evtdb(groupId, 1, sql);

				ITObject resData = TObject.newInstance();
				resData.putLong("hp", mhp);
				resData.putLong("b_hp", b_hp);
				return resData;
			}
			return null;
		} finally {
			lock.unlock();
		}
	}

	/**
	 * 删除成员
	 * 
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	public static final void delMember(int groupId, int uid, int tagId) throws Exception {
		log.info("delMember uid:" + uid + " tagId" + tagId);
		String tag_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();

		int dellev = 3;
		RedisLock lock = null;
		try {
			lock = new RedisLock(tag_key, jedis10);
			lock.lock();
			GroupMemberBean tag_bean = GroupCache.getMember(groupId, tagId);
			if (tag_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			dellev = tag_bean.lev;

			GroupMemberBean mgn = GroupCache.getMember(groupId, uid);

			if (mgn == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			int mgn_lev = mgn.lev;
			int mgn_partner = mgn.partnerLev;
			if (mgn_lev == 3 && mgn_partner == 0) {
				throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
			}

			if (mgn_lev == 2) {
				if ((mgn.permission & PERMISSION_DEL_MEMBER) == 0) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			long hp = Long.parseLong(jedis10.hget(tag_key, "hp"));
			if (hp != 0) {
				throw new WebException(ErrorCode.GROUP_HP_NOT_0);
			}

			int tag_lev = tag_bean.lev;
			if (mgn_lev < 3 && tag_lev <= mgn_lev) {
				throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
			}
			if (mgn_lev == 3 && mgn_partner > 0) {

				GroupBean gb = GroupCache.getGroup(groupId);
				if ((gb.option & 1) == 0) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}

				int parentId = tag_bean.parentId;
				if (uid != parentId) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			boolean checkRoom = GroupRoomService.checkRoom(groupId, tagId);
			if (checkRoom) {
				throw new WebException(ErrorCode.GROUP_MEMBER_ROOM_EXIST);
			}
			int tag_partner = tag_bean.partnerLev;
			String rhp_key = String.format("g{%s}:m%s:reward_hp", groupId, tagId);
			String r_hp = jedis10.get(rhp_key);
			if (StringUtil.isNotEmpty(r_hp) && Long.parseLong(r_hp) > 0) {
				throw new WebException(ErrorCode.GROUP_REWARD_NO_TAKE);
			}

			String b_hp = jedis10.hget(tag_key, "bank_hp");
			if (StringUtil.isNotEmpty(b_hp) && Long.parseLong(b_hp) > 0) {
				throw new WebException(ErrorCode.GROUP_REWARD_NO_TAKE);
			}

			String sql = String.format("select count(1) count from group_member where groupId =%s and parentId=%s ",
					groupId, tagId);
			ITArray resultList = DataBase.use().executeQueryByTArray(sql);
			long count = resultList.getTObject(0).getLong("count");
			if (count > 0) {
				throw new WebException(ErrorCode.GROUP_PARTNER_MEMBERS);
			}

			String recSql = String.format(
					"select count(1) count from room_rec_log where gid =%s and uid=%s and time>%s", groupId, tagId,
					System.currentTimeMillis() / 1000 - 259200);
			ITArray recList = DataBase.use().executeQueryByTArray(recSql);
			long recCount = recList.getTObject(0).getLong("count");
			if (recCount > 0) {
				throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
			}

			List<Integer> list = Utility.getMemberParents(jedis10, groupId, tagId, true);
			if (list != null) {
				for (Integer par : list) {
					if (tag_partner > 0) {
						String pl_key = GroupCache.genParListKey(groupId, par);
						if (par == tagId) {
							jedis10.del(pl_key);
						} else {
							jedis10.srem(pl_key, tagId + "");
						}
					}
					String mlk = GroupCache.genMemberListKey(groupId, par);
					if (par == tagId) {
						jedis10.del(mlk);
					} else {
						jedis10.srem(mlk, tagId + "");
					}
				}
			}

			jedis10.hset(tag_key, "opt", "2");
			BaseCache.updateCacheVer(jedis10, tag_key);
			jedis10.expire(rhp_key, 10);
			Jedis jedis11 = Redis.use("group1_db11").getJedis();
			try {
				String g_key = GroupCache.genKey(groupId);
				jedis11.hincrBy(g_key, "gms", -1);
				BaseCache.updateCacheVer(jedis11, g_key);
				String key = GroupCache.genGroupsKey(tagId);
				jedis11.zrem(key, groupId + "");
				String gp_key = GroupCache.genPidsKey(groupId);
				Set<String> pids = jedis11.zrangeByScore(gp_key, 10, 11);
				for (String pid : pids) {
					String p_reward_key = GroupCache.genRewardKey(groupId, Integer.parseInt(pid));
					jedis10.zrem(p_reward_key, tagId + "");
					String p_xipai_reward_key = GroupCache.genXiPaiRewardKey(groupId, Integer.parseInt(pid));
					jedis10.zrem(p_xipai_reward_key, tagId + "");
				}
			} finally {
				jedis11.close();
			}
			sql = String.format("{call sp_del_member(%s,%s)}", groupId, tagId);
			Utility.evtdb(groupId, 2, sql);

			delMemberLog(groupId, uid, tagId, dellev);

		} finally {
			if (lock != null) {
				lock.unlock();
			}
		}
	}

	public static final void delMemberLog(int groupId, int uid, int tagId, int lev) throws Exception {
		AccountBean acc = AccountCache.getAccount(uid);
		int opid = acc.id;
		String opnick = acc.nick;

		AccountBean delacc = AccountCache.getAccount(tagId);
		int delid = delacc.id;
		String delnick = delacc.nick;
		String delPortrait = delacc.portrait;

		Jedis jedis9 = Redis.use(redisdb).getJedis();
		ITObject obj = new TObject();
		try {
			GroupLogService.getMemberData(jedis9, delid, obj, groupId, 1, 0, 0, true, true);

			int total_round = obj.getInt("total_round");
			long curTimee = System.currentTimeMillis() / 1000;

			// GroupMemberBean mgr_gmb = GroupCache.getMember(groupId, delid);
			String groupName = "队员";
			// if (mgr_gmb != null) {
			if (lev < 3) {
				groupName = "队长";
			} else {
				groupName = "队员";
			}
			// }

			ITObject userData = TObject.newInstance();
			userData.putInt("id", delid);
			userData.putUtfString("nick", delnick);
			userData.putInt("round", total_round);
			userData.putUtfString("group", groupName);
			userData.putInt("opid", opid);
			userData.putUtfString("opnick", opnick);
			userData.putLong("deltime", curTimee);
			userData.putUtfString("portrait", delPortrait);

			String str = userData.toJson();

			String gm_key = "kicklog_" + groupId + "_" + uid;
			jedis9.lpush(gm_key, str);

		} finally {
			jedis9.close();
		}

	}

	public static final ITArray getKickLog(int groupId, int id, int limit, int num) throws Exception {

		Jedis jedis9 = Redis.use(redisdb).getJedis();
		ITArray kickLogList = TArray.newInstance();

		try {
			String gm_key = "kicklog_" + groupId + "_" + id;
			List<String> logs = jedis9.lrange(gm_key, limit, limit + num);

			int i = 0;

			for (String _log : logs) {
				ITObject kickLog = TObject.newFromJsonData(_log);
				kickLogList.addTObject(kickLog);

			}
		} finally {
			jedis9.close();
		}

		return kickLogList;
	}

	/**
	 * 申请加入列表
	 * 
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static final ITArray getGroupJoins(int groupId) throws Exception {
		String joins_key = GroupCache.genJoinsKey(groupId);

		Set<String> uids = Redis.use("group1_db11").smembers(joins_key);
		ITArray array = TArray.newInstance();
		for (String tem : uids) {
			AccountBean acc = AccountCache.getAccount(Integer.parseInt(tem));
			ITObject obj = TObject.newInstance();
			obj.putInt("id", acc.id);
			obj.putString("nick", acc.nick);
			obj.putString("portrait", acc.portrait);
			array.addTObject(obj);
		}
		return array;
	}

	/**
	 * 获取整线体力值
	 *
	 * @param groupId
	 * @param uid
	 * @param tagId
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getPersonHpTotal(int groupId, int uid, int tagId) throws Exception {
		String mng_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(mng_key, jedis10);
		try {
			lock.lock();

			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			return getHpTotal(groupId, tagId, 30);
		} finally {
			lock.unlock();
		}
	}

	public static final boolean checkParentsHpTotal(int groupId, int pid, int cacheTime) throws Exception {

		return false;
	}

	/**
	 * 获取整线体力值
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getHpTotal(int groupId, int uid, int cacheTime) throws Exception {
		GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
		if (gmb == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}
		if (gmb.partnerLev == 0) {
			throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
		}

		return getAllHpCache(groupId, uid, cacheTime);
		/*
		 * long allHp = 0; long allOtherHp = 0; HashSet<Integer> childrenList =
		 * getMemberAllChildren(groupId, uid); for(Integer tagId : childrenList) {
		 * String gm_key1 = GroupMemberCache.genKey(groupId, tagId); String strHp =
		 * Redis.use("group1_db10").hget(gm_key1, "hp"); if
		 * (StringUtil.isNotEmpty(strHp)) { try { allHp += Integer.parseInt(strHp); }
		 * catch (Exception e) { } }
		 * 
		 * String temKey = String.format("g{%s}:m%s:reward_hp", groupId, tagId); String
		 * strtempValue = Redis.use("group1_db10").get(temKey); if
		 * (StringUtil.isNotEmpty(strtempValue)) { try { allOtherHp +=
		 * Integer.parseInt(strtempValue); } catch (Exception e) { } }
		 * 
		 * String mng_key = GroupMemberCache.genKey(groupId, tagId); String r_hp =
		 * Redis.use("group1_db10").hget(mng_key, "bank_hp"); if
		 * (StringUtil.isNotEmpty(r_hp)) { try { allOtherHp += Integer.parseInt(r_hp); }
		 * catch (Exception e) { } } }
		 * 
		 * ITObject obj1 = TObject.newInstance(); obj1.putLong("hp", allHp +
		 * allOtherHp); obj1.putLong("total_member", childrenList.size()); return obj1;
		 */
	}

	/**
	 * 获取成员列表
	 * 
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num     1 多到少 2少到多
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getMembers(int groupId, int uid, int limit, int num) throws Exception {
		ITObject obj1 = TObject.newInstance();

		String sql = "SELECT uid,hp,join_time,lev,parentId,partnerLev,ban ";
		// String order_sql = "ORDER BY hp DESC,join_time ASC";
		String order_sql = "ORDER BY lev ASC, join_time DESC";

		GroupBean gb = GroupCache.getGroup(groupId);

		GroupMemberBean mgr_gmb = GroupCache.getMember(groupId, uid);

		boolean mgr = mgr_gmb.lev < 3;

		String where = "FROM group_member WHERE groupId = " + groupId;
		String count_where = where;
		if (!mgr) {
//			String p = Utility.getChildParentSql(groupId, uid, true);
			if (limit > 0) {
				limit = limit - 1;
			}
			count_where += String.format(" AND (parentId=%s or uid=%s)", uid, uid);
			where += String.format(" and partnerLev=0 AND parentId=%s", uid, uid);
		}
		String limit_sql = String.format("limit %s,%s", limit, num);
		sql = String.format("%s %s %s %s", sql, where, order_sql, limit_sql);
		if (limit == 0) {
			String count_sql = String.format("SELECT (SELECT COUNT(1) %s) num,(SELECT IFNULL(SUM(hp),0) %s) t_hp1",
					count_where, count_where);
			ITArray arr = DataBase.use().executeQueryByTArray(count_sql);
			if (arr.size() > 0) {
				ITObject obj = arr.getTObject(0);
				obj1.putLong("member_num", obj.getLong("num"));
				obj1.putLong("hp_num1", obj.getDouble("t_hp1").longValue());
				obj1.putLong("hp_num2", 0);
			} else {
				obj1.putLong("member_num", 0);
				obj1.putLong("hp_num1", 0);
				obj1.putLong("hp_num2", 0);
			}
		}

		ITArray arr = TArray.newInstance();
		if (!mgr && limit == 0) {
			ITObject obj = TObject.newInstance();
			obj.putInt("uid", uid);
			obj.putInt("join_time", mgr_gmb.join_time);
			obj.putInt("last_time", mgr_gmb.last_time);
			obj.putInt("permission", mgr_gmb.permission);
			obj.putInt("parentId", mgr_gmb.parentId);
			AccountBean acc = AccountCache.getAccount(uid);
			obj.putString("nick", acc.nick);
			obj.putString("portrait", acc.portrait);

			if (mgr_gmb.parentId > 0) {
				AccountBean parent_acc = AccountCache.getAccount(mgr_gmb.parentId);
				if (parent_acc != null) {
					obj.putString("parentId_nick", parent_acc.nick);
				} else {
					String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'", mgr_gmb.parentId);
					ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
					if (resultArray.size() == 0) {
						obj.putString("parentId_nick", StringUtil.Empty);
					} else {
						ITObject userData = resultArray.getTObject(0);
						obj.putString("parentId_nick", userData.getUtfString("nick"));
					}
				}
			} else {
				obj.putString("parentId_nick", StringUtil.Empty);
			}

			obj.putInt("partnerLev", mgr_gmb.partnerLev);
			obj.putInt("ban", mgr_gmb.ban);

			obj.putInt("lev", 3);

			String ugm_key = GroupMemberCache.genKey(groupId, uid);
			String hp = Redis.use("group1_db10").hget(ugm_key, "hp");
			obj.putLong("hp", Long.parseLong(hp));

			arr.addTObject(obj);
		}
		ITArray arr1 = DataBase.use().executeQueryByTArray(sql);
		if (arr1.size() > 0) {
			for (int i = 0; i < arr1.size(); ++i) {
				ITObject obj = arr1.getTObject(i);
				AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
				if (acc != null) {
					obj.putString("nick", acc.nick);
					obj.putString("portrait", acc.portrait);
				} else {
					obj.putString("nick", StringUtil.Empty);
					obj.putString("portrait", StringUtil.Empty);
				}

				if (obj.getInt("parentId") > 0) {
					AccountBean parent_acc = AccountCache.getAccount(obj.getInt("parentId"));
					if (parent_acc != null) {
						obj.putString("parentId_nick", parent_acc.nick);
					} else {
						String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'", obj.getInt("parentId"));
						ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
						if (resultArray.size() == 0) {
							obj.putString("parentId_nick", StringUtil.Empty);
						} else {
							ITObject userData = resultArray.getTObject(0);
							obj.putString("parentId_nick", userData.getUtfString("nick"));
						}
					}
				} else {
					if (mgr_gmb.lev == 1 && uid != obj.getInt("uid")) {
						AccountBean uid_acc = AccountCache.getAccount(uid);
						obj.putInt("parentId", uid);
						obj.putString("parentId_nick", uid_acc.nick);
					} else {
						obj.putString("parentId_nick", StringUtil.Empty);
					}
				}

				GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
				if (gmb == null) {
					arr1.del(i);
					i--;
					continue;
				}
				obj.putInt("last_time", gmb.last_time);
				obj.putInt("permission", gmb.permission);
				obj.putInt("score", gmb.score);

				String ugm_key = GroupMemberCache.genKey(groupId, acc.id);
				String group_ban = Redis.use("group1_db10").hget(ugm_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					obj.putInt("group_ban", 1);
				} else {
					obj.putInt("group_ban", 0);
				}

				arr.addTObject(obj);
			}
		}
		obj1.putTArray("members", arr);
		obj1.putInt("limit", limit);
		return obj1;
	}

	/**
	 * 获取合伙人所有下级成员
	 * 
	 * @param groupId
	 * @param tagId
	 * @return
	 */
	public static HashSet<Integer> getMemberAllChildren(int groupId, int tagId) {
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		HashSet<Integer> hashSet = new HashSet<>();
		hashSet.add(tagId);
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql;
			if (isPartner) {
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);

						sql = String.format(
								"select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");

								if (hashSet.contains(childId))
									continue;

								hashSet.add(childId);
								arrayList.add(childId);
							}
						}
					}
				}
			}
		} catch (Exception e) {

		} finally {
			lock.unlock();
		}

		return hashSet;
	}

	public static String getMemberAllChildren2(int groupId, int tagId) {
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		HashSet<Integer> hashSet = new HashSet<>();
		hashSet.add(tagId);
		String list = "";
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql;
			if (isPartner) {
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);

						sql = String.format(
								"select uid,partnerLev from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");
								int partnerLev = tem.getInt("partnerLev");

								if (hashSet.contains(childId))
									continue;

								hashSet.add(childId);

								if (partnerLev > 0)
									arrayList.add(childId);
							}
						}
					}
				}
			}
		} catch (Exception e) {

		} finally {
		}

		list += "" + tagId;
		hashSet.remove(tagId);
		for (Integer iid : hashSet) {
			list += "," + iid;
		}
		return list;
	}

	public static String getMemberAllChildren3(int groupId, int tagId) {
		HashSet<Integer> hashSet = new HashSet<>();
		hashSet.add(tagId);
		String list = "";
		try {
			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql = "";
			if (isPartner) {
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				while (!arrayList.isEmpty()) {
					String strParentId = "" + arrayList.get(0);
					for (int i = 1; i < arrayList.size(); i++) {
						strParentId += "," + arrayList.get(i);
					}
					arrayList.clear();

					sql = String.format(
							"select uid,partnerLev from group_member where groupId = %s and (parentId in(%s))", groupId,
							strParentId);

					ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
					if (tmplist.size() > 0) {
						for (int j = 0; j < tmplist.size(); ++j) {

							ITObject tem = tmplist.getTObject(j);
							int childId = tem.getInt("uid");
							int partnerLev = tem.getInt("partnerLev");

							if (hashSet.contains(childId))
								continue;

							hashSet.add(childId);

							if (partnerLev > 0)
								arrayList.add(childId);
						}
					}
				}
			}
		} catch (Exception e) {

		} finally {
		}

		list += "" + tagId;
		hashSet.remove(tagId);
		for (Integer iid : hashSet) {
			list += "," + iid;
		}
		return list;
	}

	public static final ITObject getAllplay(int groupId, int uid) throws Exception {
		ITArray games = new TArray();
		ITArray togames = new TArray();
		Jedis jedis1 = Redis.use("group1_db1").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		ITObject obj1 = TObject.newInstance();
		try {
			Set<String> list = jedis1.zrevrangeByScore("online_games", 1000, 1);
			for (String game : list) {
				int gameId = Integer.parseInt(game);
				GameBean gb = GameCache.getGame(gameId);
				if (gb == null)
					continue;
				ITObject gameObj = gb.getTObject();

				for (Map.Entry<String, Integer> entry : gb.pay.entrySet()) {
					gameObj.putInt(entry.getKey(), entry.getValue());
				}
				games.addTObject(gameObj);
			}

			String gm_key = GroupMemberCache.genKey(groupId, uid);
			String gameids = jedis10.hget(gm_key, "games");
			if (StringUtil.isNotEmpty(gameids)) {
				String[] tmp = gameids.split(",");
				for (String gtmp : tmp) {
					togames.addString(gtmp);
				}

			}
			obj1.putTArray("togames", togames);
		} finally {
			jedis1.close();
			jedis10.close();
		}

		obj1.putTArray("games", games);
		return obj1;
	}

	public static final void setBanplay(int groupId, int uid, String bans) throws Exception {
		GroupMemberBean tag_bean = GroupCache.getMember(groupId, uid);
		if (tag_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
		if (mng_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String gm_key = GroupMemberCache.genKey(groupId, uid);
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {

			jedis10.hset(tag_bean.redis_key, "games", bans);
			BaseCache.updateCacheVer(jedis10, GroupMemberCache.genKey(groupId, uid));

			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
			if (gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql;
			// String _ban = ban + "";
			if (isPartner) {
				HashSet<Integer> hashSet = new HashSet<>();
				hashSet.add(uid);
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(uid);

				gm_key = GroupMemberCache.genKey(groupId, uid);
				BaseCache.updateCacheVer(jedis10, gm_key);

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);
						sql = String.format(
								"select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");

								if (hashSet.contains(childId))
									continue;
								gm_key = GroupMemberCache.genKey(groupId, childId);
								jedis10.hset(gm_key, "games", bans);
								BaseCache.updateCacheVer(jedis10, gm_key);

								hashSet.add(childId);
								arrayList.add(childId);
							}
						}
					}
				}
			} else {
				jedis10.hset(gm_key, "games", bans);
				BaseCache.updateCacheVer(jedis10, gm_key);
			}
		} finally {
			lock.unlock();
		}

	}

	/**
	 *
	 * @param groupId
	 * @param uid     查询用户id
	 * @param tagId   目标id
	 * @param online
	 * @param type
	 * @param limit
	 * @param num
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getMembers_child(int groupId, int uid, int tagId, int online, int type, int limit,
			int num) throws Exception {

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			jedis10.close();
		}

		ITObject obj1 = TObject.newInstance();

		String sql = "SELECT uid,hp,join_time,lev,parentId,partnerLev,ban ";
		String order_sql = "ORDER BY lev ASC,partnerLev DESC,hp DESC,uid ASC";

		String where = "FROM group_member WHERE groupId = " + groupId;

		long allOtherHp = 0;
		long allNum = 0;

		if (limit > 0) {
			limit = limit - 1;
		}
		where += String.format("  AND parentId=%s", tagId);

		String limit_sql = String.format("limit %s,%s", limit, num);
		sql = String.format("%s %s %s %s", sql, where, order_sql, limit_sql);
		log.info("sql:" + sql);

		ITArray arr = TArray.newInstance();

		ITArray arr1 = DataBase.use().executeQueryByTArray(sql);
		if (arr1.size() > 0) {
			for (int i = 0; i < arr1.size(); ++i) {
				ITObject obj = arr1.getTObject(i);
				AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
				if (acc == null) {
					continue;
				}
				if (limit == 0 && acc.id == tagId) {
					continue;
				}
				if (acc != null) {
					obj.putString("nick", acc.nick);
					obj.putString("portrait", acc.portrait);
				} else {
					obj.putString("nick", StringUtil.Empty);
					obj.putString("portrait", StringUtil.Empty);
				}

				if (obj.getInt("parentId") > 0) {
					AccountBean parent_acc = AccountCache.getAccount(obj.getInt("parentId"));
					if (parent_acc != null) {
						obj.putString("parentId_nick", parent_acc.nick);
					} else {
						String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'", obj.getInt("parentId"));
						ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
						if (resultArray.size() == 0) {
							obj.putString("parentId_nick", StringUtil.Empty);
						} else {
							ITObject userData = resultArray.getTObject(0);
							obj.putString("parentId_nick", userData.getUtfString("nick"));
						}
					}
				} else {

					if (obj.getInt("lev") <= 2 && tagId != obj.getInt("uid")) {
						AccountBean uid_acc = AccountCache.getAccount(tagId);
						obj.putInt("parentId", tagId);
						obj.putString("parentId_nick", uid_acc.nick);

						log.info("litte mgr uid:" + obj.getInt("uid"));
					} else if (obj.getInt("lev") == 3 && tagId != obj.getInt("uid")) {
						AccountBean uid_acc = AccountCache.getAccount(tagId);
						obj.putInt("parentId", tagId);
						obj.putString("parentId_nick", uid_acc.nick);

						log.info("aa litte mgr uid:" + obj.getInt("uid"));
					}

				}

				GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
				if (gmb == null) {
					arr1.del(i);
					i--;
					continue;
				}
				obj.putInt("last_time", gmb.last_time);
				obj.putInt("permission", gmb.permission);
				obj.putInt("score", gmb.score);
				obj.putInt("queueid", gmb.queueid);
				obj.putInt("seeid", gmb.seeid);

				// --调控
				Jedis jedis1b = Redis.use("group1_db1").getJedis();
				try {
					String diaokongid = jedis1b.hget("gods_value", Integer.toString(acc.id));
					if (StringUtil.isNotEmpty(diaokongid)) {
						obj.putInt("diaokongid", Integer.parseInt(diaokongid));
					} else {
						obj.putInt("diaokongid", -1);
					}
				} finally {
					jedis1b.close();
				}

				String ugm_key = GroupMemberCache.genKey(groupId, acc.id);
				String group_ban = Redis.use("group1_db10").hget(ugm_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					obj.putInt("group_ban", 1);
				} else {
					obj.putInt("group_ban", 0);
				}

				obj.putInt("online", 1);

				Jedis jedis9 = Redis.use(redisdb).getJedis();
				try {
					int endtime = DateUtils.getBeginDay() + 86400;
					int begintime = endtime - 86400 * 1;
					GroupLogService.getMemberData(jedis9, acc.id, obj, groupId, 0, begintime, endtime, false, false);
				} finally {
					jedis9.close();
				}

				arr.addTObject(obj);
			}
		}

		log.info("arr = " + arr);

		obj1.putLong("otherHp", allOtherHp);
		obj1.putLong("member_num", 0);
		obj1.putLong("hp_num1", 0);
		obj1.putLong("hp_num2", 0);
		obj1.putTArray("members", arr);
		obj1.putInt("limit", limit);

		return obj1;
	}

	/**
	 * 获取成员列表
	 *
	 * @param groupId
	 * @param tagId
	 * @param limit
	 * @param num
	 * @param minus_only
	 * @param type       1 多到少 2少到多
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getMembers_1(int groupId, int uid, int tagId, int online, int type, int limit, int num)
			throws Exception {
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && uid != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			jedis10.close();
		}

		ITObject obj1 = TObject.newInstance();

		String sql = "SELECT uid,hp,join_time,lev,parentId,partnerLev,ban ";
		String order_sql = "ORDER BY lev ASC,partnerLev DESC,hp DESC,uid ASC";
//		String order_sql = "ORDER BY lev ASC,partnerLev DESC,hp DESC";

		GroupBean gb = GroupCache.getGroup(groupId);

		GroupMemberBean mgr_gmb = GroupCache.getMember(groupId, tagId);

		boolean mgr = mgr_gmb.lev < 3;

		String where = "FROM group_member WHERE groupId = " + groupId;
		long allOtherHp = 0;
		long allNum = 0;
		if (!mgr) {
//			String p = Utility.getChildParentSql(groupId, uid, true);
			if (limit > 0) {
				limit = limit - 1;
			}
			where += String.format("  AND parentId=%s", tagId);
		} else {
			if (type == 1) {
				GroupBean groupBean = GroupCache.getGroup(groupId);

				where += String.format("  AND (parentId=%s OR parentId = 0)", groupBean.owner);
			}
		}

		String limit_sql = String.format("limit %s,%s", limit, num);
		sql = String.format("%s %s %s %s", sql, where, order_sql, limit_sql);

		ITArray arr = TArray.newInstance();
		if (limit == 0) {
			ITObject obj = TObject.newInstance();
			obj.putInt("uid", tagId);
			obj.putInt("join_time", mgr_gmb.join_time);
			obj.putInt("last_time", mgr_gmb.last_time);
			obj.putInt("permission", mgr_gmb.permission);
			obj.putInt("parentId", mgr_gmb.parentId);
			AccountBean acc = AccountCache.getAccount(tagId);
			obj.putString("nick", acc.nick);
			obj.putString("portrait", acc.portrait);
			obj.putInt("online", 1);

			if (mgr_gmb.parentId > 0) {
				AccountBean parent_acc = AccountCache.getAccount(mgr_gmb.parentId);
				if (parent_acc != null) {
					obj.putString("parentId_nick", parent_acc.nick);
				} else {
					String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'", mgr_gmb.parentId);
					ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
					if (resultArray.size() == 0) {
						obj.putString("parentId_nick", StringUtil.Empty);
					} else {
						ITObject userData = resultArray.getTObject(0);
						obj.putString("parentId_nick", userData.getUtfString("nick"));
					}
				}
			} else {
				obj.putString("parentId_nick", StringUtil.Empty);
			}

			Jedis jedis9 = Redis.use(redisdb).getJedis();
			try {
				int endtime = DateUtils.getBeginDay() + 86400;
				int begintime = endtime - 86400 * 1;
				GroupLogService.getMemberData(jedis9, acc.id, obj, groupId, 0, begintime, endtime, false, false);
			} finally {
				jedis9.close();
			}

			obj.putInt("partnerLev", mgr_gmb.partnerLev);
			obj.putInt("ban", mgr_gmb.ban);

			obj.putInt("lev", mgr_gmb.lev);

			String ugm_key = GroupMemberCache.genKey(groupId, tagId);
			String hp = Redis.use("group1_db10").hget(ugm_key, "hp");
			obj.putLong("hp", Long.parseLong(hp));

			arr.addTObject(obj);
		}
		log.info("sql:" + sql);
		ITArray arr1 = DataBase.use().executeQueryByTArray(sql);
		if (arr1.size() > 0) {
			for (int i = 0; i < arr1.size(); ++i) {
				ITObject obj = arr1.getTObject(i);
				AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
				if (acc == null) {
					continue;
				}
				if (limit == 0 && acc.id == tagId) {
					continue;
				}
				if (acc != null) {
					obj.putString("nick", acc.nick);
					obj.putString("portrait", acc.portrait);
				} else {
					obj.putString("nick", StringUtil.Empty);
					obj.putString("portrait", StringUtil.Empty);
				}

				if (obj.getInt("parentId") > 0) {
					AccountBean parent_acc = AccountCache.getAccount(obj.getInt("parentId"));
					if (parent_acc != null) {
						obj.putString("parentId_nick", parent_acc.nick);
					} else {
						String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'", obj.getInt("parentId"));
						ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
						if (resultArray.size() == 0) {
							obj.putString("parentId_nick", StringUtil.Empty);
						} else {
							ITObject userData = resultArray.getTObject(0);
							obj.putString("parentId_nick", userData.getUtfString("nick"));
						}
					}
				} else {
					if (mgr_gmb.lev < 3) {
						if (obj.getInt("lev") <= 2 && tagId != obj.getInt("uid")) {
							AccountBean uid_acc = AccountCache.getAccount(tagId);
							obj.putInt("parentId", tagId);
							obj.putString("parentId_nick", uid_acc.nick);

							log.info("litte mgr uid:" + obj.getInt("uid"));
						} else if (obj.getInt("lev") == 3 && tagId != obj.getInt("uid")) {
							AccountBean uid_acc = AccountCache.getAccount(tagId);
							obj.putInt("parentId", tagId);
							obj.putString("parentId_nick", uid_acc.nick);

							log.info("aa litte mgr uid:" + obj.getInt("uid"));
						}
					}
				}

				GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
				if (gmb == null) {
					arr1.del(i);
					i--;
					continue;
				}
				obj.putInt("last_time", gmb.last_time);
				obj.putInt("permission", gmb.permission);
				obj.putInt("score", gmb.score);
				obj.putInt("queueid", gmb.queueid);
				obj.putInt("seeid", gmb.seeid);

				// --调控
				Jedis jedis1b = Redis.use("group1_db1").getJedis();
				try {
					String diaokongid = jedis1b.hget("gods_value", Integer.toString(acc.id));
					if (StringUtil.isNotEmpty(diaokongid)) {
						obj.putInt("diaokongid", Integer.parseInt(diaokongid));
					} else {
						obj.putInt("diaokongid", -1);
					}
				} finally {
					jedis1b.close();
				}

				String ugm_key = GroupMemberCache.genKey(groupId, acc.id);
				String hp = Redis.use("group1_db10").hget(ugm_key, "hp");
				obj.putLong("hp", Long.parseLong(hp));

				String group_ban = Redis.use("group1_db10").hget(ugm_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					obj.putInt("group_ban", 1);
				} else {
					obj.putInt("group_ban", 0);
				}

				obj.putInt("online", 1);

				Jedis jedis9 = Redis.use(redisdb).getJedis();
				try {
					int endtime = DateUtils.getBeginDay() + 86400;
					int begintime = endtime - 86400 * 1;
					GroupLogService.getMemberData(jedis9, acc.id, obj, groupId, 0, begintime, endtime, false, false);
				} finally {
					jedis9.close();
				}

				arr.addTObject(obj);
			}
		}

		// log.info("arr = "+ arr);

		obj1.putLong("otherHp", allOtherHp);
		obj1.putLong("member_num", 0);
		obj1.putLong("hp_num1", 0);
		obj1.putLong("hp_num2", 0);
		obj1.putTArray("members", arr);
		obj1.putInt("limit", limit);

		return obj1;
	}

	public static final ITObject getAllHpCache(int groupId, int uid, int cache_time) throws Exception {
		String gm_key = GroupMemberCache.genKey(groupId, uid);

		String all_temp_hp_key = gm_key + ":all_temp_hp";
		String all_temp_member_key = gm_key + ":all_temp_total_member";
		String all_temp_hp = Redis.use("group1_db10").get(all_temp_hp_key);
		String all_temp_member = Redis.use("group1_db10").get(all_temp_member_key);
		ITObject obj1 = TObject.newInstance();
		if (all_temp_hp == null || StringUtil.isEmpty(all_temp_hp) || all_temp_member == null
				|| StringUtil.isEmpty(all_temp_member)) {
			obj1.putLong("hp", 0);
			obj1.putLong("total_member", 0);

			/*
			 * String child_sql =
			 * String.format("{? = call selectPartnerChildHpByParentId(%s,%s)}", uid,
			 * groupId); String child_key =
			 * String.format("selectPartnerChildHpByParentId(%s,%s)", uid, groupId); ITArray
			 * temparr = new TArray(); try { temparr = DataBase.use().executeCall(child_sql,
			 * true); }catch (Exception e) { e.printStackTrace(); }
			 * 
			 * if (temparr.size() > 0) { TDataWrapper data = temparr.get(0); TObject map =
			 * (TObject)data.getObject(); String key_value = map.getString(child_key);
			 * String[] split = key_value.split(","); if (split.length == 2) { long _allHp =
			 * Long.parseLong(split[0]); long _allNum = Long.parseLong(split[1]);
			 * 
			 * obj1.putLong("hp", _allHp); obj1.putLong("total_member", _allNum);
			 * 
			 * Redis.use("group1_db10").set(all_temp_hp_key, ""+_allHp);
			 * Redis.use("group1_db10").expire(all_temp_hp_key, cache_time);
			 * Redis.use("group1_db10").set(all_temp_member_key, ""+_allNum);
			 * Redis.use("group1_db10").expire(all_temp_member_key, cache_time); } }
			 */

			String sql = "SELECT uid ";
			String where = "FROM group_member WHERE groupId = " + groupId;
			String count_where = where;
			String childSql = getMemberAllChildren3(groupId, uid);
			count_where = String.format(" %s and uid in (%s)", where, childSql);
			String count_sql = String.format(
					"SELECT (SELECT COUNT(1) %s) num,(SELECT IFNULL(SUM(hp+bank_hp+reward_hp),0) %s) t_hp1",
					count_where, count_where);
			log.info("lingmeng getAllHp count_sql");
			ITArray arr = DataBase.use().executeQueryByTArray(count_sql);
			if (arr.size() > 0) {
				ITObject obj = arr.getTObject(0);

//				long _allHp = obj.getDouble("t_hp1").longValue();
				long _allNum = obj.getLong("num");
//				g{618028}:m179357
//				obj1.putLong("hp", _allHp);
				String uidKey = "g{" + groupId + "}:m" + uid;
				long _allHp = obj.getDouble("t_hp1").longValue();

				obj1.putLong("hp", _allHp);
				obj1.putLong("total_member", _allNum);
				Redis.use("group1_db10").set(all_temp_hp_key, "" + _allHp);
				Redis.use("group1_db10").expire(all_temp_hp_key, cache_time);
				Redis.use("group1_db10").set(all_temp_member_key, "" + _allNum);
				Redis.use("group1_db10").expire(all_temp_member_key, cache_time);
			} else {
				obj1.putLong("hp", 0);
				obj1.putLong("total_member", 0);
			}
		} else {

			obj1.putLong("hp", Long.parseLong(all_temp_hp));
			obj1.putLong("total_member", Long.parseLong(all_temp_member));
		}

		return obj1;
	}

	/**
	 * 获取成员列表
	 *
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num
	 * @param minus_only
	 * @param type       1 多到少 2少到多
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getMemberCount(int groupId, int uid) throws Exception {
		ITObject obj1 = TObject.newInstance();

		String sql = "SELECT uid ";

		GroupMemberBean mgr_gmb = GroupCache.getMember(groupId, uid);

		boolean mgr = mgr_gmb.lev < 3;

		String where = "FROM group_member WHERE groupId = " + groupId;
		String count_where = where;
		long allOtherHp = 0;
		long allHp = 0;
		if (!mgr) {
			obj1.putLong("member_num", 0);
			obj1.putLong("hp_num1", 0);
			obj1.putLong("hp_num2", 0);

			ITObject obj2 = getAllHpCache(groupId, uid, 30);
			obj1.putLong("member_num", obj2.getLong("total_member"));
			obj1.putLong("hp_num1", obj2.getLong("hp"));
			obj1.putLong("hp_num2", 0);

			/*
			 * HashSet<Integer> childrenList = getMemberAllChildren(groupId, uid);
			 * for(Integer tagId : childrenList) { String gm_key1 =
			 * GroupMemberCache.genKey(groupId, tagId); String strHp =
			 * Redis.use("group1_db10").hget(gm_key1, "hp"); if
			 * (StringUtil.isNotEmpty(strHp)) { try { allHp += Integer.parseInt(strHp); }
			 * catch (Exception e) { } }
			 * 
			 * String temKey = String.format("g{%s}:m%s:reward_hp", groupId, tagId); String
			 * strtempValue = Redis.use("group1_db10").get(temKey); if
			 * (StringUtil.isNotEmpty(strtempValue)) { try { allOtherHp +=
			 * Integer.parseInt(strtempValue); } catch (Exception e) { } }
			 * 
			 * String mng_key = GroupMemberCache.genKey(groupId, tagId); String r_hp =
			 * Redis.use("group1_db10").hget(mng_key, "bank_hp"); if
			 * (StringUtil.isNotEmpty(r_hp)) { try { allOtherHp += Integer.parseInt(r_hp); }
			 * catch (Exception e) { } } } obj1.putLong("member_num", childrenList.size());
			 * obj1.putLong("hp_num1", allHp); obj1.putLong("hp_num2", 0);
			 */
		} else {
			/*
			 * sql = String.format("%s %s", sql, where); String count_sql = String.
			 * format("SELECT (SELECT COUNT(1) %s) num,(SELECT IFNULL(SUM(hp+reward_hp+bank_hp),0) %s) t_hp1"
			 * , count_where, count_where); ITArray arr =
			 * DataBase.use().executeQueryByTArray(count_sql); if (arr.size() > 0) {
			 * ITObject obj = arr.getTObject(0); allHp = obj.getDouble("t_hp1").longValue();
			 * obj1.putLong("member_num", obj.getLong("num")); obj1.putLong("hp_num1",
			 * obj.getDouble("t_hp1").longValue()); obj1.putLong("hp_num2", 0); } else {
			 * obj1.putLong("member_num", 0); obj1.putLong("hp_num1", 0);
			 * obj1.putLong("hp_num2", 0); }
			 */
			sql = String.format("%s %s", sql, where);
			String count_sql = String.format(
					"SELECT (SELECT COUNT(1) %s) num,(SELECT IFNULL(SUM(hp+bank_hp+reward_hp),0) %s) t_hp1",
					count_where, count_where);
			ITArray arr = DataBase.use().executeQueryByTArray(count_sql);
			if (arr.size() > 0) {
				ITObject obj = arr.getTObject(0);
				allHp = obj.getDouble("t_hp1").longValue();
				obj1.putLong("member_num", obj.getLong("num"));
				obj1.putLong("hp_num1", obj.getDouble("t_hp1").longValue());
				obj1.putLong("hp_num2", 0);
			} else {
				obj1.putLong("member_num", 0);
				obj1.putLong("hp_num1", 0);
				obj1.putLong("hp_num2", 0);
			}
			/*
			 * ITArray arr1 = DataBase.use().executeQueryByTArray(sql); if (arr1.size() > 0)
			 * { for (int i = 0; i < arr1.size(); ++i) { ITObject obj = arr1.getTObject(i);
			 * int tagId = obj.getInt("uid");
			 * 
			 * String temKey = String.format("g{%s}:m%s:reward_hp", groupId, tagId); String
			 * strtempValue = Redis.use("group1_db10").get(temKey); if
			 * (StringUtil.isNotEmpty(strtempValue)) { try { allOtherHp +=
			 * Integer.parseInt(strtempValue); } catch (Exception e) { } }
			 * 
			 * 
			 * String mng_key = GroupMemberCache.genKey(groupId, tagId); String r_hp =
			 * Redis.use("group1_db10").hget(mng_key, "bank_hp"); if
			 * (StringUtil.isNotEmpty(r_hp)) { try { allOtherHp += Integer.parseInt(r_hp); }
			 * catch (Exception e) { } } } }
			 */
		}

		// log.info("arr = "+ arr);

		obj1.putLong("otherHp", allOtherHp);

		if (mgr_gmb.lev == 1) {
			log.info("otherHp = " + allOtherHp);
			log.info("allHp = " + allHp);
		}

		return obj1;
	}

	/**
	 * 查询成员
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITObject findMember(int groupId, int uid, int queryId, String tagName) throws Exception {

		ITObject obj = TObject.newInstance();
		ITArray array = TArray.newInstance();

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			List<Integer> memberParents = Utility.getMemberParents(jedis10, groupId, queryId, true);

			String ugm_key = GroupMemberCache.genKey(groupId, uid);
			String lev = jedis10.hget(ugm_key, "lev");
			int _lev = StringUtil.isNotEmpty(lev) ? Integer.parseInt(lev) : 3;

			String gm_key = GroupMemberCache.genKey(groupId, queryId);
			Map<String, String> map = null;

			while (queryId > 0) {

				map = jedis10.hgetAll(gm_key);
				if (map.size() == 0 || Integer.parseInt(map.get("opt")) == 2) {
					break;
				}

				int parentId = Integer.parseInt(map.get("parentId"));
				if (_lev == 3) {

					if (parentId == 0) {
						break;
					}

					if (!memberParents.contains(uid)) {
						break;
					}
				}

				ITObject temp = TObject.newInstance();
				temp.putInt("uid", queryId);
				temp.putLong("hp", Long.parseLong(map.get("hp")));
				temp.putInt("join_time", Integer.parseInt(map.get("join_time")));
				temp.putInt("lev", Integer.parseInt(map.get("lev")));
				temp.putInt("parentId", Integer.parseInt(map.get("parentId")));
				temp.putInt("queueid", Integer.parseInt(map.get("queueid") == null ? "0" : map.get("queueid")));
				temp.putInt("seeid", Integer.parseInt(map.get("seeid") == null ? "0" : map.get("seeid")));
				if (Integer.parseInt(map.get("parentId")) > 0) {
					AccountBean parent_acc = AccountCache.getAccount(Integer.parseInt(map.get("parentId")));
					if (parent_acc != null) {
						temp.putString("parentId_nick", parent_acc.nick);
					} else {
						String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'",
								Integer.parseInt(map.get("parentId")));
						ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
						if (resultArray.size() == 0) {
							temp.putString("parentId_nick", StringUtil.Empty);
						} else {
							ITObject userData = resultArray.getTObject(0);
							temp.putString("parentId_nick", userData.getUtfString("nick"));
						}
					}
				} else {
					if (_lev == 1 && uid != queryId) {
						AccountBean uid_acc = AccountCache.getAccount(uid);
						temp.putInt("parentId", uid);
						temp.putString("parentId_nick", uid_acc.nick);
					} else {
						temp.putString("parentId_nick", StringUtil.Empty);
					}
				}

				temp.putInt("partnerLev", Integer.parseInt(map.get("partnerLev")));
				temp.putInt("ban", Integer.parseInt(map.get("ban")));

				String group_ban = Redis.use("group1_db10").hget(gm_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					temp.putInt("group_ban", 1);
				} else {
					temp.putInt("group_ban", 0);
				}
				AccountBean acc = AccountCache.getAccount(queryId);
				temp.putString("nick", acc.nick);
				temp.putString("portrait", acc.portrait);
				String last_time = map.get("last_time");

				temp.putInt("last_time", StringUtil.isNotEmpty(last_time) ? Integer.parseInt(last_time) : 0);
				String permission = map.get("permission");
				temp.putInt("permission", StringUtil.isNotEmpty(permission) ? Integer.parseInt(permission) : 0);
				temp.putInt("queueid", Integer.parseInt(map.get("queueid") == null ? "0" : map.get("queueid")));
				temp.putInt("seeid", Integer.parseInt(map.get("seeid") == null ? "0" : map.get("seeid")));
				// --调控
				Jedis jedis1b = Redis.use("group1_db1").getJedis();
				try {
					String diaokongid = jedis1b.hget("gods_value", Integer.toString(acc.id));
					if (StringUtil.isNotEmpty(diaokongid)) {
						temp.putInt("diaokongid", Integer.parseInt(diaokongid));
					} else {
						temp.putInt("diaokongid", -1);
					}
				} finally {
					jedis1b.close();
				}
				Jedis jedis9 = Redis.use(redisdb).getJedis();
				try {
					int endtime = DateUtils.getBeginDay() + 86400;
					int begintime = endtime - 86400 * 1;
					GroupLogService.getMemberData(jedis9, acc.id, temp, groupId, 0, begintime, endtime, false, false);
				} finally {
					jedis9.close();
				}

				array.addTObject(temp);
				break;
			}

			if (StringUtil.isNotEmpty(tagName)) {

//				String sql = String.format("select id from account as a " + "right join group_member as b "
//						+ "on a.id = b.uid and b.groupId =%s " + "where nick like '%%%s%%'", groupId, tagName);
				String sql = String.format("select id from account as a " + "right join group_member as b "
						+ "on a.id = b.uid and b.groupId =%s " + "where id ='%s'", groupId, tagName);

				ITArray arr = DataBase.use().executeQueryByTArray(sql);
				if (arr.size() > 0) {

					for (int i = 0; i < arr.size(); i++) {

						int tag = arr.getTObject(i).getInt("id");

						if (queryId > 0 && tag == queryId) {
							continue;
						}

						gm_key = GroupMemberCache.genKey(groupId, tag);

						map = jedis10.hgetAll(gm_key);
						if (map == null || map.get("opt") == null) {
							continue;
						}
						if (map.size() == 0 || Integer.parseInt(map.get("opt")) == 2) {
							continue;
						}

						int parentId = Integer.parseInt(map.get("parentId"));
						if (_lev == 3) {

							if (parentId == 0) {
								continue;
							}

							if (parentId != uid) {
								continue;
							}
						}

						ITObject temp = TObject.newInstance();
						temp.putInt("uid", tag);
						temp.putInt("hp", Integer.parseInt(map.get("hp")));
						temp.putInt("join_time", Integer.parseInt(map.get("join_time")));
						temp.putInt("lev", Integer.parseInt(map.get("lev")));
						temp.putInt("parentId", Integer.parseInt(map.get("parentId")));
						temp.putInt("queueid", Integer.parseInt(map.get("queueid") == null ? "0" : map.get("queueid")));
						temp.putInt("seeid", Integer.parseInt(map.get("seeid") == null ? "0" : map.get("seeid")));

						Jedis jedis1b = Redis.use("group1_db1").getJedis();
						try {
							String diaokongid = jedis1b.hget("gods_value", Integer.toString(tag));
							if (StringUtil.isNotEmpty(diaokongid)) {
								temp.putInt("diaokongid", Integer.parseInt(diaokongid));
							} else {
								temp.putInt("diaokongid", -1);
							}
						} finally {
							jedis1b.close();
						}

						if (Integer.parseInt(map.get("parentId")) > 0) {
							AccountBean parent_acc = AccountCache.getAccount(Integer.parseInt(map.get("parentId")));
							if (parent_acc != null) {
								temp.putString("parentId_nick", parent_acc.nick);
							} else {
								String sql2 = String.format("SELECT nick FROM account WHERE id ='%d'",
										Integer.parseInt(map.get("parentId")));
								ITArray resultArray = DataBase.use().executeQueryByTArray(sql2);
								if (resultArray.size() == 0) {
									temp.putString("parentId_nick", StringUtil.Empty);
								} else {
									ITObject userData = resultArray.getTObject(0);
									temp.putString("parentId_nick", userData.getUtfString("nick"));
								}
							}
						} else {
							if (_lev == 1 && uid != tag) {
								AccountBean uid_acc = AccountCache.getAccount(uid);
								temp.putInt("parentId", uid);
								temp.putString("parentId_nick", uid_acc.nick);
							} else {
								temp.putString("parentId_nick", StringUtil.Empty);
							}
						}

						temp.putInt("partnerLev", Integer.parseInt(map.get("partnerLev")));
						temp.putInt("ban", Integer.parseInt(map.get("ban")));
						temp.putInt("queueid", Integer.parseInt(map.get("queueid") == null ? "0" : map.get("queueid")));
						temp.putInt("seeid", Integer.parseInt(map.get("seeid") == null ? "0" : map.get("seeid")));
						AccountBean acc = AccountCache.getAccount(tag);
						temp.putString("nick", acc == null ? "" : acc.nick);
						temp.putString("portrait", acc == null ? "" : acc.portrait);
						String last_time = map.get("last_time");
						temp.putInt("last_time", StringUtil.isNotEmpty(last_time) ? Integer.parseInt(last_time) : 0);
						String permission = map.get("permission");
						temp.putInt("permission", StringUtil.isNotEmpty(permission) ? Integer.parseInt(permission) : 0);

						// --调控
						Jedis jedis1bb = Redis.use("group1_db1").getJedis();
						try {
							String diaokongid = jedis1bb.hget("gods_value", Integer.toString(tag));
							if (StringUtil.isNotEmpty(diaokongid)) {
								temp.putInt("diaokongid", Integer.parseInt(diaokongid));
							} else {
								temp.putInt("diaokongid", -1);
							}
						} finally {
							jedis1bb.close();
						}

						array.addTObject(temp);
					}
				}
			}
		} finally {
			jedis10.close();
		}

		obj.putTArray("members", array);
		return obj;
	}

	/**
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 * @throws Exception
	 */
	public static final ITObject findMember1(int groupId, int uid) throws Exception {
		String gm_key = GroupMemberCache.genKey(groupId, uid);
		Map<String, String> map = Redis.use("group1_db10").hgetAll(gm_key);
		if (map.size() == 0 || Integer.parseInt(map.get("opt")) == 2) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}
		ITObject obj = TObject.newInstance();
		obj.putInt("uid", uid);
		obj.putLong("hp", Long.parseLong(map.get("hp")));
		AccountBean acc = AccountCache.getAccount(uid);
		obj.putString("nick", acc.nick);
		obj.putString("portrait", acc.portrait);
		return obj;
	}

	/**
	 * 获取成员上级列表
	 * 
	 * @param groupId
	 * @param uid
	 * @return
	 */
	public static final ITArray getMemberParents(int groupId, int uid) {
		ITArray list = TArray.newInstance();
		List<Integer> t_list = Utility.getMemberParents(groupId, uid);
		if (t_list != null) {
			for (Integer n : t_list) {
				list.addInt(n);
			}
		}
		return list;
	}

	/**
	 *
	 * @param groupId
	 * @param uid
	 * @return
	 */
	public static final int getParents(int groupId, int uid) {
		GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
		if (gmb == null) {
			return 0;
		}
		return gmb.parentId;
	}

	/**
	 * 获取合伙人列表
	 * 
	 * @param groupId
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getPartners(int groupId, int uid, int limit, int num, int qid) throws Exception {
		ITObject obj1 = TObject.newInstance();
		String qid_sql = StringUtil.Empty;
		GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
		boolean self = qid == 0 || qid == uid;
		ITArray arr1 = TArray.newInstance();
		if (self && gmb.lev == 3 && limit == 0) {
			ITObject obj = TObject.newInstance();
			obj.putInt("uid", uid);
			AccountBean acc = AccountCache.getAccount(uid);
			obj.putString("nick", acc.nick);
			obj.putString("portrait", acc.portrait);
			obj.putInt("partnerLev", gmb.partnerLev);
			arr1.addTObject(obj);
		}
		if (qid != uid) {
			if (qid > 0) {
				qid_sql = "AND uid=" + qid;
			}
			String sql = String.format(
					"SELECT uid,partnerLev FROM group_member where groupId =%s and parentId=%s %s and partnerLev>0 limit %s,%s",
					groupId, uid, qid_sql, limit, num);
			ITArray arr = DataBase.use().executeQueryByTArray(sql);
			if (arr.size() > 0) {
				for (int i = 0; i < arr.size(); ++i) {
					ITObject obj = arr.getTObject(i);
					AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
					obj.putString("nick", acc.nick);
					obj.putString("portrait", acc.portrait);
					arr1.addTObject(obj);
				}
			}
		}

		obj1.putTArray("members", arr1);
		obj1.putInt("limit", limit);
		return obj1;
	}

	/**
	 * 获取指定合伙人奖励数据
	 * 
	 * @param groupId
	 * @param parentId
	 * @param tagId
	 * @param partnerLev
	 * @return
	 */
	public static final void getRewards(int groupId, int parentId, int tagId, int partnerLev, ITObject resData) {

		String gp_key = GroupCache.genPidsKey(groupId);
		Set<String> pids = Redis.use("group1_db11").zrangeByScore(gp_key, 11, 11);
		if (pids.size() == 0) {
			return;
		}

		boolean isShowXiPai = false;
		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb != null) {
			if (gb.owner == parentId) {
				isShowXiPai = true;
			}
		}

		ITArray arr = TArray.newInstance();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, parentId);
			if (uid_bean == null) {
				return;
			}

			if (uid_bean.lev >= 3 && parentId != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(parentId)) {
					return;
				}
			}

			for (String str : pids) {
				int pid = Integer.parseInt(str);
				GroupPlayBean gpb = GroupCache.getPlay(groupId, pid);
				if (gpb == null)
					continue;

				int cur_value = 0;
				int max_value = 0;

				int xipai_cur_value = 0;
				int xipai_max_value = 0;

				String p_reward_key = GroupCache.genRewardKey(groupId, pid);
				Double tem = jedis10.zscore(p_reward_key, tagId + "");
//				log.info("p_reward_key:"+p_reward_key);
//				log.info("tagId:"+tagId);
//				log.info("tem.intValue():"+tem.intValue());

				cur_value = tem == null ? 0 : tem.intValue();
				if (partnerLev == 1) {
					max_value = gpb.reward;
				} else {
					tem = jedis10.zscore(p_reward_key, parentId + "");
					max_value = tem == null ? 0 : tem.intValue();
				}

				String p_xipai_reward_key = GroupCache.genXiPaiRewardKey(groupId, pid);
				Double xipai_tem = jedis10.zscore(p_xipai_reward_key, tagId + "");
				xipai_cur_value = xipai_tem == null ? 0 : xipai_tem.intValue();
				if (partnerLev == 1) {
					xipai_max_value = gpb.xipai_reward;
				} else {
					xipai_tem = jedis10.zscore(p_xipai_reward_key, parentId + "");
					xipai_max_value = xipai_tem == null ? 0 : xipai_tem.intValue();
					if (gpb.xipai_rewardValueType == 1 && xipai_max_value > 99) {
						xipai_max_value = 100;
					}
				}

				if (isShowXiPai == false) {
					Double parent_xipai_tem = jedis10.zscore(p_xipai_reward_key, parentId + "");
					int parent_xipai_cur_value = parent_xipai_tem == null ? 0 : parent_xipai_tem.intValue();
					if (parent_xipai_cur_value > 0) {
						isShowXiPai = true;
					}
				}

				ITObject obj = TObject.newInstance();
				obj.putInt("pid", pid);
				obj.putInt("rewardType", gpb.rewardType);
				obj.putInt("xipai_rewardType", gpb.xipai_rewardType);
				obj.putInt("cur_value", cur_value);
				obj.putInt("max_value", max_value);
				log.info("end pid:" + pid);
				log.info("end cur_value:" + cur_value);
				log.info("end max_value:" + max_value);

				obj.putInt("xipai_cur_value", xipai_cur_value);
				obj.putInt("xipai_max_value", xipai_max_value);
				obj.putInt("rewardValueType", gpb.rewardValueType);
				obj.putInt("xipai_rewardValueType", gpb.xipai_rewardValueType);
				arr.addTObject(obj);
			}
		} finally {
			jedis10.close();
		}

		resData.putTArray("rewards", arr);
		resData.putBoolean("showxipai", isShowXiPai);
	}

	/**
	 * 
	 * @param groupId
	 */
	public static final int setReward(int groupId, int parentId, int tagId, int partnerLev, int pid, int value,
			boolean all, int isSingle) throws Exception {
		log.info("setReward parentId:" + parentId + " tagId:" + tagId + " pid:" + pid + " value:" + value + " all:"
				+ all + " isSingle:" + isSingle);

		GroupPlayBean gpbm = GroupCache.getPlay(groupId, pid);
		if (gpbm == null) {
			throw new WebException(ErrorCode.GROUP_PLAY_EXIST);
		}

		ITObject hpObj = TObject.newFromJsonData(gpbm.hpConfig);
		int maxRound = 0;
		if (hpObj.getInt("maxRound") != null) {
			maxRound = hpObj.getInt("maxRound");
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock("group_play_award_lock", jedis10);
		lock.lock();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, parentId);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && parentId != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(parentId)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			String p_reward_key = GroupCache.genRewardKey(groupId, pid);
			int p_reward = 0;

			if (partnerLev == 1) {
				p_reward = gpbm.reward;
			} else {
				Double tem = jedis10.zscore(p_reward_key, parentId + "");
				p_reward = tem == null ? 0 : tem.intValue();
			}

			if (value > p_reward) {
				return ErrorCode._FAILED;
			}

			int tagOldReward = 0;
			int diff = 0;
			Double tem = jedis10.zscore(p_reward_key, tagId + "");
			tagOldReward = tem == null ? 0 : tem.intValue();
			if (value < tagOldReward) {
				diff = tagOldReward - value;
			}

			if (!all) {

				jedis10.zadd(p_reward_key, value, tagId + "");

				if (diff > 0) {

					List<Integer> listParent = Utility.getChildParentList(groupId, tagId, false);
					for (Integer parId : listParent) {

						Double temPar = jedis10.zscore(p_reward_key, parId + "");
						p_reward = temPar == null ? 0 : temPar.intValue();

						int newValue = 0;
						if (p_reward > diff) {

							newValue = p_reward - diff;
						}
						jedis10.zadd(p_reward_key, newValue, parId + "");
					}
				}
			} else {

				String gp_key = GroupCache.genPidsKey(groupId);
				Set<String> pids = Redis.use("group1_db11").zrangeByScore(gp_key, 11, 11);
				if (pids.size() > 0) {

					for (String strPid : pids) {

						p_reward_key = GroupCache.genRewardKey(groupId, Integer.parseInt(strPid));
						GroupPlayBean gpb = GroupCache.getPlay(groupId, Integer.parseInt(strPid));

						if (isSingle > 0) {
							ITObject hpObjTemp = TObject.newFromJsonData(gpb.hpConfig);
							int tempMaxRound = 0;
							if (hpObjTemp.getInt("maxRound") != null) {
								tempMaxRound = hpObjTemp.getInt("maxRound");
							}

							if ((maxRound == 1 && tempMaxRound != 1) || (maxRound != 1 && tempMaxRound == 1)) {
								continue;
							}
						}

						if (gpb.rewardValueType == gpbm.rewardValueType) {

							int p_reward_max = 0;
							if (partnerLev == 1) {
								p_reward_max = gpb.reward;
							} else {
								tem = jedis10.zscore(p_reward_key, parentId + "");
								p_reward_max = tem == null ? 0 : tem.intValue();
							}

							if (p_reward_max < value) {
								return ErrorCode._FAILED;
							}
						}

					}

					for (String strPid : pids) {
						tagOldReward = 0;
						diff = 0;
						GroupPlayBean gpb = GroupCache.getPlay(groupId, Integer.parseInt(strPid));

						if (isSingle > 0) {
							ITObject hpObjTemp = TObject.newFromJsonData(gpb.hpConfig);
							int tempMaxRound = 0;
							if (hpObjTemp.getInt("maxRound") != null) {
								tempMaxRound = hpObjTemp.getInt("maxRound");
							}

							if ((maxRound == 1 && tempMaxRound != 1) || (maxRound != 1 && tempMaxRound == 1)) {
								continue;
							}
						}

						if (gpb.rewardValueType == gpbm.rewardValueType) {

							p_reward_key = GroupCache.genRewardKey(groupId, Integer.parseInt(strPid));
							tem = jedis10.zscore(p_reward_key, tagId + "");
							tagOldReward = tem == null ? 0 : tem.intValue();
							if (value < tagOldReward) {
								diff = tagOldReward - value;
							}
							jedis10.zadd(p_reward_key, value, tagId + "");

							if (diff > 0) {

								List<Integer> listParent = Utility.getChildParentList(groupId, tagId, false);
								for (Integer parId : listParent) {

									Double temPar = jedis10.zscore(p_reward_key, parId + "");
									p_reward = temPar == null ? 0 : temPar.intValue();

									int newValue = 0;
									if (p_reward > diff) {

										newValue = p_reward - diff;
									}
									jedis10.zadd(p_reward_key, newValue, parId + "");
								}
							}

						}
					}

				}
			}
		} finally {
			lock.unlock();
		}

		return 0;
	}

	/**
	 *
	 * @param groupId
	 */
	public static final int setXiPaiReward(int groupId, int parentId, int tagId, int partnerLev, int pid, int value,
			boolean all, int isSingle) throws Exception {

		log.info("setXiPaiReward parentId:" + parentId + " tagId:" + tagId + " pid:" + pid + " value:" + value + " all:"
				+ all + " isSingle:" + isSingle);

		GroupPlayBean gpbm = GroupCache.getPlay(groupId, pid);
		if (gpbm == null) {
			throw new WebException(ErrorCode.GROUP_PLAY_EXIST);
		}

		ITObject hpObj = TObject.newFromJsonData(gpbm.hpConfig);
		int maxRound = 0;
		if (hpObj.getInt("maxRound") != null) {
			maxRound = hpObj.getInt("maxRound");
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock("group_play_award_lock", jedis10);
		lock.lock();
		try {

			GroupMemberBean uid_bean = GroupCache.getMember(groupId, parentId);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3 && parentId != tagId) {
				List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
				if (par_list == null || !par_list.contains(parentId)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}

			String p_reward_key = GroupCache.genXiPaiRewardKey(groupId, pid);
			int p_reward = 0;

			if (partnerLev == 1) {
				p_reward = gpbm.xipai_reward;
			} else {
				Double tem = jedis10.zscore(p_reward_key, parentId + "");
				p_reward = tem == null ? 0 : tem.intValue();
			}

			if (value > p_reward) {
				return ErrorCode._FAILED;
			}

			int tagOldReward = 0;
			int diff = 0;
			Double tem = jedis10.zscore(p_reward_key, tagId + "");
			tagOldReward = tem == null ? 0 : tem.intValue();
			if (value < tagOldReward) {
				diff = tagOldReward - value;
			}

			if (!all) {

				jedis10.zadd(p_reward_key, value, tagId + "");

				if (diff > 0) {

					List<Integer> listParent = Utility.getChildParentList(groupId, tagId, false);
					for (Integer parId : listParent) {

						Double temPar = jedis10.zscore(p_reward_key, parId + "");
						p_reward = temPar == null ? 0 : temPar.intValue();

						int newValue = 0;
						if (p_reward > diff) {

							newValue = p_reward - diff;
						}
						jedis10.zadd(p_reward_key, newValue, parId + "");
					}
				}
			} else {

				String gp_key = GroupCache.genPidsKey(groupId);
				Set<String> pids = Redis.use("group1_db11").zrangeByScore(gp_key, 11, 11);
				if (pids.size() > 0) {

					for (String strPid : pids) {

						p_reward_key = GroupCache.genXiPaiRewardKey(groupId, Integer.parseInt(strPid));
						GroupPlayBean gpb = GroupCache.getPlay(groupId, Integer.parseInt(strPid));

						if (isSingle > 0) {
							ITObject hpObjTemp = TObject.newFromJsonData(gpb.hpConfig);
							int tempMaxRound = 0;
							if (hpObjTemp.getInt("maxRound") != null) {
								tempMaxRound = hpObjTemp.getInt("maxRound");
							}

							if ((maxRound == 1 && tempMaxRound != 1) || (maxRound != 1 && tempMaxRound == 1)) {
								continue;
							}
						}

						if (gpb.xipai_rewardValueType == gpbm.xipai_rewardValueType) {

							int p_reward_max = 0;
							if (partnerLev == 1) {
								p_reward_max = gpb.xipai_reward;
							} else {
								tem = jedis10.zscore(p_reward_key, parentId + "");
								p_reward_max = tem == null ? 0 : tem.intValue();
							}

							if (p_reward_max < value) {
								return ErrorCode._FAILED;
							}
						}

					}

					for (String strPid : pids) {
						tagOldReward = 0;
						diff = 0;

						GroupPlayBean gpb = GroupCache.getPlay(groupId, Integer.parseInt(strPid));
						if (isSingle > 0) {
							ITObject hpObjTemp = TObject.newFromJsonData(gpb.hpConfig);
							int tempMaxRound = 0;
							if (hpObjTemp.getInt("maxRound") != null) {
								tempMaxRound = hpObjTemp.getInt("maxRound");
							}

							if ((maxRound == 1 && tempMaxRound != 1) || (maxRound != 1 && tempMaxRound == 1)) {
								continue;
							}
						}

						if (gpb.xipai_rewardValueType == gpbm.xipai_rewardValueType) {

							p_reward_key = GroupCache.genXiPaiRewardKey(groupId, Integer.parseInt(strPid));
							tem = jedis10.zscore(p_reward_key, tagId + "");
							tagOldReward = tem == null ? 0 : tem.intValue();
							if (value < tagOldReward) {
								diff = tagOldReward - value;
							}

							jedis10.zadd(p_reward_key, value, tagId + "");

							if (diff > 0) {

								List<Integer> listParent = Utility.getChildParentList(groupId, tagId, false);
								for (Integer parId : listParent) {

									Double temPar = jedis10.zscore(p_reward_key, parId + "");
									p_reward = temPar == null ? 0 : temPar.intValue();

									int newValue = 0;
									if (p_reward > diff) {

										newValue = p_reward - diff;
									}
									jedis10.zadd(p_reward_key, newValue, parId + "");
								}
							}

						}
					}
				}
			}
		} finally {
			lock.unlock();
		}

		return 0;
	}

	/**
	 * 置顶圈子
	 * 
	 * @param groupId
	 * @param uid
	 * @param top
	 * @throws Exception
	 */
	public static final int topGroup(int groupId, int uid, boolean top) throws Exception {
		String group_key = GroupCache.genKey(groupId);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		RedisLock lock = new RedisLock(group_key, jedis11);
		try {
			String opt1 = jedis11.hget(group_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NO_EXIST);
			}
			long time = top ? System.currentTimeMillis() / 1000 : 0;
			jedis11.zadd(GroupCache.genGroupsKey(uid), time, groupId + "");
			String gm_key = GroupMemberCache.genKey(groupId, uid);
			Jedis jedis10 = Redis.use("group1_db10").getJedis();
			try {
				jedis10.hset(gm_key, "top_time", time + "");
				BaseCache.updateCacheVer(jedis10, gm_key);
			} finally {
				jedis10.close();
			}

			return (int) time;
		} finally {
			lock.unlock();
		}
	}

	/**
	 * 更新圈子信息
	 * 
	 * @param groupId
	 * @param name
	 * @param reqData
	 * @throws Exception
	 */
	public static final void updateGroupInfo(int groupId, String name, ITObject reqData) throws Exception {
		log.info("updateGroupInfo name:" + name + " reqData:" + reqData.toString());
		boolean ban = reqData.getBoolean("ban");
		String notice = reqData.getString("notice");
		int dissolve_opt = reqData.getInt("dissolve_opt");
		int kick_opt = reqData.getInt("kick_opt");
		int ban_apply = reqData.getInt("ban_apply");
		boolean ban_chat1 = reqData.getBoolean("ban_chat1");
		boolean ban_chat2 = reqData.getBoolean("ban_chat2");

		int option = reqData.getInt("option");
		int showNum = reqData.getInt("show_num");

		// int exit_opt = reqData.getInt("exit_opt");
		int exit_opt = 0;
		String _ban = (ban ? 1 : 0) + StringUtil.Empty;
		String sql = String.format("update groups set name='%s' where id = %s", name, groupId);
		Utility.evtdb(groupId, 1, sql);
		Map<String, String> map = new HashMap<String, String>();
		map.put("name", name);
		map.put("notice", name);
		map.put("ban", _ban);
		map.put("dissolve_opt", dissolve_opt + "");
		map.put("kick_opt", kick_opt + "");
		map.put("notice", notice);
		map.put("ban_apply", ban_apply + "");
		map.put("ban_chat1", ban_chat1 + "");
		map.put("ban_chat2", ban_chat2 + "");
		map.put("exit_opt", exit_opt + "");
		map.put("option", option + "");
		map.put("show_num", showNum + "");
		String key = GroupCache.genKey(groupId);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		try {
			jedis11.hmset(key, map);
			BaseCache.updateCacheVer(jedis11, key);
		} finally {
			jedis11.close();
		}

		GroupPublisherService.updateGroupEvt(groupId, name, ban, notice, option, showNum);
	}

	/**
	 * 成员禁止娱乐
	 * 
	 * @param groupId
	 * @throws Exception
	 */
	public static final void banMember(int groupId, int uid, int tagId, int optType, int ban) throws Exception {
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			RedisLock lock1 = new RedisLock(mng_key, jedis10);
			try {
				lock1.lock();
				int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
				if (mgn_lev == 2) {
					String permission = jedis10.hget(mng_key, "permission");
					if (StringUtil.isEmpty(permission) || (Integer.parseInt(permission) & PERMISSION_BAN) == 0) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				} else if (mgn_lev >= 3) {
					if (uid != tagId) {
						List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
						if (par_list == null || !par_list.contains(uid)) {
							throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
						}
						// log.info("uid:" + uid + " ban_member:" + tagId + " par_list:" + par_list);
					}
				}

				String group_ban = jedis10.hget(mng_key, "group_ban");
				if (!StringUtil.isEmpty(group_ban) && Integer.parseInt(group_ban) == 1) {
					throw new WebException(ErrorCode.GROUP_MEMBER_BAN);
				}
			} finally {
				lock1.unlock(false);
			}

			log.info("uid:" + uid + " ban_member:" + tagId + " optType:" + optType + " ban:" + ban);

			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (optType == 2 && gmb.partnerLev > 0) {
				isPartner = true;
			}

			String sql;
			String _ban = ban + "";
			if (isPartner) {
				HashSet<Integer> hashSet = new HashSet<>();
				hashSet.add(tagId);
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				gm_key = GroupMemberCache.genKey(groupId, tagId);
				jedis10.hset(gm_key, "ban", _ban);
				jedis10.hset(gm_key, "group_ban", _ban);
				BaseCache.updateCacheVer(jedis10, gm_key);

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);

						sql = String.format(
								"update group_member set ban = %s where groupId = %s and (parentId in(%s) or uid = %s)",
								_ban, groupId, child_list2, tmpTagId);
						Utility.evtdb(groupId, 1, sql);

						sql = String.format(
								"select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");

								if (hashSet.contains(childId))
									continue;

								gm_key = GroupMemberCache.genKey(groupId, childId);
								jedis10.hset(gm_key, "ban", _ban);
								jedis10.hset(gm_key, "group_ban", _ban);
								BaseCache.updateCacheVer(jedis10, gm_key);

								hashSet.add(childId);
								arrayList.add(childId);
							}
						}
					}
				}
			} else {

				sql = String.format("update group_member set ban = %s where uid = %s AND groupId = %s", _ban, tagId,
						groupId);
				Utility.evtdb(groupId, 1, sql);
				jedis10.hset(gm_key, "ban", _ban);
				BaseCache.updateCacheVer(jedis10, gm_key);
			}
		} finally {
			lock.unlock();
		}
	}

	public static final void blackPartner(int groupId, int uid, int tagId, int optType, int ban, int ban_rate,
			int ban_value) throws Exception {
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (optType == 2 && gmb.partnerLev > 0) {
				isPartner = true;
			}
			gm_key = GroupMemberCache.genKey(groupId, tagId);
			// jedis10.hset(gm_key, "black", ban+"");
			// jedis10.hset(gm_key, "group_black_rate", ban_rate+"");
			jedis10.hset(gm_key, "parent_black_max_value", ban_value + ""); // 门槛值

		} finally {
			lock.unlock();
		}
	}

	/**
	 * 成员禁止娱乐
	 *
	 * @param groupId
	 * @throws Exception
	 */
	public static final void blackMember(int groupId, int uid, int tagId, int optType, int ban, int ban_rate,
			int ban_value) throws Exception {
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			RedisLock lock1 = new RedisLock(mng_key, jedis10);
//			try {
//				lock1.lock();
//				int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
//				if (mgn_lev ==3) {
//					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//				}
//			} finally {
//				lock1.unlock(false);
//			}

			boolean isPartner = false;
			GroupMemberBean gmb = GroupCache.getMember(groupId, tagId);
			if (optType == 2 && gmb.partnerLev > 0) {
				isPartner = true;
			}

			if (ban != 0) {
				if (Redis.use("group1_db1").sismember("gods", Integer.toString(tagId))) {
//					String specail = Redis.use("group1_db1").hget("gods_special", Integer.toString(tagId));
//					if (StringUtil.isEmpty(specail))
//					{
//						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
//					}
//					else {
//						log.info("set gods tagId:" + tagId + " to ban " + ban + " srem gods");
//						//Redis.use("group1_db1").srem("gods", Integer.toString(tagId));
//					}
				}
			}

			String sql;
			String _ban = ban + "";
			if (ban == 1) {
				try {
					ban_rate = Integer.parseInt(Redis.use("group1_db1").hget("black_rate", "black"));
				} catch (Exception e) {
				}
			} else if (ban == 2) {
				try {
					ban_rate = Integer.parseInt(Redis.use("group1_db1").hget("black_rate", "white"));
				} catch (Exception e) {
				}
			}

			log.info("uid:" + uid + " black_member:" + tagId + " ban:" + ban + " ban_rate:" + ban_rate + " ban_value:"
					+ ban_value);
			// isPartner = true;

			if (isPartner) {
				HashSet<Integer> hashSet = new HashSet<>();
				hashSet.add(tagId);
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tagId);

				gm_key = GroupMemberCache.genKey(groupId, tagId);
				jedis10.hset(gm_key, "black", _ban);
				jedis10.hset(gm_key, "group_black", _ban);
				String _ban_rate = ban_rate + "";
				jedis10.hset(gm_key, "group_black_rate", _ban_rate);
				jedis10.hset(gm_key, "group_black_key", gm_key);
				String _ban_value = ban_value + "";
				jedis10.hset(gm_key, "group_black_max_value", _ban_value);
				jedis10.hset(gm_key, "group_black_now_value", "0");
				BaseCache.updateCacheVer(jedis10, gm_key);
				/*
				 * while(!arrayList.isEmpty()) { ArrayList<Integer> arrayListTemp = new
				 * ArrayList<>(); arrayListTemp.addAll(arrayList); arrayList.clear();
				 * 
				 * for(int i = 0; i < arrayListTemp.size(); ++i) { int tmpTagId =
				 * arrayListTemp.get(i); String child_list2 = Utility.getChildParentSql(groupId,
				 * tmpTagId, true);
				 * 
				 * sql = String.
				 * format("select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)"
				 * , groupId,child_list2, tmpTagId);
				 * 
				 * ITArray tmplist = DataBase.use().executeQueryByTArray(sql);
				 * if(tmplist.size()>0) { for(int j=0; j<tmplist.size();++j) {
				 * 
				 * ITObject tem = tmplist.getTObject(j); int childId = tem.getInt("uid");
				 * 
				 * if (hashSet.contains(childId)) continue;
				 * 
				 * if (!Redis.use("group1_db1").sismember("gods", Integer.toString(childId))) {
				 * gm_key = GroupMemberCache.genKey(groupId, childId); jedis10.hset(gm_key,
				 * "group_black_key", GroupMemberCache.genKey(groupId, tagId));
				 * BaseCache.updateCacheVer(jedis10, gm_key); } else { String specail =
				 * Redis.use("group1_db1").hget("gods_special", Integer.toString(childId)); if
				 * (StringUtil.isEmpty(specail)) {
				 * 
				 * } else { //Redis.use("group1_db1").srem("gods", Integer.toString(childId));
				 * log.info("set gods childId:" + childId + " to ban " + ban + " srem gods");
				 * gm_key = GroupMemberCache.genKey(groupId, childId); jedis10.hset(gm_key,
				 * "group_black_key", GroupMemberCache.genKey(groupId, tagId));
				 * BaseCache.updateCacheVer(jedis10, gm_key); } }
				 * 
				 * hashSet.add(childId); arrayList.add(childId); } } } }
				 */
			} else {
				jedis10.hset(gm_key, "group_black_max_value", ban_value + "");
				BaseCache.updateCacheVer(jedis10, gm_key);
			}
		} finally {
			lock.unlock();
		}
	}

	/**
	 * 玩法禁止娱乐
	 * 
	 * @param groupId
	 * @throws Exception
	 */
	public static final void banPlay(int groupId, int pid, int ban) throws Exception {
		log.info("banPlay pid:" + pid + " ban:" + ban);
		String p_key = GroupCache.genPlayKey(groupId, pid);
		String _ban = ban + "";
		Redis.use("group1_db11").hset(p_key, "ban", _ban);
		Redis.use("group1_db11").hincrBy(p_key, "cache_ver", 1);
	}

	/**
	 * 玩法禁止娱乐
	 * 
	 * @param groupId
	 * @throws Exception
	 */
	public static final void banMemberPlay(int groupId, String pids, int uid) throws Exception {
		log.info("banPlay pid:" + pids + " member:" + uid);
		Redis.use("group1_db2").hset("genusersplay", uid + "",
				pids.replace("[", "").replace("]", "").replaceAll(" ", ""));
	}

	/**
	 * 玩法禁止娱乐
	 * 
	 * @param groupId
	 * @throws Exception
	 */
	public static final void cleanMemberPlay(int groupId, int uid) throws Exception {
		log.info("cleanPlay  member:" + uid);
		Redis.use("group1_db2").hdel("genusersplay", uid + "");
	}

	/**
	 * 玩法禁止娱乐
	 * 
	 * @param groupId
	 * @throws Exception
	 */
	public static final ITArray getMemberPlayList(int groupId, int uid) throws Exception {
		log.info("cleanPlay  member:" + uid);
		String list = Redis.use("group1_db2").hget("genusersplay", uid + "");
		ITArray arr = TArray.newInstance();
		if (list != null && list.length() != 0) {
			String[] pids = list.split(",");
			if (pids.length > 0) {
				for (int i = 0; i < pids.length; i++) {
					arr.addInt(Integer.parseInt(pids[i]));
				}
			}

		}
		return arr;
	}

	/**
	 * 标识玩法
	 *
	 * @param groupId
	 * @throws Exception
	 */
	public static final void markPlay(int groupId, int pid, int mark) throws Exception {
		String p_key = GroupCache.genPlayKey(groupId, pid);
		String _mark = mark + "";
		Redis.use("group1_db11").hset(p_key, "mark", _mark);
		Redis.use("group1_db11").hincrBy(p_key, "cache_ver", 1);
	}

	public static final int getMarkedPlayCount(int groupId) throws Exception {
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		int count = 0;
		try {
			String gpids_key = GroupCache.genPidsKey(groupId);
			Set<String> pids = jedis11.zrangeByScore(gpids_key, 10, 11);
			String key = "g{" + groupId + "}:play:";

			for (String tem : pids) {
				String gp_key = key + tem;

				String str = Redis.use("group1_db11").hget(gp_key, "mark");
				if (str != null) {
					int val = Integer.parseInt(str);
					if (val == 1) {
						count++;
					}
				}
			}
		} finally {
			jedis11.close();
		}

		return count;
	}

	/**
	 * 更新成员管理
	 * 
	 * @param groupId
	 * @param tagId
	 * @param opt     1 设置管理员 2 取消管理员
	 * @throws Exception
	 */
	public static final ITObject updateMemberMgr(int groupId, int tagId, int opt) throws Exception {
		log.info("updateMemberMgr tagId:" + tagId + " opt:" + opt);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			int clev = Integer.parseInt(jedis10.hget(gm_key, "lev"));
			if (opt == 1 && clev == 3) {
				int partnerLev = Integer.parseInt(jedis10.hget(gm_key, "partnerLev"));
				if (partnerLev > 0) {
					throw new WebException(ErrorCode.GROUP_ALREADY_PARTNER);
				}
				int member_parentId = Integer.parseInt(jedis10.hget(gm_key, "parentId"));
				if (member_parentId > 0) {
					throw new WebException(ErrorCode.GROUP_MEMBER_EXIST_PARTENER);
				}
				clev = 2;
			} else if (opt == 2 && clev == 2) {
				clev = 3;
			} else {
				throw new WebException(ErrorCode._FAILED);
			}
			String sql = String.format("update group_member set lev = %s where uid = %s AND groupId = %s", clev, tagId,
					groupId);
			Utility.evtdb(groupId, 1, sql);
			GroupPublisherService.updateMemberEvt(groupId, tagId, 2, clev);
			jedis10.hset(gm_key, "lev", clev + "");
			BaseCache.updateCacheVer(jedis10, gm_key);
			int permission = 0;
			if (clev == 2) {
				permission = PERMISSION_DEL_MEMBER | PERMISSION_ADD_MEMBER | PERMISSION_HP_OPT | PERMISSION_BAN
						| PERMISSION_BAN_DESKMATE;
				jedis10.hset(gm_key, "permission", permission + "");
			} else {
				jedis10.hset(gm_key, "permission", permission + "");
			}

			ITObject resData = TObject.newInstance();
			resData.putInt("permission", permission);
			return resData;
		} finally {
			lock.unlock();
		}

	}

	/**
	 * 添加玩法
	 * 
	 * @param groupId
	 * @param gameId
	 * @param name
	 * @param config
	 * @param hpData
	 * @return
	 * @throws Exception
	 */
	public static final ITObject addPlay(int groupId, int gameId, String name, int deskId, ITObject config,
			ITObject hpData, int hpOnOff, int uid) throws Exception {

		log.info("addPlay gameId:" + gameId + " name:" + name + " config:" + config + " hpData:" + hpData + " hpOnOff:"
				+ hpOnOff);

		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis9 = Redis.use(redisdb).getJedis();

		RedisLock lock = null;
		try {
			String gp_key = GroupCache.genPidsKey(groupId);
			long time = System.currentTimeMillis();
			long free_num = jedis11.zcount(gp_key, 20, time);
			if (free_num == 0) {
				throw new WebException(ErrorCode.GROUP_PLAY_FULL);
			}
			lock = new RedisLock(gp_key, jedis11);
			lock.lock();
			Set<String> pids = jedis11.zrangeByScore(gp_key, 20, time, 0, 1);
			if (pids.size() == 0) {
				throw new WebException(ErrorCode.GROUP_PLAY_FULL);
			}
			int pid = 0;
			for (String tem : pids) {
				pid = Integer.parseInt(tem);
				break;
			}
			GameBean gb = GameCache.getGame(gameId);
			int gameType = gb.gameType;
			int maxPlayers = gb.maxPlayers;
			if (config.containsKey("maxPlayers")) {
				maxPlayers = config.getInt("maxPlayers");
			}

			int opt = config.getInt("opt");
			int maxRound = getMaxRound(gameId, opt);
			hpData.putInt("maxRound", maxRound);

			log.info("addPlay() maxRound=" + maxRound);

			config.putInt("maxPlayers", maxPlayers);
			config.putInt("pid", pid);
			int rewardType = hpData.getInt("rewards_type");
			int rewardValueType = hpData.getInt("rewardValueType");
			int xipai_rewardType = 1;
			if (hpData.containsKey("xipai_rewardType")) {
				xipai_rewardType = hpData.getInt("xipai_rewardType");
			}
			int xipai_rewardValueType = 1;
			if (hpData.containsKey("xipai_rewardValueType")) {
				xipai_rewardValueType = hpData.getInt("xipai_rewardValueType");
			}

			int reward = hpData.getInt("rewards_val");
			int xipai_reward = 1000000;
			if (hpData.containsKey("xipai_rewards_val")) {
				xipai_reward = hpData.getInt("xipai_rewards_val");
			}

			int robot_room = 0;
			if (hpData.containsKey("robot_room")) {
				robot_room = hpData.getInt("robot_room");
			}

			hpData.remove("rewards_type");
			hpData.remove("rewards_val");
			hpData.remove("rewardValueType");
			hpData.remove("xipai_rewards_val");
			hpData.remove("xipai_rewards_type");
			hpData.remove("xipai_rewardValueType");

			jedis9.del(String.format("g%s:diamo_cost:p%s", groupId, pid));
			jedis9.del(String.format("g%s:valid_room:p%s", groupId, pid));
			jedis9.del(String.format("g%s:no_valid_room:p%s", groupId, pid));
			jedis9.del(String.format("g%s:round:p%s", groupId, pid));

			int hp_times = hpData.getInt("times");
			String config_json = config.toJson();
			String hpData_json = hpData.toJson();
			String p_key = GroupCache.genPlayKey(groupId, pid);
			Map<String, String> redis_map = payToRedis(groupId, pid, gameId, name, deskId, config_json, hpData_json,
					hpOnOff, hp_times, reward, rewardType, rewardValueType, xipai_reward, xipai_rewardType,
					xipai_rewardValueType, robot_room);
			redis_map.put("opt", "1");
			jedis11.hmset(p_key, redis_map);
			jedis11.hincrBy(p_key, "cache_ver", 1);
			jedis11.zadd(gp_key, 1 * 10 + hpOnOff, pid + "");

			// 得到所有的玩法
			pids = jedis11.zrangeByScore(gp_key, 11, 11);
			// 删除新增加的玩法
			pids.remove(pid);

			// 查找所有的一级合伙人
			String sql = String.format("SELECT uid FROM group_member where groupId =%s and partnerLev = 1", groupId);
			ITArray arr = DataBase.use().executeQueryByTArray(sql);
			if (arr.size() > 0) {

				Jedis jedis10 = Redis.use("group1_db10").getJedis();
				String qunzhu_reward_key = GroupCache.genRewardKey(groupId, pid);
				// 设置群主的奖励 rewards_list.getTObject(0).getInt("UpperLimit")
				int qunzhu_reward = hpData.getTArray("rewards_list").getTObject(0).getInt("pumpProportion");
				Double qunzhu_reward_double = (double) qunzhu_reward;
				jedis10.zadd(qunzhu_reward_key, qunzhu_reward_double, uid + "");

				try {

					// 遍历所有的一级合伙人
					for (int i = 0; i < arr.size(); ++i) {

						ITObject obj = arr.getTObject(i);
						int tem_id = obj.getInt("uid");

						// 遍历所有的玩法，查看现在的玩法的推广奖励是否一致
						int sameValue = 0;
						int xipai_sameValue = 0;
						for (String strPid : pids) {

							int indexPid = Integer.parseInt(strPid);
							if (indexPid != pid) {
								GroupPlayBean gpb = GroupCache.getPlay(groupId, indexPid);
								if (gpb.rewardValueType == rewardValueType) {

									String p_reward_key = GroupCache.genRewardKey(groupId, indexPid);
									Double tem = jedis10.zscore(p_reward_key, tem_id + "");
									int value = tem == null ? 0 : tem.intValue();

									if (sameValue == 0) {
										sameValue = value;
									} else if (sameValue != value) {
										sameValue = 0;
										break;
									}
								}
								if (gpb.xipai_rewardValueType == xipai_rewardValueType) {
									String p_reward_key = GroupCache.genXiPaiRewardKey(groupId, indexPid);
									Double tem = jedis10.zscore(p_reward_key, tem_id + "");
									int value = tem == null ? 0 : tem.intValue();

									if (xipai_sameValue == 0) {
										xipai_sameValue = value;
									} else if (xipai_sameValue != value) {
										xipai_sameValue = 0;
										break;
									}
								}
							}
						}

						// 设置该一级合伙人的奖励
						String p_reward_key = GroupCache.genRewardKey(groupId, pid);
						jedis10.zadd(p_reward_key, sameValue * 1000 / maxPlayers, tem_id + "");

						// 设置该一级合伙人的奖励
						String p_xipai_reward_key = GroupCache.genXiPaiRewardKey(groupId, pid);
						jedis10.zadd(p_xipai_reward_key, xipai_sameValue, tem_id + "");
					}
				} finally {
					jedis10.close();
				}
			}

			GroupPublisherService.addPlayEvt(groupId, pid);

			ITObject obj = TObject.newInstance();
			obj.putInt("pid", pid);
			obj.putInt("gameType", gameType);
			obj.putInt("maxPlayers", maxPlayers);
			obj.putInt("maxRound", maxRound);

			return obj;
		} finally {
			if (lock != null)
				lock.unlock();

			jedis9.close();
		}
	}

	public static final int getMaxRound(int gameId, int opt) throws Exception {
		int maxRound = 0;
		Jedis jedis1 = Redis.use("group1_db1").getJedis();
		try {
			String keyStr = "game:" + gameId;
			String optStr = "opt" + opt;
			String val = jedis1.hget(keyStr, optStr);
			maxRound = Integer.parseInt(val);

		} finally {
			jedis1.close();
		}

		return maxRound;
	}

	/**
	 * 删除玩法
	 * 
	 * @param groupId
	 * @param pid
	 * @return
	 * @throws Exception
	 */
	public static final int delPlay(int groupId, int pid) throws Exception {
		log.info("delPlay pid:" + pid);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();

		int min_value = pid * 10000 + 1000;
		int max_value = pid * 10000 + 9999;
		String grooms_key = GroupCache.genRoomsKey(groupId);
		Set<String> rooms = jedis11.zrevrangeByScore(grooms_key, max_value, min_value);
		Jedis jedis0 = Redis.use("group1_db0").getJedis();
		try {
			for (String room : rooms) {
				boolean check = GroupRoomService.checkRoom(room, jedis0);
				if (!check) {
					jedis11.zrem(grooms_key, room);
				} else {
					check = GroupRoomService.checkFakeRoom(room, jedis0);
					if (!check) {
						return ErrorCode.GROUP_PLAY_EXIST_ROOM;
					} else {
						jedis11.zrem(grooms_key, room);
					}
				}
			}
		} finally {
			jedis0.close();
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			String p_reward_key = GroupCache.genRewardKey(groupId, pid);
			String p_xipai_reward_key = GroupCache.genXiPaiRewardKey(groupId, pid);
			jedis10.del(p_reward_key);
			jedis10.del(p_xipai_reward_key);
		} finally {
			jedis10.close();
		}

		String gp_key = GroupCache.genPidsKey(groupId);
		String p_key = GroupCache.genPlayKey(groupId, pid);
		RedisLock lock = new RedisLock(gp_key, jedis11);
		Jedis jedis9 = Redis.use(redisdb).getJedis();
		try {
			lock.lock();

			jedis9.del(String.format("g%s:diamo_cost:p%s", groupId, pid));
			jedis9.del(String.format("g%s:valid_room:p%s", groupId, pid));
			jedis9.del(String.format("g%s:no_valid_room:p%s", groupId, pid));
			jedis9.del(String.format("g%s:round:p%s", groupId, pid));

			jedis11.hset(p_key, "opt", "2");
			jedis11.hincrBy(p_key, "cache_ver", 1);
			long time = System.currentTimeMillis() / 1000;
			jedis11.zadd(gp_key, time, pid + "");
			GroupPublisherService.delPlayEvt(groupId, pid);
		} finally {
			lock.unlock();
			jedis9.close();
		}
		return 0;
	}

	/**
	 * 添加玩法
	 * 
	 * @param groupId
	 * @param gameId
	 * @param name
	 * @param config
	 * @param hpData
	 * @return
	 * @throws Exception
	 */
	public static final ITObject updatePlay(int groupId, int pid, int gameId, String name, int deskId, ITObject config,
			ITObject hpData, int hpOnOff) throws Exception {
		log.info("updatePlay pid:" + pid + " gameId:" + gameId + " name:" + name + " config:" + config + " hpData:"
				+ hpData + " hpOnOff:" + hpOnOff);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		int min_value = pid * 10000 + 1000;
		int max_value = pid * 10000 + 9999;
		String grooms_key = GroupCache.genRoomsKey(groupId);
		Set<String> rooms = jedis11.zrevrangeByScore(grooms_key, max_value, min_value);
		Jedis jedis0 = Redis.use("group1_db0").getJedis();
		try {
			for (String room : rooms) {
				boolean check = GroupRoomService.checkRoom(room, jedis0);
				if (!check) {
					jedis11.zrem(grooms_key, room);
				}
			}
		} finally {
			jedis0.close();
		}

		String p_key = GroupCache.genPlayKey(groupId, pid);
		RedisLock lock = new RedisLock(p_key, jedis11);
		try {

			GameBean gb = GameCache.getGame(gameId);
			int maxPlayers = gb.maxPlayers;
			if (config.containsKey("maxPlayers")) {
				maxPlayers = config.getInt("maxPlayers");
			}
			config.putInt("maxPlayers", maxPlayers);
			config.putInt("pid", pid);
			int rewardType = hpData.getInt("rewards_type");
			int rewardValueType = hpData.getInt("rewardValueType");
			int xipai_rewardType = 0;
			if (hpData.containsKey("xipai_rewardType")) {
				xipai_rewardType = hpData.getInt("xipai_rewardType");
			}
			int xipai_rewardValueType = 0;
			if (hpData.containsKey("xipai_rewardValueType")) {
				xipai_rewardValueType = hpData.getInt("xipai_rewardValueType");
			}
			int reward = hpData.getInt("rewards_val");
			int xipai_reward = 0;
			if (hpData.containsKey("xipai_rewards_val")) {
				xipai_reward = hpData.getInt("xipai_rewards_val");
			}
			int robot_room = 0;
			if (hpData.containsKey("robot_room")) {
				robot_room = hpData.getInt("robot_room");
			}
			hpData.remove("rewards_type");
			hpData.remove("rewards_val");
			hpData.remove("xipai_rewards_val");
			hpData.remove("rewardValueType");
			hpData.remove("xipai_rewards_type");
			hpData.remove("xipai_rewardValueType");
			int hp_times = hpData.getInt("times");

			int opt = config.getInt("opt");
			int maxRound = getMaxRound(gameId, opt);
			hpData.putInt("maxRound", maxRound);
			config.putInt("maxRound", maxRound);

			String config_json = config.toJson();
			String hpData_json = hpData.toJson();

			Map<String, String> redis_map = payToRedis(groupId, pid, gameId, name, deskId, config_json, hpData_json,
					hpOnOff, hp_times, reward, rewardType, rewardValueType, xipai_reward, xipai_rewardType,
					xipai_rewardValueType, robot_room);
			jedis11.hmset(p_key, redis_map);
			jedis11.hincrBy(p_key, "cache_ver", 1);
			String gp_key = GroupCache.genPidsKey(groupId);
			jedis11.zadd(gp_key, 1 * 10 + hpOnOff, pid + "");

			GroupPublisherService.updatePlayEvt(groupId, pid);
		} finally {
			lock.unlock();
		}
		return config;
	}

	/**
	 * 分配成员
	 * 
	 * @param groupId
	 * @param parId
	 * @param tagId
	 * @throws Exception
	 */
	public static final void distributeMember(int groupId, int parId, int tagId) throws Exception {
		String tag_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = null;
		RedisLock lock1 = null;
		try {
			lock = new RedisLock(tag_key, jedis10);
			lock.lock();

			String par_key = GroupMemberCache.genKey(groupId, parId);
			lock1 = new RedisLock(par_key, jedis10);
			lock1.lock();
			int par_partner = Integer.parseInt(jedis10.hget(par_key, "partnerLev"));
			if (par_partner == 0) {
				throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
			}

			String member_opt = jedis10.hget(tag_key, "opt");
			if (StringUtil.isNotEmpty(member_opt) && member_opt.equals("1")) {
				int member_lev = Integer.parseInt(jedis10.hget(tag_key, "lev"));
				if (member_lev < 3) {
					throw new WebException(ErrorCode.GROUP_TAG_ISMGR);
				}
				int member_partner = Integer.parseInt(jedis10.hget(tag_key, "partnerLev"));
				if (member_partner > 0) {
					throw new WebException(ErrorCode.GROUP_ALREADY_PARTNER);
				}
				int member_parentId = Integer.parseInt(jedis10.hget(tag_key, "parentId"));
				if (member_parentId > 0) {
					throw new WebException(ErrorCode.GROUP_MEMBER_EXIST_PARTENER);
				}
				// int member_hp = Integer.parseInt(jedis10.hget(tag_key, "hp"));
				// List<Integer> par_list = Utility.getMemberParents(jedis10,groupId, parId,
				// true);
				// for(Integer p : par_list) {
				// String ph_key = String.format("g{%s}:par_hp:%s", groupId,p);
				// jedis10.incrBy(ph_key, member_hp);
				// }
				String sql = String.format("update group_member set parentId = %s WHERE uid = %s and groupId = %s",
						parId, tagId, groupId);
				Utility.evtdb(groupId, 1, sql);
				jedis10.hset(tag_key, "parentId", parId + "");
				BaseCache.updateCacheVer(jedis10, tag_key);
			} else {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
		} finally {
			if (lock != null) {
				lock.unlock(false);
			}
			if (lock1 != null) {
				lock1.unlock();
			}
		}

	}

	/**
	 * 获取合伙人管理列表
	 * 
	 * @param groupId
	 * @param tagId
	 * @param limit
	 * @param num
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getPartnerInfos(int groupId, int uid, int tagId, int limit, int num, int simpleAll,
			boolean diff) throws Exception {
		Jedis jedis10_1 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
			if (uid_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			if (uid_bean.lev >= 3) {
				List<Integer> par_list = Utility.getMemberParents(jedis10_1, groupId, tagId, true);
				if (par_list == null || !par_list.contains(uid)) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			jedis10_1.close();
		}

		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		String strLev = Redis.use("group1_db10").hget(gm_key, "lev");
		boolean mgr = StringUtil.isNotEmpty(strLev) && Integer.parseInt(strLev) < 3;

		ITArray list = null;

		String limitSql = "";
		String paramSql = "";
		if (simpleAll == 0) {

			limitSql = String.format("limit %s,%s", limit, num);
			paramSql = String.format(
					"SELECT A.uid,A.hp,A.lev,A.partnerLev, A.autoscore," + "(SELECT COUNT(uid) "
							+ "FROM group_member B " + "where B.groupId = %s AND B.parentId = A.uid) AS total",
					groupId);
		} else {

			paramSql = "SELECT A.uid";
		}

		if (mgr) {

			String sql = String.format("%s FROM group_member AS A " + "where A.groupId= %s and A.partnerLev=1 %s",
					paramSql, groupId, limitSql);

			list = DataBase.use().executeQueryByTArray(sql);

		} else {
			if (diff) {
				String sql = String.format("%s FROM group_member AS A " + "where A.groupId= %s and A.parentId=%s %s",
						paramSql, groupId, tagId, limitSql);

				list = DataBase.use().executeQueryByTArray(sql);
			} else {
				String sql = String.format(
						"%s FROM group_member AS A " + "where A.groupId= %s and A.parentId=%s AND partnerLev >0 %s",
						paramSql, groupId, tagId, limitSql);

				list = DataBase.use().executeQueryByTArray(sql);
			}
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			for (int i = 0; i < list.size(); ++i) {
				ITObject obj = list.getTObject(i);
				int tagUid = obj.getInt("uid");
				AccountBean acc = AccountCache.getAccount(tagUid);
				obj.putString("nick", acc.nick);

				if (simpleAll == 0) {
					obj.putString("portrait", acc.portrait);

					GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
					int score = 0;
					if (gmb != null) {
						score = gmb.score;
					}
					obj.putInt("score", score);

				}
			}
		} finally {
			jedis10.close();
		}

		ITObject obj1 = TObject.newInstance();
		obj1.putTArray("members", list);
		obj1.putInt("limit", limit);
		return obj1;
	}

	/**
	 * 获取合伙人管理列表
	 * 
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num
	 * @return
	 * @throws Exception
	 */
	public static final ITObject queryPartnerInfos(int groupId, int uid, int qid, String tagName) throws Exception {

		String gm_key = GroupMemberCache.genKey(groupId, uid);
		String strLev = Redis.use("group1_db10").hget(gm_key, "lev");
		boolean mgr = StringUtil.isNotEmpty(strLev) && Integer.parseInt(strLev) < 3;
		String partnerParents;
		ITArray list = null;

		if (mgr) {
			partnerParents = "and A.partnerLev=1";
		} else {
			partnerParents = String.format("and A.parentId=%s", uid);
		}

		String paramSql = String.format("SELECT A.uid,A.hp,A.partnerLev," + "(SELECT COUNT(uid) "
				+ "FROM group_member B " + "where B.groupId = %s AND B.parentId = A.uid) AS total", groupId);

		String qid_sql = StringUtil.Empty;
		if (qid > 0) {

			qid_sql = "AND A.uid=" + qid;

			String sql = String.format("%s FROM group_member AS A " + "where A.groupId= %s %s %s", paramSql, groupId,
					qid_sql, partnerParents);

			list = DataBase.use().executeQueryByTArray(sql);
		}

		if (StringUtil.isNotEmpty(tagName)) {

			String sql = String.format("%s FROM group_member AS A "
					+ "right join account as B on A.uid = B.id and B.nick like '%%%s%%' " + "where A.groupId= %s %s",
					paramSql, tagName, groupId, partnerParents);

			ITArray tempL = DataBase.use().executeQueryByTArray(sql);
			if (tempL != null && tempL.size() > 0) {

				if (list != null) {
					for (int k = 0; k < tempL.size(); k++) {

						ITObject mo = tempL.getTObject(k);
						if (qid > 0 && qid == mo.getInt("uid")) {
							continue;
						}
						list.addTObject(mo);
					}
				} else {
					list = tempL;
				}
			}
		}

		ITObject obj1 = TObject.newInstance();

		if (list != null) {

			Jedis jedis10 = Redis.use("group1_db10").getJedis();
			try {
				for (int i = 0; i < list.size(); ++i) {
					ITObject obj = list.getTObject(i);
					int tagUid = obj.getInt("uid");
					AccountBean acc = AccountCache.getAccount(tagUid);
					obj.putString("nick", acc.nick);
					obj.putString("portrait", acc.portrait);
				}
			} finally {
				jedis10.close();
			}
			obj1.putTArray("members", list);
		}

		return obj1;
	}

	/**
	 * 获取合伙人成员列表
	 * 
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getPartnerMemebers(int groupId, int uid, int limit, int num, int qid)
			throws Exception {
		ITArray list = null;
		boolean self = (qid > 0 && qid == uid);
		if (self) {
			list = TArray.newInstance();
			ITObject obj = TObject.newInstance();
			obj.putInt("uid", uid);
			obj.putInt("partnerLev", 1);
			list.addTObject(obj);
		}

		if (qid == 0 || !self) {

			String qid_sql = StringUtil.Empty;
			if (qid > 0) {
				qid_sql = "AND uid=" + qid;
			}
			String sql = String.format(
					"SELECT uid,partnerLev,join_time FROM group_member WHERE groupId=%s AND parentId=%s %s limit %s,%s",
					groupId, uid, qid_sql, limit, num);
			ITArray arr = DataBase.use().executeQueryByTArray(sql);
			if (list != null) {
				for (int i = 0; i < arr.size(); ++i) {
					list.addTObject(arr.getTObject(i));
				}
			} else {
				list = arr;
			}
		}

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			for (int i = 0; i < list.size(); ++i) {
				ITObject obj = list.getTObject(i);
				AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
				obj.putString("nick", acc.nick);
				obj.putString("portrait", acc.portrait);

				String gmr = String.format("g{%s}:m%s:round_log", groupId, obj.getInt("uid"));
				String val = jedis10.get(gmr);
				int value = 0;
				if (StringUtil.isNotEmpty(val)) {
					value = Integer.parseInt(val);
				}
				obj.putInt("round", value);

				GroupMemberBean gmb = GroupCache.getMember(groupId, acc.id);
				if (gmb != null) {
					obj.putInt("online", 1);
				} else {
					obj.putInt("online", 0);
				}

				// _fillLog(jedis9, jedis10,obj, groupId, uid, acc.id, false, true);
			}
		} finally {
			jedis10.close();
			// jedis9.close();
		}
		ITObject obj1 = TObject.newInstance();
		obj1.putTArray("members", list);
		obj1.putInt("limit", limit);
		return obj1;
	}

	/**
	 * 
	 * @param permission
	 * @throws Exception
	 */
	public static void setMgrPermission(int groupId, int tag, int permission) throws Exception {
		log.info("setMgrPermission tag:" + tag + " permission:" + permission);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String gm_key = GroupMemberCache.genKey(groupId, tag);
		RedisLock lock = new RedisLock(gm_key, jedis10);
		try {
			lock.lock();
			List<String> _list = jedis10.hmget(gm_key, "opt", "lev");
			String opt1 = _list.get(0);
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			int _lev = Integer.parseInt(_list.get(1));
			if (_lev != 2) {
				throw new WebException(ErrorCode.GROUP_MGR_EXIST);
			}
			jedis10.hset(gm_key, "permission", permission + "");
			BaseCache.updateCacheVer(jedis10, gm_key);
		} finally {
			lock.unlock();
		}
	}

	/**
	 * 获取禁止同桌列表
	 * 
	 * @param groupId
	 * @param uid
	 * @param tag
	 * @return
	 * @throws Exception
	 */
	public static ITArray getBanDeskList(int groupId, int uid, int tag) throws Exception {
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String mng_key = GroupMemberCache.genKey(groupId, uid);
		RedisLock lock1 = new RedisLock(mng_key, jedis10);
		try {
			lock1.lock();
			int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
			if (mgn_lev == 2) {
				String permission = jedis10.hget(mng_key, "permission");
				if (StringUtil.isEmpty(permission) || (Integer.parseInt(permission) & PERMISSION_BAN_DESKMATE) == 0) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			lock1.unlock(false);
		}
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		String ban_key = "ban{" + groupId + "}:" + tag;
		try {
			Set<String> sets = jedis11.smembers(ban_key);
			ITArray arr = TArray.newInstance();
			for (String id : sets) {
				ITObject obj = TObject.newInstance();
				int _id = Integer.parseInt(id);
				AccountBean acc = AccountCache.getAccount(_id);
				if (acc == null) {
					jedis11.srem(ban_key, id);
					continue;
				}
				String key = GroupMemberCache.genKey(groupId, acc.id);
				String opt = jedis10.hget(key, "opt");
				if (StringUtil.isEmpty(opt) || Integer.parseInt(opt) != 1) {
					jedis11.srem(ban_key, id);
					continue;
				}
				obj.putInt("uid", _id);
				obj.putString("nick", acc.nick);
				obj.putString("portrait", acc.portrait);
				arr.addTObject(obj);
			}
			return arr;
		} finally {
			jedis11.close();
			jedis10.close();
		}
	}

	/**
	 * 设置禁止同桌
	 * 
	 * @param groupId
	 * @param uid
	 * @param tag
	 * @param ban_list
	 */
	public static void setBanDesk(int groupId, int uid, int tag, ITArray ban_list, ITArray del_list) throws Exception {
		log.info("setBanDesk uid:" + uid + " tag:" + tag + " ban_list:" + ban_list + " del_list:" + del_list);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		String mng_key = GroupMemberCache.genKey(groupId, uid);
		RedisLock lock1 = new RedisLock(mng_key, jedis10);
		try {
			lock1.lock();
			int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
			if (mgn_lev == 2) {
				String permission = jedis10.hget(mng_key, "permission");
				if (StringUtil.isEmpty(permission) || (Integer.parseInt(permission) & PERMISSION_BAN_DESKMATE) == 0) {
					throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
				}
			}
		} finally {
			lock1.unlock(false);
		}
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		String ban_key = "ban{" + groupId + "}:" + tag;
		RedisLock lock = new RedisLock("ban_desk" + groupId, jedis11);
		try {
			lock.lock();
			String tag_str = tag + "";
			Pipeline pipeline = jedis11.pipelined();
			pipeline.del(ban_key);
			if (del_list.size() > 0) {
				for (int i = 0; i < del_list.size(); ++i) {
					int _id = del_list.getInt(i);
					if (_id == 0)
						continue;
					if (_id == tag)
						continue;
					String key = "ban{" + groupId + "}:" + _id;
					pipeline.srem(key, tag_str);
				}
			}
			if (ban_list.size() == 1 && ban_list.getInt(0) == 0) {
				return;
			}
			int size = Math.min(ban_list.size(), 20);
			for (int i = 0; i < size; ++i) {
				int _id = ban_list.getInt(i);
				if (_id == 0)
					continue;
				if (_id == tag)
					continue;
				String key = GroupMemberCache.genKey(groupId, _id);
				String opt = jedis10.hget(key, "opt");
				if (StringUtil.isEmpty(opt) || Integer.parseInt(opt) != 1) {
					continue;
				}
				String _bkey = "ban{" + groupId + "}:" + _id;
				pipeline.sadd(ban_key, _id + "");
				pipeline.sadd(_bkey, tag_str);
			}
			pipeline.sync();
		} finally {
			lock.unlock();
			jedis10.close();
		}

	}

	public static final ITObject movePartner(int groupId, int uid, int tag) throws Exception {
		log.info("movePartner uid:" + uid + " tag:" + tag);
		if (uid == tag) {
			throw new WebException(ErrorCode._FAILED);
		}
		// GroupMemberBean tag_bean = GroupCache.getMember(groupId, tag);
		// uid //目标
		String group_key = GroupCache.genKey(groupId);
		Jedis jedis11 = Redis.use("group1_db11").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		RedisLock lock = new RedisLock(group_key, jedis11);
		RedisLock lock1 = null;
		RedisLock lock2 = null;

		String gm_key3 = GroupMemberCache.genKey(groupId, tag);
		// Jedis jedis10 = Redis.use("group1_db10").getJedis();
		// RedisLock lock3 = new RedisLock(gm_key3, jedis10);

		try {
			lock.lock();
			// lock3.lock();
			jedis11.hset(group_key, "stop", "1");
			BaseCache.updateCacheVer(jedis11, group_key);
			String tag_key = GroupMemberCache.genKey(groupId, tag);
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			lock1 = new RedisLock(mng_key, jedis10);
			lock2 = new RedisLock(tag_key, jedis10);
			lock1.lock();
			lock2.lock();
			GroupMemberBean tag_bean = GroupCache.getMember(groupId, tag);// 目标用户信息
			if (tag_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
			if (mng_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			int mgn_lev = mng_bean.lev;
			int mgn_partner = mng_bean.partnerLev;

			int member_partner = tag_bean.partnerLev;

			int member_parentId = tag_bean.parentId;
			if (member_parentId == uid) {
				throw new WebException(ErrorCode._FAILED);
			}
			List<Integer> child_list = Utility.getChildParentList(groupId, tag, true);

			/*
			 * if (mgn_lev == 3) { if (child_list.contains(uid)) { throw new
			 * WebException(ErrorCode._FAILED); } }
			 */

			// //--同步分组
			log.info("movePartner uid:" + uid + " tag:" + tag + "queueid:" + mng_bean.queueid);
			jedis10.hset(tag_bean.redis_key, "queueid", mng_bean.queueid + "");
			BaseCache.updateCacheVer(jedis10, GroupMemberCache.genKey(groupId, tag));

			String opt1 = jedis10.hget(gm_key3, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}

			boolean isPartner = false;
			GroupMemberBean gmb3 = GroupCache.getMember(groupId, tag);
			if (gmb3.partnerLev > 0) {
				isPartner = true;
			}

			String sql3;
			// String _ban = ban + "";
			if (isPartner) {
				HashSet<Integer> hashSet = new HashSet<>();
				hashSet.add(tag);
				ArrayList<Integer> arrayList = new ArrayList<>();
				arrayList.add(tag);

				gm_key3 = GroupMemberCache.genKey(groupId, tag);
				// jedis10.hset(gm_key, "ban", _ban);
				// jedis10.hset(gm_key, "group_ban", _ban);
				BaseCache.updateCacheVer(jedis10, gm_key3);

				while (!arrayList.isEmpty()) {
					ArrayList<Integer> arrayListTemp = new ArrayList<>();
					arrayListTemp.addAll(arrayList);
					arrayList.clear();

					for (int i = 0; i < arrayListTemp.size(); ++i) {
						int tmpTagId = arrayListTemp.get(i);
						String child_list2 = Utility.getChildParentSql(groupId, tmpTagId, true);

						// sql = String.format("update group_member set ban = %s where groupId = %s and
						// (parentId in(%s) or uid = %s)", _ban, groupId,child_list2,tmpTagId);
						// Utility.evtdb(groupId, 1, sql);

						sql3 = String.format(
								"select uid from group_member where groupId = %s and (parentId in(%s) or uid = %s)",
								groupId, child_list2, tmpTagId);

						ITArray tmplist = DataBase.use().executeQueryByTArray(sql3);
						if (tmplist.size() > 0) {
							for (int j = 0; j < tmplist.size(); ++j) {

								ITObject tem = tmplist.getTObject(j);
								int childId = tem.getInt("uid");

								if (hashSet.contains(childId))
									continue;

								gm_key3 = GroupMemberCache.genKey(groupId, childId);
								jedis10.hset(gm_key3, "queueid", mng_bean.queueid + "");
								// jedis10.hset(gm_key, "group_ban", _ban);
								BaseCache.updateCacheVer(jedis10, gm_key3);

								hashSet.add(childId);
								arrayList.add(childId);
							}
						}
					}
				}
			} else {

				// sql = String.format("update group_member set ban = %s where uid = %s AND
				// groupId = %s", _ban, tagId,groupId);
				// Utility.evtdb(groupId, 1, sql);
				jedis10.hset(gm_key3, "queueid", mng_bean.queueid + "");
				BaseCache.updateCacheVer(jedis10, gm_key3);
			}

			// end 分组同步

			List<Integer> tag_par_list = Utility.getMemberParents(jedis10, groupId, tag, false);
			// String _tag_hp = jedis10.get(String.format("g{%s}:par_hp:%s", groupId,tag));
			List<Integer> mng_par_list = null;
			if (mgn_lev == 3) {
				mng_par_list = Utility.getMemberParents(jedis10, groupId, uid, true);
			}
			Pipeline pipeline = jedis10.pipelined();
			String gp_key = GroupCache.genPidsKey(groupId);
			Set<String> pids = Redis.use("group1_db11").zrangeByScore(gp_key, 10, 11);
			for (String pid : pids) {
				String p_reward_key = GroupCache.genRewardKey(groupId, Integer.parseInt(pid));
				for (Integer child : child_list) {
					pipeline.zrem(p_reward_key, child.toString());
				}
				String p_xipai_reward_key = GroupCache.genXiPaiRewardKey(groupId, Integer.parseInt(pid));
				for (Integer child : child_list) {
					pipeline.zrem(p_xipai_reward_key, child.toString());
				}
			}
			if (tag_par_list != null) {
				for (Integer par : tag_par_list) {
					String list_key = GroupCache.genParListKey(groupId, par);
					for (Integer child : child_list) {
						pipeline.srem(list_key, child.toString());
					}
				}
			}
			pipeline.hset(tag_key, "parentId", uid + "");
			pipeline.hincrBy(tag_key, "cache_ver", 1);
			int diff = (mgn_partner + 1) - member_partner;
			if (diff != 0) {
				for (Integer child : child_list) {
					String key = GroupMemberCache.genKey(groupId, child);
					pipeline.hincrBy(key, "partnerLev", diff);
					pipeline.hincrBy(key, "cache_ver", 1);
				}
			}
			if (mgn_lev == 3) {
				for (Integer par : mng_par_list) {
					String list_key = GroupCache.genParListKey(groupId, par);
					// String hp_key = String.format("g{%s}:par_hp:%s", groupId,par);
					// pipeline.incrBy(hp_key, tag_hp);
					for (Integer child : child_list) {
						pipeline.sadd(list_key, child.toString());
					}
				}
			}
			pipeline.sync();
			String sql = String.format("update group_member set parentId=%s where uid=%s and groupId=%s", uid, tag,
					groupId);
			Utility.evtdb(groupId, 1, sql);
			if (diff != 0) {
				String p = StringUtil.Empty;
				int len = child_list.size();
				for (int i = 0; i < len; i++) {
					Integer child = child_list.get(i);
					p += child + ((i < len - 1) ? "," : StringUtil.Empty);
				}
				sql = String.format("update group_member set partnerLev=partnerLev+%s where uid in(%s) and groupId=%s",
						diff, p, groupId);
				Utility.evtdb(groupId, 1, sql);
			}
			ITObject resData = TObject.newInstance();
			resData.putInt("parentId", uid);
			resData.putInt("partnerLev", member_partner + diff);
			return resData;

		} finally {
			jedis11.hset(group_key, "stop", "0");
			BaseCache.updateCacheVer(jedis11, group_key);
			lock.unlock();
			lock1.unlock(false);
			lock2.unlock();
			// lock3.unlock();
		}
	}

	/**
	 * 获取邮件列表
	 * 
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num
	 * @return
	 */
	public static ITArray getMailList(int groupId, int uid, int limit, int num) {
		String mail_key = GroupCache.genMailKey(groupId, uid);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			int time = (int) (System.currentTimeMillis() / 1000);
			int s_time = time - 10 * 24 * 3600;
			jedis10.zremrangeByScore(mail_key, 0, s_time);
			Set<String> sets = jedis10.zrevrangeByScore(mail_key, time, s_time, limit, num);
			ITArray arr = TArray.newInstance();
			for (String str : sets) {
				arr.addString(str);
			}
			String mail_tip_key = GroupCache.genMailTipKey(groupId);
			jedis10.zadd(mail_tip_key, 0, uid + "");
			return arr;
		} finally {
			jedis10.close();
		}
	}

	/**
	 * 删除所有邮件
	 * 
	 * @param groupId
	 * @param uid
	 */
	public static void delMailAll(int groupId, int uid) {
		String mail_key = GroupCache.genMailKey(groupId, uid);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			jedis10.del(mail_key);
			String mail_tip_key = GroupCache.genMailTipKey(groupId);
			jedis10.zadd(mail_tip_key, 0, uid + "");
		} finally {
			jedis10.close();
		}

	}

	/**
	 * 设置合伙人阀值groupId, acc.id, tagId, score
	 */
	public static final int setPartenerAutoScore(int groupId, int uid, int tagId, int score) {
		log.info("setPartenerAutoScore uid:" + uid + " tagId:" + tagId + " score:" + score);
		String gm_key = GroupMemberCache.genKey(groupId, tagId);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			String opt1 = jedis10.hget(gm_key, "opt");
			if (StringUtil.isEmpty(opt1) || Integer.parseInt(opt1) != 1) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			String mng_key = GroupMemberCache.genKey(groupId, uid);
			RedisLock lock1 = new RedisLock(mng_key, jedis10);
			try {
				lock1.lock();
				int mgn_lev = Integer.parseInt(jedis10.hget(mng_key, "lev"));
				if (mgn_lev == 2) {
					String permission = jedis10.hget(mng_key, "permission");
					if (StringUtil.isEmpty(permission) || (Integer.parseInt(permission) & PERMISSION_BAN) == 0) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				} else if (mgn_lev >= 3) {
					if (uid != tagId) {
						List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, tagId, true);
						if (par_list == null || !par_list.contains(uid)) {
							throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
						}
					}
				}
			} finally {
				lock1.unlock(false);
			}
			score = score * 1000; // deyou 1000
			// score = score * 100; // wanshun 100
			String sql = String.format("update group_member set autoscore = %s where uid = %s " + "AND groupId = %s ",
					score, tagId, groupId);
			try {
				Utility.evtdb(groupId, 1, sql);
				jedis10.hset(gm_key, "autoscore", score + "");
				BaseCache.updateCacheVer(jedis10, gm_key);
			} finally {

			}

		} catch (Exception ex) {

		} finally {
			jedis10.close();
		}
		return 1;
	}

	/**
	 * 获取成员列表
	 *
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num
	 * @param minus_only
	 * @param type       1 多到少 2少到多
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getAllPartner(int groupId, int uid) throws Exception {
		ITObject obj1 = TObject.newInstance();

		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb.owner == uid) {
			// 查看所有合伙人
			String sql = "select gm.uid,gm.partnerLev,a.nick from group_member gm,account a where gm.uid=a.id and gm.groupId=? and gm.partnerLev>0";
			String[] params1 = new String[1];
			params1[0] = groupId + "";

			ITArray arr = DataBase.use().executeQueryByTArrayLogin(sql, params1);
			if (arr.size() > 0) {
				obj1.putTArray("partners", arr);
			}
		} else {
			// 查看伞下合伙人
			ITArray allPartners = getAllUnderPartnersBatch(groupId, uid);
			obj1.putTArray("partners", allPartners);
		}

		return obj1;

	}

	/**
	 * 批量递归查询伞下合伙人
	 */
	public static ITArray getAllUnderPartnersBatch(int groupId, int uid) {
		try {
			ITArray allPartners = TArray.newInstance();
			Set<Integer> processedUids = new HashSet<>(); // 防止循环引用
			Queue<Integer> uidQueue = new LinkedList<>();

			uidQueue.add(uid);

			while (!uidQueue.isEmpty()) {
				Integer currentUid = uidQueue.poll();

				// 查询当前用户的下属合伙人
				String sql = "select gm.uid,gm.partnerLev,a.nick from group_member gm,account a where gm.uid=a.id and gm.groupId=? and gm.partnerLev>0 and gm.parentId=?";
				String[] params1 = new String[2];
				params1[0] = groupId + "";
				params1[1] = currentUid + "";

				ITArray directPartners;

				directPartners = DataBase.use().executeQueryByTArrayLogin(sql, params1);

				for (int i = 0; i < directPartners.size(); i++) {
					ITObject partner = directPartners.getTObject(i);
					Integer partnerUid = partner.getInt("uid");

					// 避免重复处理
					if (!processedUids.contains(partnerUid)) {
						processedUids.add(partnerUid);
						allPartners.addTObject(partner);
						uidQueue.add(partnerUid); // 加入队列继续查询其下属
					}
				}

			}
			return allPartners;
		} catch (Exception e) {
			log.error("查询伞下合伙人失败", e);
		}
		return null;
	}

	public static void sp_add_member(int groupId, int mgn_uid, int tagId) {
		try {
			String sql = String.format("select count(1)  from group_member where uid = %s AND groupId = %s for update",
					tagId, groupId);
			ITArray arr = DataBase.use().executeQueryByTArray(sql);
			if (arr.size() > 0) {
				throw new WebException(ErrorCode.GROUP_MEMBER_EXIST);
			} else {
				log.info("录入group_member: tagId-" + tagId + ",parentId-" + mgn_uid);
				String addSql = String.format(
						"INSERT INTO group_member(uid,groupId,join_time,parentId) VALUES(%s,%s,%s,%s)", tagId, groupId,
						System.currentTimeMillis(), mgn_uid);
				DataBase.use().executeUpdate(addSql);
			}
		} catch (Exception e) {
			log.error("加入成员失败", e);
		}

	}
}
