提交 9ec70060 authored 作者: yueweilu's avatar yueweilu

数据库操作添加日志

上级 3083522e
......@@ -84,7 +84,8 @@ class BookDetailController extends GetxController with GetSingleTickerProviderSt
void getBookDetails() async {
bookDetails = await LibraryAPI.details(bookId:bookId);
// 将阅读最后章节写入到数据库
SqlManager.updateReadHistoryByBookId(int.parse(bookId), bookDetails.chapterId!.toInt());
final result = await SqlManager.updateReadHistoryByBookId(int.parse(bookId), bookDetails.chapterId!.toInt());
Console.log('Sql-------存入数据库读到的章节----------------book_id:$bookId-----chapterId:${bookDetails.chapterId!.toInt()}---------result:$result--');
update();
}
/// 收藏 与 取消收藏
......
......@@ -246,7 +246,7 @@ class LibraryController extends GetxController with GetTickerProviderStateMixin{
void upload() async{
List<Map<String, dynamic>> data = await SqlManager.queryNoUploadData();
Console.log('查询到的数据----------------------------------------------------$data');
Console.log('Sql-----查询到的数据-----------------------$data');
if(data.isNotEmpty){
for (Map<String, dynamic> temp in data){
......
......@@ -103,10 +103,13 @@ class ReadController extends FullLifeCycleController with GetSingleTickerProvide
netStatus = await Tools.checkCurrentNetStatus();
final exist = await _isExistFile(bookId);
// chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
// Console.log('数据库中得到最后阅读的章节----------------------$chapterId----------');
if (!netStatus && exist){
// 1、从数据库中获取读到那个章节
chapterId = await SqlManager.queryReadHistoryByBookId(int.parse(bookId));
Console.log('Sql-----------数据库中得到最后阅读的章节----------------------$chapterId----------');
chapterName = getChapterName(chapterId);
// 2、通过 chapterId 获取 对应离线的 html路径
String toReadHtmlPath = await getLocalReadHtml(chapterId);
......@@ -607,24 +610,26 @@ class ReadController extends FullLifeCycleController with GetSingleTickerProvide
// 从缓存中获取 ZIP 文件
var file = await DefaultCacheManager().getSingleFile(url);
if (file != null) {
Toast.show('离线成功');
// 读取 ZIP 文件内容
Uint8List bytes = await file.readAsBytes();
// 解压缩 ZIP 文件
Archive archive = ZipDecoder().decodeBytes(bytes);
// 获取设备上的临时目录
Directory? tempDir = await getExternalStorageDirectory();
String tempPath = await Tools.getDirectory();
// 将解压缩后的文件保存到临时目录中
for (var file in archive) {
if (file.isFile) {
String fileName = file.name;
String filePath = '${tempDir!.path}/$bookId/$fileName';
String filePath = '$tempPath/$bookId/$fileName';
File(filePath)
..createSync(recursive: true)
..writeAsBytesSync(file.content as List<int>);
('解压缩文件:$fileName,保存路径:$filePath');
Console.log('解压缩文件:$fileName,保存路径:$filePath');
}
}
Toast.show('离线成功');
final exit = await _isExistFile(bookId);
update();
} else {
Console.log('未找到缓存中的文件或文件不存在');
......@@ -651,7 +656,7 @@ class ReadController extends FullLifeCycleController with GetSingleTickerProvide
}
Future<String> getLocalReadHtml(String chapterId) async{
String docPath = await _getDirectory();
String docPath = await Tools.getDirectory();
String filePath = '$docPath/$bookId';
Directory directory = Directory(filePath);
// 获取目录下的所有文件
......
......@@ -16,7 +16,7 @@ class SqlManager {
var databasesPath = await getDatabasesPath();
// var databasesPath = await Tools.getDirectory();
String dbName = _name;
Console.log('databasesPath---------$databasesPath');
Console.log('Sql-----------databasesPath---------$databasesPath');
if(databasesPath != null) {
String path = databasesPath + dbName;
if (Platform.isIOS) {
......@@ -72,29 +72,44 @@ class SqlManager {
where: 'upload = ?',
whereArgs: [0],
);
Console.log('更新数据-------------------------------$result');
Console.log('Sql---------------更新数据----------------$result');
}
// 根据 book_id 查询当前读到的 章节
static Future<String> queryReadHistoryByBookId(int bookId) async {
List<Map<String, dynamic>> results = await _database!.query(
'read_history',
where: 'book_id = ?',
whereArgs: [bookId],
);
return results.isNotEmpty ? results.first['chapter_id'] : 0;
return results.isNotEmpty ? results.first['chapter_id'].toString() : '';
}
// 根据 book_id 写入当前读到的 章节
static Future<int> updateReadHistoryByBookId(int bookId ,int chapterId) async {
final result = await _database!.update(
'members_book_notes',
{'chapter_id':chapterId},
where: 'book_id = ?',
whereArgs: [bookId],
);
return result;
final queryResult = await queryReadHistoryByBookId(bookId);
if (queryResult.isEmpty){
Console.log('Sql----------没有当前书籍的数据----------------------');
final result = await _database!.insert(
'read_history',
{'chapter_id':chapterId, 'book_id':bookId},
conflictAlgorithm: ConflictAlgorithm.replace,
);
Console.log('Sql----------插入数据结果:$result----------------------');
return result;
}
else{
Console.log('Sql----------有当前书籍的数据----------------------');
final result = await _database!.update(
'read_history',
{'chapter_id':chapterId},
where: 'book_id = ?',
whereArgs: [bookId],
);
Console.log('Sql----------更新数据结果:$result----------------------');
return result;
}
}
......
......@@ -38,7 +38,7 @@ abstract class Tools {
static Future<String> getDirectory() async {
// getTemporaryDirectory
final directory = await getApplicationSupportDirectory();
final directory = await getExternalStorageDirectory();
return directory!.path;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论