package com.app.admin.framework.log.aspect;

import cn.hutool.core.date.DateUtil;
import com.app.framework.core.log.annotation.LogOps;
import com.app.framework.core.utils.request.RequestUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 利用AOP拦截记录日志
 */
@Aspect
@Component
public class OperationLogAspect {

    @Autowired
    OperationLogTask operationLogTask;

    /**
     * 方法执行后的切入点[采用LogOpsAfter注解的方法]
     */
    @Pointcut("@annotation(com.app.framework.core.log.annotation.LogOps)")
    public void doControllerAspectAfter() {
    }


    @After(value = "doControllerAspectAfter() && @annotation(annotation) && args(object,..)", argNames = "annotation,object")
    public void doMethodAfter(JoinPoint jp, LogOps annotation, Object object) throws Throwable {
        HttpServletRequest request = RequestUtil.getRequest();
        operationLogTask.handler(request, jp, annotation, DateUtil.date().toString());
    }

}
