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

/**
 * 市场栏位编号生成（01～99，本表内全局唯一）。
 */
@Component
public class StallCodeGenerator
{
    @Autowired
    private BizMarketStallMapper bizMarketStallMapper;

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