package thyyxxk.webserver.aop.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.aop.interfaces.NeedRule;
import thyyxxk.webserver.config.exception.BizException;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.service.PublicServer;


/**
 * @author 肖蟾
 */
@Aspect
@Component
@Slf4j
public class NoDeedRule {

    private final PublicServer publicServer;

    public NoDeedRule(PublicServer publicServer) {
        this.publicServer = publicServer;
    }

    @Pointcut("@annotation(thyyxxk.webserver.aop.interfaces.NeedRule)")
    public void noDeedRule() {
    }

    /**
     * 如果这里不这样写就无法获取 注解里面的值
     *
     * @param noNeedRule
     */
    @Before("noDeedRule() && @annotation(noNeedRule)")
    public void before(NeedRule noNeedRule) {
        if (publicServer.needRule(noNeedRule.value())) {
            return;
        }
        throw new BizException(ExceptionEnum.LOGICAL_ERROR, "您没有权限。");
    }

}
