package com.mjlogin.service;

import com.data.bean.AccountBean;
import com.data.cache.AccountCache;
import com.data.util.ErrorCode;
import com.data.util.Utility;
import com.mjlogin.Protocol;
import com.mjlogin.WebInterceptor;
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.routes.ActionKey;
import com.taurus.core.util.DateUtils;
import com.taurus.web.Controller;
import com.taurus.web.WebException;

import redis.clients.jedis.Jedis;

public class MilitaryService extends Controller {


	/**
	 * 获取战绩列表
	 * @throws Exception
	 */
	@ActionKey(value = Protocol.GET_MILITARY, validate=WebInterceptor.V_SESSION)
	public final void getMilitaryList() throws Exception {
		String session = this.getSession();
		ITObject reqData = this.getParams();
		
		int time3last = DateUtils.getBeginDay() - 259200; 
		int endTime = DateUtils.getEndDay();
		AccountBean acc = AccountCache.getAccount(session);
		String sql = String.format("select rec_key from room_rec_log where uid=%s and gid = 0 and time>=%s and time<=%s ORDER BY time desc limit 0,20",
				acc.id,time3last,endTime);
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		ITArray militaryList = TArray.newInstance();
		if(list.size()>0) {
			String platform = reqData.getUtfString("platform");
			Jedis jedis5 = Redis.use("group1_db5").getJedis();
			try {
				for(int i=0;i<list.size();++i) {
					ITObject tem = list.getTObject(i);
					String rec_key = tem.getUtfString("rec_key");
					ITObject obj = Utility.getMilitaryList(jedis5,rec_key,platform);
					if(obj==null) {
						continue;
					}
					militaryList.addTObject(obj);
				}
			}finally {
				jedis5.close();
			}
		}
		ITObject resData = TObject.newInstance();
		resData.putTArray("military_list", militaryList);
		sendResponse(ErrorCode._SUCC, resData);
	}

	@ActionKey(value = Protocol.GET_MILITARY_BY_ROOMID, validate=WebInterceptor.V_SESSION)
	public final void getMilitaryListByRoomId() throws Exception {
		ITObject reqData = this.getParams();
		String roomId = reqData.getUtfString("roomId");

		ITArray militaryList = TArray.newInstance();
		int time3last = DateUtils.getBeginDay() - 259200; 
		String sql = String.format("select rec_key from room_rec_log where roomid='%s' and time>=%s limit 0,1",roomId,time3last);
		ITArray list = DataBase.use().executeQueryByTArray(sql);
		if(list.size()>0) {
			String platform = reqData.getUtfString("platform");
			String rec_key = list.getTObject(0).getUtfString("rec_key");
			Jedis jedis5 = Redis.use("group1_db5").getJedis();
			try {
				ITObject data = Utility.getMilitaryList(jedis5,rec_key, platform);
				militaryList.addTObject(data);
			}finally {
				jedis5.close();
			}
		}
		
		ITObject resData = TObject.newInstance();
		resData.putTArray("military_list", militaryList);
		sendResponse(ErrorCode._SUCC, resData);
	}


	/**
	 * 获取回放数据
	 * @throws Exception
	 */
	@ActionKey(value = Protocol.GET_PLAYBACK, validate=WebInterceptor.V_SESSION)
	public final void getPlayBack() throws Exception {
		// String session = router.getSession();
		ITObject reqData = this.getParams();

		String military = reqData.getUtfString("military_id");
		String round = reqData.getUtfString("round");
		String playback = Redis.use("group1_db5").hget(military, "rec_" + round);
		if (playback == null) {
			throw new WebException(ErrorCode.NO_REC);
		}

		ITObject resData = TObject.newInstance();
		resData.putUtfString("playback", playback);
		sendResponse(ErrorCode._SUCC, resData);
	}


}
