package com.kym.admin.config;

import cn.dev33.satoken.stp.StpInterface;
import com.kym.common.utils.CommUtil;
import com.kym.entity.vo.AdminUserVo;
import com.kym.service.AdminUserService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * @author skyline
 * @description 权限控制
 * @date 2023-08-22 18:33
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    private final AdminUserService adminUserService;

    public StpInterfaceImpl(AdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object userId, String loginType) {
        List<String> permissions = new ArrayList<String>();
        List<AdminUserVo> adminUserVos = adminUserService.listUserPermissions(CommUtil.null2Long(userId));
        if(!CommUtil.isEmptyOrNull(adminUserVos)){
            permissions = adminUserVos.stream().map(AdminUserVo::getPermissions).filter(kPermissions -> !CommUtil.isEmptyOrNull(kPermissions)).flatMap(Collection::stream).distinct().toList();
        }
        return permissions;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return adminUserService.listUserRoles(CommUtil.null2Long(loginId));
    }

}
