package com.mjlogin.service;


import java.util.List;

import com.data.util.ErrorCode;
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.redis.Redis;
import com.taurus.core.routes.ActionKey;
import com.taurus.web.Controller;

import redis.clients.jedis.Jedis;

public class IndexService extends Controller{
	
	private static ITArray noticeList = TArray.newInstance();
	private static long lastTime;
	static ITArray getNoticeList() {
		Jedis jedis1 = Redis.use("group1_db1").getJedis();
		try {
			if(System.currentTimeMillis()-lastTime >10000) {
				noticeList.clear();
				lastTime = System.currentTimeMillis();
				List<String> notices = jedis1.lrange("admin_notice", 0, -1);
				
				for (String tem : notices) {
					ITObject notice = TObject.newFromJsonData(tem);
					noticeList.addTObject(notice);
				}
			}
		}finally {
			jedis1.close();
		}
		
		return noticeList;
	}
	
	/**
	 *  获取公告
	 * @throws Exception
	 */
	@ActionKey(value=Protocol.GET_NOTICE,validate=WebInterceptor.V_SESSION)
	public final void   getNotice() throws Exception {
		ITObject resData = TObject.newInstance();
		getNoticeList();
		resData.putTArray("notice_list", noticeList);
		
		this.sendResponse(ErrorCode._SUCC, resData);
	}


}
