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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.trading.mapper.BizDistributorMapper;

/**
 * 承销商编号生成（01～99，全局唯一）。
 */
@Component
public class DistributorCodeGenerator
{
    @Autowired
    private BizDistributorMapper bizDistributorMapper;

    public String allocateNextCode()
    {
        String maxCode = bizDistributorMapper.selectMaxDistributorCodeForUpdate();
        int next = 1;
        if (StringUtils.isNotEmpty(maxCode))
        {
            next = Integer.parseInt(maxCode.trim()) + 1;
        }
        if (next > DistributorRules.MAX_DISTRIBUTOR_CODE)
        {
            throw new ServiceException("承销商编号已达上限，请联系管理员");
        }
        return String.format("%02d", next);
    }
}
