document.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /// 文档模型
  2. class Document {
  3. final String id;
  4. final String name;
  5. final String? description;
  6. final DocumentType type;
  7. final DocumentStatus status;
  8. final DateTime createdAt;
  9. final DateTime? updatedAt;
  10. final int? fileSize; // bytes
  11. final String? fileUrl;
  12. final String? parsedText;
  13. final Map<String, dynamic>? metadata;
  14. Document({
  15. required this.id,
  16. required this.name,
  17. this.description,
  18. required this.type,
  19. required this.status,
  20. required this.createdAt,
  21. this.updatedAt,
  22. this.fileSize,
  23. this.fileUrl,
  24. this.parsedText,
  25. this.metadata,
  26. });
  27. /// 从JSON创建
  28. factory Document.fromJson(Map<String, dynamic> json) {
  29. return Document(
  30. id: json['id'] as String,
  31. name: json['name'] as String,
  32. description: json['description'] as String?,
  33. type: DocumentType.values.firstWhere(
  34. (e) => e.toString().split('.').last == json['type'],
  35. orElse: () => DocumentType.other,
  36. ),
  37. status: DocumentStatus.values.firstWhere(
  38. (e) => e.toString().split('.').last == json['status'],
  39. orElse: () => DocumentStatus.pending,
  40. ),
  41. createdAt: DateTime.parse(json['createdAt'] as String),
  42. updatedAt: json['updatedAt'] != null
  43. ? DateTime.parse(json['updatedAt'] as String)
  44. : null,
  45. fileSize: json['fileSize'] as int?,
  46. fileUrl: json['fileUrl'] as String?,
  47. parsedText: json['parsedText'] as String?,
  48. metadata: json['metadata'] as Map<String, dynamic>?,
  49. );
  50. }
  51. /// 转换为JSON
  52. Map<String, dynamic> toJson() {
  53. return {
  54. 'id': id,
  55. 'name': name,
  56. 'description': description,
  57. 'type': type.toString().split('.').last,
  58. 'status': status.toString().split('.').last,
  59. 'createdAt': createdAt.toIso8601String(),
  60. 'updatedAt': updatedAt?.toIso8601String(),
  61. 'fileSize': fileSize,
  62. 'fileUrl': fileUrl,
  63. 'parsedText': parsedText,
  64. 'metadata': metadata,
  65. };
  66. }
  67. /// 复制并更新
  68. Document copyWith({
  69. String? id,
  70. String? name,
  71. String? description,
  72. DocumentType? type,
  73. DocumentStatus? status,
  74. DateTime? createdAt,
  75. DateTime? updatedAt,
  76. int? fileSize,
  77. String? fileUrl,
  78. String? parsedText,
  79. Map<String, dynamic>? metadata,
  80. }) {
  81. return Document(
  82. id: id ?? this.id,
  83. name: name ?? this.name,
  84. description: description ?? this.description,
  85. type: type ?? this.type,
  86. status: status ?? this.status,
  87. createdAt: createdAt ?? this.createdAt,
  88. updatedAt: updatedAt ?? this.updatedAt,
  89. fileSize: fileSize ?? this.fileSize,
  90. fileUrl: fileUrl ?? this.fileUrl,
  91. parsedText: parsedText ?? this.parsedText,
  92. metadata: metadata ?? this.metadata,
  93. );
  94. }
  95. /// 格式化文件大小
  96. String get formattedFileSize {
  97. if (fileSize == null) return '未知';
  98. if (fileSize! < 1024) return '${fileSize}B';
  99. if (fileSize! < 1024 * 1024)
  100. return '${(fileSize! / 1024).toStringAsFixed(2)}KB';
  101. return '${(fileSize! / (1024 * 1024)).toStringAsFixed(2)}MB';
  102. }
  103. }
  104. /// 文档类型
  105. enum DocumentType { pdf, word, image, markdown, other }
  106. /// 文档状态
  107. enum DocumentStatus {
  108. pending, // 待处理
  109. uploading, // 上传中
  110. parsing, // 解析中
  111. processing, // 处理中
  112. completed, // 已完成
  113. failed, // 失败
  114. }
  115. extension DocumentStatusExtension on DocumentStatus {
  116. String get label {
  117. switch (this) {
  118. case DocumentStatus.pending:
  119. return '待处理';
  120. case DocumentStatus.uploading:
  121. return '上传中';
  122. case DocumentStatus.parsing:
  123. return '解析中';
  124. case DocumentStatus.processing:
  125. return '处理中';
  126. case DocumentStatus.completed:
  127. return '已完成';
  128. case DocumentStatus.failed:
  129. return '失败';
  130. }
  131. }
  132. }