
package com.tmerclub.cloud.rbac.feign;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.platform.vo.UriPermissionVO;
import com.tmerclub.cloud.api.rbac.bo.UriPermissionBO;
import com.tmerclub.cloud.api.rbac.feign.PermissionFeignClient;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.PmsContext;
import com.tmerclub.cloud.common.util.BooleanUtil;
import com.tmerclub.cloud.rbac.service.MenuPermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;

import java.util.*;

/**
 * @author tmerclub
 * @date 2020/7/15
 */
@DubboService
public class PermissionFeignController implements PermissionFeignClient {

    private static final Logger logger = LoggerFactory.getLogger(PermissionFeignController.class);

    @Autowired
    private MenuPermissionService menuPermissionService;

    @Override
    public ServerResponseEntity<Boolean> checkPermission(Long userId, Integer sysType, String uri, Integer isAdmin, Integer method) {
        // 从上下文获取
        List<UriPermissionVO> uriPermissionVOList = PmsContext.get();
        AntPathMatcher pathMatcher = new AntPathMatcher();
        boolean match = true;
        // 第三方接口调用权限判断
        if (CollUtil.isNotEmpty(uriPermissionVOList)) {
            match = false;
            for (UriPermissionVO uriPermissionVO : uriPermissionVOList) {
                // 判断是否有权限
                if (Objects.equals(uriPermissionVO.getUri(), uri)) {
                    return ServerResponseEntity.success(Boolean.TRUE);
                }
            }
        }
        // 系统接口权限判断
        else {
            List<UriPermissionBO> uriPermissions = new ArrayList<>();
            // 系统管理员，拥有最高权限
            if (BooleanUtil.isTrue(isAdmin)) {
                uriPermissions = menuPermissionService.listUriPermissionInfo(sysType);
            }
            // 目前该用户拥有的权限
            else {
                uriPermissions = menuPermissionService.listPermissionByUserIdAndSysType(userId, sysType);
            }

            // 看看该uri对应需要什么权限
            for (UriPermissionBO uriPermission : uriPermissions) {
                // uri + 请求方式匹配
                if (pathMatcher.match(uriPermission.getUri(), uri) && Objects.equals(uriPermission.getMethod(), method)) {
                    // uri 用户有这个权限
                    if (uriPermission.getHasPermission()) {
                        return ServerResponseEntity.success(Boolean.TRUE);
                    } else {
                        match = false;
                    }
                }
            }
        }


        if (!match) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        // 如果uri 没有匹配到，则说明uri不需要权限，直接校验成功
        return ServerResponseEntity.success(Boolean.TRUE);
    }

    @Override
    public ServerResponseEntity<List<UriPermissionBO>> getUriPermissionListByMenuPermIdList(List<Long> menuPermIdList) {
        return ServerResponseEntity.success(menuPermissionService.getUriPermissionListByMenuPermIdList(menuPermIdList));
    }

}
