first commit
This commit is contained in:
130
lib/database/time_goal_dao.dart
Normal file
130
lib/database/time_goal_dao.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
import 'package:flutter/foundation.dart' show kIsWeb;
|
||||
import '../models/time_goal.dart';
|
||||
import 'database_helper.dart';
|
||||
|
||||
class TimeGoalDao {
|
||||
final DatabaseHelper _dbHelper = DatabaseHelper.instance;
|
||||
|
||||
// Web 平台使用内存存储
|
||||
final List<TimeGoal> _webGoals = [];
|
||||
|
||||
// Web 平台检查
|
||||
bool get _isWeb => kIsWeb;
|
||||
|
||||
// 插入或更新时间目标
|
||||
Future<void> upsertTimeGoal(TimeGoal goal) async {
|
||||
if (_isWeb) {
|
||||
// Web 平台使用内存存储
|
||||
final index = _webGoals.indexWhere((g) =>
|
||||
g.goalType == goal.goalType && g.category == goal.category);
|
||||
if (index >= 0) {
|
||||
_webGoals[index] = goal;
|
||||
} else {
|
||||
_webGoals.add(goal);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
final db = await _dbHelper.database;
|
||||
final now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
|
||||
|
||||
final map = {
|
||||
'goal_type': goal.goalType,
|
||||
'category': goal.category,
|
||||
'target_time': goal.targetTime,
|
||||
'is_active': goal.isActive ? 1 : 0,
|
||||
'created_at': now,
|
||||
'updated_at': now,
|
||||
};
|
||||
|
||||
// 检查是否已存在相同类型的目标
|
||||
final existing = await db.query(
|
||||
'time_goal',
|
||||
where: 'goal_type = ? AND (category = ? OR category IS NULL)',
|
||||
whereArgs: [
|
||||
goal.goalType,
|
||||
goal.category,
|
||||
],
|
||||
);
|
||||
|
||||
if (existing.isEmpty) {
|
||||
await db.insert('time_goal', map);
|
||||
} else {
|
||||
await db.update(
|
||||
'time_goal',
|
||||
map,
|
||||
where: 'goal_type = ? AND (category = ? OR category IS NULL)',
|
||||
whereArgs: [
|
||||
goal.goalType,
|
||||
goal.category,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有激活的目标
|
||||
Future<List<TimeGoal>> getActiveGoals() async {
|
||||
if (_isWeb) {
|
||||
return _webGoals.where((g) => g.isActive).toList();
|
||||
}
|
||||
|
||||
final db = await _dbHelper.database;
|
||||
final maps = await db.query(
|
||||
'time_goal',
|
||||
where: 'is_active = 1',
|
||||
orderBy: 'goal_type, category',
|
||||
);
|
||||
|
||||
return maps.map((map) => TimeGoal.fromMap(map)).toList();
|
||||
}
|
||||
|
||||
// 获取所有目标(包括非激活的)
|
||||
Future<List<TimeGoal>> getAllGoals() async {
|
||||
if (_isWeb) {
|
||||
return List<TimeGoal>.from(_webGoals);
|
||||
}
|
||||
|
||||
final db = await _dbHelper.database;
|
||||
final maps = await db.query(
|
||||
'time_goal',
|
||||
orderBy: 'goal_type, category',
|
||||
);
|
||||
|
||||
return maps.map((map) => TimeGoal.fromMap(map)).toList();
|
||||
}
|
||||
|
||||
// 获取指定类型的目标
|
||||
Future<TimeGoal?> getGoal(String goalType, {String? category}) async {
|
||||
final db = await _dbHelper.database;
|
||||
final maps = await db.query(
|
||||
'time_goal',
|
||||
where: 'goal_type = ? AND (category = ? OR category IS NULL) AND is_active = 1',
|
||||
whereArgs: [goalType, category],
|
||||
);
|
||||
|
||||
if (maps.isEmpty) return null;
|
||||
return TimeGoal.fromMap(maps.first);
|
||||
}
|
||||
|
||||
// 删除目标
|
||||
Future<void> deleteGoal(int id) async {
|
||||
final db = await _dbHelper.database;
|
||||
await db.delete(
|
||||
'time_goal',
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
|
||||
// 停用目标
|
||||
Future<void> deactivateGoal(int id) async {
|
||||
final db = await _dbHelper.database;
|
||||
await db.update(
|
||||
'time_goal',
|
||||
{'is_active': 0, 'updated_at': DateTime.now().millisecondsSinceEpoch ~/ 1000},
|
||||
where: 'id = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user