package com.evt.mgr.handler;

import com.data.util.ConsumeCode;
import com.data.util.CountUtil;
import com.data.util.Utility;
import com.evt.mgr.EventServer;
import com.evt.mgr.IHandler;
import com.evt.mgr.Utils;
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.DateUtils;

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

import java.sql.SQLException;

public class HandlerGroupMemberRound implements IHandler {

	@Override
	public int process(int uid, ITObject param) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	private static boolean addMemberLog(int gid, int pid, int uid, int score, int win, int time, int perfectRound,
			int valid_count) throws SQLException {
		String sql = String.format("{call sp_add_member_log(%s,%s,%s,%s,%s,%s,%s,%s)}", gid, pid, uid, score, win, time,
				perfectRound, valid_count);
		Utility.evtdbLog(gid, 2, sql);
		return false;
	}

	@Override
	public int processGroup(int groupId, ITObject param) throws Exception {
		EventServer.log.info("processGroup begin:" + param);

		int pid = param.getInt("pid");
		int score = param.getInt("score");
		int uid = param.getInt("uid");
		int win = param.getInt("win");
		int time = DateUtils.getBeginDay();
		int perfectRound = param.getInt("perfect_round");
		int valid_count = param.getInt("valid_count");
		int pump = param.getInt("pump");
		int pumpBase = 0;
		if (pump == 0) {
			pumpBase = param.getInt("pumpBase");
		}
		int pumpReal = param.getInt("pumpReal");

		int cur_hp = param.getInt("cur_hp");
		int xipai_total = param.getInt("xi_pai_total");
		String roomid = param.getString("room");
		int cur_time = (int) (System.currentTimeMillis() / 1000);
		if (param.containsKey("time")) {
			cur_time = param.getInt("time");
		}

		// ITArray param1 = TArray.newInstance();
		// param1.addInt(groupId);
		// param1.addInt(pid);
		// param1.addInt(uid);
		// param1.addInt(score - pump - xipai_total);
		// param1.addInt(win);
		// param1.addInt(time);
		// param1.addInt(perfectRound);
		// param1.addInt(valid_count);
		// DataBase.use().prepareCallNonResult("sp_add_member_log", param1);
		addMemberLog(groupId, pid, uid, score > 0 ? score - pumpReal - xipai_total : score - xipai_total, win, time,
				perfectRound, valid_count);
//		EventServer.log.info("pumpReal end:" + pumpReal);

		String sql = String.format(
				"INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time) "
						+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s)",
				groupId, uid, ConsumeCode.HP_PUMP_TOTAL,
				score > 0 ? score - pumpReal - xipai_total : score - xipai_total - pumpBase, cur_hp, pid, roomid,
				(int) (cur_time));

		if (score == 0) {
//			EventServer.log.info("洗牌保底:" +( score - xipai_total - param.getInt("pumpBase")));

			sql = String.format(
					"INSERT INTO group_hp_log(gid,uid,reason,hp,cur_hp,pid,roomid,time) "
							+ "VALUES(%s,%s,%s,%s,%s,%s,%s,%s)",
					groupId, uid, ConsumeCode.HP_PUMP_BAODI, score - xipai_total - param.getInt("pumpBase") , cur_hp, pid, roomid,
					(int) (cur_time));

		}
		Utility.evtdb(groupId, 1, sql);
		// DataBase.use().executeUpdate(sql);

		String gmrp_key = String.format("g%s:m%s:round:p%s", groupId, uid, pid);
		String gmr_key = String.format("g%s:m%s:round", groupId, uid);

		Jedis jedis13 = Redis.use("group1_db13").getJedis();

		try {
			CountUtil.countLog(gmrp_key, 1, jedis13);
			CountUtil.countLog(gmr_key, 1, jedis13);

			Pipeline pipeline13 = jedis13.pipelined();
			if (score > 0) {
				Utils.countRoundWin(pipeline13, groupId, pid, uid, win == 1, score - pumpReal);
			} else {
				Utils.countRoundWin(pipeline13, groupId, pid, uid, win == 1, score);

			}
			pipeline13.sync();

		} finally {
			jedis13.close();
		}
		return 0;
	}

}
