package com.jeesharp.business.modules.member.config;

import cn.dev33.satoken.stp.StpInterface;
import com.jeesharp.business.modules.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 会员端 Sa-Token 权限接口实现
 * <p>
 * 根据会员等级返回功能权限码列表，供 {@code @SaCheckPermission} 或 {@code StpUtil.checkPermission} 校验。
 * 权限码格式：member:feature:xxx，如 member:feature:coupon_exchange
 * </p>
 *
 * @author jeesharp
 * @version 2024-01-24
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class MemberStpInterface implements StpInterface {

    private final MemberService memberService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        if (loginId == null) {
            return Collections.emptyList();
        }
        try {
            return memberService.getMemberFeatureCodes(loginId.toString());
        } catch (Exception e) {
            log.warn("获取会员功能权限失败: loginId={}, loginType={}", loginId, loginType, e);
            return Collections.emptyList();
        }
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 会员端暂不使用角色，返回空列表
        return Collections.emptyList();
    }
}
