package cn.com.qmth.stmms.common.interceptor;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
import cn.com.qmth.stmms.biz.exam.model.OperationLog;
import cn.com.qmth.stmms.biz.exam.service.OperationLogService;
import cn.com.qmth.stmms.biz.user.service.UserService;
import cn.com.qmth.stmms.common.annotation.Logging;
import cn.com.qmth.stmms.common.domain.ApiUser;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * @author ting.yin
 * @Description: 日志操作拦截器
 */
public class LogInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private OperationLogService logService;

    @Autowired
    private UserService userService;

    @Override
    public void postHandle(HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler,
            ModelAndView modelAndView) {
        WebUser webUser = RequestUtils.getWebUser(request);
        ApiUser apiUser = RequestUtils.getApiUser(request);
        HandlerMethod method = (HandlerMethod) handler;
        String uri = request.getRequestURI();
        if (uri.startsWith("/login")) {
            webUser = buildWebUser(request);
        }
        Logging logging = method.getMethodAnnotation(Logging.class);
        if (webUser != null && logging != null) {
            saveLog(logging, webUser, request);
        }
        if (apiUser != null && logging != null) {
            saveLog(logging, apiUser, request);
        }
    }

    private void saveLog(Logging logging, ApiUser wu, HttpServletRequest request) {
        OperationLog log = new OperationLog();
        log.setCreateTime(new Date());
        log.setMenu(logging.menu());
        log.setType(logging.type());
        log.setMarker(false);
        log.setOperatorId(wu.getUserData().getId());
        log.setIpAddress(wu.getUserData().getLastLoginIp());
        log.setLoginName(wu.getUserData().getLoginName());
        int examId = SessionExamUtils.getExamId(request);
        log.setExamId(examId > 0 ? examId : null);
        log.setSchoolId(wu.getSchoolId());
        log.setDescription(RequestUtils.getLog(request));
        logService.save(log);
    }

    private void saveLog(Logging logging, WebUser wu, HttpServletRequest request) {
        OperationLog log = new OperationLog();
        log.setCreateTime(new Date());
        log.setMenu(logging.menu());
        log.setType(logging.type());
        log.setMarker(wu.isMarker());
        log.setOperatorId(wu.getUser().getId());
        log.setIpAddress(wu.getUser().getLastLoginIp());
        log.setLoginName(wu.getUser().getLoginName());
        log.setSchoolId(wu.getUser().getSchoolId());
        int examId = SessionExamUtils.getExamId(request);
        log.setExamId(examId > 0 ? examId : null);
        log.setDescription(RequestUtils.getLog(request));
        logService.save(log);
    }

    /**
     * 从session中构造WebUser对象
     *
     * @param request
     */
    private WebUser buildWebUser(HttpServletRequest request) {
        return WebUser.buildFromSession(RequestUtils.getSession(request), userService);
    }
}
