Hibok
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

246 Zeilen
7.6 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 ChatDataTableModel {
  6. final String table = 'record';
  7. Sql sql;
  8. ChatDataTableModel() {
  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. //查询未读消息条数
  37. Future<Map<int, int>> getUnreadCount() async {
  38. var curUserId = UserData().basicInfo.userId;
  39. var lists = await sql.query(table,
  40. columns: ['count(sessionId) as unread'],
  41. where: 'userId = $curUserId and readState = 1',
  42. groupBy: 'sessionId');
  43. print('数据库未读数:${lists.length}');
  44. Map<int, int> result = {};
  45. for (var i = 0; i < lists.length; i++) {
  46. Map row = lists[i];
  47. int session = row['sessionId'];
  48. if (session != null) {
  49. int count = row['unread'];
  50. result[session] = count;
  51. }
  52. }
  53. return result;
  54. }
  55. //查询记录
  56. Future<Map<int, List<MsgModel>>> getAllRecord({int count = 100}) async {
  57. var curUserId = UserData().basicInfo.userId;
  58. print('数据库curUserId : $curUserId');
  59. var lists = await sql.query(table,
  60. where:
  61. 'userId = $curUserId and sessionId in (select DISTINCT sessionId from $table where userId = $curUserId)',
  62. orderBy: 'rowId desc');
  63. print('数据库记录数:${lists.length}');
  64. Map<int, List<MsgModel>> result = {};
  65. for (var i = 0; i < lists.length; i++) {
  66. int session = lists[i]['sessionId'];
  67. if (session != null) {
  68. List<MsgModel> msgList = result[session];
  69. if (msgList == null) {
  70. msgList = [];
  71. result[session] = msgList;
  72. }
  73. msgList.add(MsgModel.fromJson(lists[i]));
  74. }
  75. }
  76. return result;
  77. }
  78. ///插入到数据库
  79. Future insert(MsgModel msgModel) async {
  80. print('插入私聊数据库');
  81. if (msgModel.sessionId == null) {
  82. print('sessionId 为空.............................');
  83. return;
  84. }
  85. var curUserId = UserData().basicInfo.userId;
  86. await sql.insert({
  87. 'userId': curUserId,
  88. 'transTag': msgModel.transTag,
  89. 'fromId': msgModel.from,
  90. 'friendId': msgModel.friendId,
  91. 'msgType': msgModel.msgType,
  92. 'msgContent': Uint8List.fromList(msgModel.msgContent),
  93. 'time': msgModel.time,
  94. 'sessionId': msgModel.sessionId,
  95. 'readState': msgModel.readState ?? 0,
  96. 'extraFile': msgModel.extraFile,
  97. 'extraInfo': msgModel.extraInfo ?? 0,
  98. 'translateContent': msgModel.translateContent == null
  99. ? Uint8List(0)
  100. : Uint8List.fromList(msgModel.translateContent),
  101. 'enTranslateContent': msgModel.enTranslateContent == null
  102. ? Uint8List(0)
  103. : Uint8List.fromList(msgModel.enTranslateContent),
  104. 'localFile': msgModel.localFile,
  105. 'msgState': 0,
  106. 'soundListened': 0,
  107. 'refMsgContent': msgModel.refMsgContent == null
  108. ? Uint8List(0)
  109. : Uint8List.fromList(msgModel.refMsgContent),
  110. 'altUsers': msgModel.altUsers == null
  111. ? Uint8List(0)
  112. : Uint8List.fromList(msgModel.altUsers),
  113. });
  114. }
  115. //更新人工翻译内容
  116. updateUserTranslateContent(
  117. int session, int sendTime, List<int> transContent, int tranState) async {
  118. var curUserId = UserData().basicInfo.userId;
  119. if (transContent == null || transContent.length == 0) {
  120. return;
  121. }
  122. var trans = Uint8List.fromList(transContent);
  123. sql.db.update(
  124. table,
  125. {
  126. 'translateContent': trans,
  127. 'enTranslateContent': Uint8List(0),
  128. 'transTag': tranState
  129. },
  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. }