package com.judong.chuanyiserver.annotation;

import com.judong.chuanyiserver.dao.UserDao;
import com.judong.chuanyiserver.entity.User;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.exception.CustomException;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.ConstantStr;
import com.judong.chuanyiserver.util.RedisUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Component
@Aspect
public class AdminPermissionsAspect {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private HttpServletRequest request;

    /**
     * 定义切点 @Pointcut
     * 在注解的位置切入代码
     */
    @Pointcut("execution(* com.judong.chuanyiserver..*.*(..))")
    public void adminPermissions() {
    }

    /**
     * 环绕通知
     */
    @Around(value = "adminPermissions() && @annotation(AdminPermissions)")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if (args != null && args.length > 0) {
            for (Object arg : args) {
                if (arg instanceof HttpServletRequest) {
                    request = (HttpServletRequest) arg;
                    break;
                }
            }
        }
        if (Blank.isEmpty(request)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        String userId = (String) redisUtil.get(request.getHeader("token"));
        if (Blank.isEmpty(userId)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        User user = userDao.getUserById(userId);
        if (Blank.isEmpty(user)) {
            throw new CustomException(ResultEnum.UNAUTHORIZED.getRespCode(), ResultEnum.UNAUTHORIZED.getRespMsg());
        }
        List<Integer> roleIdList = userDao.getRoleIdListByUserId(user.getUserId());
        if (Blank.isEmpty(roleIdList)) {
            throw new CustomException(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
        }
        for (Integer roleId : roleIdList) {
            if (roleId == ConstantStr.ROLE_ADMIN) {
                return joinPoint.proceed();
            }
        }
        throw new CustomException(ResultEnum.NO_OPERATION_AUTHORITY.getRespCode(), ResultEnum.NO_OPERATION_AUTHORITY.getRespMsg());
    }

    @Before(value = "adminPermissions() && @annotation(AdminPermissions)")
    public void before(JoinPoint joinPoint) {
        // 请求参数
//        System.out.println("Before");
    }

    @After(value = "adminPermissions() && @annotation(AdminPermissions)")
    public void after() {
//        System.out.println("After");
    }
}
