|
|
|
@ -11,15 +11,15 @@ class HxDatabase {
|
|
|
|
|
Database db; |
|
|
|
|
|
|
|
|
|
void open({String key}) async { |
|
|
|
|
// _migrations.add(Migration(1, 2, (Database database) async { |
|
|
|
|
// database.execute('ALTER TABLE `Message` ADD COLUMN `replyId` VARCHAR(20) DEFAULT NULL AFTER `toId`'); |
|
|
|
|
// _migrations.add(Migration(3, 4, (Database database) async { |
|
|
|
|
// await database.execute('ALTER TABLE ImUser ADD COLUMN IF NOT EXISTS `isTop` INTEGER DEFAULT 0'); |
|
|
|
|
// })); |
|
|
|
|
|
|
|
|
|
String databaseName = 'hx.db'; |
|
|
|
|
if (key?.isNotEmpty ?? false) { |
|
|
|
|
databaseName = 'hx_$key.db'; |
|
|
|
|
} |
|
|
|
|
await openDatabase(databaseName, version: 2, |
|
|
|
|
await openDatabase(databaseName, version: 1, |
|
|
|
|
onCreate: (Database db, int version) async { |
|
|
|
|
db.execute( |
|
|
|
|
'CREATE TABLE IF NOT EXISTS `Message` (`id` INTEGER, `conversationId` VARCHAR(40), `fromId` VARCHAR(20), `toId` VARCHAR(20), `replyId` VARCHAR(20), `content` TEXT, `attach` TEXT, `msgType` INTEGER, `time` VARCHAR(20), `state` INTEGER, `isDelete` INTEGER, PRIMARY KEY (`id`))'); |
|
|
|
@ -27,10 +27,7 @@ class HxDatabase {
|
|
|
|
|
'CREATE TABLE IF NOT EXISTS `ImUser` (`id` INTEGER, `mid` VARCHAR(20), `nickname` VARCHAR(20), `avatar` VARCHAR(200), `phone` VARCHAR(200), `isDelete` INTEGER, `isTop` INTEGER, PRIMARY KEY (`id`))'); |
|
|
|
|
}, onConfigure: (database) async { |
|
|
|
|
await database.execute('PRAGMA foreign_keys = ON'); |
|
|
|
|
try { |
|
|
|
|
await database.execute( |
|
|
|
|
'ALTER TABLE ImUser ADD COLUMN isTop INTEGER DEFAULT 0'); |
|
|
|
|
}catch (e){} |
|
|
|
|
debugPrint("database-version: ${await database.getVersion()}"); |
|
|
|
|
}, onUpgrade: (database, startVersion, endVersion) async { |
|
|
|
|
await runMigrations(database, startVersion, endVersion, _migrations); |
|
|
|
|
}, onOpen: (Database db) { |
|
|
|
@ -51,10 +48,8 @@ class HxDatabase {
|
|
|
|
|
|
|
|
|
|
Future<Message> lastMessage(String conversationId) async { |
|
|
|
|
await _dbIsOpen(); |
|
|
|
|
String sql = |
|
|
|
|
'SELECT * FROM Message WHERE conversationId = ? ORDER BY time DESC LIMIT 1'; |
|
|
|
|
List<Message> messages = |
|
|
|
|
await db.rawQuery(sql, [conversationId]).then((value) { |
|
|
|
|
String sql = 'SELECT * FROM Message WHERE conversationId = ? ORDER BY time DESC LIMIT 1'; |
|
|
|
|
List<Message> messages = await db.rawQuery(sql, [conversationId]).then((value) { |
|
|
|
|
return value.map((e) { |
|
|
|
|
debugPrint("Message: $e"); |
|
|
|
|
return Message.fromJson(e); |
|
|
|
@ -89,7 +84,7 @@ class HxDatabase {
|
|
|
|
|
return Message.fromJson(e); |
|
|
|
|
}).toList(); |
|
|
|
|
}, onError: (error) { |
|
|
|
|
debugPrint("MessageError: $error"); |
|
|
|
|
debugPrint("Message-error: $error"); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -100,7 +95,7 @@ class HxDatabase {
|
|
|
|
|
return db.rawQuery(sql, [conversationId, start, pageSize]).then((value) { |
|
|
|
|
return value.map((e) => Message.fromJson(e)).toList(); |
|
|
|
|
}, onError: (error) { |
|
|
|
|
debugPrint("Messageerror: $error"); |
|
|
|
|
debugPrint("Message-error: $error"); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -110,7 +105,7 @@ class HxDatabase {
|
|
|
|
|
return db.rawQuery(sql, [conversationId]).then((value) { |
|
|
|
|
return value.map((e) => Message.fromJson(e)).toList(); |
|
|
|
|
}, onError: (error) { |
|
|
|
|
debugPrint("Messageerror: $error"); |
|
|
|
|
debugPrint("Message-error: $error"); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -118,14 +113,18 @@ class HxDatabase {
|
|
|
|
|
await _dbIsOpen(); |
|
|
|
|
String userStr = conversationIds.join(","); |
|
|
|
|
debugPrint("userStr: $userStr"); |
|
|
|
|
List<Message> messages = await db.rawQuery( |
|
|
|
|
"SELECT * FROM Message WHERE `conversationId` IN ($userStr) AND state = 0 AND isDelete = 0", |
|
|
|
|
List<Message> messages = await db.query("Message", |
|
|
|
|
where: "`conversationId` IN (?) AND state = 0 AND isDelete = 0", |
|
|
|
|
whereArgs: [userStr], |
|
|
|
|
).then((value) { |
|
|
|
|
return value.map((e) => Message.fromJson(e)).toList(); |
|
|
|
|
}, onError: (error) { |
|
|
|
|
debugPrint("Message-error: $error"); |
|
|
|
|
}); |
|
|
|
|
return (messages??[]).lGroupBy((p) => p.fromId).mGroupCount; |
|
|
|
|
messages.forEach((element) { |
|
|
|
|
debugPrint("messageUnreadCount: ${element.toJson()}"); |
|
|
|
|
}); |
|
|
|
|
return (messages??[]).lGroupBy((p) => p.conversationId).mGroupCount; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Future<List<Map>> queryListAll() async{ |
|
|
|
|