package com.yinlihupo.enlish.service.config;


import cn.dev33.satoken.stp.StpInterface;

import com.google.common.cache.Cache;
import com.yinlihupo.enlish.service.constant.RoleConstants;
import com.yinlihupo.framework.common.util.JsonUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {

        return List.of();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        long l = 0L;
        if (loginId instanceof String loginIdStr) {
            l = Long.parseLong(loginIdStr);
        }
        return userToRole(l);
    }

    private List<String> userToRole(Long userId) {

        String keys = stringRedisTemplate.opsForValue().get(RoleConstants.buildUserRoleKey(userId));
        if (keys != null) {
            try {
                return JsonUtils.parseList(keys, String.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return List.of();
    }
}
