|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:problem_check_system/data/models/image_metadata_model.dart';
|
|
|
|
import 'package:problem_check_system/data/models/operation.dart';
|
|
|
|
import 'package:problem_check_system/data/models/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 SyncStatus syncStatus;
|
|
|
|
|
|
|
|
/// 对问题的操作类型,默认为创建。
|
|
|
|
final Operation operation;
|
|
|
|
|
|
|
|
/// 是否逻辑删除
|
|
|
|
final bool isDeleted;
|
|
|
|
|
|
|
|
/// 相关的审查任务ID,可空。
|
|
|
|
final String? censorTaskId;
|
|
|
|
|
|
|
|
/// 绑定的附加数据,可空。
|
|
|
|
final String? bindData;
|
|
|
|
|
|
|
|
/// 问题是否已被检查,默认为false。
|
|
|
|
final bool isChecked;
|
|
|
|
|
|
|
|
/// 静态对象uuid
|
|
|
|
static final Uuid _uuid = Uuid();
|
|
|
|
|
|
|
|
Problem({
|
|
|
|
this.id,
|
|
|
|
required this.description,
|
|
|
|
required this.location,
|
|
|
|
required this.imageUrls,
|
|
|
|
required this.creationTime,
|
|
|
|
this.syncStatus = SyncStatus.notSynced,
|
|
|
|
this.operation = Operation.create,
|
|
|
|
this.isDeleted = false,
|
|
|
|
this.censorTaskId,
|
|
|
|
this.bindData,
|
|
|
|
this.isChecked = false,
|
|
|
|
});
|
|
|
|
|
|
|
|
/// 工厂方法:创建新问题(自动生成ID)
|
|
|
|
factory Problem.create({
|
|
|
|
required String description,
|
|
|
|
required String location,
|
|
|
|
required List<ImageMetadata> imageUrls,
|
|
|
|
}) {
|
|
|
|
return Problem(
|
|
|
|
id: _uuid.v4(),
|
|
|
|
description: description,
|
|
|
|
location: location,
|
|
|
|
imageUrls: imageUrls,
|
|
|
|
creationTime: DateTime.now(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// copyWith 方法,用于创建对象的副本并修改指定字段
|
|
|
|
Problem copyWith({
|
|
|
|
String? id,
|
|
|
|
String? description,
|
|
|
|
String? location,
|
|
|
|
List<ImageMetadata>? imageUrls,
|
|
|
|
DateTime? creationTime,
|
|
|
|
SyncStatus? syncStatus,
|
|
|
|
Operation? operation,
|
|
|
|
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,
|
|
|
|
syncStatus: syncStatus ?? this.syncStatus,
|
|
|
|
operation: operation ?? this.operation,
|
|
|
|
isDeleted: isDeleted ?? this.isDeleted,
|
|
|
|
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,
|
|
|
|
'syncStatus': syncStatus.index,
|
|
|
|
'operation': operation.index,
|
|
|
|
'isDeleted': isDeleted ? 1 : 0,
|
|
|
|
'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']),
|
|
|
|
syncStatus: SyncStatus.values[map['syncStatus']],
|
|
|
|
operation: Operation.values[map['operation']],
|
|
|
|
isDeleted: map['isDeleted'] == 1,
|
|
|
|
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));
|
|
|
|
}
|