package com.smppw.modaq.infrastructure.components;

import cn.hutool.core.util.IdUtil;
import com.smppw.modaq.common.conts.Constants;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ScheduledTaskAspect {

    @Around("@annotation(org.springframework.scheduling.annotation.Scheduled)")
    public Object wrapScheduledTask(ProceedingJoinPoint pjp) throws Throwable {
        try {
            // 生成任务唯一ID
            String taskId = "TASK-" + IdUtil.nanoId();
            MDC.put(Constants.MDC_REQUEST_ID_KEY, taskId);
            return pjp.proceed();
        } finally {
            MDC.remove(Constants.MDC_REQUEST_ID_KEY);
        }
    }
}
