package com.custom.webui.service; import com.custom.webui.exception.BusinessException; /** * 会话导出服务:把一个对话导出成 JSON / TXT / PDF。 * *

三者的定位不同,不是同一份内容换三种后缀: *

*/ public interface ChatExportService { /** 导出格式 */ enum Format { JSON, TXT, PDF; /** 解析格式参数,不认识的值直接报错而不是悄悄按默认值处理 */ public static Format parse(String raw) { if (raw == null || raw.isBlank()) { return TXT; } for (Format f : values()) { if (f.name().equalsIgnoreCase(raw.trim())) { return f; } } throw BusinessException.badRequest( "不支持的导出格式:" + raw + "(可选 json / txt / pdf)"); } public String extension() { return name().toLowerCase(); } public String contentType() { return switch (this) { case JSON -> "application/json;charset=UTF-8"; case TXT -> "text/plain;charset=UTF-8"; case PDF -> "application/pdf"; }; } } /** * 导出结果。 * * @param data 文件字节 * @param filename 建议的文件名(已做非法字符清洗) * @param contentType MIME 类型 */ record ExportFile(byte[] data, String filename, String contentType) { } /** * 导出一个会话。 * * @throws com.custom.webui.exception.BusinessException 会话不存在或不属于该用户(404) */ ExportFile export(Long userId, Long chatId, Format format); }