Files
AutoTime-Tracker/lib/models/time_goal.dart
2025-11-13 15:45:28 +08:00

55 lines
1.5 KiB
Dart

class TimeGoal {
final int? id;
final String goalType; // 'daily_total' 或 'daily_category'
final String? category; // 如果是分类目标,指定分类
final int targetTime; // 目标时长(秒)
final bool isActive;
final DateTime createdAt;
final DateTime updatedAt;
TimeGoal({
this.id,
required this.goalType,
this.category,
required this.targetTime,
this.isActive = true,
required this.createdAt,
required this.updatedAt,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'goal_type': goalType,
'category': category,
'target_time': targetTime,
'is_active': isActive ? 1 : 0,
'created_at': createdAt.millisecondsSinceEpoch ~/ 1000,
'updated_at': updatedAt.millisecondsSinceEpoch ~/ 1000,
};
}
factory TimeGoal.fromMap(Map<String, dynamic> map) {
return TimeGoal(
id: map['id'] as int?,
goalType: map['goal_type'] as String,
category: map['category'] as String?,
targetTime: map['target_time'] as int,
isActive: (map['is_active'] as int) == 1,
createdAt: DateTime.fromMillisecondsSinceEpoch((map['created_at'] as int) * 1000),
updatedAt: DateTime.fromMillisecondsSinceEpoch((map['updated_at'] as int) * 1000),
);
}
// 格式化目标时长
String get formattedTargetTime {
final hours = targetTime ~/ 3600;
final minutes = (targetTime % 3600) ~/ 60;
if (hours > 0) {
return '${hours}小时${minutes}分钟';
}
return '${minutes}分钟';
}
}