|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
import 'package:path/path.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
import '../models/problem_model.dart';
|
|
|
|
|
|
|
|
class LocalDatabase {
|
|
|
|
static final LocalDatabase _instance = LocalDatabase._internal();
|
|
|
|
factory LocalDatabase() => _instance;
|
|
|
|
static Database? _database;
|
|
|
|
static const String _tableName = 'problems';
|
|
|
|
|
|
|
|
LocalDatabase._internal();
|
|
|
|
|
|
|
|
Future<Database> get database async {
|
|
|
|
if (_database != null) return _database!;
|
|
|
|
_database = await _initDatabase();
|
|
|
|
return _database!;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Database> _initDatabase() async {
|
|
|
|
String path = join(await getDatabasesPath(), 'problems.db');
|
|
|
|
return await openDatabase(path, version: 1, onCreate: _onCreate);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onCreate(Database db, int version) async {
|
|
|
|
await db.execute('''
|
|
|
|
CREATE TABLE $_tableName(
|
|
|
|
id TEXT PRIMARY KEY,
|
|
|
|
description TEXT NOT NULL,
|
|
|
|
location TEXT NOT NULL,
|
|
|
|
imageUrls TEXT NOT NULL,
|
|
|
|
createdAt INTEGER NOT NULL,
|
|
|
|
isUploaded INTEGER NOT NULL,
|
|
|
|
censorTaskId TEXT,
|
|
|
|
bindData TEXT
|
|
|
|
)
|
|
|
|
''');
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> insertProblem(Problem problem) async {
|
|
|
|
final db = await database;
|
|
|
|
problem.id = const Uuid().v4();
|
|
|
|
return await db.insert(_tableName, problem.toMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> updateProblem(Problem problem) async {
|
|
|
|
final db = await database;
|
|
|
|
return await db.update(
|
|
|
|
_tableName,
|
|
|
|
problem.toMap(),
|
|
|
|
where: 'id = ?',
|
|
|
|
whereArgs: [problem.id],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int> deleteProblem(String id) async {
|
|
|
|
final db = await database;
|
|
|
|
return await db.delete(_tableName, where: 'id = ?', whereArgs: [id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 通用查询方法,根据时间范围、上传状态和绑定状态(censorTaskId)筛选问题。
|
|
|
|
Future<List<Problem>> getProblems({
|
|
|
|
required DateTime startDate,
|
|
|
|
required DateTime endDate,
|
|
|
|
required String uploadStatus, // '已上传', '未上传', '全部'
|
|
|
|
required String bindStatus, // '已绑定', '未绑定', '全部'
|
|
|
|
}) async {
|
|
|
|
final db = await database;
|
|
|
|
final int startTimestamp = startDate.millisecondsSinceEpoch;
|
|
|
|
final int endTimestamp = endDate.millisecondsSinceEpoch;
|
|
|
|
|
|
|
|
final List<String> whereClauses = ['createdAt >= ?', 'createdAt <= ?'];
|
|
|
|
final List<dynamic> whereArgs = [startTimestamp, endTimestamp];
|
|
|
|
|
|
|
|
// 根据上传状态添加筛选条件
|
|
|
|
if (uploadStatus == '已上传') {
|
|
|
|
whereClauses.add('isUploaded = ?');
|
|
|
|
whereArgs.add(1);
|
|
|
|
} else if (uploadStatus == '未上传') {
|
|
|
|
whereClauses.add('isUploaded = ?');
|
|
|
|
whereArgs.add(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 根据 censorTaskId 的值判断绑定状态
|
|
|
|
if (bindStatus == '已绑定') {
|
|
|
|
whereClauses.add('censorTaskId IS NOT NULL');
|
|
|
|
} else if (bindStatus == '未绑定') {
|
|
|
|
whereClauses.add('censorTaskId IS NULL');
|
|
|
|
}
|
|
|
|
|
|
|
|
final String whereString = whereClauses.join(' AND ');
|
|
|
|
|
|
|
|
final List<Map<String, dynamic>> maps = await db.query(
|
|
|
|
_tableName,
|
|
|
|
where: whereString.isEmpty ? null : whereString,
|
|
|
|
whereArgs: whereArgs.isEmpty ? null : whereArgs,
|
|
|
|
orderBy: 'createdAt DESC',
|
|
|
|
);
|
|
|
|
|
|
|
|
return List.generate(maps.length, (i) {
|
|
|
|
return Problem.fromMap(maps[i]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|