package com.uas.account.web.filter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.uas.account.core.Logger;
import com.uas.account.core.util.ExecuteUtils;
import com.uas.account.dao.LogDao;
import com.uas.account.entity.Log;

public class LogInterceptor extends HandlerInterceptorAdapter {

	@Autowired
	private LogDao logDao;

	private Logger logger = Logger.getLogger();

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
		// 记录日志
		final List<Log> logs = logger.getLogsAndClear();
		if (!CollectionUtils.isEmpty(logs)) {
			ExecuteUtils.asyncExecute(new Runnable() {

				@Override
				public void run() {
					logDao.save(logs);
				}
			});
		}
	}

}
