package com.gci.boot.test;

import com.gci.boot.common.util.JsonUtil;
import com.gci.boot.modules.system.entity.SysDepart;
import com.gci.boot.modules.system.entity.SysPermission;
import com.gci.boot.modules.system.util.JwtUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.core.annotation.AnnotationUtils;

import javax.annotation.*;
import java.lang.annotation.*;
import java.lang.reflect.Field;

public class PermissionTest {

    public static void main(String[] args) {
        test3();
    }

    public static void test1() {
        System.out.println(JsonUtil.object2JsonAllPretty(new SysDepart()));

        Field[] fields = SysPermission.class.getDeclaredFields();
        for (Field f : fields) {
            if (f.isAnnotationPresent(ApiModelProperty.class)) {
                ApiModelProperty annotation = f.getAnnotation(ApiModelProperty.class);
                String value = annotation.value();
                System.out.println(f.getName() + "\t" + value + "\t" + f.getType().getSimpleName() + "\t是\t" + value);
            }
        }
    }

    public static void test2() {
        try {
            String token = JwtUtil.sign("admin", "abcdef", 200);
            Thread.sleep(1000L);
            boolean verify = JwtUtil.verify(token, "admin", "abcdef");
            System.out.println(verify);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public static void test3(){
        System.out.println(TestAB.class.isAnnotationPresent(Aaa.class));
        System.out.println(TestAB.class.isAnnotationPresent(Bbb.class));
        Aaa annotation = AnnotationUtils.findAnnotation(TestAB.class, Aaa.class);
        System.out.println(annotation.annotationType().toString());

        Annotation[] annotations = TestAB.class.getAnnotations();
        for(Annotation a : annotations){
            System.out.println(a.toString());
        }
        getAnnos(TestAB.class);
    }

    @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    public static @interface Aaa{

    }

    @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
    @Retention(RetentionPolicy.RUNTIME)
    @Aaa
    public static @interface Bbb{

    }

    @Bbb
    public static class TestAB{

    }

    public static void getAnnos(Class<?> classz) {
        Annotation[] annotations = classz.getAnnotations();
        for (Annotation annotation : annotations) {
            if (annotation.annotationType() != Deprecated.class &&
                    annotation.annotationType() != SuppressWarnings.class &&
                    annotation.annotationType() != Override.class &&
                    annotation.annotationType() != PostConstruct.class &&
                    annotation.annotationType() != PreDestroy.class &&
                    annotation.annotationType() != Resource.class &&
                    annotation.annotationType() != Resources.class &&
                    annotation.annotationType() != Generated.class &&
                    annotation.annotationType() != Target.class &&
                    annotation.annotationType() != Retention.class &&
                    annotation.annotationType() != Documented.class &&
                    annotation.annotationType() != Inherited.class
            ) {
                if (annotation.annotationType() == Aaa.class) {
                    System.out.println(" 存在注解 @Aaa ");
                    return;
                } else {
                    getAnnos(annotation.annotationType());
                }
            }
        }
    }

}
