Hibok
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

248 řádky
7.8 KiB

  1. import 'dart:typed_data';
  2. import 'package:chat/data/UserData.dart';
  3. import 'package:chat/models/ChatMsg.dart';
  4. import 'package:chat/utils/sql_model.dart';
  5. class GroupChatTableModel {
  6. final String table = 'group_data';
  7. Sql sql;
  8. GroupChatTableModel() {
  9. sql = Sql.setTable(table);
  10. }
  11. createTable() async {
  12. print('创建表$table');
  13. await sql.createTable(table, '''rowId INTEGER PRIMARY KEY,
  14. userId INTEGER,
  15. transTag INTEGER,
  16. fromId INTEGER,
  17. friendId INTEGER,
  18. msgType INTEGER,
  19. msgContent BLOB,
  20. time INTEGER,
  21. sessionId INTEGER,
  22. readState INTEGER,
  23. extraInfo INTEGER,
  24. extraFile TEXT,
  25. translateContent BLOB,
  26. enTranslateContent BLOB,
  27. localFile TEXT,
  28. msgState INTEGER,
  29. soundListened INTEGER,
  30. refMsgContent BLOB,
  31. altUsers BLOB''');
  32. }
  33. Future clear() {
  34. return sql.clearTable(table);
  35. }
  36. Future clearSelfData() async {
  37. var curUserId = UserData().basicInfo.userId;
  38. var res =
  39. await sql.db.delete(table, where: 'userId=?', whereArgs: [curUserId]);
  40. print('删除结果$res');
  41. }
  42. //查询未读消息条数
  43. Future<Map<int, int>> getUnreadCount() async {
  44. var curUserId = UserData().basicInfo.userId;
  45. var lists = await sql.query(table,
  46. columns: ['count(sessionId) as unread'],
  47. where: 'userId = $curUserId and readState = 1',
  48. groupBy: 'sessionId');
  49. print('数据库未读数:${lists.length}');
  50. Map<int, int> result = {};
  51. for (var i = 0; i < lists.length; i++) {
  52. Map row = lists[i];
  53. int session = row['sessionId'];
  54. if (session != null) {
  55. int count = row['unread'];
  56. result[session] = count;
  57. }
  58. }
  59. return result;
  60. }
  61. //查询记录
  62. Future<Map<int, List<MsgModel>>> getAllRecord({int count = 100}) async {
  63. var curUserId = UserData().basicInfo.userId;
  64. print('数据库curUserId : $curUserId');
  65. var lists = await sql.query(table,
  66. where:
  67. 'userId = $curUserId and sessionId in (select DISTINCT sessionId from $table where userId = $curUserId)',
  68. orderBy: 'rowId desc');
  69. print('数据库记录数:${lists.length}');
  70. Map<int, List<MsgModel>> result = {};
  71. for (var i = 0; i < lists.length; i++) {
  72. int session = lists[i]['sessionId'];
  73. if (session != null) {
  74. List<MsgModel> msgList = result[session];
  75. if (msgList == null) {
  76. msgList = [];
  77. result[session] = msgList;
  78. }
  79. msgList.add(MsgModel.fromJson(lists[i], true));
  80. }
  81. }
  82. return result;
  83. }
  84. ///插入到数据库
  85. Future insert(MsgModel msgModel) async {
  86. print('插入消息到群聊数据库');
  87. if (msgModel.sessionId == null) {
  88. print('sessionId 为空.............................');
  89. return;
  90. }
  91. var curUserId = UserData().basicInfo.userId;
  92. await sql.insert({
  93. 'userId': curUserId,
  94. 'transTag': msgModel.transTag,
  95. 'fromId': msgModel.from,
  96. 'friendId': msgModel.friendId,
  97. 'msgType': msgModel.msgType,
  98. 'msgContent': Uint8List.fromList(msgModel.msgContent),
  99. 'time': msgModel.time,
  100. 'sessionId': msgModel.sessionId,
  101. 'readState': msgModel.readState ?? 0,
  102. 'extraFile': msgModel.extraFile,
  103. 'extraInfo': msgModel.extraInfo ?? 0,
  104. 'translateContent': msgModel.translateContent == null
  105. ? Uint8List(0)
  106. : Uint8List.fromList(msgModel.translateContent),
  107. 'enTranslateContent': msgModel.enTranslateContent == null
  108. ? Uint8List(0)
  109. : Uint8List.fromList(msgModel.enTranslateContent),
  110. 'localFile': msgModel.localFile,
  111. 'msgState': 0,
  112. 'soundListened': 0,
  113. 'refMsgContent': msgModel.refMsgContent == null
  114. ? Uint8List(0)
  115. : Uint8List.fromList(msgModel.refMsgContent),
  116. 'altUsers': msgModel.altUsers == null
  117. ? Uint8List(0)
  118. : Uint8List.fromList(msgModel.altUsers),
  119. });
  120. }
  121. //更新人工翻译内容
  122. updateUserTranslateContent(
  123. int session, int sendTime, List<int> transContent, int tranState) async {
  124. var curUserId = UserData().basicInfo.userId;
  125. if (transContent == null || transContent.length == 0) {
  126. return;
  127. }
  128. var trans = Uint8List.fromList(transContent);
  129. sql.db.update(table, {'translateContent': trans, 'transTag': tranState},
  130. where: 'userId=? and sessionId = ? and time = ?',
  131. whereArgs: [curUserId, session, sendTime]);
  132. }
  133. //更新机器翻译内容
  134. updateMachineTranslateContent(int session, int sendTime,
  135. List<int> transContent, List<int> enTransContent, int tranState) async {
  136. var curUserId = UserData().basicInfo.userId;
  137. var trans =
  138. transContent == null ? Uint8List(0) : Uint8List.fromList(transContent);
  139. var enTrans = enTransContent == null
  140. ? Uint8List(0)
  141. : Uint8List.fromList(enTransContent);
  142. sql.db.update(
  143. table,
  144. {
  145. 'translateContent': trans,
  146. 'enTranslateContent': enTrans,
  147. 'transTag': tranState
  148. },
  149. where: 'userId=? and sessionId = ? and time = ?',
  150. whereArgs: [curUserId, session, sendTime]);
  151. }
  152. //更新人工翻译评价
  153. updateUserTranslateState(int session, int sendTime, int tranState) async {
  154. var curUserId = UserData().basicInfo.userId;
  155. sql.db.update(table, {'transTag': tranState},
  156. where: 'userId=? and sessionId = ? and time = ?',
  157. whereArgs: [curUserId, session, sendTime]);
  158. }
  159. //更新消息发送状态
  160. updateMsgState(int session, int sendTime, int state) async {
  161. var curUserId = UserData().basicInfo.userId;
  162. sql.db.update(table, {'msgState': state},
  163. where: 'userId=? and sessionId = ? and time = ?',
  164. whereArgs: [curUserId, session, sendTime]);
  165. }
  166. //更新红包状态
  167. updateWalletState(MsgModel msgModel) async {
  168. var curUserId = UserData().basicInfo.userId;
  169. var content = Uint8List.fromList(msgModel.msgContent);
  170. var session = msgModel.sessionId;
  171. var msgTime = msgModel.time;
  172. sql.db.update(table, {'msgContent': content},
  173. where: 'userId=? and sessionId = ? and time = ?',
  174. whereArgs: [curUserId, session, msgTime]);
  175. }
  176. //更新数据是否已被读取
  177. updateReadState(int session) async {
  178. print('更新数据$session 已读状态 ');
  179. var curUserId = UserData().basicInfo.userId;
  180. sql.db.update(table, {'readState': 0},
  181. where: 'userId=? and sessionId = ? and readState = ?',
  182. whereArgs: [curUserId, session, 1]);
  183. }
  184. //更新文件本地路径
  185. updateLocalFile(String extraData, String local) async {
  186. var curUserId = UserData().basicInfo.userId;
  187. sql.db.update(table, {'localFile': local},
  188. where: 'userId=? and extraFile = ?', whereArgs: [curUserId, extraData]);
  189. }
  190. //语音是否听过
  191. updateSoundListened(MsgModel msg) {
  192. var curUserId = UserData().basicInfo.userId;
  193. sql.db.update(table, {'soundListened': 1},
  194. where: 'userId=? and sessionId = ? and time = ?',
  195. whereArgs: [curUserId, msg.sessionId, msg.time]);
  196. }
  197. deleteSigleRecordWith(int session, int timeatmp) async {
  198. var curUserId = UserData().basicInfo.userId;
  199. sql.db.delete(table,
  200. where: 'userId=? and sessionId = ? and time=?',
  201. whereArgs: [curUserId, session, timeatmp]);
  202. }
  203. //删除聊天数据
  204. delteRecord(int session) async {
  205. var curUserId = UserData().basicInfo.userId;
  206. print('删除数据 $session');
  207. sql.db.delete(table,
  208. where: 'userId=? and sessionId = ?', whereArgs: [curUserId, session]);
  209. }
  210. }