package cn.gov.customs.wxjy.common.enums;

/**
 * 系统错误码枚举
 */
public enum SysErrorCodeEnum {
    INSERT_ERROR("INSERT_ERROR","新增失败"),
    UPDATE_ERROR("UPDATE_ERROR","修改失败"),
    DELETE_ERROR("DELETE_ERROR","删除失败"),
    OPER_ERROR("DELETE_ERROR","操作失败"),
    INVALID_TOKEN("INVALID_TOKEN","令牌无效，请重新登录 (失效原因: %s)"),

    ENABLE_CODE("1","启用"),
    DISABLE_CODE("2","禁用");


    private final String code;
    private final String messageTemplate;
    SysErrorCodeEnum(String code, String messageTemplate) {
        this.code = code;
        this.messageTemplate = messageTemplate;
    }
    public String getCode() {
        return code;
    }

    public String getMessageTemplate() {
        return messageTemplate;
    }

    public String format(Object... args) {
        // 计算占位符数量
        int placeholders = countPlaceholders(messageTemplate);
        if (args.length != placeholders) {
            throw new IllegalArgumentException("参数数量不匹配，需要" + placeholders + "个参数");
        }
        return String.format(messageTemplate, args);
    }
    private int countPlaceholders(String template) {
        int count = 0;
        int index = -1;
        while ((index = template.indexOf("%s", index + 1)) != -1) {
            count++;
        }
        return count;
    }
}
