package com.ruoyi.web.modules.account.facade.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.account.constant.MemberConstants;
import com.ruoyi.web.modules.account.domain.BizMember;
import com.ruoyi.web.modules.account.facade.IMemberFacade;
import com.ruoyi.web.modules.account.mapper.BizMemberMapper;
import com.ruoyi.web.modules.account.service.IMemberConsumptionService;

/**
 * 会员 Facade 实现
 */
@Service
public class MemberFacadeImpl implements IMemberFacade
{
    @Autowired
    private BizMemberMapper memberMapper;

    @Autowired
    private IMemberConsumptionService consumptionService;

    @Override
    public BizMember getMember(Long memberId)
    {
        if (memberId == null)
        {
            return null;
        }
        return memberMapper.selectById(memberId);
    }

    @Override
    public boolean isMemberEnabled(Long memberId)
    {
        BizMember member = getMember(memberId);
        return member != null && MemberConstants.STATUS_ENABLED.equals(member.getStatus());
    }

    @Override
    public boolean existsByMobile(String mobile)
    {
        return memberMapper.countByMobile(mobile, null) > 0;
    }

    @Override
    public void refreshConsumption(Long memberId)
    {
        consumptionService.refreshConsumption(memberId);
    }

    @Override
    public String getWxOpenid(Long memberId)
    {
        if (memberId == null)
        {
            return null;
        }
        return memberMapper.selectWxOpenid(memberId);
    }

    @Override
    public void bindWxOpenid(Long memberId, String openid)
    {
        if (memberId == null || StringUtils.isEmpty(openid))
        {
            return;
        }
        memberMapper.updateWxOpenid(memberId, openid);
    }
}
