package cn.iocoder.yudao.framework.common.enums;

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.common.util.string.StrUtils;

import java.util.Date;

public enum CodeEnum {

    /**
     * 目前编码分为2种类型
     * =================================
     * type = 1:
     * 字段 20 位。 4位标识码+12位时间格式+4位随机码
     * 例如： BSCD202203081202A88E
     * =================================
     * type = 2:
     * 字段 28 位。  6位标识码+14位时间格式+8位随机码
     * 例如： INPJOE20220308120212ASJD89HS
     */
    /****************************  type = 1  *****************************/
    BUSINESS_CODE(1,"BSCD", "商户标识码"),
    GOODS_INFO_CODE(1,"GOCD", "商品标识码"),
    INPUTS_INFO_CODE(1,"INCD", "投入品标识码"),
    VENDOR_INFO_CODE(1,"VECD", "供应商标识码"),
    DEALER_INFO_CODE(1,"DACD", "经销商标识码"),
    LAND_INFO_CODE(1,"AECD", "地块/鱼塘/牧场标识码"),
    TRACE_TEMPLATE_CODE(1,"TATE", "溯源模板标识码"),
    UNI_TEMPLATE_CODE(1,"UNTE", "小程序证书模板标识码"),
    BUSINESS_RULE_CODE(1,"CARE", "溯源码规则标识码"),
    CHAIN_OPERATE_CODE(2,"COCD", "上链操作专属标识码"),
    TRACE_LINK_CODE(2,"TLCD", "溯源模板环节标识码"),
    INSPECTION_TEMPLATE_CODE(1,"VETE", "检测报告模板标识码"),
    INSPECTION_TEMPLATE_ITEM_CODE(1,"VTTE", "检测项目模板标识码"),
    LAND_PLAN_CODE(1,"JHPA", "种植计划标识码"),
    LAND_JOB_CODE(1,"ZYPA", "种植作业标识码"),

    /****************************  type = 2  *****************************/
    INPUTS_BUY_CODE(2,"BYPJOE", "采购单号标识码"),
    HARVEST_INFO_CODE(2,"INPJOE", "入库单号标识码"),
    OUTBOUND_CODE(2,"OUPJOE", "出库单号标识码"),
    INPUTS_BATCH_CODE(2,"INBTBC", "投入品批次标识码"),
    HARVEST_BATCH_CODE(2,"HABTBC", "采收批次标识码"),
    INSPECTION_CODE(2,"VEBTBC", "检验批次标识码"),
    INSPECTION_ITEM_CODE(2,"VEITBC", "检验检测项目标识码"),
    CODE_CODE(2,"CDREOE", "生码记录标识码"),
    OUTBOUND_BIND_CODE(2,"BINDOE", "出库单-商品批次关联绑定id"),
    GENERATE_BATCH_CODE(2,"PACEOE", "溯源码包标识码");

    private final Integer type;

    private final String prefix;

    private final String content;


    CodeEnum(Integer type, String prefix, String content) {
        this.type = type;
        this.prefix = prefix;
        this.content = content;
    }

    public Integer getType() {
        return type;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getContent() {
        return content;
    }

    /**
     * 根据类型和前缀得到一个随机编码
     * @return 随机编码
     */
    public String getCode() {
        StringBuilder sb = new StringBuilder(this.prefix);

        switch (this.type) {
            case 1:
                sb.append(DateUtil.format(new Date(), "yyyyMMddHHmm"))
                    .append(StrUtils.getRandomUpperString(4));
                break;
            case 2:
                sb.append(DateUtil.format(new Date(), "yyyyMMddHHmmss"))
                        .append(StrUtils.getRandomUpperString(8));
                break;
            default:
        }

        return sb.toString();
    }

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        System.out.println(CodeEnum.LAND_PLAN_CODE.getCode());
    }
}
