package com.bs.common.aspectj;

import com.bs.common.annotation.TenantClear;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import com.bs.common.config.TenantConfigProperties;

/**
 * @作者 tjw
 * @创建时间 2024-03-02-15:10
 * @说明 ：
 */

@Aspect
@Component
public class TenantClearAspect {
    @Autowired
    private TenantConfigProperties tenantConfigProperties;

    @SneakyThrows
    @Around("@within(clear) || @annotation(clear)")
    public Object around(ProceedingJoinPoint point, TenantClear clear) {
        // 如果在类上，而不是在方法上，则为空
        if (clear == null) {
            Class<?> aClass = point.getTarget().getClass();
            clear = AnnotationUtils.findAnnotation(aClass, TenantClear.class);
        }
        if (clear != null) {
            if(clear.globalFlag()){
                tenantConfigProperties.setTenantSkip();
            }
        }
        Object result = point.proceed();
        if (!clear.globalFlag()) {
            tenantConfigProperties.clearSkip();
        }
        return result;
    }

}