bmService.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "fmt"
  5. "github.com/RoaringBitmap/roaring"
  6. "sync"
  7. )
  8. var rwMutex sync.RWMutex
  9. // 将Roaring Bitmap写入MySQL
  10. func writeBitmapToSendLog(rb *roaring.Bitmap, msgLogId int64) error {
  11. // 将Roaring Bitmap转换为字节数组
  12. bytes, err := rb.ToBytes()
  13. if err != nil {
  14. return err
  15. }
  16. ok := entity.Mysql.Update("message_send_log", map[string]interface{}{"id": msgLogId}, map[string]interface{}{"baseUserIds": bytes})
  17. if !ok {
  18. return fmt.Errorf("更新message_user_summary msg_log_id: %s 出错", msgLogId)
  19. }
  20. return nil
  21. }
  22. // 从MySQL中读取Roaring Bitmap
  23. func readBitmapFromSendLog(msgLogId int64) (*roaring.Bitmap, error) {
  24. // 从MySQL读取字节数组
  25. var bytes []byte
  26. err := entity.Mysql.DB.QueryRow("SELECT baseUserIds FROM message_send_log WHERE `id` = ?", msgLogId).Scan(&bytes)
  27. if err != nil {
  28. return nil, err
  29. }
  30. // 将字节数组转换为Roaring Bitmap
  31. rb := roaring.NewBitmap()
  32. _, err = rb.FromBuffer(bytes)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return rb, nil
  37. }
  38. // 更新用户消息汇总表
  39. func writeBitmapToUserSummary(rb *roaring.Bitmap, userId string) error {
  40. allMsg, err := rb.ToBytes()
  41. if err != nil {
  42. return err
  43. }
  44. ok := entity.Mysql.Update("message_user_summary", map[string]interface{}{"userId": userId}, map[string]interface{}{
  45. "allMsg": allMsg,
  46. })
  47. if !ok {
  48. return fmt.Errorf("更新message_user_summary USERID: %s 出错", userId)
  49. }
  50. return nil
  51. }
  52. // 更新用户消息汇总表 -已读未读
  53. func UpdateBitmapToUserUnread(rb2 *roaring.Bitmap, userId string) error {
  54. readMsg, err := rb2.ToBytes()
  55. if err != nil {
  56. return err
  57. }
  58. ok := entity.Mysql.Update("message_user_summary", map[string]interface{}{"userId": userId}, map[string]interface{}{
  59. "readMsg": readMsg,
  60. })
  61. if !ok {
  62. return fmt.Errorf("更新message_user_summary USERID: %s 出错", userId)
  63. }
  64. return nil
  65. }
  66. type MsgSummaryEntity struct {
  67. GroupId int
  68. Byte1 []byte
  69. }