package com.evt.mgr;

import java.util.List;

import org.eclipse.jetty.util.log.Log;

import com.data.cache.GroupCache;
import com.data.util.CountUtil;
import com.data.util.Utility;
import com.taurus.core.entity.ITArray;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.DateUtils;

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

public class Utils {

	public static final void countValidAndTotal(Jedis jedis10, Pipeline pipeline9, int uid, int groupId, int pid,
			int valid_count, int valid_diamo, int all_count,int playerCount) {

		if (valid_count > 0) {
			String gmv = String.format("g{%s}:m%s:valid_round", groupId, uid);
			CountUtil.countLogByDay30(gmv, valid_count, pipeline9);
			CountUtil.countLogByDay30(gmv + "_self", valid_count, pipeline9);
		}

		if (valid_diamo > 0) {
			String gmv = String.format("g{%s}:m%s:valid_diamo", groupId, uid);
			CountUtil.countLogByDay30(gmv, valid_diamo, pipeline9);
			CountUtil.countLogByDay30(gmv + "_self", valid_diamo, pipeline9);
		}

		String gmr = String.format("g{%s}:m%s:round_log", groupId, uid);
		CountUtil.countLogByDay30(gmr, 1, pipeline9, true);
		CountUtil.countLogByDay30(gmr + "_self", 1, pipeline9, true);

		List<Integer> temp = Utility.getMemberParents(jedis10, groupId, uid, true);
		if (temp != null && temp.size() > 0) {

			if (valid_count > 0) {

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

					int par = temp.get(i);
					if (par != uid) {
						String gmv = String.format("g{%s}:m%s:valid_round", groupId, par);
						CountUtil.countLogByDay30(gmv, valid_count, pipeline9);
					}

					String gmvp = String.format("g{%s}:m%s:valid_round:p%s", groupId, par, pid);
					CountUtil.countLogByDay30(gmvp, valid_count, pipeline9);

					String gmvp2 = String.format("g{%s}:m%s:valid_round2:p%s", groupId, par, pid);
					CountUtil.countLogByDay30(gmvp2, valid_count, pipeline9);

//					boolean add_uid = (uid == par || i == 0);
//					if (add_uid == true) {
						String gmv = String.format("g{%s}:m%s:d_valid_round", groupId, par);
						CountUtil.countLogByDay30(gmv, valid_count, pipeline9);
//					}
				}
			}

			for (int i = 0; i < temp.size(); ++i) {
				if (all_count > 0) {
					int par = temp.get(i);
					if (par != uid) {
						gmr = String.format("g{%s}:m%s:round_log", groupId, par);
						CountUtil.countLogByDay30(gmr, 1, pipeline9, true);
					}

					gmr = String.format("g{%s}:m%s:valid_diamo:p%s", groupId, par, pid);
					CountUtil.countLogByDay30(gmr, valid_diamo, pipeline9);

					gmr = String.format("g{%s}:m%s:all_count:p%s", groupId, par, pid);

					CountUtil.countLogByDay30(gmr, all_count, pipeline9, true);

//					boolean add_uid = (uid == par || i == 0);
//					if (add_uid == true) {
						gmr = String.format("g{%s}:m%s:d_round_log", groupId, par);
						CountUtil.countLogByDay30(gmr, 1, pipeline9, true);
//					}
				}
			}
		}
	}

	public static final void countValidAndTotal(Pipeline pipeline9, ITArray playerList, int groupId, int pid,
			int valid_count, int valid_diamo, int all_count, int win_player) {

		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {

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

				int uid = playerList.getInt(i);
				EventServer.log.info("uid:" + uid);
				EventServer.log.info("win_player:" + win_player);

				if (win_player == uid) {
					// 大赢家增加场次
					EventServer.log.info("win_player:" + uid);
					countValidAndTotal(jedis10, pipeline9, uid, groupId, pid, valid_count, valid_diamo, all_count,1);

				} else {
					EventServer.log.info("lost_player:" + uid);
					countValidAndTotal(jedis10, pipeline9, uid, groupId, pid, 0, valid_diamo, all_count,1);

				}

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

	/**
	 * 统计合伙人局数和赢
	 * 
	 * @param jedis9
	 * @param groupId
	 * @param uid
	 * @param win
	 */
	public static final void countRoundWin(Pipeline pipeline9, int groupId, int pid, int uid, boolean win, int score) {
		List<Integer> par_list = null;
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			par_list = Utility.getMemberParents(jedis10, groupId, uid, true);

			Pipeline pipeline10 = jedis10.pipelined();

			if (par_list == null)
				return;

			String key = String.format("g{%s}:m%s:total_win", groupId, uid);
			CountUtil.countLogByDay30(key, score, pipeline9);
			CountUtil.countLogByDay30(key + "_self", score, pipeline9);

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

				int par = par_list.get(i);

				boolean add_uid = (uid == par || i == 0);
				if (add_uid == true) {

					key = String.format("g{%s}:m%s:d_total_win", groupId, par);
					EventServer.log.info("par:" + par+"获得："+score);

					CountUtil.countLogByDay30(key, score, pipeline9);
				}

				String mlk = GroupCache.genMemberListKey(groupId, par);
				pipeline10.sadd(mlk, uid + "");

				if (par != uid) {
					key = String.format("g{%s}:m%s:total_win", groupId, par);
					EventServer.log.info("par:" + par+"获得："+score);

					CountUtil.countLogByDay30(key, score, pipeline9);
				}
			}

			pipeline10.sync();

		} finally {
			jedis10.close();
		}
	}

	/**
	 * 统计合伙人体力值消耗,输赢统计
	 * 
	 * @param jedis9
	 * @param groupId
	 * @param uid
	 * @param pump
	 * @param hp
	 */
	public static final void countHpconsume(Pipeline pipeline9, int groupId, int uid, int pump, int hp) {
		List<Integer> par_list = null;
		Jedis jedis10 = Redis.use("group1_db10").getJedis();
		try {
			par_list = Utility.getMemberParents(jedis10, groupId, uid, true);
		} finally {
			jedis10.close();
		}
		if (par_list == null)
			return;
		String key;

		if (hp != 0) {

			key = String.format("g{%s}:m%s:hp_consume_log", groupId, uid);
			CountUtil.countLogByDay(key, hp, pipeline9);
		}

		int day = DateUtils.getBeginDay();
		boolean self = par_list.get(0) == uid;
		for (int i = 0; i < par_list.size(); ++i) {

			int par = par_list.get(i);
			boolean add_uid = i == 0 || (i <= 1 && self);

			if (pump > 0) {

				key = String.format("g%s:hp_cost:par%s_%s", groupId, par, day);
				pipeline9.hincrBy(key, "total", pump);
				if (add_uid) {
					pipeline9.hincrBy(key, uid + "", pump);
				}

				pipeline9.expire(key, 3600 * 24 * 2);
			}

			if (hp != 0) {

				if (uid != par) {
					key = String.format("g{%s}:m%s:hp_consume_log", groupId, par);
					CountUtil.countLogByDay(key, hp, pipeline9);
				}

				key = String.format("g%s:hp_consume:par%s_%s", groupId, par, day);
				pipeline9.hincrBy(key, "total", hp);
				if (add_uid) {
					pipeline9.hincrBy(key, uid + "", hp);
				}

				pipeline9.expire(key, 3600 * 24 * 2);
			}
		}

	}
}
