package com.group.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.data.bean.AccountBean;
import com.data.bean.GroupMemberBean;
import com.data.cache.AccountCache;
import com.data.cache.GroupCache;
import com.data.cache.GroupMemberCache;
import com.data.util.ConsumeCode;
import com.data.util.ErrorCode;
import com.data.util.Utility;
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.util.DateUtils;
import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;
import com.taurus.web.WebException;

import redis.clients.jedis.Jedis;


public class GroupLogService {

	private static final Logger log = Logger.getLogger(GroupLogService.class);
	//体力值变化
	private static final int CHOOSE_CHAGE = 1;
	//操作上分
	private static final int CHOOSE_UPPER = 2;
	//操作下分
	private static final int CHOOSE_SUB = 4;
	//抽水
	private static final int CHOOSE_PUMP = 8;
	//奖励
	private static final int CHOOSE_RWARD = 16;
	//转账
	private static final int CHOOSE_TRADE = 32;
	//提取
	private static final int CHOOSE_TAKE_REWARD = 64;
	//洗牌
	private static final int CHOOSE_XIPAI = 128;

	//暗抽
	private static final int CHOOSE_ANCHOU = 256;

	/**
	 * 上下分记录类型
	 */
	private static final int[] MGRLOG_TYPE = {ConsumeCode.HP_MGR_UPPER,ConsumeCode.HP_MGR_SUB,ConsumeCode.HP_PARTNER_UPPER,ConsumeCode.HP_PARTNER_SUB};
	/**
	 * 获取成员排行
	 * @param groupId
	 * @param pid
	 * @param limit
	 * @param num
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getMemberRank(int groupId, int uid, int pid, int type, int limit,int num,int beginTime,int endTime)  throws Exception{
		ITObject obj1 = TObject.newInstance();
		GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
		if (mng_bean == null) {
			throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
		}

		int mgn_partner = mng_bean.partnerLev;
		int mgn_lev = mng_bean.lev;

		String strGodsList = null;
		Set<String> godsList = Redis.use("group1_db1").smembers("gods");
		boolean findok = false;
		if(!godsList.isEmpty()) {
			for (String mem : godsList) {
				if (strGodsList == null) {
					strGodsList = mem;
				} else {
					strGodsList += "," + mem;
				}
			}
			strGodsList = "("+strGodsList+")";
			findok = true;
		}

		//log.info("getMemberRank strGodsList:"+strGodsList+" mgn_partner:"+mgn_partner + " mgn_lev:"+mgn_lev);


		//if (mgn_lev == 1 && mgn_partner == 0) {
			String sql = String.format("SELECT uid,SUM(win) AS win,SUM(round) as round,SUM(score) as score FROM group_member_log WHERE groupId = %s ",groupId);
			if(pid > 0) {
				sql = sql + " AND pid =" + pid;
			}
			if (findok){
				sql = sql+ " AND uid not in "+strGodsList;
			}
			if (type == 0)
			{
				sql = sql + String.format(" AND time >=%s AND time <%s GROUP BY uid ORDER BY round DESC limit %s,%s",beginTime,endTime,limit,num);
			}
			else if (type == 1)
			{
				sql = sql + String.format(" AND time >=%s AND time <%s GROUP BY uid ORDER BY score DESC limit %s,%s",beginTime,endTime,limit,num);
			}

			//log.info("getMemberRank sql:"+sql);
			ITArray arr =DataBase.use().executeQueryByTArray(sql);
			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);
			}
			obj1.putTArray("ranks", arr);
			String sql2 = String.format("select count(uid) as num from group_member_log where groupId = %s and round > 0 and time >=%s AND time <%s ", groupId, beginTime, endTime);
			if (findok){
				sql2 = sql2+ " and uid not in "+strGodsList;
			}
			//log.info("getMemberRank sql2:"+sql2);
			int allActiveNum = 0;
			ITArray arr2 =DataBase.use().executeQueryByTArray(sql2);
			if(arr2.size()>0) {
				ITObject obj = arr2.getTObject(0);
				allActiveNum = obj.getLong("num").intValue();
			}
			obj1.putInt("allActiveNum", allActiveNum);
		//}

		obj1.putInt("limit", limit);
		return obj1;
	}

	/**
	 * 获取战绩列表
	 * @param platform
	 * @param groupId
	 * @param limit
	 * @param num
	 * @return
	 */
	public static ITObject getPersonRecords(String platform,int groupId,int timeType,int beginTime,int endTime,int limit,int num,int qid,int uid) throws Exception{
		String qid_sql = StringUtil.Empty;
		qid_sql = "and uid="+qid;
		
		ITArray militaryList = TArray.newInstance();

		ITObject obj1 = TObject.newInstance();
		if(timeType == 0) {
			
			beginTime = DateUtils.getBeginDay() ;
			endTime = DateUtils.getBeginDay() + 3600 * 24;
		}else if(timeType == 1) {
			
			beginTime = DateUtils.getBeginDay() - 3600 * 24 ; 
			endTime = DateUtils.getBeginDay();
		}
		else if(timeType == 2) {
			
			beginTime = DateUtils.getBeginDay() - 3600 * 48 ; 
			endTime = DateUtils.getBeginDay() - 3600 * 24;
		}
		
		int total = 0;
		int consume = 0;
		
		String where = String.format("gid=%s and time>=%s and time<=%s",groupId,beginTime,endTime);
		if(limit==0) {
		
			String count_sql = String.format("SELECT count(*) AS total FROM room_rec_log where %s %s",where,qid_sql);
			ITArray arr =DataBase.use().executeQueryByTArray(count_sql);
			if(arr.size()>0) {
				ITObject obj = arr.getTObject(0);
				total = obj.getLong("total").intValue();
			}
			
			Jedis jedis9 = Redis.use("group1_db9").getJedis();
			try {
				
				String key = String.format("g%s:m%s:consume_hp", groupId,uid);
				if(timeType >= 0 && timeType <=2) {
					
					String hp_key = String.format("%s:d%s",key,beginTime);
					String r_str = jedis9.get(hp_key);
					int r_num = 0;
					if(StringUtil.isNotEmpty(r_str)) {
						r_num = Integer.parseInt(r_str);
					}
					consume = r_num;
				}
				else{
					int timeBegin = beginTime;
					while(timeBegin <= endTime) {
						
						String hp_key = String.format("%s:d%s",key,timeBegin);
						String r_str = jedis9.get(hp_key);
						int r_num = 0;
						if(StringUtil.isNotEmpty(r_str)) {
							r_num = Integer.parseInt(r_str);
						}
						consume += r_num;
						timeBegin += 86400;
					}
				}

				getMemberData(jedis9,uid,obj1,groupId, 0,beginTime,endTime,false,false);
			}finally {
					jedis9.close();
			}
		}
		
		String sql = String.format("select rec_key from room_rec_log where %s %s GROUP BY roomid ORDER BY time desc limit %s,%s",
				where,qid_sql,limit,num);
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		
		if(list.size()>0) {
			Jedis jedis5 = Redis.use("group1_db5").getJedis();
			try {
				for(int i=0;i<list.size();++i) {
					ITObject tem = list.getTObject(i);
					String rec_key = tem.getString("rec_key");
					ITObject obj = Utility.getMilitaryList(jedis5,rec_key,platform);
					if(obj==null) {
						continue;
					}
					militaryList.addTObject(obj);
				}
			}finally {
				jedis5.close();
			}
		}
		
		obj1.putTArray("records", militaryList);
		obj1.putInt("limit", limit);
		obj1.putInt("total", total);
		obj1.putInt("consume", consume);
		return obj1;
	}
	
