package com.evt.mgr.handler;

import com.data.util.ConsumeCode;
import com.data.util.CountUtil;
import com.data.util.Utility;
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 com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.sql.SQLException;

public class HandlerGroupMemberRound implements IHandler{
	static Logger log = Logger.getLogger(HandlerHpConsume.class);
	@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 {
		
		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 cur_hp = param.getInt("cur_hp");
		int xipai_total = param.getInt("xi_pai_total");
		int anchou_total = param.getInt("an_chou_total");

		String roomid = param.getString("room");
		int cur_time = (int)(System.currentTimeMillis() / 1000);
		if (param.containsKey("time"))
		{
			cur_time = param.getInt("time");
		}
		log.info("uid:"+uid+"anchou_total:"+ anchou_total+"xipai_total:"+xipai_total);
		//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 - pump - xipai_total-anchou_total, win, time, perfectRound, valid_count);
		
		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 - pump - xipai_total-anchou_total,
				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 jedis9 = Redis.use("group1_db9").getJedis();
		
		try {
			CountUtil.countLog(gmrp_key, 1, jedis9);
			CountUtil.countLog(gmr_key, 1, jedis9);
			
			Pipeline pipeline9 = jedis9.pipelined();
			Utils.countRoundWin(pipeline9, groupId, pid, uid, win==1,score - pump);
			pipeline9.sync();
			
		}finally {
			jedis9.close();
		}
		return 0;
	}

}
