package com.dtok.framework.authenticate;

import cn.dev33.satoken.stp.StpInterface;
import com.dtok.entity.Account;
import com.dtok.entity.Permission;
import com.dtok.repository.AccountRepository;
import com.dtok.repository.PermissionRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    AccountRepository userRepository;

    @Autowired
    PermissionRepository permissionRepository;

    private static final Logger LOGIN_LOG = LoggerFactory.getLogger("LoginLog");

    @Override
    public List<String> getPermissionList(Object o, String s) {
        List<Permission> permissions = permissionRepository.findPermissionByUsername((String) o);
        return permissions.stream().map(Permission::getCode).collect(Collectors.toList());
    }

    @Override
    public List<String> getRoleList(Object o, String s) {
        Account account = userRepository.getFirstByUsername((String) o);
        List<String> role = new ArrayList<>();
        role.add(account.getRoleCode());
        return role;
    }
}
