package com.evt.mgr;

import java.sql.SQLException;

import com.data.util.CountUtil;
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.StringUtil;

import redis.clients.jedis.Jedis;

public class EventController {
	private static final String EVENT_TYPE = "E";
	private static final String EVENT_GID = "gid";
	private static final String EVENT_UID = "uid";
	
	public static final int execEvt(String key,boolean isGroupEvt) {
		int count = 0;
		do {
			
			String jsonStr = Redis.use(EventReceiver.CHACHE_KEY).rpop(key);
			if(StringUtil.isEmpty(jsonStr))
			{
			    break;
			}

			long startTime = System.currentTimeMillis();
			ITObject data = null;
			try {
				data = TObject.newFromJsonData(jsonStr);
			} catch (Exception e) {
				EventServer.log.error(jsonStr + ":event json error!", e);
			}
			int type = 0;
			int uid = 0;
			int groupId =0;
			int reulst = 0;
			try {
				type = data.getInt(EVENT_TYPE);

				if(isGroupEvt) {
					groupId = data.getInt(EVENT_GID);
				}else {
					uid = data.getInt(EVENT_UID);
				}

				IHandler handler = EventServer.eventReceiver.getHandler(type);
				if(handler!=null) {
					if(isGroupEvt) {
						reulst = handler.processGroup(groupId, data);
						EventServer.log.info(jsonStr + " use time:" + (System.currentTimeMillis() - startTime) + " ms");
					}else {
						reulst = handler.process(uid, data);
						EventServer.log.info(jsonStr + " use time:" + (System.currentTimeMillis() - startTime) + " ms");
					}
					if(reulst!=0) {
						if(reulst == -1) {
							jsonStr = data.toJson();
						}
						Redis.use(EventReceiver.CHACHE_KEY).lpush(key, jsonStr);
					}
				}
			}catch (Exception e) {
				e.printStackTrace();
				Redis.use("group1_db15").lpush(key, jsonStr);
				break;
			}

			if(reulst == 0) {
				count++;
			}
		} while(true);

		return count;
	}
	
	/**
	 * 
	 * @param uid
	 * @param pay
	 * @param cur_diamo
	 * @param reason
	 * @param gameId
	 * @param groupId
	 * @param pid
	 * @throws SQLException 
	 */
	public static final void payDiamo(int uid,int pay,int cur_diamo,int reason,int gameId,int groupId,int pid) throws SQLException{
		ITArray data = TArray.newInstance();
		data.addInt(uid);
		data.addInt(pay);
		data.addInt(reason);
		data.addInt(gameId);
		data.addInt(groupId);
		data.addInt(cur_diamo);
		DataBase.use().prepareCallNonResult("sp_transfer_diamo", data);
		if(groupId>0) {
			Jedis jedis9 = Redis.use("group1_db13").getJedis();
			try {
				CountUtil.countLog(String.format("g%s:diamo_cost",groupId), pay, jedis9);
				if(pid > 0) {
					CountUtil.countLog(String.format("g%s:diamo_cost:p%s",groupId,pid), pay, jedis9,false,true);
				}
			}finally {
				jedis9.close();
			}
		}
	}
	
	public static final int execSql(String key) {
	    int count = 0;
		do {
			String str = Redis.use(EventReceiver.CHACHE_KEY).rpop(key);
			if(str==null || str.equalsIgnoreCase("null")) {
				
			}else {
				EventServer.log.info( " execsql:"+str);

			}
			if(StringUtil.isEmpty(str))
			{
				break;
			}

			int reulst = 0;
			long startTime = System.currentTimeMillis();
			try {
				int type = Integer.parseInt(str.substring(0,1));
				String sql = str.substring(1);
				if(type==1) {
					DataBase.use().executeUpdate(sql);
					EventServer.log.info(sql + " use time:" + (System.currentTimeMillis() - startTime) + " ms");
				}else {
					DataBase.use().executeCall(sql, false);
					EventServer.log.info(sql + " use time:" + (System.currentTimeMillis() - startTime) + " ms");
				}

			}catch (Exception e) {
				e.printStackTrace();
				Redis.use("group1_db15").lpush(key, str);
				break;
			}

			if(reulst == 0) {
				count++;
			}
		} while(true);

		return count;
	}
}
