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.BizSupplierMapper;

/**
 * 供应商编号生成（01～99，全局唯一）。
 */
@Component
public class SupplierCodeGenerator
{
    @Autowired
    private BizSupplierMapper bizSupplierMapper;

    public String allocateNextCode()
    {
        String maxCode = bizSupplierMapper.selectMaxSupplierCodeForUpdate();
        int next = 1;
        if (StringUtils.isNotEmpty(maxCode))
        {
            next = Integer.parseInt(maxCode.trim()) + 1;
        }
        if (next > SupplierRules.MAX_SUPPLIER_CODE)
        {
            throw new ServiceException("供应商编号已达上限，请联系管理员");
        }
        return String.format("%02d", next);
    }
}
