package com.ruoyi.web.modules.trading.support;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.modules.trading.domain.BizDistributor;
import com.ruoyi.web.modules.trading.mapper.BizDistributorMapper;

/**
 * 小程序承销商登录上下文。
 */
@Component
public class AppDistributorSessionSupport
{
    @Autowired
    private BizDistributorMapper bizDistributorMapper;

    public Long requireDistributorId()
    {
        Long userId = SecurityUtils.getUserId();
        if (userId == null)
        {
            throw new ServiceException(AppDistributorHomeRules.MSG_NO_DISTRIBUTOR);
        }
        PageHelper.clearPage();
        BizDistributor row = bizDistributorMapper.selectBizDistributorBySysUserId(userId);
        if (row == null || row.getId() == null)
        {
            throw new ServiceException(AppDistributorHomeRules.MSG_NO_DISTRIBUTOR);
        }
        return row.getId();
    }
}
