insertMsgList method
Implementation
insertMsgList(List<WKConversationMsg> list) async {
if (WKDBHelper.shared.getDB() == null) {
return;
}
List<String> channelIds = [];
for (var i = 0; i < list.length; i++) {
if (list[i].channelID != '') {
channelIds.add(list[i].channelID);
}
}
List<WKConversationMsg> existList = await queryWithChannelIds(channelIds);
List<Map<String, dynamic>> insertList = [];
List<Map<String, dynamic>> updateList = [];
for (WKConversationMsg msg in list) {
bool isAdd = true;
if (existList.isNotEmpty) {
for (var i = 0; i < existList.length; i++) {
if (existList[i].channelID == msg.channelID &&
existList[i].channelType == msg.channelType) {
updateList.add(getMap(msg, true));
isAdd = false;
break;
}
}
}
if (isAdd) {
insertList.add(getMap(msg, true));
}
}
if (insertList.isNotEmpty || updateList.isNotEmpty) {
WKDBHelper.shared.getDB()!.transaction((txn) async {
if (insertList.isNotEmpty) {
for (int i = 0; i < insertList.length; i++) {
txn.insert(WKDBConst.tableConversation, insertList[i],
conflictAlgorithm: ConflictAlgorithm.replace);
}
}
if (updateList.isNotEmpty) {
for (Map<String, dynamic> value in updateList) {
txn.update(WKDBConst.tableConversation, value,
where: "channel_id=? and channel_type=?",
whereArgs: [value['channel_id'], value['channel_type']]);
}
}
});
}
}