package cn.iocoder.yudao.module.system.service.biz;

import cn.hutool.core.util.IdUtil;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 物资出入库单据号生成器
 * 规则 (使用雪花ID，不保证连续):
 * - 入库：RKDHyyyyMMdd + 雪花ID
 * - 出库：CKDHyyyyMMdd + 雪花ID
 * - 入库退回：RKRHyyyyMMdd + 雪花ID
 * - 出库退回：CKRHyyyyMMdd + 雪花ID
 */
@Component
public class MaterialIoOrderNoGenerator {

    private static final DateTimeFormatter DATE_FMT = DateTimeFormatter.ofPattern("yyyyMMdd");

    public String generate(String bizType, LocalDate bizDate) {
        String prefix;
        switch (bizType) {
            case "IN":
                prefix = "RKDH";
                break;
            case "OUT":
                prefix = "CKDH";
                break;
            case "IN_RETURN":
                prefix = "RKRH";
                break;
            case "OUT_RETURN":
                prefix = "CKRH";
                break;
            default:
                prefix = "IO";
        }

        // 使用 hutool 的雪花算法生成唯一ID字符串
        String snowflakeId = IdUtil.getSnowflake().nextIdStr();

        return prefix + bizDate.format(DATE_FMT) + snowflakeId;
    }
}
