messageService.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  5. "app.yhyue.com/moapp/MessageCenter/util"
  6. qutil "app.yhyue.com/moapp/jybase/common"
  7. "app.yhyue.com/moapp/jybase/redis"
  8. "context"
  9. "errors"
  10. "fmt"
  11. "log"
  12. "strings"
  13. "time"
  14. )
  15. type MessageService struct{}
  16. // 修改消息阅读状态
  17. func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) error {
  18. row := entity.ClickhouseConn.QueryRow(context.Background(), fmt.Sprintf("SELECT count(*) from message_user_summary WHERE userId = '%s' ANd bitmapContains(readMsg,%d)", data.UserId, data.Id))
  19. var count uint64
  20. row.Scan(&count)
  21. if count > 0 {
  22. return nil
  23. }
  24. msg := entity.Mysql.FindOne("message_send_log", map[string]interface{}{"id": data.Id}, "group_id", "")
  25. if msg != nil && len(*msg) > 0 {
  26. groupId := qutil.IntAll((*msg)["group_id"])
  27. //更新用户未读消息bitmap
  28. sql := fmt.Sprintf(`alter table message_user_summary UPDATE readMsg = bitmapOr(readMsg,bitmapBuild([toUInt64(%d)])) where userId = '%s'`, data.Id, data.UserId)
  29. fmt.Println(sql)
  30. err1 := entity.ClickhouseConn.Exec(context.Background(), sql)
  31. if err1 != nil {
  32. return err1
  33. }
  34. //清缓存
  35. keyString := fmt.Sprintf(MsgCountKey, data.UserId, groupId)
  36. if redis.GetInt(redisModule, keyString) > 0 {
  37. redis.Decrby(redisModule, keyString, 1)
  38. }
  39. } else {
  40. return errors.New(fmt.Sprintf("消息不存在:%d", data.Id))
  41. }
  42. return nil
  43. }
  44. // 未读消息合计 isRedis 是否需要初始化redis
  45. func (service *MessageService) CountUnread(userId string, isRedis bool) (map[string]int64, int64) {
  46. var (
  47. count int64
  48. msgTypes, groupIds []string
  49. )
  50. data := make(map[string]int64)
  51. for _, v := range entity.MessageColumn {
  52. if util.IntAll(v["group_id"]) > 0 && util.IntAll(v["group_id"]) < 999 {
  53. //去除全部与私信
  54. msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
  55. key := fmt.Sprintf(MsgCountKey, userId, util.IntAll(v["group_id"]))
  56. groupIds = append(groupIds, qutil.InterfaceToStr(v["group_id"]))
  57. if exists, _ := redis.Exists(redisModule, key); exists {
  58. ct := util.Int64All(redis.GetInt(redisModule, key))
  59. data[qutil.InterfaceToStr(v["group_id"])] = ct
  60. count += ct
  61. }
  62. }
  63. }
  64. if len(msgTypes) > 0 && len(msgTypes) != len(data) {
  65. count = 0
  66. query := entity.Mysql.SelectBySql(fmt.Sprintf("SELECT group_id,COUNT(CASE WHEN isRead=0 THEN 1 END) as count FROM message where receive_userid=? and isdel=1 and group_id IS NOT NULL GROUP BY group_id ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), userId)
  67. if query != nil && len(*query) > 0 {
  68. for _, v := range *query {
  69. unread := qutil.Int64All(v["count"])
  70. data[qutil.InterfaceToStr(v["group_id"])] = unread
  71. count += unread
  72. }
  73. }
  74. if isRedis { //初始化未读数
  75. for _, v1 := range groupIds {
  76. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v1))
  77. redis.Put(redisModule, key, data[v1], -1)
  78. }
  79. }
  80. }
  81. return data, count
  82. }
  83. func (service *MessageService) CountClassUnread(userId string, groupId int64) (classCount map[string]int64, total int64) {
  84. var (
  85. count int64
  86. )
  87. data := make(map[string]int64)
  88. if _, ok := entity.ClassSearchMap[groupId]; !ok {
  89. return
  90. }
  91. for i := 0; i < len(entity.ClassSearchMap[groupId]); i++ {
  92. msgClass := entity.ClassSearchMap[groupId][i]
  93. key := fmt.Sprintf(MsgClassCountKey, userId, msgClass.MsgType)
  94. if exists, _ := redis.Exists(redisModule, key); exists {
  95. ct := util.Int64All(redis.GetInt(redisModule, key))
  96. data[fmt.Sprintf("%d", msgClass.MsgType)] = ct
  97. count += ct
  98. } else {
  99. q := "select count(*) from message where receive_userid=? and isdel=1 and msg_type=?"
  100. classCount := entity.Mysql.CountBySql(q, userId, msgClass.MsgType)
  101. if classCount != -1 {
  102. redis.Put(redisModule, key, classCount, -1)
  103. data[fmt.Sprintf("%d", msgClass.MsgType)] = classCount
  104. count += classCount
  105. } else {
  106. log.Println("查询classCount失败:", classCount, q)
  107. }
  108. }
  109. }
  110. return data, count
  111. }
  112. // 查询消息详情
  113. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  114. //直接查询message_send_log
  115. msg = entity.Mysql.FindOne("message_send_log", map[string]interface{}{"id": msgLogId}, "", "")
  116. if msg != nil && len(*msg) > 0 {
  117. return msg, nil
  118. }
  119. return nil, errors.New("没有查询到消息")
  120. }
  121. // GetMsgType 消息的分类
  122. /*func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  123. types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
  124. if types != nil && len(*types) > 0 {
  125. for _, val := range *types {
  126. data = append(data, &message.MsgTypes{
  127. MsgType: qutil.Int64All(val["group_id"]),
  128. Name: qutil.ObjToString(val["name"]),
  129. Img: qutil.ObjToString(val["img"]),
  130. Code: qutil.ObjToString(val["switch"]),
  131. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  132. })
  133. }
  134. return data, nil
  135. }
  136. return nil, nil
  137. }*/
  138. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) error {
  139. //判断用户是否已经在pc端打开过
  140. sql := fmt.Sprintf("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = %d and platform = %d and userid = '%s'", msgLogId, platFrom, userId)
  141. row := entity.ClickhouseConn.QueryRow(context.Background(), sql)
  142. var count uint64
  143. row.Scan(&count)
  144. if count <= 0 {
  145. tmp := map[string]interface{}{
  146. "msg_log_id": msgLogId,
  147. "platform": platFrom,
  148. "userid": userId,
  149. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  150. }
  151. SaveCache <- tmp
  152. }
  153. return nil
  154. }