messageService.go 5.8 KB

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