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

/**
 * 隔离圈位编号生成（01～99，全局唯一）。
 */
@Component
public class PenCodeGenerator
{
    @Autowired
    private BizIsolationPenMapper bizIsolationPenMapper;

    public String allocateNextCode()
    {
        String maxCode = bizIsolationPenMapper.selectMaxPenCodeForUpdate();
        int next = 1;
        if (StringUtils.isNotEmpty(maxCode))
        {
            next = Integer.parseInt(maxCode.trim()) + 1;
        }
        if (next > IsolationPenRules.MAX_PEN_CODE)
        {
            throw new ServiceException("圈位编号已达上限，请联系管理员");
        }
        return String.format("%02d", next);
    }
}
