package com.jeesharp.business.modules.contentsecurity.application.support;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.member.service.MemberOauthService;
import com.jeesharp.business.wechat.config.WeChatConfig;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 解析会员在微信小程序下的 openid，供 msgSecCheck v2 使用。
 *
 * @author jeesharp
 * @since 2026-06-27
 */
@Component
@AllArgsConstructor
public class ContentSecurityOpenIdResolver {

    private final MemberOauthService memberOauthService;
    private final WeChatConfig weChatConfig;

    /**
     * 按当前配置的小程序 appId 查询会员 openid。
     */
    public String resolveOpenid(String memberId) {
        if (StrUtil.isBlank(memberId)) {
            return null;
        }
        String appId = weChatConfig.getMiniappAppId();
        if (StrUtil.isBlank(appId)) {
            return null;
        }
        return memberOauthService.getWechatMiniOpenidByMemberIdAppId(memberId, appId);
    }
}
