|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:problem_check_system/data/models/image_metadata_model.dart';
|
|
|
|
import 'package:problem_check_system/data/models/problem_sync_status.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
/// 问题的数据模型。
|
|
|
|
/// 用于表示系统中的一个具体问题,包含了问题的描述、位置、图片等信息。
|
|
|
|
class Problem {
|
|
|
|
/// 问题的唯一标识符,可空。
|
|
|
|
final String? id;
|
|
|
|
|
|
|
|
/// 问题的详细描述。
|
|
|
|
final String description;
|
|
|
|
|
|
|
|
/// 问题发生的位置。
|
|
|
|
final String location;
|
|
|
|
|
|
|
|
/// 问题的图片元数据列表。
|
|
|
|
final List<ImageMetadata> imageUrls;
|
|
|
|
|
|
|
|
/// 问题创建的时间。
|
|
|
|
final DateTime creationTime;
|
|
|
|
|
|
|
|
/// 问题的同步状态,默认为未同步。
|
|
|
|
final ProblemSyncStatus syncStatus;
|
|
|
|
|
|
|
|
/// 最后修改时间
|
|
|
|
final DateTime lastModifiedTime;
|
|
|
|
|
|
|
|
/// 相关的审查任务ID,可空。
|
|
|
|
final String? censorTaskId;
|
|
|
|
|
|
|
|
/// 绑定的附加数据,可空。
|
|
|
|
final String? bindData;
|
|
|
|
|
|
|
|
/// 问题是否已被检查,默认为false。
|
|
|
|
final bool isChecked;
|
|
|
|
|
|
|
|
Problem({
|
|
|
|
this.id,
|
|
|
|
required this.description,
|
|
|
|
required this.location,
|
|
|
|
required this.imageUrls,
|
|
|
|
required this.creationTime,
|
|
|
|
required this.lastModifiedTime,
|
|
|
|
this.syncStatus = ProblemSyncStatus.pendingCreate,
|
|
|
|
this.censorTaskId,
|
|
|
|
this.bindData,
|
|
|
|
this.isChecked = false,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// copyWith 方法,用于创建对象的副本并修改指定字段
|
|
|
|
Problem copyWith({
|
|
|
|
String? id,
|
|
|
|
String? description,
|
|
|
|
String? location,
|
|
|
|
List<ImageMetadata>? imageUrls,
|
|
|
|
DateTime? creationTime,
|
|
|
|
DateTime? lastModifiedTime,
|
|
|
|
ProblemSyncStatus? syncStatus,
|
|
|
|
bool? isDeleted,
|
|
|
|
String? censorTaskId,
|
|
|
|
String? bindData,
|
|
|
|
bool? isChecked,
|
|
|
|
}) {
|
|
|
|
return Problem(
|
|
|
|
id: id ?? this.id,
|
|
|
|
description: description ?? this.description,
|
|
|
|
location: location ?? this.location,
|
|
|
|
imageUrls: imageUrls ?? this.imageUrls,
|
|
|
|
creationTime: creationTime ?? this.creationTime,
|
|
|
|
lastModifiedTime: lastModifiedTime ?? this.lastModifiedTime,
|
|
|
|
syncStatus: syncStatus ?? this.syncStatus,
|
|
|
|
censorTaskId: censorTaskId ?? this.censorTaskId,
|
|
|
|
bindData: bindData ?? this.bindData,
|
|
|
|
isChecked: isChecked ?? this.isChecked,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 将对象转换为Map,用于SQLite存储
|
|
|
|
Map<String, dynamic> toMap() {
|
|
|
|
return {
|
|
|
|
'id': id,
|
|
|
|
'description': description,
|
|
|
|
'location': location,
|
|
|
|
'imageUrls': json.encode(imageUrls.map((e) => e.toMap()).toList()),
|
|
|
|
'creationTime': creationTime.millisecondsSinceEpoch,
|
|
|
|
'lastModifiedTime': lastModifiedTime.millisecondsSinceEpoch,
|
|
|
|
'syncStatus': syncStatus.index,
|
|
|
|
'censorTaskId': censorTaskId,
|
|
|
|
'bindData': bindData,
|
|
|
|
'isChecked': isChecked ? 1 : 0,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 从Map创建对象,用于从SQLite读取
|
|
|
|
factory Problem.fromMap(Map<String, dynamic> map) {
|
|
|
|
// 处理imageUrls的转换
|
|
|
|
List<ImageMetadata> imageUrlsList = [];
|
|
|
|
if (map['imageUrls'] != null) {
|
|
|
|
try {
|
|
|
|
final List<dynamic> imageList = json.decode(map['imageUrls']);
|
|
|
|
imageUrlsList = imageList.map((e) => ImageMetadata.fromMap(e)).toList();
|
|
|
|
} catch (e) {
|
|
|
|
// 如果解析失败,保持空列表
|
|
|
|
imageUrlsList = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Problem(
|
|
|
|
id: map['id'],
|
|
|
|
description: map['description'],
|
|
|
|
location: map['location'],
|
|
|
|
imageUrls: imageUrlsList,
|
|
|
|
creationTime: DateTime.fromMillisecondsSinceEpoch(map['creationTime']),
|
|
|
|
lastModifiedTime: DateTime.fromMillisecondsSinceEpoch(
|
|
|
|
map['lastModifiedTime'],
|
|
|
|
),
|
|
|
|
syncStatus: ProblemSyncStatus.values[map['syncStatus']],
|
|
|
|
censorTaskId: map['censorTaskId'],
|
|
|
|
bindData: map['bindData'],
|
|
|
|
isChecked: map['isChecked'] == 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 转换为JSON字符串
|
|
|
|
String toJson() => json.encode(toMap());
|
|
|
|
|
|
|
|
/// 从JSON字符串创建对象
|
|
|
|
factory Problem.fromJson(String source) =>
|
|
|
|
Problem.fromMap(json.decode(source));
|
|
|
|
}
|