my_msg_db.dart 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import 'package:bbyyy/beans/message_bean_entity.dart';
  2. import 'package:bbyyy/msgDB/sql_manager.dart';
  3. import 'package:bbyyy/my_tools/event_bus.dart';
  4. import 'package:bbyyy/my_tools/my_cookie.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. class MsgDB extends BaseDbProvider {
  7. String name;
  8. final String uuid = 'uuid';
  9. final String sentAt = 'sent_at';
  10. final String receiverUid = 'receiver_uid';
  11. final String receiverName = 'receiver_name';
  12. final String receiverPic = 'receiver_pic';
  13. final String senderUid = 'sender_uid';
  14. final String senderName = 'sender_name';
  15. final String senderPic = 'sender_pic';
  16. final String type = 'type';
  17. final String content = 'content';
  18. MsgDB(this.name);
  19. @override
  20. createTableString() {
  21. return '''CREATE TABLE $name
  22. ($uuid TEXT,
  23. $sentAt TEXT,
  24. $receiverUid INT,
  25. $receiverName TEXT,
  26. $receiverPic TEXT,
  27. $senderUid INT,
  28. $senderName TEXT,
  29. $senderPic TEXT,
  30. $type TEXT,
  31. $content TEXT)''';
  32. }
  33. @override
  34. tableName() {
  35. return name;
  36. }
  37. //添加表数据
  38. addTableData(List<MessageBeanContent> data) async {
  39. Database db = await getDataBase();
  40. Batch batch = db.batch();
  41. String roomName = this.name.replaceAll('table', '').replaceAll('_', '-');
  42. String lastTime = MyCookie().prefs.getString('最后阅读时间$roomName');
  43. data.forEach((element) {
  44. DateTime d1 = DateTime.parse(element.sentAt);
  45. DateTime d2;
  46. try {
  47. d2 = DateTime.parse(lastTime);
  48. } catch (e) {}
  49. if (d2 == null) {
  50. int noRead = MyCookie().prefs.getInt('未读消息$roomName');
  51. if (noRead == null) {
  52. noRead = 0;
  53. }
  54. noRead++;
  55. MyCookie().prefs.setInt('未读消息$roomName', noRead);
  56. } else {
  57. if (d1.isAfter(d2)) {
  58. int noRead = MyCookie().prefs.getInt('未读消息$roomName');
  59. noRead++;
  60. MyCookie().prefs.setInt('未读消息$roomName', noRead);
  61. } else {}
  62. }
  63. batch.insert(name, element.toJson());
  64. });
  65. print(await batch.commit());
  66. }
  67. //删除表数据
  68. deleteTableData() async {
  69. Database db = await getDataBase();
  70. int count = await db.rawDelete('DELETE FROM $name');
  71. EventBus().emit('delDBChatMsg$name');
  72. return count == 1;
  73. }
  74. //撤回消息删除数据
  75. withdrawMessage(String uuID) async {
  76. Database db = await getDataBase();
  77. int count = await db.rawDelete('DELETE FROM $name WHERE $uuid = \'$uuID\'');
  78. EventBus().emit('withdrawMessage');
  79. return count == 1;
  80. }
  81. //更新表数据
  82. updateTableData(String c, String ID) async {
  83. Database db = await getDataBase();
  84. int count = await db
  85. .rawUpdate('UPDATE $name SET $content = \'$c\' WHERE $uuid = \'$ID\'');
  86. EventBus().emit('updateTableData');
  87. return count == 1;
  88. }
  89. //查询表数据
  90. queryTableData(int page, int count) async {
  91. Database db = await getDataBase();
  92. List<Map<String, dynamic>> maps = await db.rawQuery(
  93. "select distinct ${this.uuid},"
  94. "${this.sentAt},"
  95. "${this.receiverUid},"
  96. "${this.receiverName},"
  97. "${this.receiverPic},"
  98. "${this.senderUid},"
  99. "${this.senderName},"
  100. "${this.senderPic},"
  101. "${this.type},"
  102. "${this.content}"
  103. " from $name ORDER BY ${this.sentAt} DESC LIMIT ${count * (page - 1)},$count");
  104. return maps;
  105. }
  106. //删除保存记录前的聊天数据
  107. delPassMsg() {}
  108. }