package com.ym.mec.auth.interceptor;

import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.auth.service.SysUserService;
import com.ym.mec.common.security.AuthUser;
import com.ym.mec.common.security.SecurityUtils;
import com.yonge.log.interceptor.AuditLogInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Component
public class OperationLogInterceptor extends AuditLogInterceptor {

	@Autowired
	private SysUserService sysUserService;

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
		AuthUser authUser = SecurityUtils.getUser();
		if (authUser != null) {
			SysUser sysUser = sysUserService.get(authUser.getUserId());
			if (sysUser != null) {
				request.setAttribute("userId",sysUser.getId());
				request.setAttribute("username",sysUser.getRealName());
			}
		}
		super.afterCompletion(request, response, handler, ex);
	}

}
