import 'package:sqflite/sqflite.dart'; import 'package:wukongimfluttersdk/entity/msg.dart'; import 'const.dart'; import 'wk_db_helper.dart'; class ReactionDB { ReactionDB._privateConstructor(); static final ReactionDB _instance = ReactionDB._privateConstructor(); static ReactionDB get shared => _instance; Future queryMaxSeqWithChannel(String channelID, int channelType) async { String sql = "select max(seq) seq from ${WKDBConst.tableMessageReaction} where channel_id=? and channel_type=? limit 0, 1"; int version = 0; List> list = await WKDBHelper.shared.getDB().rawQuery(sql, [channelID, channelType]); if (list.isNotEmpty) { dynamic data = list[0]; if (data != null) { version = WKDBConst.readInt(data, 'seq'); } } return version; } Future> queryWithMessageId(String messageId) async { List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableMessageReaction, where: "message_id=? and is_deleted=0", whereArgs: [messageId], orderBy: "created_at desc"); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeMsgReation(data)); } } return list; } Future> queryWithMessageIds( List messageIds) async { List list = []; List> results = await WKDBHelper.shared.getDB().query( WKDBConst.tableMessageReaction, where: "message_id in (${WKDBConst.getPlaceholders(messageIds.length)}) and is_deleted=0", whereArgs: messageIds, orderBy: "created_at desc"); if (results.isNotEmpty) { for (Map data in results) { list.add(WKDBConst.serializeMsgReation(data)); } } return list; } insertOrUpdateReactionList(List list) { if (list.isEmpty) return; for (int i = 0, size = list.length; i < size; i++) { insertOrUpdateReaction(list[i]); } } insertOrUpdateReaction(WKMsgReaction reaction) async { bool isExist = await isExistReaction(reaction.uid, reaction.messageID); if (isExist) { updateReaction(reaction); } else { insertReaction(reaction); } } updateReaction(WKMsgReaction reaction) { var map = {}; map['is_deleted'] = reaction.isDeleted; map['seq'] = reaction.seq; map['emoji'] = reaction.emoji; WKDBHelper.shared.getDB().update(WKDBConst.tableMessageReaction, map, where: "message_id=? and uid=?", whereArgs: [reaction.messageID, reaction.uid]); } insertReaction(WKMsgReaction reaction) { WKDBHelper.shared.getDB().insert( WKDBConst.tableMessageReaction, getReactionMap(reaction), conflictAlgorithm: ConflictAlgorithm.replace); } Future isExistReaction(String uid, String messageID) async { bool isExist = false; List> list = await WKDBHelper.shared.getDB().query( WKDBConst.tableMessageReaction, where: "message_id=? and uid=?", whereArgs: [messageID, uid]); if (list.isNotEmpty) { isExist = true; } return isExist; } dynamic getReactionMap(WKMsgReaction reaction) { var map = {}; map['channel_id'] = reaction.channelID; map['channel_id'] = reaction.channelID; map['channel_type'] = reaction.channelType; map['message_id'] = reaction.messageID; map['uid'] = reaction.uid; map['name'] = reaction.name; map['is_deleted'] = reaction.isDeleted; map['seq'] = reaction.seq; map['emoji'] = reaction.emoji; map['created_at'] = reaction.createdAt; } }