package com.njzscloud.common.sn.support;

import com.njzscloud.common.core.ex.Exceptions;

import java.util.List;
import java.util.stream.Collectors;

public class Sn {
    private final List<ISnSection> sections;

    public Sn(List<SectionConfig> configs) {
        this.sections = configs.stream().map(SectionConfig::section).collect(Collectors.toList());
    }

    public Sn() {
        this.sections = null;
    }

    public String next() {
        return next(null);
    }

    /**
     * 下一个编码
     *
     * @param pici 批次
     * @return 下一个编码
     */
    public String next(String pici) {
        if (sections == null || sections.isEmpty()) {
            return null;
        }

        StringBuilder builder = new StringBuilder();

        try {
            for (ISnSection section : sections) {
                builder.append(section.next(pici));
            }
        } catch (Exception e) {
            throw Exceptions.error("编码配置配置错误");
        }

        return builder.toString();
    }
}
