messageService.go 5.3 KB

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