package cn.cslg.pas.common.core.annotation;

import cn.cslg.pas.common.core.base.RequestHolder;
import cn.cslg.pas.common.core.exception.PermissionException;
import cn.cslg.pas.common.model.PersonnelVO;
import cn.cslg.pas.common.utils.CacheUtils;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.domain.Project;
import cn.cslg.pas.domain.ProjectUser;
import cn.cslg.pas.service.ProjectService;
import cn.cslg.pas.service.ProjectUserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
@Order(1)
@Slf4j
@Aspect
@Component
public class PermissionAspect {

    @Autowired
    private ProjectUserService projectUserService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private CacheUtils cacheUtils;
    @Autowired
    private LoginUtils loginUtils;
    @Pointcut("execution(public * cn.cslg.pas.controller.*.*(..))")
    public void privilege() {
    }

    @ResponseBody
    @Around("privilege()")
    public Object isAccessMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        //获取访问目标方法
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();
        final int[] methodAccess = AnnotationParse.permissionParse(targetMethod);
        if (methodAccess != null && methodAccess.length != 0) {
            HttpServletRequest request = RequestHolder.getRequest();
            if (request != null) {
                String projectId = request.getHeader("x-project-id");
                Integer userId = loginUtils.getId();
                if (StringUtils.isNotEmpty(projectId)) {
                    Project project = projectService.getById(projectId);
                    PersonnelVO user = cacheUtils.getLoginUserPersonnel(userId);
                    ProjectUser projectUser = projectUserService.getProjectUserByProjectIdAndUserId(Integer.parseInt(projectId), userId);
                    if (projectUser != null && project != null && user != null) {
                        if (project.getCreateBy().equals(userId)) {
                            return joinPoint.proceed();
                        }
                        if (Arrays.stream(methodAccess).noneMatch(item -> item == projectUser.getType())) {
                            throw new PermissionException("用户操作拦截");
                        }
                    }
                }
            }
        }
        return joinPoint.proceed();
    }
}
