import '../database_helper.dart'; /// EPUB 阅读器数据访问层 class ReaderDao { final DatabaseHelper _db = DatabaseHelper.instance; // ─── reader_books ───────────────────────────────────────────────── /// 获取所有未删除的阅读记录(包含关联书籍封面) Future>> getAllReaderBooks() async { final db = await _db.database; final results = await db.rawQuery(''' SELECT rb.*, COALESCE(b.cover_path, rb.cover_path) as display_cover_path FROM reader_books rb LEFT JOIN books b ON rb.book_id = b.id AND b.is_deleted = 0 WHERE rb.is_deleted = 0 ORDER BY rb.updated_at DESC '''); return results.map((row) { final map = Map.from(row); // 如果有关联封面,覆盖 cover_path 供 UI 使用 final displayCover = map['display_cover_path'] as String?; if (displayCover != null && displayCover.isNotEmpty) { map['cover_path'] = displayCover; } map.remove('display_cover_path'); return map; }).toList(); } /// 根据 ID 获取阅读记录 Future?> getReaderBookById(String id) async { final db = await _db.database; final results = await db.query( 'reader_books', where: 'id = ?', whereArgs: [id], limit: 1, ); return results.isNotEmpty ? results.first : null; } /// 插入阅读记录 Future insertReaderBook(Map book) async { final db = await _db.database; return db.insert('reader_books', book); } /// 更新阅读记录字段 Future updateReaderBook(String id, Map fields) async { final db = await _db.database; return db.update( 'reader_books', fields, where: 'id = ?', whereArgs: [id], ); } /// 更新阅读进度 Future updateReadingProgress( String id, String cfi, double percentage) async { final db = await _db.database; return db.update( 'reader_books', { 'last_read_cfi': cfi, 'reading_percentage': percentage, 'updated_at': DateTime.now().toIso8601String(), }, where: 'id = ?', whereArgs: [id], ); } /// 软删除 Future deleteReaderBook(String id) async { final db = await _db.database; return db.update( 'reader_books', {'is_deleted': 1, 'updated_at': DateTime.now().toIso8601String()}, where: 'id = ?', whereArgs: [id], ); } // ─── 关联 books 表 ───────────────────────────────────────────── /// 关联 EPUB 到 books 表中的书籍 Future linkToBook(String readerBookId, String bookId) async { final db = await _db.database; return db.update( 'reader_books', {'book_id': bookId, 'updated_at': DateTime.now().toIso8601String()}, where: 'id = ?', whereArgs: [readerBookId], ); } /// 取消关联 Future unlinkBook(String readerBookId) async { final db = await _db.database; // 同步删除该书的摘抄蓝色高亮标注 await db.delete( 'book_annotations', where: 'book_id = ? AND color = ?', whereArgs: [readerBookId, 'excerpt'], ); return db.update( 'reader_books', {'book_id': '', 'updated_at': DateTime.now().toIso8601String()}, where: 'id = ?', whereArgs: [readerBookId], ); } /// 通过 books.id 查找关联的 reader_book Future?> getReaderBookByBookId(String bookId) async { final db = await _db.database; final results = await db.query( 'reader_books', where: 'book_id = ? AND is_deleted = 0', whereArgs: [bookId], limit: 1, ); return results.isNotEmpty ? results.first : null; } // ─── book_annotations ───────────────────────────────────────────── /// 获取某本书的所有批注 Future>> getAnnotationsByBookId( String bookId) async { final db = await _db.database; return db.query( 'book_annotations', where: 'book_id = ?', whereArgs: [bookId], orderBy: 'created_at DESC', ); } /// 插入批注 Future insertAnnotation(Map annotation) async { final db = await _db.database; return db.insert('book_annotations', annotation); } /// 删除批注 Future deleteAnnotation(int id) async { final db = await _db.database; return db.delete('book_annotations', where: 'id = ?', whereArgs: [id]); } /// 更新批注感悟 Future updateAnnotationNote(int id, String note) async { final db = await _db.database; return db.update( 'book_annotations', {'reader_note': note, 'updated_at': DateTime.now().toIso8601String()}, where: 'id = ?', whereArgs: [id], ); } // ─── highlights / annotations (epub) ────────────────────── /// 保存 EPUB 高亮批注 Future saveHighlight(Map annotation) async { final db = await _db.database; return db.insert('book_annotations', { ...annotation, 'type': 'highlight', }); } /// 获取某 book_id(books.id)的所有 EPUB 高亮 Future>> getHighlightsByBookId( String bookId) async { final db = await _db.database; return db.query( 'book_annotations', where: 'book_id = ? AND type = ?', whereArgs: [bookId, 'highlight'], orderBy: 'created_at DESC', ); } /// 删除高亮 Future deleteHighlight(int id) async { final db = await _db.database; return db.delete( 'book_annotations', where: 'id = ? AND type = ?', whereArgs: [id, 'highlight'], ); } /// 删除摘抄对应的蓝色高亮标注(通过内容匹配) Future deleteExcerptHighlightByContent(String readerBookId, String content) async { final db = await _db.database; await db.delete( 'book_annotations', where: 'book_id = ? AND content = ? AND color = ?', whereArgs: [readerBookId, content, 'excerpt'], ); } // ─── bookmarks ────────────────────────────────────────────────── /// 获取某本书的所有书签 Future>> getBookmarksByBookId(String bookId) async { final db = await _db.database; return db.query( 'book_annotations', where: 'book_id = ? AND type = ?', whereArgs: [bookId, 'bookmark'], orderBy: 'created_at DESC', ); } /// 插入书签 Future insertBookmark(Map bookmark) async { final db = await _db.database; return db.insert('book_annotations', { ...bookmark, 'type': 'bookmark', }); } /// 删除书签 Future deleteBookmark(int id) async { final db = await _db.database; return db.delete('book_annotations', where: 'id = ?', whereArgs: [id]); } }