package com.evt.mgr.handler;

import java.sql.SQLException;

import com.data.bean.GroupBean;
import com.data.cache.GroupCache;
import com.data.cache.GroupMemberCache;
import com.data.util.ConsumeCode;
import com.data.util.CountUtil;
import com.data.util.Utility;
import com.evt.mgr.IHandler;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;

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

public class HandlerHpConsume implements IHandler{
	static Logger log = Logger.getLogger(HandlerHpConsume.class);
	final static String DB = "group1_db10";
	
	private static boolean hpConsume(int gid, int uid,int pid, int hp,int cur_hp,int reason, String roomid, int time,String desc) throws SQLException {
		String sql = String.format("{call sp_update_hp(%s,%s,%s,%s,%s,%s,'%s',%s,'%s')}",
				gid,uid,hp,cur_hp,reason,pid,roomid,time,desc);
		Utility.evtdb(gid, 2, sql);
		return false;
	}

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

	private void to_reward(Pipeline pipeline, int uid, String prs,int rewardType, int valueType, int real_pump, int pump,int groupId,int pid,String roomid,int time,int max_player) throws Exception {

		int useValue = 0;
		if(StringUtil.isNotEmpty(prs)) {
			
			int round = 100 / max_player;
			ITArray list = TArray.newFromJsonData(prs);
			
			for(int i=list.size()-1;i>=0;--i) {
				
				ITObject obj1 = list.getTObject(i);
				int parentId = obj1.getInt("p");
				int rewardPercent = 0;
				if (obj1.containsKey("r"))
				{
					rewardPercent = obj1.getInt("r");
				}
				int rv = 0;
				if(i > 0) {
					ITObject obj2 = list.getTObject(i - 1);
					if (!obj2.containsKey("r"))
					{
						rv =rewardPercent - 0;
					}
					else {
						rv =rewardPercent -obj2.getInt("r");
					}
				}else {
					
					rv=rewardPercent;
				}
				int reward_value = rv;
				if(valueType == 1) {
					reward_value = Math.round(pump * (rv / 100f));
				}
				else {
					if(rewardType == 2) {
						reward_value = reward_value / max_player;
					}
				}

				if (reward_value < 0)
				{
					continue;
				}
				
				String key = String.format("g{%s}:m%s:reward_hp", groupId,parentId);
				Redis.use(DB).incrBy(key, reward_value);
				String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), parentId,groupId);
				Utility.evtdb(groupId, 1, rewardsql);
				//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,parentId);
				//Redis.use(DB).incrBy(all_key, reward_value);
				useValue += reward_value;
				
				Jedis jedis9 =  Redis.use("group1_db9").getJedis();
				try {
					String desc = "" + uid;
					long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,parentId), reward_value, jedis9);
					String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round, info) "
							+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,parentId,ConsumeCode.HP_PARTNER_REWARD,reward_value,cur_hp,pid,roomid,time,round, desc);
					DataBase.use().executeUpdate(sql);
				}finally {
					jedis9.close();
				}
				
				// 记录代理每天的推广奖励
				CountUtil.countLogByDay30(key, reward_value, pipeline);
				
				key = String.format("g{%s}:m%s:reward_log", groupId,parentId);
				CountUtil.countLogByDay30(key, reward_value, pipeline);
				
				key = String.format("g{%s}:m%s:p%s:reward_log", groupId,parentId,pid);
				CountUtil.countLogByDay30(key, reward_value, pipeline);
				
				// 记录每个人对上级的推广奖励是多少
				key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,uid, parentId);
				CountUtil.countLogByDay30(key, reward_value, pipeline);
				
				boolean add_uid = (uid == parentId || i == 0);
				if(add_uid == true) {
					key = String.format("g{%s}:m%s:d_reward", groupId,parentId);
					CountUtil.countLogByDay30(key, reward_value, pipeline);
				}
				
				if(i>0) {
					
					for(int j = i - 1; j >= 0; j--) {
						
						ITObject obj2 = list.getTObject(j);
						int temp = obj2.getInt("p");
						if(temp != uid) {
							key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,temp, parentId);
							CountUtil.countLogByDay30(key, reward_value, pipeline);
						}
					}		
				}
				
				
				//某个玩法的抽水总值
				CountUtil.countLogByDay30(String.format("g%s:hp_cost:m%s:p%s",groupId,parentId,pid),pump,pipeline);
				
				CountUtil.countLogByDay30(String.format("g%s:hp_reward",groupId), reward_value, pipeline);
			}
		}

		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb != null) {
			int round = 100 / max_player;
			int leftValue = real_pump - useValue;
			String key = String.format("g{%s}:m%s:reward_hp", groupId,gb.owner);
			Redis.use(DB).incrBy(key, leftValue);
			String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), gb.owner,groupId);
			Utility.evtdb(groupId, 1, rewardsql);
			//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,gb.owner);
			//Redis.use(DB).incrBy(all_key, leftValue);
			// 记录代理每天的推广奖励
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:reward_log", groupId,gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:p%s:reward_log", groupId,gb.owner,pid);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			// 记录每个人对上级的推广奖励是多少
			key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,uid, gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			boolean add_uid = (uid == gb.owner);
			if(add_uid == true) {
				key = String.format("g{%s}:m%s:d_reward", groupId,gb.owner);
				CountUtil.countLogByDay30(key, leftValue, pipeline);
			}
			Jedis jedis9 =  Redis.use("group1_db9").getJedis();
			try {
				String desc = "" + uid;
				long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,gb.owner), leftValue, jedis9);
				String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
						+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,gb.owner,ConsumeCode.HP_PARTNER_REWARD,leftValue,cur_hp,pid,roomid,time,round,desc);
				DataBase.use().executeUpdate(sql);
			}finally {
				jedis9.close();
			}
		}
	}

	private void anchou_to_reward(Pipeline pipeline, int uid, String prs,int rewardType, int valueType, int real_pump, int pump,int groupId,int pid,String roomid,int time,int max_player) throws Exception {

		log.info("uid:"+uid+"prs:"+prs+"valueType:"+valueType+"real_pump:"+real_pump+"pump:"+pump+"groupId:"+groupId+"pid:"+pid+"time:"+time+"max_player:"+max_player);


		int useValue = 0;
		log.info("uid:"+uid+"prs:"+StringUtil.isNotEmpty(prs)+"time:"+time);
		if(StringUtil.isNotEmpty(prs)) {
			int round = 100 / max_player;
			ITArray list = TArray.newFromJsonData(prs);
			log.info("uid:"+uid+"size:"+list.size()+"time:"+time);
			for(int i=list.size()-1;i>=0;--i) {

				ITObject obj1 = list.getTObject(i);
				int parentId = obj1.getInt("p");
				int rewardPercent = 0;
				if (obj1.containsKey("a"))
				{
					rewardPercent = obj1.getInt("a");
				}
				int rv = 0;
				if(i > 0) {
					ITObject obj2 = list.getTObject(i - 1);
					if (!obj2.containsKey("a"))
					{
						rv = rewardPercent - 0;
					}
					else {
						rv = rewardPercent -obj2.getInt("a");
					}
				}else {

					rv=rewardPercent;
				}
				int reward_value = rv;
				if(valueType == 1) {
					reward_value = Math.round(pump * (rv / 100f));
				}

				log.info("uid:"+uid+"reward_value:"+reward_value+"time:"+time);

				if (reward_value < 0)
				{
					continue;
				}

				String key = String.format("g{%s}:m%s:reward_hp", groupId,parentId);
				Redis.use(DB).incrBy(key, reward_value);
				String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), parentId,groupId);
				Utility.evtdb(groupId, 1, rewardsql);
				//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,parentId);
				//Redis.use(DB).incrBy(all_key, reward_value);
				useValue += reward_value;

				Jedis jedis9 =  Redis.use("group1_db9").getJedis();
				try {
					String desc = "" + uid;
					long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,parentId), reward_value, jedis9);
					String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
							+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,parentId,ConsumeCode.HP_PARTNER_ANCHOU_REWARD,reward_value,cur_hp,pid,roomid,time,round,desc);
					log.info("uid:"+uid+"sql:"+sql+"time:"+time);
					DataBase.use().executeUpdate(sql);
				}finally {
					jedis9.close();
				}

				// 记录代理每天的推广奖励
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				key = String.format("g{%s}:m%s:anchou_reward_log", groupId,parentId);
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				key = String.format("g{%s}:m%s:p%s:anchou_reward_log", groupId,parentId,pid);
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				// 记录每个人对上级的推广奖励是多少
				key = String.format("g{%s}:m%s:anchou_reward_log_to:par%s", groupId,uid, parentId);
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				boolean add_uid = (uid == parentId || i == 0);
				if(add_uid == true) {
					key = String.format("g{%s}:m%s:d_anchou_reward", groupId,parentId);
					CountUtil.countLogByDay30(key, reward_value, pipeline);
				}

				if(i>0) {

					for(int j = i - 1; j >= 0; j--) {

						ITObject obj2 = list.getTObject(j);
						int temp = obj2.getInt("p");
						if(temp != uid) {
							key = String.format("g{%s}:m%s:anchou_reward_log_to:par%s", groupId,temp, parentId);
							CountUtil.countLogByDay30(key, reward_value, pipeline);
						}
					}
				}

				//某个玩法的抽水总值
				CountUtil.countLogByDay30(String.format("g%s:anchou_hp_cost:m%s:p%s",groupId,parentId,pid),pump,pipeline);

				CountUtil.countLogByDay30(String.format("g%s:anchou_hp_reward",groupId), reward_value, pipeline);
			}
		}
		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb != null) {
			int round = 100 / max_player;
			int leftValue = real_pump - useValue;
			String key = String.format("g{%s}:m%s:reward_hp", groupId,gb.owner);
			Redis.use(DB).incrBy(key, leftValue);
			String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), gb.owner,groupId);
			Utility.evtdb(groupId, 1, rewardsql);
			//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,gb.owner);
			//Redis.use(DB).incrBy(all_key, leftValue);
			// 记录代理每天的推广奖励
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:anchou_reward_log", groupId,gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:p%s:anchou_reward_log", groupId,gb.owner,pid);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			// 记录每个人对上级的推广奖励是多少
			key = String.format("g{%s}:m%s:anchou_reward_log_to:par%s", groupId,uid, gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			boolean add_uid = (uid == gb.owner);
			if(add_uid == true) {
				key = String.format("g{%s}:m%s:d_anchou_reward", groupId,gb.owner);
				CountUtil.countLogByDay30(key, leftValue, pipeline);
			}
			Jedis jedis9 =  Redis.use("group1_db9").getJedis();
			try {
				String desc = "" + uid;
				long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,gb.owner), leftValue, jedis9);
				String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
						+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,gb.owner,ConsumeCode.HP_PARTNER_ANCHOU_REWARD,leftValue,cur_hp,pid,roomid,time,round,desc);
				DataBase.use().executeUpdate(sql);
			}finally {
				jedis9.close();
			}
		}
	}

	private void xipai_to_reward(Pipeline pipeline, int uid, String prs,int rewardType, int valueType, int real_pump, int pump,int groupId,int pid,String roomid,int time,int max_player) throws Exception {
		log.info("xipai uid:"+uid+"prs:"+prs+"time:"+time);
	    int useValue = 0;
		log.info("xipaiuid:"+uid+"prs:"+StringUtil.isNotEmpty(prs)+"time:"+time);
		if(StringUtil.isNotEmpty(prs)) {
			log.info("xipaiok uid:"+uid+"prs:"+prs+"time:"+time);
			int round = 100 / max_player;
			ITArray list = TArray.newFromJsonData(prs);

			for(int i=list.size()-1;i>=0;--i) {

				ITObject obj1 = list.getTObject(i);
				int parentId = obj1.getInt("p");
				int rewardPercent = 0;
				if (obj1.containsKey("x"))
				{
					rewardPercent = obj1.getInt("x");
				}
				int rv = 0;
				if(i > 0) {
					ITObject obj2 = list.getTObject(i - 1);
					if (!obj2.containsKey("x"))
					{
						rv = rewardPercent - 0;
					}
					else {
						rv = rewardPercent -obj2.getInt("x");
					}
				}else {

					rv=rewardPercent;
				}
				int reward_value = rv;
				if(valueType == 1) {
					reward_value = Math.round(pump * (rv / 100f));
				}

				if (reward_value < 0)
				{
					continue;
				}

				String key = String.format("g{%s}:m%s:reward_hp", groupId,parentId);
				Redis.use(DB).incrBy(key, reward_value);
				String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), parentId,groupId);
				Utility.evtdb(groupId, 1, rewardsql);
				//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,parentId);
				//Redis.use(DB).incrBy(all_key, reward_value);
				useValue += reward_value;

				Jedis jedis9 =  Redis.use("group1_db9").getJedis();
				try {
					String desc = "" + uid;
					long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,parentId), reward_value, jedis9);
					String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
							+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,parentId,ConsumeCode.HP_PARTNER_XIPAI_REWARD,reward_value,cur_hp,pid,roomid,time,round,desc);
					DataBase.use().executeUpdate(sql);
				}finally {
					jedis9.close();
				}

				// 记录代理每天的推广奖励
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				key = String.format("g{%s}:m%s:xipai_reward_log", groupId,parentId);
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				key = String.format("g{%s}:m%s:p%s:xipai_reward_log", groupId,parentId,pid);
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				// 记录每个人对上级的推广奖励是多少
				key = String.format("g{%s}:m%s:xipai_reward_log_to:par%s", groupId,uid, parentId);
				CountUtil.countLogByDay30(key, reward_value, pipeline);

				boolean add_uid = (uid == parentId || i == 0);
				if(add_uid == true) {
					key = String.format("g{%s}:m%s:d_xipai_reward", groupId,parentId);
					CountUtil.countLogByDay30(key, reward_value, pipeline);
				}

				if(i>0) {

					for(int j = i - 1; j >= 0; j--) {

						ITObject obj2 = list.getTObject(j);
						int temp = obj2.getInt("p");
						if(temp != uid) {
							key = String.format("g{%s}:m%s:xipai_reward_log_to:par%s", groupId,temp, parentId);
							CountUtil.countLogByDay30(key, reward_value, pipeline);
						}
					}
				}

				//某个玩法的抽水总值
				CountUtil.countLogByDay30(String.format("g%s:xipai_hp_cost:m%s:p%s",groupId,parentId,pid),pump,pipeline);

				CountUtil.countLogByDay30(String.format("g%s:xipai_hp_reward",groupId), reward_value, pipeline);
			}
		}
		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb != null) {
			int round = 100 / max_player;
			int leftValue = real_pump - useValue;
			String key = String.format("g{%s}:m%s:reward_hp", groupId,gb.owner);
			Redis.use(DB).incrBy(key, leftValue);
			String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), gb.owner,groupId);
			Utility.evtdb(groupId, 1, rewardsql);
			//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,gb.owner);
			//Redis.use(DB).incrBy(all_key, leftValue);
			// 记录代理每天的推广奖励
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:xipai_reward_log", groupId,gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:p%s:xipai_reward_log", groupId,gb.owner,pid);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			// 记录每个人对上级的推广奖励是多少
			key = String.format("g{%s}:m%s:xipai_reward_log_to:par%s", groupId,uid, gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			boolean add_uid = (uid == gb.owner);
			if(add_uid == true) {
				key = String.format("g{%s}:m%s:d_xipai_reward", groupId,gb.owner);
				CountUtil.countLogByDay30(key, leftValue, pipeline);
			}
			Jedis jedis9 =  Redis.use("group1_db9").getJedis();
			try {
				String desc = "" + uid;
				long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,gb.owner), leftValue, jedis9);
				String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
						+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,gb.owner,ConsumeCode.HP_PARTNER_XIPAI_REWARD,leftValue,cur_hp,pid,roomid,time,round,desc);
				DataBase.use().executeUpdate(sql);
			}finally {
				jedis9.close();
			}
		}
	}

	private void to_mengzhu_reward(Pipeline pipeline, int uid, String prs,int rewardType, int valueType, int real_pump, int pump,int groupId,int pid,String roomid,int time,int max_player) throws Exception {
		GroupBean gb = GroupCache.getGroup(groupId);
		if (gb != null) {
			int round = 100 / max_player;
			int leftValue = real_pump;
			String key = String.format("g{%s}:m%s:reward_hp", groupId,gb.owner);
			Redis.use(DB).incrBy(key, leftValue);
			String rewardsql = String.format("update group_member set reward_hp = %s where uid = %s AND groupId = %s", Redis.use(DB).get(key), gb.owner,groupId);
			Utility.evtdb(groupId, 1, rewardsql);
			//String all_key = String.format("g{%s}:m%s:all_reward_hp", groupId,gb.owner);
			//Redis.use(DB).incrBy(all_key, leftValue);
			// 记录代理每天的推广奖励
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:reward_log", groupId,gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			key = String.format("g{%s}:m%s:p%s:reward_log", groupId,gb.owner,pid);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			// 记录每个人对上级的推广奖励是多少
			key = String.format("g{%s}:m%s:reward_log_to:par%s", groupId,uid, gb.owner);
			CountUtil.countLogByDay30(key, leftValue, pipeline);

			boolean add_uid = (uid == gb.owner);
			if(add_uid == true) {
				key = String.format("g{%s}:m%s:d_reward", groupId,gb.owner);
				CountUtil.countLogByDay30(key, leftValue, pipeline);
			}

			Jedis jedis9 =  Redis.use("group1_db9").getJedis();
			try {
				String desc = "" + uid;
				long cur_hp = CountUtil.countLogByDay3(String.format("g%s:hp_reward:m%s",groupId,gb.owner), leftValue, jedis9);
				String sql = String.format("INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time,round,info) "
						+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", groupId,gb.owner,ConsumeCode.HP_PARTNER_REWARD,leftValue,cur_hp,pid,roomid,time,round,desc);
				DataBase.use().executeUpdate(sql);
			}finally {
				jedis9.close();
			}
		}
	}
	
	@Override
	public int processGroup(int groupId, ITObject param) throws Exception {
		log.info("processGroup",param);
		int uid = param.getInt("uid");
		try {
			int hp = param.getInt("hp");
			int cur_hp = param.getInt("cur_hp");
			boolean is_pump = param.containsKey("pump");
			int pump = 0; 
			String roomid = param.getString("room");
			int pid = param.getInt("pid");
			int time = param.getInt("time");
			int reward_value = 0;
			int xipai_reward_value = 0;
			int max_player = 0;
			int rewardValueType = 0;
			int xipai_rewardValueType = 0;
			int rewardType = 1;
			int xipai_rewardType = 1;
			int anchou_rewardType = 1;
			int anchou_reward_value = 0;
			int anchou_rewardValueType = 0;

			boolean is_xipai = param.containsKey("xipai");
			int xipai = 0;


			String prs =null;
			if(is_pump) {
				pump = param.getInt("pump");
				max_player = param.getInt("max_player");
				if(param.containsKey("reward_value")) {
					reward_value = param.getInt("reward_value");
					prs = param.getString("prs");
				}
				if(param.containsKey("reward_type")) {
					rewardType = param.getInt("reward_type");
				}
				if(param.containsKey("rewardValueType")) {
					rewardValueType = param.getInt("rewardValueType");
				}
			}

			if (is_xipai)
			{
				xipai = param.getInt("xipai");
				max_player = param.getInt("max_player");
				if(param.containsKey("xipai_reward_value")) {
					xipai_reward_value = param.getInt("xipai_reward_value");
					prs = param.getString("prs");
				}
				if(param.containsKey("xipai_reward_type")) {
					xipai_rewardType = param.getInt("xipai_reward_type");
				}

				if(param.containsKey("xipai_rewardValueType")) {
					xipai_rewardValueType = param.getInt("xipai_rewardValueType");
				}
			}
			int anchou = 0;
			if(param.containsKey("anchou")) {
				anchou = param.getInt("anchou");
			}

			if(param.containsKey("anchou_reward_value")) {
				max_player = param.getInt("max_player");
				anchou_reward_value = param.getInt("anchou_reward_value");
				prs = param.getString("prs");
			}

			if(param.containsKey("anchou_reward_type")) {
				anchou_rewardType = param.getInt("anchou_reward_type");
			}

			if(param.containsKey("anchou_rewardValueType")) {
				anchou_rewardValueType = param.getInt("anchou_rewardValueType");
			}
			log.info("jefe anchou_reward_value:"+anchou_reward_value);

			if (param.containsKey("hp_than_max_value"))
			{
			    int more_hp = param.getInt("hp_than_max_value");
				String gm_key = GroupMemberCache.genKey(groupId, uid);
				String bank_hp = Redis.use("group1_db10").hget(gm_key, "bank_hp");
				String sql = String.format("{call sp_bank_hp(%s,%s,%s,%s)}", groupId, uid, more_hp, cur_hp);
				Utility.evtdb(groupId, 2, sql);
				String sql2 = String.format("update group_member set bank_hp = %s where uid = %s AND groupId = %s", bank_hp, uid, groupId);
				Utility.evtdb(groupId, 1, sql2);
			}
			
			String desc = param.getString("desc");
			desc = StringUtil.isNotEmpty(desc) ? desc : StringUtil.Empty;
			if(hp!=0) {
				hpConsume(groupId, uid,pid, hp,cur_hp + pump,ConsumeCode.HP_CLEARING, roomid,time, desc);
			}
			
			Jedis jedis9 = Redis.use("group1_db9").getJedis();
			try {
				
				String key = String.format("g%s:m%s:consume_hp", groupId,uid);
				if(hp!=0) {
					CountUtil.countLogByDay(key, hp, jedis9, 691200);
				}
				
				if(pump>0) {
					
					CountUtil.countLogByDay(key, -pump, jedis9, 691200);
					time += 1;
					hpConsume(groupId, uid,pid, -pump,cur_hp, ConsumeCode.HP_PUMP, roomid,time, desc);
					
					CountUtil.countLog(String.format("g%s:hp_cost",groupId), pump, jedis9);
					CountUtil.countLogByDay(String.format("g%s:hp_cost:p%s",groupId,pid),pump,jedis9);
					CountUtil.countLogByDay(String.format("g%s:hp_cost:m%s",groupId,uid),pump,jedis9);
				}

				if (xipai > 0)
				{
					CountUtil.countLogByDay(key, -xipai, jedis9, 691200);
					time += 1;
					hpConsume(groupId, uid,pid, -xipai,cur_hp, ConsumeCode.HP_XIPAI_PUMP, roomid,time, desc);

					CountUtil.countLog(String.format("g%s:hp_cost",groupId), xipai, jedis9);
					CountUtil.countLogByDay(String.format("g%s:hp_cost:p%s",groupId,pid),xipai,jedis9);
					CountUtil.countLogByDay(String.format("g%s:hp_cost:m%s",groupId,uid),xipai,jedis9);
				}

				if (anchou > 0)
				{
					CountUtil.countLogByDay(key, -anchou, jedis9, 691200);
					time += 1;
					hpConsume(groupId, uid,pid, -anchou,cur_hp, ConsumeCode.HP_ANCHOU_PUMP, roomid,time, desc);

					CountUtil.countLog(String.format("g%s:hp_cost",groupId), anchou, jedis9);
					CountUtil.countLogByDay(String.format("g%s:hp_cost:p%s",groupId,pid),anchou,jedis9);
					CountUtil.countLogByDay(String.format("g%s:hp_cost:m%s",groupId,uid),anchou,jedis9);
				}
				
				Pipeline pipeline = jedis9.pipelined();
				if(reward_value > 0 || (rewardValueType == 2 && is_pump)) {
					
					time += 1;
					to_reward(pipeline,uid,prs,rewardType,rewardValueType, pump, reward_value ,groupId,pid,roomid,time,max_player);
				}
				/*
				else {
					if (is_pump && pump > 0)
					{
						log.error("pump to_mengzhu_reward:"+pump+" uid:" + uid + " groupId:" + groupId);
						to_mengzhu_reward(pipeline,uid,prs,rewardType,rewardValueType, pump, reward_value ,groupId,pid,roomid,time,max_player);
					}
				}
				 */
				if(xipai_reward_value > 0 || (xipai_rewardValueType == 2 && is_xipai)) {

					time += 1;
					xipai_to_reward(pipeline,uid,prs,xipai_rewardType,xipai_rewardValueType, xipai, xipai_reward_value ,groupId,pid,roomid,time,max_player);
				}
				if (anchou_reward_value>0){
					time += 1;
					anchou_to_reward(pipeline,uid,prs,anchou_rewardType,anchou_rewardValueType, anchou, anchou_reward_value ,groupId,pid,roomid,time,max_player);
				}

				/*
				else {
					if (is_xipai && xipai > 0)
					{
						log.error("xipai to_mengzhu_reward:"+pump+" uid:" + uid + " groupId:" + groupId);
						to_mengzhu_reward(pipeline,uid,prs,xipai_rewardType,xipai_rewardValueType, xipai, xipai_reward_value ,groupId,pid,roomid,time,max_player);
					}
				}
				 */
				pipeline.sync();
				
			}finally {
				jedis9.close();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return 0;
	}
}
