package com.xingxi.api.configuration;

import com.xingxi.common.annotation.Log;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Aspect
//@Component
@RequiredArgsConstructor
class OperLogAspect {
    private final OperLogService operLogService;
    @Pointcut("@annotation(com.xingxi.common.annotation.Log)")
    public void logPointCut() {
    }

    @Before("@annotation(controllerLog)")
    public void before(JoinPoint joinPoint, Log controllerLog){
        HttpServletRequest servletRequest = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        operLogService.handleBeforeLog(joinPoint, controllerLog, servletRequest);
    }
}