	public static ITObject getPartnerStatPlay(int groupId,int uid,int parent_id,
			int limit, int num,
			int timeType,int beginTime, int endTime) throws Exception{
		
		ITObject obj = TObject.newInstance();
		
		String gp_key = GroupCache.genPidsKey(groupId);
		Set<String> pids = Redis.use("group1_db11").zrangeByScore(gp_key, 10, 11);
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		ITArray arr = TArray.newInstance();
		try {
			for(String pid:pids) {
				
				ITObject objp = TObject.newInstance();
				
				objp.putInt("pid", Integer.parseInt(pid));
				
				if(timeType == 0) {
					
					String key = String.format("g{%s}:m%s:valid_round:p%s", groupId,uid,pid);
					int round = _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("round", round);
					
					key = String.format("g%s:hp_cost:m%s:p%s",groupId,uid,pid);
					int pump = _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("pump", pump);
					
					key = String.format("g{%s}:m%s:p%s:reward_log", groupId,uid,pid);
					int award =  _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("award", award);
				}
				else if(timeType == 1) {
					
					String curMonth = ":m" + DateUtils.getBeginMonth();
					
					String key = String.format("g{%s}:m%s:valid_round:p%s%s", groupId,uid,pid,curMonth);
					int round = _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("round", round);
					
					key = String.format("g%s:hp_cost:m%s:p%s%s",groupId,uid,pid,curMonth);
					int pump = _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("pump", pump);
					
					key = String.format("g{%s}:m%s:p%s:reward_log%s", groupId,uid,pid,curMonth);
					int award =  _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("award", award);
				}
				else if(timeType == 2) {
					
					String curMonth = ":m" + DateUtils.getBeginLastMonth();
					
					String key = String.format("g{%s}:m%s:valid_round:p%s%s", groupId,uid,pid,curMonth);
					int round = _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("round", round);
					
					key = String.format("g%s:hp_cost:m%s:p%s%s",groupId,uid,pid,curMonth);
					int pump = _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("pump", pump);
					
					key = String.format("g{%s}:m%s:p%s:reward_log%s", groupId,uid,pid,curMonth);
					int award =  _getCountValue(jedis9,key,beginTime,endTime);
					objp.putInt("award", award);
				}
				arr.addTObject(objp);
			}
		}finally {
			jedis9.close();
		}
		
		obj.putTArray("members", arr);
		obj.putInt("limit", limit);
		
		
		return obj;
	}
	
	
	public static ITObject getDirectStatMember(int groupId,int uid,int rootUid,int limit, int num,int timeType,int beginTime, int endTime) throws Exception{
		
		ITObject obj = TObject.newInstance();
		
		String limitSql = String.format("limit %s,%s",limit, num);
		String sql = String.format("SELECT uid, partnerLev"
				+ " FROM group_member"
				+ " WHERE groupId = %s and ((parentId=%s and partnerLev = 0) or uid = %s) GROUP BY uid %s", 
				groupId, uid, uid, limitSql);
		
		
		ITArray temp =  TArray.newInstance();
		
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		try {
	
			for (int i = 0; i < list.size(); ++i) {
				
				ITObject objLine = list.getTObject(i);
				if(objLine.containsKey("uid")) {
					
					int tagUid = objLine.getInt("uid");
					
					AccountBean acc = AccountCache.getAccount(tagUid);
					objLine.putString("nick", acc.nick);

					//int newBeginTime = beginTime - 86400*30;
					getMemberData(jedis9,tagUid,objLine,groupId,timeType, beginTime,endTime,false,false);
					
					if(timeType == 0) {
						{
							String tmpKey = String.format("g{%s}:m%s:", groupId,tagUid);
							int reward_hp = _getCountValue(jedis9,tmpKey + "reward_log",beginTime,endTime);
							objLine.putInt("reward_tongji", reward_hp);
						}

						String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
						int reward_hp = _getCountValue(jedis9,key,beginTime,endTime);
						objLine.putInt("reward_hp", (int)reward_hp);
					}
					else if(timeType == 1){
						{
							String key = String.format("g{%s}:m%s:", groupId,tagUid);
							String curMonth = ":m" + DateUtils.getBeginMonth();
							int reward_hp = _getValue(jedis9,key + "reward_log" + curMonth);
							objLine.putInt("reward_tongji", reward_hp);
						}


						String curMonth = ":m" + DateUtils.getBeginMonth();
						String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
						int reward_hp =_getValue(jedis9,key+ curMonth);
						objLine.putInt("reward_hp", reward_hp);
					}
					else if(timeType == 2){
						{
							String key = String.format("g{%s}:m%s:", groupId,tagUid);
							String curMonth = ":m" + DateUtils.getBeginLastMonth();
							int reward_hp = _getValue(jedis9,key + "reward_log" + curMonth);
							objLine.putInt("reward_tongji", reward_hp);
						}

						String curMonth = ":m" + DateUtils.getBeginLastMonth();
						String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
						int reward_hp =_getValue(jedis9,key+ curMonth);
						objLine.putInt("reward_hp", reward_hp);
					}
					temp.addTObject(objLine);
				}
			}
		}
		finally {
			jedis9.close();
		}
		
		obj.putTArray("members", temp);
		obj.putInt("limit", limit);
		
		
		return obj;
	}

	public static ITObject getXingYunHaoMember(int groupId, int limit, int num,int timeType,int beginTime, int endTime) throws Exception{
		ITObject obj = TObject.newInstance();

		String limitSql = String.format("limit %s,%s",limit, num);

		String strGodsList = null;
		Set<String> godsList = Redis.use("group1_db1").smembers("gods");
		Map<String, String> mapString = Redis.use("group1_db1").hgetAll("gods_special");
		for (Map.Entry<String, String> entry : mapString.entrySet()) {
			String strPlayerId = entry.getKey();
			godsList.remove(strPlayerId);
		}

		List<String> allList = new ArrayList<String>();
		allList.addAll(godsList);
		for(String mem : godsList)
		{
		    if (strGodsList == null)
			{
				strGodsList = mem;
			}
		    else {
				strGodsList += "," + mem;
			}
		}

		String sql = String.format("SELECT uid, partnerLev"
						+ " FROM group_member"
						+ " WHERE groupId = %s and uid in (%s) GROUP BY uid %s",
				groupId, strGodsList, limitSql);



		ITArray temp =  TArray.newInstance();
		int allTotalWin = 0;
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		try {

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

				ITObject objLine = list.getTObject(i);
				if(objLine.containsKey("uid")) {

					int tagUid = objLine.getInt("uid");

					AccountBean acc = AccountCache.getAccount(tagUid);
					objLine.putString("nick", acc.nick);

					getMemberData(jedis9,tagUid,objLine,groupId,timeType, beginTime,endTime,false,false);

					temp.addTObject(objLine);
				}
			}

			for (String user : allList) {
				String key = String.format("g{%s}:m%s:", groupId,user);
				int total_win = _getCountValue(jedis9,key + "total_win_self",beginTime,endTime);
				allTotalWin += total_win;
			}
		}
		finally {
			jedis9.close();
		}

		obj.putTArray("members", temp);
		obj.putInt("limit", limit);
		obj.putLong("all_total_win", allTotalWin);


