messageService.go 6.0 KB

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