messageService.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "errors"
  9. "fmt"
  10. "log"
  11. "strings"
  12. "time"
  13. )
  14. type MessageService struct{}
  15. // 修改消息阅读状态
  16. func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) (int64, string) {
  17. msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": data.Id, "isdel": 1, "appid": data.Appid}, "", "")
  18. //log.Println("查询到消息:", msg)
  19. if msg == nil {
  20. return 0, "该消息不存在"
  21. }
  22. b := entity.Mysql.Update("message", map[string]interface{}{"id": data.Id, "isdel": 1}, map[string]interface{}{"isRead": int(data.ReadStatus)})
  23. if !b {
  24. return 0, "修改消息阅读状态失败"
  25. }
  26. MsgCountMinusOne(qutil.ObjToString((*msg)["receive_userid"]), data.Appid, qutil.Int64All((*msg)["group_id"]))
  27. return 1, "修改消息阅读状态成功"
  28. }
  29. // 删除消息
  30. func (service *MessageService) DeleteMessage(id []string, appId string) (int64, string) {
  31. orm := entity.Engine.NewSession()
  32. defer orm.Close()
  33. msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": id, "appid": appId}, "", "")
  34. if msg == nil {
  35. return 0, "该消息不存在"
  36. }
  37. m := entity.Message{}
  38. m.Isdel = -1
  39. count, err := orm.Where("appid=?", appId).In("id", id).Cols("isdel").Update(&m)
  40. if err != nil || count == 0 {
  41. log.Println(err)
  42. orm.Rollback()
  43. return 0, "删除消息失败"
  44. }
  45. err2 := orm.Commit()
  46. if err2 != nil {
  47. return 0, "删除消息失败"
  48. }
  49. FindUserMsg(message.FindUserMsgReq{
  50. UserId: qutil.ObjToString((*msg)["receive_userid"]),
  51. Appid: appId,
  52. OffSet: 1,
  53. PageSize: 5,
  54. MsgType: -1,
  55. Read: 0,
  56. }, true)
  57. return 1, "删除消息成功"
  58. }
  59. // 未读消息合计 isRedis 是否需要初始化redis
  60. func (service *MessageService) CountUnread(userId string, isRedis bool) (map[string]int64, int64) {
  61. var (
  62. count int64
  63. msgTypes []string
  64. )
  65. data := make(map[string]int64)
  66. for _, v := range entity.MessageColumn {
  67. if util.IntAll(v["group_id"]) > 0 && util.IntAll(v["group_id"]) < 999 {
  68. //去除全部与私信
  69. msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
  70. key := fmt.Sprintf(MsgCountKey, userId, util.IntAll(v["group_id"]))
  71. if exists, _ := redis.Exists(redisModule, key); exists {
  72. ct := util.Int64All(redis.GetInt(redisModule, key))
  73. data[qutil.InterfaceToStr(v["group_id"])] = ct
  74. count += ct
  75. }
  76. }
  77. }
  78. if len(msgTypes) > 0 && len(msgTypes) != len(data) {
  79. count = 0
  80. 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)
  81. if query != nil && len(*query) > 0 {
  82. for _, v := range *query {
  83. unread := qutil.Int64All(v["count"])
  84. data[qutil.InterfaceToStr(v["group_id"])] = unread
  85. count += unread
  86. }
  87. }
  88. if isRedis { //初始化未读数
  89. for _, v1 := range msgTypes {
  90. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v1))
  91. redis.Put(redisModule, key, data[v1], -1)
  92. }
  93. }
  94. }
  95. return data, count
  96. }
  97. // 查询消息详情
  98. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  99. if id > 0 {
  100. msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
  101. } else {
  102. msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
  103. }
  104. if msg != nil && len(*msg) > 0 {
  105. return msg, nil
  106. }
  107. return nil, errors.New("没有查询到消息")
  108. }
  109. // GetMsgType 消息的分类
  110. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  111. types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
  112. if types != nil && len(*types) > 0 {
  113. for _, val := range *types {
  114. data = append(data, &message.MsgTypes{
  115. MsgType: qutil.Int64All(val["group_id"]),
  116. Name: qutil.ObjToString(val["name"]),
  117. Img: qutil.ObjToString(val["img"]),
  118. Code: qutil.ObjToString(val["switch"]),
  119. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  120. })
  121. }
  122. return data, nil
  123. }
  124. return nil, nil
  125. }
  126. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
  127. //判断用户是否已经在pc端打开过
  128. count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
  129. if count <= 0 {
  130. tmp := map[string]interface{}{
  131. "msg_log_id": msgLogId,
  132. "platform": platFrom,
  133. "userid": userId,
  134. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  135. }
  136. SaveCache <- tmp
  137. }
  138. return 0
  139. }