messageService.go 5.3 KB

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