package cn.com.countrygarden.bi.main.util.roleUtils;

import cn.com.countrygarden.bi.main.dao.sys.UserRolesMapper;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.sys.UserRoles;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import lombok.extern.slf4j.Slf4j;
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.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author 木木子
 * @Description: 自定义权限校验的具体实现
 * @email 944528607@qq.com
 * @date 2019/5/27 9:08
 */
@Aspect
@Component
@Slf4j
public class AopAuthorityCheck {

    @Autowired
    private UserRolesMapper userRolesMapper;
    @Autowired
    private AccessTokenService accessTokenService;

    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        getMethodAuthorityCheck( joinPoint);
    }

@Pointcut("@annotation(cn.com.countrygarden.bi.main.util.roleUtils.AuthorityCheck)")
public void controllerAspect() {
   //自定义注解实现模块的权限控制切入点
}


    public void getMethodAuthorityCheck(JoinPoint proceedingJoinPoint) throws NoSuchMethodException {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        //        根据授权获取用户信息
        String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        //1-1通过proceedingJoinPoint切入点获得目标代理对象方法名称
        String methodName = proceedingJoinPoint.getSignature().getName();
        //1-2获得目标对象
        Class<?> classTarget = proceedingJoinPoint.getTarget().getClass();
        //1-3获取目标对象类型
        Class[] declaringType = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterTypes();
        //1-4获取目标对象方法
        Method method = classTarget.getMethod(methodName, declaringType);

        //2.获取该方法上是否加上自定义的注解(ExtTransaction)
        AuthorityCheck authorityCheck = method.getDeclaredAnnotation(AuthorityCheck.class);
        if (null != authorityCheck) {
            String userCode =token.getUserCode();
            String[] authorityArray = method.getAnnotation(AuthorityCheck.class).authorityArray();
            Condition condition = new Condition(UserRoles.class);
            Example.Criteria criteria = condition.createCriteria();
            criteria.andEqualTo("userCode", userCode);
            criteria.andIn("roleCode", Arrays.asList(authorityArray));
            int count = userRolesMapper.selectCountByCondition(condition);
            if (count < 1) {
                throw new ServiceException("你当前没有相应权限，无数据展示");
            }
        }
    }
}