		return obj;
	}
	
	public static ITObject getPartnerStatMember(int groupId,int uid,int rootUid,int limit, int num,int timeType,int beginTime, int endTime) throws Exception{
		
		ITObject obj = TObject.newInstance();
		
		String limitSql = String.format("limit %s,%s",limit, num);

		String sql = String.format("SELECT uid, partnerLev"
		+ " FROM group_member"
		+ " WHERE groupId = %s and (parentId=%s or uid = %s) and partnerLev > 0 GROUP BY uid %s",
		groupId, uid, uid, limitSql);


		
		ITArray temp =  TArray.newInstance();
		
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		try {
	
			for (int i = 0; i < list.size(); ++i) {
				
				ITObject objLine = list.getTObject(i);
				if(objLine.containsKey("uid")) {
					
					int tagUid = objLine.getInt("uid");
					
					AccountBean acc = AccountCache.getAccount(tagUid);
					objLine.putString("nick", acc.nick);
					
					if(tagUid != uid) {
					    //int newBeginTime = beginTime - 86400 * 30;
						getMemberData(jedis9,tagUid,objLine,groupId,timeType, beginTime,endTime,false,true);
					}
					else {
						//int newBeginTime = beginTime - 86400 * 30;
						//getMemberData(jedis9,tagUid,objLine,groupId,timeType, beginTime,endTime,false,true);
						getDirectMemberData(jedis9,tagUid,objLine,groupId,timeType,beginTime,endTime);
					}


					if(tagUid == uid) {
						
						if(timeType == 0) {
							{
								String tmpKey = String.format("g{%s}:m%s:", groupId,tagUid);
								int reward_hp = _getCountValue(jedis9,tmpKey + "reward_log",beginTime,endTime);
								objLine.putInt("reward_tongji", reward_hp);
							}

							String key = String.format("g{%s}:m%s:d_reward", groupId,tagUid);
							int reward_hp = _getCountValue(jedis9,key,beginTime,endTime);
							objLine.putInt("reward_hp", reward_hp);
						}
						else if(timeType == 1){
							{
								String key = String.format("g{%s}:m%s:", groupId,tagUid);
								String curMonth = ":m" + DateUtils.getBeginMonth();
								int reward_hp = _getValue(jedis9,key + "reward_log" + curMonth);

								objLine.putInt("reward_tongji", reward_hp);
							}

							String curMonth = ":m" + DateUtils.getBeginMonth();
							String key = String.format("g{%s}:m%s:d_reward", groupId,tagUid);
							int reward_hp =_getValue(jedis9,key + curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
						else if(timeType == 2){
							{
								String key = String.format("g{%s}:m%s:", groupId, tagUid);
								String curMonth = ":m" + DateUtils.getBeginLastMonth();
								int reward_hp = _getValue(jedis9, key + "reward_log" + curMonth);
								objLine.putInt("reward_tongji", reward_hp);
							}
							String curMonth = ":m" + DateUtils.getBeginLastMonth();
							String key = String.format("g{%s}:m%s:d_reward", groupId,tagUid);
							int reward_hp =_getValue(jedis9, key+curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
					}
					else {
						if(timeType == 0) {
							{
								long reward_hp_total = 0;
								List<Integer> listParent = Utility.getChildParentList(groupId, tagUid, true);
								for (Integer parId : listParent) {
									String tmpKey = String.format("g{%s}:m%s:", groupId, parId);
									int reward_hp = _getCountValue(jedis9, tmpKey + "reward_log", beginTime, endTime);
									reward_hp_total += reward_hp;
								}
								objLine.putInt("reward_tongji", (int) reward_hp_total);
							}

							String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
							int reward_hp = _getCountValue(jedis9,key,beginTime,endTime);
							objLine.putInt("reward_hp", (int)reward_hp);
						}
						else if(timeType == 1){
							{
								String key = String.format("g{%s}:m%s:", groupId, tagUid);
								String curMonth = ":m" + DateUtils.getBeginMonth();
								int reward_hp = _getValue(jedis9, key + "reward_log" + curMonth);
								objLine.putInt("reward_tongji", reward_hp);
							}

							String curMonth = ":m" + DateUtils.getBeginMonth();
							String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
							int reward_hp =_getValue(jedis9,key + curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
						else if(timeType == 2){
							{
								String key = String.format("g{%s}:m%s:", groupId, tagUid);
								String curMonth = ":m" + DateUtils.getBeginLastMonth();
								int reward_hp = _getValue(jedis9, key + "reward_log" + curMonth);

								objLine.putInt("reward_tongji", reward_hp);
							}

							String curMonth = ":m" + DateUtils.getBeginLastMonth();
							String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
							int reward_hp =_getValue(jedis9, key+curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
					}

					temp.addTObject(objLine);
				}
			}
		}
		finally {
			jedis9.close();
		}
		
		obj.putTArray("members", temp);
		obj.putInt("limit", limit);
		
		
		return obj;
	}

	public static ITObject findPartnerStatMember(int groupId,int uid,int rootUid,int tagId, int limit, int num,int timeType,int beginTime, int endTime) throws Exception{

		ITObject obj = TObject.newInstance();

		String limitSql = String.format("limit %s,%s",limit, num);

		String sql = String.format("SELECT uid, partnerLev"
						+ " FROM group_member"
						+ " WHERE groupId = %s and (parentId=%s or uid = %s) and partnerLev > 0 and uid=%s GROUP BY uid %s",
				groupId, uid, uid, tagId, limitSql);



		ITArray temp =  TArray.newInstance();

		ITArray list = DataBase.use().executeQueryByTArray(sql);
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		try {

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

				ITObject objLine = list.getTObject(i);
				if(objLine.containsKey("uid")) {

					int tagUid = objLine.getInt("uid");

					AccountBean acc = AccountCache.getAccount(tagUid);
					objLine.putString("nick", acc.nick);

					if(tagUid != uid) {
						//int newBeginTime = beginTime - 86400 * 30;
						getMemberData(jedis9,tagUid,objLine,groupId,timeType, beginTime,endTime,false,true);
					}
					else {
						//int newBeginTime = beginTime - 86400 * 30;
						//getMemberData(jedis9,tagUid,objLine,groupId,timeType, beginTime,endTime,false,true);
						getDirectMemberData(jedis9,tagUid,objLine,groupId,timeType,beginTime,endTime);
					}


					if(tagUid == uid) {

						if(timeType == 0) {
							{
								String tmpKey = String.format("g{%s}:m%s:", groupId,tagUid);
								int reward_hp = _getCountValue(jedis9,tmpKey + "reward_log",beginTime,endTime);
								objLine.putInt("reward_tongji", reward_hp);
							}

							String key = String.format("g{%s}:m%s:d_reward", groupId,tagUid);
							int reward_hp = _getCountValue(jedis9,key,beginTime,endTime);
							objLine.putInt("reward_hp", reward_hp);
						}
						else if(timeType == 1){
							{
								String key = String.format("g{%s}:m%s:", groupId,tagUid);
								String curMonth = ":m" + DateUtils.getBeginMonth();
								int reward_hp = _getValue(jedis9,key + "reward_log" + curMonth);

								objLine.putInt("reward_tongji", reward_hp);
							}

							String curMonth = ":m" + DateUtils.getBeginMonth();
							String key = String.format("g{%s}:m%s:d_reward", groupId,tagUid);
							int reward_hp =_getValue(jedis9,key + curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
						else if(timeType == 2){
							{
								String key = String.format("g{%s}:m%s:", groupId, tagUid);
								String curMonth = ":m" + DateUtils.getBeginLastMonth();
								int reward_hp = _getValue(jedis9, key + "reward_log" + curMonth);
								objLine.putInt("reward_tongji", reward_hp);
							}
							String curMonth = ":m" + DateUtils.getBeginLastMonth();
							String key = String.format("g{%s}:m%s:d_reward", groupId,tagUid);
							int reward_hp =_getValue(jedis9, key+curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
					}
					else {
						if(timeType == 0) {
							{
								long reward_hp_total = 0;
								List<Integer> listParent = Utility.getChildParentList(groupId, tagUid, true);
								for (Integer parId : listParent) {
									String tmpKey = String.format("g{%s}:m%s:", groupId, parId);
									int reward_hp = _getCountValue(jedis9, tmpKey + "reward_log", beginTime, endTime);
									reward_hp_total += reward_hp;
								}
								objLine.putInt("reward_tongji", (int) reward_hp_total);
							}

							String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
							int reward_hp = _getCountValue(jedis9,key,beginTime,endTime);
							objLine.putInt("reward_hp", (int)reward_hp);
						}
						else if(timeType == 1){
							{
								String key = String.format("g{%s}:m%s:", groupId, tagUid);
								String curMonth = ":m" + DateUtils.getBeginMonth();
								int reward_hp = _getValue(jedis9, key + "reward_log" + curMonth);
								objLine.putInt("reward_tongji", reward_hp);
							}

							String curMonth = ":m" + DateUtils.getBeginMonth();
							String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
							int reward_hp =_getValue(jedis9,key + curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
						else if(timeType == 2){
							{
								String key = String.format("g{%s}:m%s:", groupId, tagUid);
								String curMonth = ":m" + DateUtils.getBeginLastMonth();
								int reward_hp = _getValue(jedis9, key + "reward_log" + curMonth);

								objLine.putInt("reward_tongji", reward_hp);
							}

							String curMonth = ":m" + DateUtils.getBeginLastMonth();
							String key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,tagUid,uid);
							int reward_hp =_getValue(jedis9, key+curMonth);
							objLine.putInt("reward_hp", reward_hp);
						}
					}

					temp.addTObject(objLine);
				}
			}
		}
		finally {
			jedis9.close();
		}

		obj.putTArray("members", temp);
		obj.putInt("limit", limit);


		return obj;
	}
	
	
	public static ITObject getPartnerStat(int groupId,int uid,int limit, int num,int timeType,int beginTime, int endTime) throws Exception{
		
		ITObject obj = TObject.newInstance();
		GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
		int lev = gmb.lev;
		boolean mgr = lev < 3;
		ITArray list = null;
		String limitSql = String.format("limit %s,%s",limit, num);
		if (mgr) {
			
			String sql = String.format("SELECT A.uid,A.partnerLev, A.autoscore,"
					+ "(SELECT COUNT(uid) FROM group_member B where B.groupId = %s AND B.parentId = A.uid) AS total "
					+ "FROM group_member AS A"
					+ " where A.groupId= %s and A.partnerLev=1 %s",
					groupId,groupId, limitSql);
		
			list = DataBase.use().executeQueryByTArray(sql);
		} else {
			
			String sql = String.format(
					"SELECT A.uid,A.partnerLev, A.autoscore, (SELECT COUNT(uid) FROM group_member B where B.groupId = %s AND B.parentId = A.uid) AS total "
					+ "FROM group_member AS A"
					+ " where A.groupId= %s and (A.parentId=%s or A.uid=%s) AND A.partnerLev >0 %s",
					groupId,groupId, uid, uid, limitSql);
			
			list = DataBase.use().executeQueryByTArray(sql);
		}
		
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
	
		try {
			
			for (int i = 0; i < list.size(); ++i) {
				
				ITObject objLine = list.getTObject(i);
				int tagUid = objLine.getInt("uid");
				AccountBean acc = AccountCache.getAccount(tagUid);
				objLine.putString("nick", acc.nick);
				
				getMemberData(jedis9,tagUid,objLine,groupId,timeType,beginTime,endTime,true,true);
			}
		}
		finally {
			jedis9.close();
			jedis10.close();
		}
		
		obj.putTArray("members", list);
		obj.putInt("limit", limit);
		
		return obj;
	}

	public static ITObject findPartnerStat(int groupId,int uid, int tagId, int limit, int num,int timeType,int beginTime, int endTime) throws Exception{

		ITObject obj = TObject.newInstance();
		GroupMemberBean gmb = GroupCache.getMember(groupId, uid);
		int lev = gmb.lev;
		boolean mgr = lev < 3;
		ITArray list = null;
		String limitSql = String.format("limit %s,%s",limit, num);
		if (mgr) {

			String sql = String.format("SELECT A.uid,A.partnerLev, A.autoscore,"
							+ "(SELECT COUNT(uid) FROM group_member B where B.groupId = %s AND B.parentId = A.uid) AS total "
							+ "FROM group_member AS A"
							+ " where A.groupId= %s and A.uid=%s %s",
					groupId,groupId, tagId, limitSql);

			list = DataBase.use().executeQueryByTArray(sql);
		} else {
			if (uid != tagId)
			{
				Jedis jedis10 = Redis.use("group1_db10").getJedis();
				try {
					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 sql = String.format(
					"SELECT A.uid,A.partnerLev, A.autoscore, (SELECT COUNT(uid) FROM group_member B where B.groupId = %s AND B.parentId = A.uid) AS total "
							+ "FROM group_member AS A"
							+ " where A.groupId= %s AND A.uid=%s %s",
					groupId,groupId, tagId, limitSql);

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

		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		Jedis jedis10 = Redis.use("group1_db10").getJedis();

		try {

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

				ITObject objLine = list.getTObject(i);
				int tagUid = objLine.getInt("uid");
				AccountBean acc = AccountCache.getAccount(tagUid);
				objLine.putString("nick", acc.nick);

				getMemberData(jedis9,tagUid,objLine,groupId,timeType,beginTime,endTime,true,true);
			}
		}
		finally {
			jedis9.close();
			jedis10.close();
		}

		obj.putTArray("members", list);
		obj.putInt("limit", limit);

		return obj;
	}
	
	public static void getMemberData(Jedis jedis9,
			int uid, 
			ITObject param,
			int groupId,
			int timeType,int beginTime, int endTime,
			boolean rewardHP,boolean total) {
		
		String key = String.format("g{%s}:m%s:", groupId,uid);
		String self = total ? "" : "_self";
		if(timeType == 0) {
			
			if(rewardHP) {
//				int reward_hp = _getCountValue(jedis9,key + "reward_log",beginTime,endTime);
//				param.putInt("reward_hp", reward_hp);

				// 包含下级合伙人
				long reward_hp_total = 0;
				List<Integer> listParent = Utility.getChildParentList(groupId, uid, true);
				for(Integer parId : listParent) {
					String tmpKey = String.format("g{%s}:m%s:", groupId,parId);
					int reward_hp = _getCountValue(jedis9,tmpKey + "reward_log",beginTime,endTime);
					reward_hp_total += reward_hp;
				}
				param.putLong("reward_hp", reward_hp_total);
			}
			
			int valid_round = _getCountValue(jedis9,key + "valid_round"+ self,beginTime,endTime);
			param.putInt("valid_round" , valid_round);
			
			int total_round = _getCountValue(jedis9,key + "round_log"+ self,beginTime,endTime);
			param.putInt("total_round" , total_round);
			
			int total_win = _getCountValue(jedis9,key + "total_win"+ self,beginTime,endTime);
			param.putInt("total_win" , total_win);

			int xipai_reward_log = _getCountValue(jedis9,key + "xipai_reward_log"+ self,beginTime,endTime);
			param.putInt("xipai_reward_log" , xipai_reward_log);
		}
		else if(timeType == 1){
			
			String curMonth = ":m" + DateUtils.getBeginMonth();
			
			if(rewardHP) {
				int reward_hp = _getValue(jedis9,key + "reward_log" + curMonth);
				param.putInt("reward_hp", reward_hp);
			}
			
			
			int valid_round = _getValue(jedis9,key + "valid_round"+ self + curMonth);
			param.putInt("valid_round" , valid_round);
			
			int total_round = _getValue(jedis9,key + "round_log" + self + curMonth);
			param.putInt("total_round", total_round);
			
			int total_win = _getValue(jedis9,key + "total_win" + self + curMonth);
			param.putInt("total_win", total_win);

			int xipai_reward_log = _getValue(jedis9,key + "xipai_reward_log" + self + curMonth);
			param.putInt("xipai_reward_log", xipai_reward_log);

		}
		else if(timeType == 2){
			
			String curMonth = ":m" + DateUtils.getBeginLastMonth();
			
			if(rewardHP) {
				int reward_hp = _getValue(jedis9,key + "reward_log" + curMonth);
				param.putInt("reward_hp", reward_hp);
			}
			
			int valid_round = _getValue(jedis9,key + "valid_round" + self + curMonth);
			param.putInt("valid_round", valid_round);
			
			int total_round = _getValue(jedis9,key + "round_log" + self + curMonth);
			param.putInt("total_round" , total_round);
			
			int total_win = _getValue(jedis9,key + "total_win"+ self + curMonth);
			param.putInt("total_win" , total_win);
			int xipai_reward_log = _getValue(jedis9,key + "xipai_reward_log"+ self + curMonth);
			param.putInt("xipai_reward_log" , xipai_reward_log);
		}
	}
	
	private static void getDirectMemberData(Jedis jedis9, 
			int uid, 
			ITObject param,
			int groupId,
			int timeType,int beginTime, int endTime) {
		
		String key = String.format("g{%s}:m%s:", groupId,uid);
		
		if(timeType == 0) {
			
			int valid_round = _getCountValue(jedis9,key + "d_valid_round",beginTime,endTime);
			param.putInt("valid_round" , valid_round);
			
			int total_round = _getCountValue(jedis9,key + "d_round_log",beginTime,endTime);
			param.putInt("total_round" , total_round);
			
			int total_win = _getCountValue(jedis9,key + "d_total_win",beginTime,endTime);
			param.putInt("total_win" , total_win);

			int xipai_reward_log = _getCountValue(jedis9,key + "xipai_reward_log_to",beginTime,endTime);
			param.putInt("xipai_reward_log" , xipai_reward_log);
		}
		else if(timeType == 1){
			
			String curMonth = ":m" + DateUtils.getBeginMonth();
			
			int valid_round = _getValue(jedis9,key + "d_valid_round"+ curMonth);
			param.putInt("valid_round" , valid_round);
			
			int total_round = _getValue(jedis9,key + "d_round_log" + curMonth);
			param.putInt("total_round", total_round);
			
			int total_win = _getValue(jedis9,key + "d_total_win" + curMonth);
			param.putInt("total_win", total_win);
			int xipai_reward_log = _getValue(jedis9,key + "xipai_reward_log_to" + curMonth);
			param.putInt("xipai_reward_log", xipai_reward_log);
		}
		else if(timeType == 2){
			
			String curMonth = ":m" + DateUtils.getBeginLastMonth();
			
			int valid_round = _getValue(jedis9,key + "d_valid_round" + curMonth);
			param.putInt("valid_round", valid_round);
			
			int total_round = _getValue(jedis9,key + "d_round_log" + curMonth);
			param.putInt("total_round" , total_round);
			
			int total_win = _getValue(jedis9,key + "d_total_win"+ curMonth);
			param.putInt("total_win" , total_win);

			int xipai_reward_log = _getValue(jedis9,key + "xipai_reward_log_to"+ curMonth);
			param.putInt("xipai_reward_log" , xipai_reward_log);
		}
	}
	
	/**
	 * 获取战绩列表
	 * @param platform
	 * @param groupId
	 * @param limit
	 * @param num
	 * @return
	 */
	public static ITObject getMemberStat(int groupId, int uid, int qid,int partnerId,int limit,int num,int timeType,int beginTime,int endTime) throws Exception{
		
		ITObject obj1 = TObject.newInstance();
		
		String qid_sql = StringUtil.Empty;
		if(qid > 0) {
			
			GroupMemberBean mng_bean = GroupCache.getMember(groupId, uid);
			if (mng_bean == null) {
				throw new WebException(ErrorCode.GROUP_NOT_MEMBER);
			}
			
			int mgn_partner = mng_bean.partnerLev;
			int mgn_lev = mng_bean.lev;
			if (mgn_lev == 3 && mgn_partner == 0) {
				throw new WebException(ErrorCode.GROUP_NOT_PARTNER);
			}
		
			if (mgn_lev == 3) {
				Jedis jedis10 = Redis.use("group1_db10").getJedis();
				try {
					List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, qid, false);
					if (par_list == null || !par_list.contains(uid)) {
						throw new WebException(ErrorCode.GROUP_NOT_PERMISSION);
					}
				}
				finally {
					jedis10.close();
				}
				
			}
			
			qid_sql = " AND A.uid="+qid;
		}
		else if(partnerId > 0) {
			
			String ugm_key = GroupMemberCache.genKey(groupId, partnerId);
			String lev = Redis.use("group1_db10").hget(ugm_key, "lev");
			boolean mgr = StringUtil.isNotEmpty(lev) && Integer.parseInt(lev) < 3;
			
			String p = Utility.getChildParentSql(groupId, partnerId, true);
			if(mgr) {
				p = p + ",0";
			}
			
			qid_sql = String.format(" AND (A.parentId in(%s) or A.uid = %s)", p, partnerId);
		}
	
		if(timeType == 0) {
			
			beginTime = DateUtils.getBeginDay() ;
			endTime =DateUtils.getEndDay();
		}else if(timeType == 1) {
			
			beginTime = DateUtils.getBeginDay() - 3600 * 24 ; 
			endTime = DateUtils.getBeginDay();
		}
		else if(timeType == 2) {
			
			beginTime = DateUtils.getBeginDay() - 3600 * 48 ; 
			endTime = DateUtils.getBeginDay() - 3600 * 24;
		}
		
		String sql = String.format("SELECT IFNULL(SUM(B.win), 0) AS win,IFNULL(SUM(B.round), 0) AS round,IFNULL(SUM(B.score), 0) AS score,A.uid,A.hp,A.lev,A.partnerLev "
				+ "FROM group_member AS A "
				+ "LEFT JOIN group_member_log AS B ON A.groupId = B.groupId and A.uid = B.uid AND B.time >=%s AND B.time <%s "
				+ "WHERE A.groupId = %s %s "
				+ "GROUP BY A.uid "
				+ "ORDER BY round DESC "
				+ "limit %s,%s",
				beginTime,endTime,groupId,qid_sql,limit,num);

		ITArray arr = DataBase.use().executeQueryByTArray(sql);
		if (arr.size() > 0) {
			Jedis jedis9 = Redis.use("group1_db9").getJedis();
			try {
				for (int i = 0; i < arr.size(); ++i) {
					
					ITObject obj = arr.getTObject(i);
					AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
					if (acc != null) {
						obj.putString("nick", acc.nick);
					} else {
						obj.putString("nick", StringUtil.Empty);
					}
				}
			} finally {
				jedis9.close();
			}
		}
		obj1.putTArray("members", arr);
		obj1.putInt("limit", limit);
		
		return obj1;
	}
	
	/**
	 * 获取战绩列表
	 * @param platform
	 * @param groupId
	 * @param limit
	 * @param num
	 * @return
	 */
	public static ITObject getRecords(String platform,int groupId,
			int limit,int num,
			int qid,int uid,int includeMembers,
			int timeType,int beginTime,int endTime) throws Exception{
		
		boolean find = true;
		boolean mgr = true;
		boolean tagIsPartner = false;
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			
			GroupMemberBean mng = GroupCache.getMember(groupId, uid);
			int _lev = mng.lev;
			mgr = _lev < 3;
			
			if(qid>0) {
				
				GroupMemberBean tag = GroupCache.getMember(groupId, qid);
				if (tag!=null) {
					
					int parentId = tag.parentId;
					if(!mgr) {
						
						if(parentId==0) {
							
							find = false;
						}else {
							
							if(tag.partnerLev > 0) {
								tagIsPartner= true;
							}
							List<Integer> par_list = Utility.getMemberParents(jedis10, groupId, includeMembers == 0 ? parentId : qid, true);
							if(par_list==null || !par_list.contains(uid)) {
								
								find = false;
							}
						}
					}
				}else {
					
					find = false;
				}
			}
		}finally {
			jedis10.close();
		}
		
		
		ITArray militaryList = TArray.newInstance();
		if(find) {
			
			String where = StringUtil.Empty;
			if(timeType == 0) {
				
				where = String.format("gid=%s and time>=%s and time<=%s",groupId,beginTime,endTime);
			}
			else if(timeType == 1) {
				
				beginTime = DateUtils.getBeginMonth();
				where = String.format("gid=%s and time>=%s",groupId,beginTime);
			}
			else if(timeType == 2) {
				
				beginTime = DateUtils.getBeginLastMonth();
				endTime = DateUtils.getBeginMonth();
				where = String.format("gid=%s and time>=%s and time<=%s",groupId,beginTime,endTime);
			}
			else {
				beginTime = DateUtils.getBeginDay() - 259200; 
				endTime = DateUtils.getEndDay();
				where = String.format("gid=%s and time>=%s and time<=%s",groupId,beginTime,endTime);
			}
			
			if(qid==0) {
				if (!mgr)
				{
					String p = Utility.getChildParentSql(groupId, uid, true);
					where += String.format(" and (parentId in(%s) or uid = %s)", p, uid);
				}
			}else {
				
				if(includeMembers == 1) {
					
					String p;
					if(tagIsPartner) {
						p = Utility.getChildParentSql(groupId, qid, true);
						where += String.format(" and (parentId in(%s) or uid = %s)", p, qid);
					}
					else {
						where += " and uid="+qid;
					}
				}
				else {
					where += " and uid="+qid;
				}
			}
			
			
			
			String sql = String.format("select rec_key from room_rec_log where %s ORDER BY time desc limit %s,%s",
					where,limit,num);
			
			
			ITArray list = DataBase.use().executeQueryByTArray(sql);
			
			if(list.size()>0) {
				Jedis jedis5 = Redis.use("group1_db5").getJedis();
				try {
					for(int i=0;i<list.size();++i) {
						ITObject tem = list.getTObject(i);
						String rec_key = tem.getString("rec_key");
						ITObject obj = Utility.getMilitaryList(jedis5,rec_key,platform);
						if(obj==null) {
							continue;
						}
						militaryList.addTObject(obj);
					}
				}finally {
					jedis5.close();
				}
			}
		}
		
		ITObject obj1 = TObject.newInstance();
		obj1.putTArray("records", militaryList);
		obj1.putInt("limit", limit);
		return obj1;
	}

	/**
	 * 根据房间ID查询战绩
	 * @param platform
	 * @param groupId
	 * @param roomid
	 * @return
	 */
	public static ITArray findRecordByRoom(String platform,int groupId,String roomid,int uid)throws Exception {
		if(StringUtil.isNotEmpty(roomid)) {
			if(roomid.length() != 6) {
				return null;
			}
		}
		boolean find = false;
		int time3last = DateUtils.getBeginDay() - 259200;
		String sql = String.format("select rec_key,uid from room_rec_log where gid=%s and roomid='%s' and time>=%s",
				groupId,roomid,time3last);
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		if(list.size()==0)return null;
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			GroupMemberBean mng = GroupCache.getMember(groupId, uid);
			int _lev = mng.lev;
			boolean mgr = _lev < 3;
			if(!mgr) {
				for(int i=0;i<list.size();++i) {
					ITObject obj = list.getTObject(i);
					int id = obj.getInt("uid");
					if(id==uid) {
						find = true;
						break;
					}
					GroupMemberBean tag = GroupCache.getMember(groupId, id);
					if(tag==null)continue;
					int par = tag.parentId;
					if(par==0)continue;
					List<Integer> par_list = Utility.getMemberParents(jedis10,groupId, par, true);
					if(par_list!=null&&par_list.contains(uid)) {
						find = true;
						break;
					}
				}
			}else {
				find = true;
			}
		}finally {
			jedis10.close();
		}
		if(!find)return null;

		ITArray militaryList = TArray.newInstance();
		Jedis jedis5 = Redis.use("group1_db5").getJedis();
		try {
			for(int i = 0; i < list.size(); i++) {
				String rec_key = list.getTObject(i).getString("rec_key");
				ITObject data = Utility.getMilitaryList(jedis5,rec_key, platform);
				boolean flag = true;
				for(int j = 0; j < militaryList.size(); j++) {
					ITObject temp = militaryList.getTObject(j);
					String temp_create_time = temp.getString("create_time");
					String create_time = data.getString("create_time");
					String temp_rec_key = temp.getString("military_id");

					if (temp_rec_key.equals(rec_key) && create_time.equals(temp_create_time))
					{
						flag = false;
						break;
					}
				}

				if (flag)
				{
					militaryList.addTObject(data);
				}
			}

			return militaryList;
		}finally {
			jedis5.close();
		}
	}
	
	
	/**
	 * 
	 * @param groupId
	 * @param uid
	 * @param limit
	 * @param num
	 * @param type 1上分 2下分 3合伙人上分 4合伙人下分
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getHpLogMgr(int groupId,int uid,int limit,int num,int type,int qid,String tagName) throws Exception {
		GroupMemberBean mng = GroupCache.getMember(groupId, uid);
		int beginDay = DateUtils.getBeginDay();
		int beginTime = beginDay - 9 * 24 *3600;
		if (mng.lev >= 3)
		{
			beginTime = beginDay - 2 * 24 *3600;
		}

		long endTime =  DateUtils.getEndDay();
		
		int reason = MGRLOG_TYPE[type-1];
		String mgr_sql = StringUtil.Empty;
		String qid_sql = StringUtil.Empty;
		
		ITArray arr = null;
		
		if(type > 2) {
			
			mgr_sql = "and A.mgr_id="+uid;
			
			if(StringUtil.isNotEmpty(tagName) || qid > 0) {
				
				if(qid > 0) {
					
					qid_sql = "and A.uid="+qid;
					
					String where_sql = String.format("WHERE A.gid = %s "
							+ "and A.reason = %s "
							+ "and A.time>=%s "
							+ "and A.time <=%s %s %s",groupId,reason,beginTime,endTime,mgr_sql,qid_sql);
					
					String sql = String.format("SELECT uid,mgr_id,hp,cur_hp,time FROM group_hp_log as A %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
					arr =DataBase.use().executeQueryByTArray(sql);
				}
				
				if(StringUtil.isNotEmpty(tagName)) {
					
					String where_sql = String.format("WHERE A.gid = %s "
							+ "and A.reason = %s "
							+ "and A.time>=%s "
							+ "and A.time <=%s %s %s",groupId,reason,beginTime,endTime,mgr_sql,qid_sql);
					
					String sql = String.format("SELECT A.uid,A.mgr_id,A.hp,A.cur_hp,A.time "
							+ "FROM group_hp_log as A "
							+ "RIGHT JOIN account as B "
							+ "ON A.uid = B.id and B.nick like '%%%s%%' "
							+ "%s ORDER BY time desc "
							+ "limit %s,%s",tagName,where_sql,limit,num);
					
					ITArray temp = DataBase.use().executeQueryByTArray(sql);
					if(temp.size() > 0) {
						if(arr == null) {
							arr = temp;
						}
						else {
							
							for(int i = 0; i <temp.size(); i++) {
								ITObject mo = temp.getTObject(i);
								if(qid > 0 && mo.getInt("uid") == qid) {
									continue;
								}
								arr.addTObject(temp.getTObject(i));
							}
						}
					}
				}
			}
			else {
				String where_sql = String.format("WHERE A.gid = %s "
						+ "and A.reason = %s "
						+ "and A.time>=%s "
						+ "and A.time <=%s %s %s",groupId,reason,beginTime,endTime,mgr_sql,qid_sql);
				
				String sql = String.format("SELECT uid,mgr_id,hp,cur_hp,time FROM group_hp_log as A %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
				arr =DataBase.use().executeQueryByTArray(sql);
			}
		}
		else {
			
			if(StringUtil.isNotEmpty(tagName) || qid > 0) {
				
				if(qid > 0) {
					qid_sql = "and (A.uid="+qid;
					qid_sql +=" or A.mgr_id="+qid + ")";
					
					String where_sql = String.format("WHERE A.gid = %s "
							+ "and A.reason = %s "
							+ "and A.time>=%s "
							+ "and A.time <=%s %s %s",groupId,reason,beginTime,endTime,mgr_sql,qid_sql);
					
					String sql = String.format("SELECT uid,mgr_id,hp,cur_hp,time FROM group_hp_log as A %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
					arr =DataBase.use().executeQueryByTArray(sql);
				}
				
				if(StringUtil.isNotEmpty(tagName)) {
					
					String where_sql = String.format("WHERE A.gid = %s "
							+ "and A.reason = %s "
							+ "and A.time>=%s "
							+ "and A.time <=%s %s %s",groupId,reason,beginTime,endTime,mgr_sql,qid_sql);
					
					String sql = String.format("SELECT A.uid,A.mgr_id,A.hp,A.cur_hp,A.time "
							+ "FROM group_hp_log as A "
							+ "RIGHT JOIN account as B "
							+ "ON (A.uid = B.id OR A.mgr_id = B.id) and B.nick like '%%%s%%' "
							+ "%s ORDER BY time desc "
							+ "limit %s,%s",tagName,where_sql,limit,num);
					
					ITArray temp = DataBase.use().executeQueryByTArray(sql);
					if(temp.size() > 0) {
						if(arr == null) {
							arr = temp;
						}
						else {
							
							for(int i = 0; i <temp.size(); i++) {
								
								ITObject mo = temp.getTObject(i);
								if(qid > 0 && mo.getInt("uid") == qid) {
									continue;
								}
								arr.addTObject(temp.getTObject(i));
							}
						}
					}
					
				}
			}else {
				
				String where_sql = String.format("WHERE A.gid = %s "
						+ "and A.reason = %s "
						+ "and A.time>=%s "
						+ "and A.time <=%s %s %s",groupId,reason,beginTime,endTime,mgr_sql,qid_sql);
				
				String sql = String.format("SELECT uid,mgr_id,hp,cur_hp,time FROM group_hp_log as A %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
				arr =DataBase.use().executeQueryByTArray(sql);
			}
				
		}
		
		ITObject resData = TObject.newInstance();
		resData.putInt("limit", limit);
		
		if(arr != null) {
			
			for(int i=0;i<arr.size();++i) {
				
				ITObject obj = arr.getTObject(i);
				AccountBean acc = AccountCache.getAccount(obj.getInt("uid"));
				obj.putString("t_nick", acc.nick);
				acc = AccountCache.getAccount(obj.getInt("mgr_id"));
				obj.putString("m_nick", acc.nick);
			}
			resData.putTArray("hp_logs", arr);
		}
		
		return resData;
	}
	
	/**
	 * 获取体力值管理统计数据
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getHpLogMgrCount(int groupId)  throws Exception{
		int beginTime = DateUtils.getBeginDay() - 9 * 24 *3600;
		long upper_count = 0;
		long sub_count = 0;
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		try {
			String str = null;
			for(int i=0;i<10;++i) {
				String hp_mgr_upper_key =  String.format("g%s:hp_mgr:upper:d%s",groupId,beginTime);
				String hp_mgr_sub_key =  String.format("g%s:hp_mgr:sub:d%s",groupId,beginTime);
				str = jedis9.get(hp_mgr_upper_key);
				if(StringUtil.isNotEmpty(str)) {
					upper_count +=Long.parseLong(str);
				}
				str = jedis9.get(hp_mgr_sub_key);
				if(StringUtil.isNotEmpty(str)) {
					sub_count +=Long.parseLong(str);
				}
				beginTime += 24*3600;
			}
			String hp_cost_key = String.format("g%s:hp_cost:d%s",groupId,DateUtils.getBeginDay());
			long hp_cost_count = 0;
			str = jedis9.get(hp_cost_key);
			if(StringUtil.isNotEmpty(str)) {
				hp_cost_count +=Long.parseLong(str);
			}
			
			hp_cost_key = String.format("g%s:hp_cost:d%s",groupId,DateUtils.getBeginLastday());
			long last_hp_cost_count = 0;
			str = jedis9.get(hp_cost_key);
			if(StringUtil.isNotEmpty(str)) {
				last_hp_cost_count +=Long.parseLong(str);
			}
			ITObject resData = TObject.newInstance();
			resData.putLong("hp_cost", hp_cost_count);
			resData.putLong("last_hp_cost", last_hp_cost_count);
			resData.putLong("hp_upper", upper_count);
			resData.putLong("hp_sub", sub_count);
			return resData;
		}finally {
			jedis9.close();
		}
	}
	
	/**
	 * 获取体力值管理员详细信息
	 * @param jedis9
	 * @param groupId
	 * @param uid
	 * @param beginTime
	 * @param endTime
	 * @param mgr
	 * @return
	 */
	private static final ITObject _getHpLogMgrInfo(Jedis jedis9,int groupId,int uid,int beginTime,int endTime,boolean mgr) {
		long upper_count = 0;
		long sub_count = 0;
		AccountBean acc = AccountCache.getAccount(uid);
		String str = null;
		int tem = beginTime;
		int tem_day = (endTime - beginTime) /86400;
		String hp_key = mgr?"hp_mgr":"hp_par";
		for(int k=0;k<tem_day;++k) {
			String hp_mgr_upper_key =  String.format("g%s:%s:upper:u%s:d%s",groupId,hp_key,uid,tem);
			String hp_mgr_sub_key =  String.format("g%s:%s:sub:u%s:d%s",groupId,hp_key,uid,tem);
			str = jedis9.get(hp_mgr_upper_key);
			if(StringUtil.isNotEmpty(str)) {
				upper_count +=Long.parseLong(str);
			}
			str = jedis9.get(hp_mgr_sub_key);
			if(StringUtil.isNotEmpty(str)) {
				sub_count +=Long.parseLong(str);
			}
			
			tem += 86400;
		}
		ITObject info = TObject.newInstance();
		info.putLong("hp_upper", upper_count);
		info.putLong("hp_sub", sub_count);
		info.putString("nick", acc.nick);
		if(mgr) {
			String gm_key = "gm_"+groupId+"_"+uid;
			String lev_str = Redis.use("group1_db10").hget(gm_key, "lev");
			info.putInt("lev", StringUtil.isEmpty(lev_str)?2:Integer.parseInt(lev_str));
		}
		return info;
	}
	
	/**
	 * 获取体力值管理员详细信息
	 * @param groupId
	 * @param uid  			操作人
	 * @param beginTime
	 * @param endTime
	 * @param type			1 管理员  2合伙人
	 * @return
	 * @throws Exception
	 */
	public static final ITArray getHpLogMgrInfo(int groupId,int uid,int beginTime,int endTime,int type)  throws Exception{
		if(type == 1) {
			String sql = String.format("SELECT uid FROM group_member WHERE groupId =%s AND lev <3", groupId);
			ITArray arr =DataBase.use().executeQueryByTArray(sql);
			Jedis jedis9 = Redis.use("group1_db9").getJedis();
			try {
				ITArray list = TArray.newInstance();
				for(int i=0;i<arr.size();++i) {
					ITObject obj = arr.getTObject(i);
					ITObject info = _getHpLogMgrInfo(jedis9,groupId,obj.getInt("uid"),beginTime,endTime,type==1);
					list.addTObject(info);
				}
				
				return list;
			}finally {
				jedis9.close();
			}
		}else {
			Jedis jedis9 = Redis.use("group1_db9").getJedis();
			try {
				ITArray list = TArray.newInstance();
				ITObject info = _getHpLogMgrInfo(jedis9,groupId,uid,beginTime,endTime,type==1);
				list.addTObject(info);
				return list;
			}finally {
				jedis9.close();
			}
		}
		
		
		
	}
	
	/**
	 * 体力值抽水记录
	 * @param groupId
	 * @param tagId
	 * @param limit
	 * @param num
	 * @param choose
	 * @return
	 */
	public static final ITObject getHpLogPumpInfo(int groupId,int limit,int num,int beginTime,int endTime) throws Exception{
		String where_sql = String.format("WHERE gid = %s and reason = %s and time>=%s and time <=%s",groupId,ConsumeCode.HP_PUMP,beginTime,endTime);
		
		String sql = String.format("SELECT uid,hp,roomid,pid,time FROM group_hp_log  %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
		ITArray arr =DataBase.use().executeQueryByTArray(sql);
		for(int i=0;i<arr.size();++i) {
			ITObject obj = arr.getTObject(i);
			int uid = obj.getInt("uid");
			AccountBean acc = AccountCache.getAccount(uid);
			obj.putString("nick", acc.nick);
		}
		int hp_cost = 0;
		int hp_reward = 0;
		int count = 0;
		int valid_count = 0;
		
		if(limit==0) {
			Jedis jedis9 = Redis.use("group1_db9").getJedis();
			try {
				String hp_cost_key = String.format("g%s:hp_cost",groupId);
				String hp_reward_key = String.format("g%s:hp_reward",groupId);
				String total_round = String.format("g%s:round",groupId);
				String valid_round = String.format("g%s:valid_room",groupId);
				
				hp_cost = _getCountValue(jedis9,hp_cost_key,beginTime,endTime);
				hp_reward = _getCountValue(jedis9,hp_reward_key,beginTime,endTime);
				count = _getCountValue(jedis9,total_round,beginTime,endTime);
				valid_count = _getCountValue(jedis9,valid_round,beginTime,endTime);
				
			}finally {
				jedis9.close();
			}
		}
		
		ITObject resData = TObject.newInstance();
		resData.putInt("limit", limit);
		resData.putTArray("hp_logs", arr);
		resData.putInt("pump", hp_cost);
		resData.putInt("gains", hp_cost-hp_reward);
		resData.putInt("count", count);
		resData.putInt("valid_count", valid_count);
		
		return resData;
	}
	

	/**
	 * 体力值详情
	 * @param groupId
	 * @param tagId
	 * @param limit
	 * @param num
	 * @param choose
	 * @return
	 */
	public static final ITObject getHpLogInfo(int groupId,int tagId,int limit,int num,int choose) throws Exception{
		String reason_sql = StringUtil.Empty;
		if((choose&CHOOSE_UPPER)!=0) {
			reason_sql += ConsumeCode.HP_MGR_UPPER + "," + ConsumeCode.HP_PARTNER_UPPER+",";
			reason_sql += ConsumeCode.HP_MGR_SUB + "," + ConsumeCode.HP_PARTNER_SUB+",";
		}
		if((choose&CHOOSE_SUB)!=0) {
			reason_sql += ConsumeCode.HP_MGR_SUB + "," + ConsumeCode.HP_PARTNER_SUB+",";
		}
		if((choose&CHOOSE_PUMP)!=0) {
			reason_sql += ConsumeCode.HP_PUMP + ",";

		}
		if((choose&CHOOSE_RWARD)!=0) {
			reason_sql += ConsumeCode.HP_PARTNER_REWARD + ",";
		}
		if((choose&CHOOSE_CHAGE)!=0) {
			reason_sql += ConsumeCode.HP_PUMP_TOTAL+ ",";
		}
		if((choose&CHOOSE_TRADE)!=0) {
			reason_sql += ConsumeCode.HP_TRADE+ ",";
		}
		if((choose&CHOOSE_TAKE_REWARD)!=0) {
			reason_sql +=ConsumeCode.HP_TAKE_REWARD+ ",";

		}
		if((choose&CHOOSE_XIPAI)!=0) {
			reason_sql +=ConsumeCode.HP_PARTNER_XIPAI_REWARD+ ",";
		}
		if((choose&CHOOSE_ANCHOU)!=0) {
			reason_sql +=ConsumeCode.HP_PARTNER_ANCHOU_REWARD+ ",";

		}
		//reason_sql += ConsumeCode.HP_ANCHOU_PUMP + ",";


		log.info("jefe reason_sql:"+reason_sql);

		if(StringUtil.isNotEmpty(reason_sql)) {
			reason_sql = reason_sql.substring(0, reason_sql.length()-1);
		}else {
			reason_sql = "0";
		}

		int time = DateUtils.getBeginDay() - 2 * 24 * 3600;
		
		String where_sql = String.format("WHERE gid = %s and uid =%s and reason in(%s) and time > %s",groupId,tagId,reason_sql,time);
		String sql = String.format("SELECT mgr_id,hp,reason,cur_hp,time,info,roomid "
				+ "FROM group_hp_log  %s "
				+ "ORDER BY time desc "
				+ "limit %s,%s",
				where_sql,limit,num);
		log.info("jefe sql:"+sql);
		ITArray arr =DataBase.use().executeQueryByTArray(sql);
		for(int i=0;i<arr.size();++i) {
			ITObject obj = arr.getTObject(i);
			int mgr_id = obj.getInt("mgr_id");
			if(mgr_id>0) {
				AccountBean acc = AccountCache.getAccount(obj.getInt("mgr_id"));
				obj.putString("m_nick", acc.nick);
			}
		}
		ITObject resData = TObject.newInstance();
		resData.putInt("limit", limit);
		resData.putTArray("hp_logs", arr);
		return resData;
	}
	
	/**
	 * 体力值详情
	 * @param groupId
	 * @param tagId
	 * @param limit
	 * @param num
	 * @param choose
	 * @return
	 */
	public static final ITObject getHpLogDetailInfo(int groupId,int tagId,String roomId,int time) throws Exception{
		String reason_sql = StringUtil.Empty;
		reason_sql += ConsumeCode.HP_CLEARING+ ",";
		reason_sql += ConsumeCode.HP_PUMP+ ",";
		reason_sql += ConsumeCode.HP_XIPAI_PUMP+ ",";
		reason_sql += ConsumeCode.HP_ANCHOU_PUMP + ",";
		if(StringUtil.isNotEmpty(reason_sql)) {
			reason_sql = reason_sql.substring(0, reason_sql.length()-1);
		}else {
			reason_sql = "0";
		}

		String where_sql = String.format("WHERE gid = %s and uid =%s and reason in(%s) and roomId ='%s' and time >= %s and time <= %s",groupId,tagId,reason_sql,roomId,time - 7200,time + 3600);
		String sql = String.format("SELECT mgr_id,hp,reason,cur_hp,time,info,roomid "
				+ "FROM group_hp_log  %s "
				+ "ORDER BY time desc",
				where_sql);
		try {
			ITArray arr =DataBase.use().executeQueryByTArray(sql);
			for(int i=0;i<arr.size();++i) {
				ITObject obj = arr.getTObject(i);
				int mgr_id = obj.getInt("mgr_id");
				if(mgr_id>0) {
					AccountBean acc = AccountCache.getAccount(obj.getInt("mgr_id"));
					obj.putString("m_nick", acc.nick);
				}
			}
			ITObject resData = TObject.newInstance();
			resData.putTArray("hp_logs", arr);
			return resData;
		}
		catch (Exception e)
		{
		    log.info("sql error:"+sql);
			log.error(e);
		}

		ITObject resData = TObject.newInstance();
		return resData;
	}
	
	private static final int _getCountValue(Jedis jedis9,String key,int beginTime,int endTime) {
		String str = null;
		int tem = beginTime;
		int tem_day = (endTime - beginTime) /86400;
		int count =0;
		for(int k=0;k<tem_day;++k) {
			String tem_key =  String.format("%s:d%s",key,tem);
			str = jedis9.get(tem_key);
			if(StringUtil.isNotEmpty(str)) {
				count +=Integer.parseInt(str);
			}
			tem += 86400;
		}
		return count;
	}
	
	private static final int _getValue(Jedis jedis9,String key) {
		
		int count =0;
		String str = jedis9.get(key);
		if(StringUtil.isNotEmpty(str)) {
			count = Integer.parseInt(str);
		}
		return count;
	}
	
	/**
	 * 获取奖励日志
	 * @param groupId
	 * @param tagId
	 * @param limit
	 * @param num
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getRewardLog(int groupId,int tagId,int limit,int num,int beginTime,int endTime) throws Exception{
		String where_sql = String.format("WHERE gid = %s and (reason = %s or reason = %s or reason = %s ) and time>=%s and time <=%s and uid=%s and hp != 0",
				groupId,ConsumeCode.HP_PARTNER_REWARD,ConsumeCode.HP_PARTNER_XIPAI_REWARD,ConsumeCode.HP_PARTNER_ANCHOU_REWARD,beginTime,endTime,tagId);
		int total_num = 0;
		int total_hp = 0;
		if(limit==0) {
			String count_sql = String.format("SELECT IFNULL(SUM(round),0) AS num,IFNULL(SUM(hp),0) AS total_hp FROM group_hp_log %s",where_sql);
			ITArray arr =DataBase.use().executeQueryByTArray(count_sql);
			if(arr.size()>0) {
				ITObject obj = arr.getTObject(0);
				total_num = obj.getDouble("num").intValue();
				total_hp = obj.getDouble("total_hp").intValue();
			}
		}
		
		String sql = String.format("SELECT hp,pid,roomid,time,round,reason,info FROM group_hp_log  %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
		ITArray arr =DataBase.use().executeQueryByTArray(sql);
		ITObject resData = TObject.newInstance();
		resData.putInt("limit", limit);
		resData.putTArray("reward_logs", arr);
		resData.putInt("total_num", total_num);
		resData.putInt("total_hp", total_hp);
		return resData;
	}
	
	/**
	 * 奖励统计
	 * @param groupId
	 * @param pid
	 * @param beginTime
	 * @param endTime
	 * @return
	 */
	public static final ITObject getRewardLogCount(int groupId,int pid,int beginTime,int endTime) {
		String p_key = pid>0?(":p"+pid):StringUtil.Empty;
		String valid_key = String.format("g%s:valid_room%s",groupId,p_key);
		String round_key = String.format("g%s:round%s",groupId,p_key);
		String hp_cost_key = String.format("g%s:hp_cost%s",groupId,p_key);
		String hp_reward_key = String.format("g%s:hp_reward%s",groupId,p_key);
		
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		try {
			int v_round = _getCountValue(jedis9,valid_key,beginTime,endTime);
			int round = _getCountValue(jedis9,round_key,beginTime,endTime);
			int hp_cost = _getCountValue(jedis9,hp_cost_key,beginTime,endTime);
			int hp_reward = _getCountValue(jedis9,hp_reward_key,beginTime,endTime);
			ITObject resData = TObject.newInstance();
			resData.putInt("v_round", v_round);
			resData.putInt("round", round);
			resData.putInt("hp_cost", hp_cost);
			resData.putInt("gains", hp_cost-hp_reward);
			return resData;
		}finally {
			jedis9.close();
		}
		
	}
	
	/**
	 * 体力值提取记录
	 * @param groupId
	 * @param tagid
	 * @param limit
	 * @param num
	 * @param beginTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getHpLogTakeInfo(int groupId,int uid, int tagId, int limit,int num,int beginTime,int endTime) 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)
			{
				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 where_sql = String.format("WHERE gid = %s and uid=%s and reason = %s and time>=%s and time <=%s",groupId,tagId,ConsumeCode.HP_TAKE_REWARD,beginTime,endTime);
		
		String sql = String.format("SELECT hp,time FROM group_hp_log  %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
		ITArray arr =DataBase.use().executeQueryByTArray(sql);
		ITObject resData = TObject.newInstance();
		resData.putInt("limit", limit);
		resData.putTArray("hp_logs", arr);
		return resData;
	}

	public static final ITObject getHpLogTakeBankInfo(int groupId,int uid, int tagId,int limit,int num,int beginTime,int endTime) 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 where_sql = String.format("WHERE gid = %s and uid=%s and (reason = %s or reason = %s) and time>=%s and time <=%s",groupId,tagId,ConsumeCode.HP_TAKE_BANK,ConsumeCode.HP_SAVE_BANK,beginTime,endTime);

		String sql = String.format("SELECT hp,time FROM group_hp_log  %s ORDER BY time desc limit %s,%s",where_sql,limit,num);
		ITArray arr =DataBase.use().executeQueryByTArray(sql);
		ITObject resData = TObject.newInstance();
		resData.putInt("limit", limit);
		resData.putTArray("hp_logs", arr);
		return resData;
	}

	
	
	/**
	 * 获取消耗统计
	 * @param groupId
	 * @return
	 */
	public static final ITArray getCostCount(int groupId, int uid, int beginTime,int endTime) {
		String gp_key = GroupCache.genPidsKey(groupId);
		Set<String> pids = Redis.use("group1_db11").zrangeByScore(gp_key, 10, 11);
		GroupMemberBean uid_bean = GroupCache.getMember(groupId, uid);
		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		ITArray arr = TArray.newInstance();
		try {
		    if (uid_bean.lev < 3)
			{
				for(String pid:pids) {

					ITObject obj = TObject.newInstance();

					String round_key = String.format("g%s:round:p%s",groupId,pid);
					obj.putInt("round", _getCountValue(jedis9,round_key,beginTime,endTime)*100);

					String valid_key = String.format("g%s:valid_room:p%s",groupId,pid);
					obj.putInt("valid", _getCountValue(jedis9,valid_key,beginTime,endTime)*100);

					String no_valid_key = String.format("g%s:no_valid_room:p%s",groupId,pid);
					obj.putInt("no_valid", _getCountValue(jedis9,no_valid_key,beginTime,endTime)*100);

					String diamo_cost_key = String.format("g%s:diamo_cost:p%s",groupId,pid);
					obj.putInt("diamo_cost", _getCountValue(jedis9,diamo_cost_key,beginTime,endTime)*100);

					obj.putInt("pid", Integer.parseInt(pid));

					arr.addTObject(obj);
				}
			}
			else {
				for(String pid:pids) {

					ITObject obj = TObject.newInstance();
					String round_key = String.format("g{%s}:m%s:all_count:p%s", groupId, uid,pid);
					int all_round = _getCountValue(jedis9,round_key,beginTime,endTime);
					obj.putInt("round", all_round);

					String valid_key = String.format("g{%s}:m%s:valid_round2:p%s", groupId,uid,pid);
					int valid = _getCountValue(jedis9,valid_key,beginTime,endTime);
					obj.putInt("valid", valid);

					int no_valid_key = all_round - valid;
					obj.putInt("no_valid", no_valid_key);

					String diamo_cost_key = String.format("g{%s}:m%s:valid_diamo:p%s", groupId,uid,pid);
					obj.putInt("diamo_cost", _getCountValue(jedis9,diamo_cost_key,beginTime,endTime));

					obj.putInt("pid", Integer.parseInt(pid));

					arr.addTObject(obj);
				}
			}
		}finally {
			jedis9.close();
		}
		return arr;
	}
	
	/**
	 * 获取局数统计
	 * @param groupId
	 * @return
	 */
	public static final ITArray getRoundCount(int groupId,int pid) {
		int bengin = DateUtils.getBeginDay() - 29 *86400;

		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		ITArray arr = TArray.newInstance();
		try {
			String p_key = pid>0?(":p"+pid):StringUtil.Empty;
			for(int i=0;i<30;++i) {
				String round_key = String.format("g%s:round%s:d%s",groupId,p_key,bengin);
				String r_str = jedis9.get(round_key);
				ITObject obj = TObject.newInstance();
				obj.putInt("time", bengin);
				int r_num = 0;
				if(StringUtil.isNotEmpty(r_str)) {
					r_num = Integer.parseInt(r_str);
				}
				obj.putInt("num", r_num);
				arr.addTObject(obj);
				bengin +=86400;
			}
		}finally {
			jedis9.close();
		}
		return arr;
	}
	
	/**
	 * 获取体力值消耗统计
	 * @param groupId
	 * @return
	 */
	public static final ITArray getHpConsumeCount(int groupId,int uid) {
		int bengin = DateUtils.getBeginDay() - 2 *86400;

		Jedis jedis9 = Redis.use("group1_db9").getJedis();
		ITArray arr = TArray.newInstance();
		try {
			String key = String.format("g%s:m%s:consume_hp", groupId,uid);
			for(int i=0;i<3;++i) {
				String hp_key = String.format("%s:d%s",key,bengin);
				String r_str = jedis9.get(hp_key);
				ITObject obj = TObject.newInstance();
				obj.putInt("time", bengin);
				int r_num = 0;
				if(StringUtil.isNotEmpty(r_str)) {
					r_num = Integer.parseInt(r_str);
				}
				obj.putInt("num", r_num);
				arr.addTObject(obj);
				bengin +=86400;
			}
		}finally {
			jedis9.close();
		}
		return arr;
	}
	
	/**
	 * 获取体力值统计信息
	 * @param groupId
	 * @return
	 * @throws Exception
	 */
	public static final ITObject getHpCountInfo(int groupId) throws Exception {
		String sql = String.format("SELECT uid FROM group_member WHERE groupId=%s"
				+ " AND partnerLev >0",groupId);
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		int _t_hp = 0;
		try {
			for(int i=0;i<list.size();++i) {
				ITObject obj = list.getTObject(i);
				int parentId = obj.getInt("uid");
				String key = String.format("g{%s}:m%s:reward_hp", groupId,parentId);
				String r_hp = jedis10.get(key);
				if(StringUtil.isNotEmpty(r_hp)) {
					_t_hp += Integer.parseInt(r_hp);
				}
			}
		}finally {
			jedis10.close();
		}
		String where = String.format("FROM group_member WHERE groupId = %s", groupId);
		String count_sql = String.format("SELECT (SELECT IFNULL(SUM(hp),0) %s AND hp >0) t_hp1,"
				+ " (SELECT IFNULL(SUM(ABS(hp)),0) %s AND hp <0) t_hp2", where, where);
		ITArray arr = DataBase.use().executeQueryByTArray(count_sql);
		ITObject obj1 = TObject.newInstance();
		if (arr.size() > 0) {
			ITObject obj = arr.getTObject(0);
			obj1.putLong("hp_num1", obj.getDouble("t_hp1").longValue());
			obj1.putLong("hp_num2", obj.getDouble("t_hp2").longValue());
		} else {
			obj1.putLong("hp_num1", 0);
			obj1.putLong("hp_num2", 0);
		}
		obj1.putLong("hp_num3", _t_hp);
		return obj1;
	}
}
