getBuoyMsg.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. "encoding/json"
  10. "fmt"
  11. "log"
  12. "strconv"
  13. "strings"
  14. )
  15. func FindUserBuoyMsg(this *message.FindUserBuoyMsgReq) *message.FindUserBuoyMsgRes {
  16. var err error
  17. data := message.FindUserBuoyMsgRes{}
  18. res := entity.Mysql.SelectBySql(fmt.Sprintf(`SELECT * FROM message WHERE receive_userid = "%s" and isdel = 1 and appid = %s and isRead =0 and show_buoy = 1 and show_content != "" ORDER BY createtime DESC LIMIT 0,%d`, this.UserId, this.Appid, int(this.PageSize)))
  19. //log.Println("数据:", res)
  20. if res != nil && len(*res) > 0 {
  21. for _, v := range *res {
  22. _id := util.Int64All(v["id"])
  23. id := strconv.FormatInt(_id, 10)
  24. links2 := util.ObjToString((v["link"]))
  25. link2, androidUrl2, iosUrl2, weChatUrl2 := LinkSplit(links2)
  26. data.Data = append(data.Data, &message.BuoyMessages{
  27. Id: id,
  28. BuoyDetail: util.ObjToString(v["show_content"]),
  29. PcUrl: link2,
  30. AndroidUrl: androidUrl2,
  31. IosUrl: iosUrl2,
  32. WeChatUrl: weChatUrl2,
  33. })
  34. }
  35. }
  36. if err == nil {
  37. data.Code = 0
  38. data.Message = "查询成功"
  39. } else {
  40. data.Code = 1
  41. data.Message = "查询失败"
  42. }
  43. return &data
  44. }
  45. func LinkSplit(url string) (link, androidUrl, iosUrl, weChatUrl string) {
  46. if url != "" {
  47. arr := strings.Split(url, ",")
  48. if len(arr) == 4 {
  49. link = arr[0]
  50. androidUrl = arr[1]
  51. iosUrl = arr[2]
  52. weChatUrl = arr[3]
  53. } else {
  54. if len(arr) > 0 {
  55. link = arr[0]
  56. } else {
  57. link = ""
  58. }
  59. androidUrl = ""
  60. iosUrl = ""
  61. weChatUrl = ""
  62. }
  63. }
  64. return link, androidUrl, iosUrl, weChatUrl
  65. }
  66. func ClearUnreadMsg(in *message.ClearUnreadMsgReq) error {
  67. if in.Userid != "" {
  68. err := PutRedisRead(in.Userid, 0)
  69. if err != nil {
  70. return err
  71. }
  72. //更新用户未读消息bitmap
  73. sql := fmt.Sprintf(`alter table message_user_summary UPDATE readMsg = bitmapOr(allMsg,readMsg) where userId = '%s'`, in.Userid)
  74. err1 := entity.ClickhouseConn.Exec(context.Background(), sql)
  75. if err1 != nil {
  76. return err1
  77. }
  78. //清缓存
  79. for _, v := range entity.MessageColumn {
  80. keyString := fmt.Sprintf(MsgCountKey, in.Userid, qutil.Int64All(v["group_id"]))
  81. redis.Put(redisModule, keyString, 0, -1)
  82. }
  83. // p436 清空消息时 "msg_class_count_%s_%d" 下的消息数量也清空
  84. for _, classes := range entity.ClassSearchMap {
  85. for i := 0; i < len(classes); i++ {
  86. classKeyString := fmt.Sprintf(MsgClassCountKey, in.Userid, classes[i].MsgType)
  87. redis.Put(redisModule, classKeyString, 0, -1)
  88. }
  89. }
  90. redis.Del(redisModule, fmt.Sprintf(UserWorkDeskKey, in.Userid))
  91. redis.Del(redisModule, fmt.Sprintf(UserMsgSummery, in.Userid))
  92. redis.Del(redisModule, fmt.Sprintf(UserClassMapKey, in.Userid))
  93. }
  94. //更新私信未读数
  95. if in.PositionId > 0 {
  96. sQuery := map[string]interface{}{
  97. "my_position_id": in.PositionId,
  98. //"ent_id": in.EntId,
  99. }
  100. if !entity.BaseMysql.Update("socialize_summary", sQuery, map[string]interface{}{"unread": 0}) {
  101. log.Println("更新用户聊天未读数失败")
  102. }
  103. }
  104. if in.NewUserId > 0 {
  105. sQuery := map[string]interface{}{
  106. "user_id": in.NewUserId,
  107. //"ent_id": in.EntId,
  108. }
  109. if !entity.BaseMysql.Update("socialize_summary", sQuery, map[string]interface{}{"unread": 0}) {
  110. log.Println("更新客服私信未读数失败")
  111. }
  112. }
  113. return nil
  114. }
  115. func PutRedisRead(userId string, msgId int) error {
  116. //查询用户在清除未读消息之前的未读消息存如缓存
  117. redisData, err := redis.GetNewBytes(redisModule, fmt.Sprintf(UserClearUnread, userId))
  118. if err != nil {
  119. return err
  120. }
  121. redisArr := []uint32{}
  122. if redisData != nil && len(*redisData) > 0 {
  123. err := json.Unmarshal(*redisData, &redisArr)
  124. if err != nil {
  125. return err
  126. }
  127. }
  128. userUnreadMsg := []uint32{}
  129. if msgId == 0 {
  130. _, _, userUnreadMsg, err = GetUserMsgSummary(userId, false)
  131. if err != nil {
  132. return err
  133. }
  134. } else {
  135. userUnreadMsg = append(userUnreadMsg, uint32(msgId))
  136. }
  137. if len(redisArr) > 0 {
  138. userUnreadMsg = append(userUnreadMsg, redisArr...)
  139. }
  140. fmt.Println("存入redis值", userUnreadMsg)
  141. redis.Put(redisModule, fmt.Sprintf(UserClearUnread, userId), userUnreadMsg, 60*5)
  142. return nil
  143. }