messageService.go 5.3 KB

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