package cn.iocoder.yudao.module.system.config.mdc;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Slf4j
@Aspect
@Component
public class MDCJobAopConfig {
    private static final String REQUEST_ID = "requestId";

    /**
     * xxl-job 切点
     */
    @Pointcut("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
    public void pointCutExecution() {}

    @Before("pointCutExecution()")
    public void doBefore(JoinPoint point) {
        // 增加xxl-job任务的链式日志id
        MDC.put(REQUEST_ID, UUID.randomUUID().toString().replace("-", ""));
    }

    @After("pointCutExecution()")
    public void doAfter(JoinPoint point) {
        // xxl-job任务结束，MDC移除
        MDC.remove(REQUEST_ID);
    }


}
