package com.njzscloud.common.sn.support;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.sn.SnIncService;
import com.njzscloud.common.sn.contant.PadMode;
import com.njzscloud.common.sn.contant.RollbackMode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class IncSection implements ISnSection {
    /**
     * 编码段编号
     */
    private final String code;
    /**
     * 步长
     */
    private final Integer step;
    /**
     * 初始数值
     */
    private final Integer initialVal;
    /**
     * 填充模式
     */
    private final PadMode padMode;
    /**
     * 填充值
     */
    private final String padVal;
    /**
     * 填充长度
     */
    private final Integer padLen;
    private final SnIncService snIncService = SpringUtil.getBean(SnIncService.class);
    /**
     * 回卷模式
     */
    private RollbackMode rollbackMode;
    /**
     * 是否允许溢出
     */
    private Boolean allowOverflow;

    @Override
    public String next(String pici) {
        Long val = snIncService.inc(code, pici);
        return padMode == PadMode.Zuo ? StrUtil.padPre(val.toString(), padLen, padVal) :
            padMode == PadMode.You ? StrUtil.padAfter(val.toString(), padLen, padVal) :
                val.toString();
    }

}
