package cn.com.countrygarden.bi.main.service.test.aopUtils;

import cn.com.countrygarden.bi.main.service.test.TestService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Random;

/**
 * @author 木木子
 * @Description: java类作用描述
 * @email 944528607@qq.com
 * @date 2019/5/24 18:03
 */
@Aspect
@Component
public class UploadCountLogsAspect {
    @Autowired
    private TestService testService;

    //Controller层切点
    @Pointcut("@annotation(cn.com.countrygarden.bi.main.service.test.aopUtils.UploadCountLogs)")
    public void controllerAspect() {
        System.out.println("切入点");
    }

    /**
     * 前置通知 用于拦截controller层，获取记录的操作信息
     *
     * @param joinPoint 切点
     */

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) {
        System.out.println("》》》》》》》》》》》具体业务逻辑方法：前置通知 用于拦截controller层，获取记录的操作信息》》》》》》》》》》》》》");
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     *
     * @param joinPoint
     * @return
     */
    public static String[] getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class<?> targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        String[] annos = {""};
        for (Method method : methods ) {
if(method.getName().equals(methodName)){
    Class[] clazzs=method.getParameterTypes();
    if(clazzs.length==arguments.length){
        annos[0]=method.getAnnotation(UploadCountLogs.class).moduleName();
        if(annos[0].equals("新闻")){
            Random random=new Random();
            int ran=random.nextInt(30);
            if(ran<10){
                annos[0]="政务公示";
            }else if(ran>=10&&ran<20){
                annos[0]="政府情况";
            }else {
                annos[0]="新闻数据";
            }
            break;
        }
    }
}
        }
        return annos;
    }

}
