messageService.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, groupIds []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. groupIds = append(groupIds, qutil.InterfaceToStr(v["group_id"]))
  72. if exists, _ := redis.Exists(redisModule, key); exists {
  73. ct := util.Int64All(redis.GetInt(redisModule, key))
  74. data[qutil.InterfaceToStr(v["group_id"])] = ct
  75. count += ct
  76. }
  77. }
  78. }
  79. if len(msgTypes) > 0 && len(msgTypes) != len(data) {
  80. count = 0
  81. 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)
  82. if query != nil && len(*query) > 0 {
  83. for _, v := range *query {
  84. unread := qutil.Int64All(v["count"])
  85. data[qutil.InterfaceToStr(v["group_id"])] = unread
  86. count += unread
  87. }
  88. }
  89. if isRedis { //初始化未读数
  90. for _, v1 := range groupIds {
  91. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v1))
  92. redis.Put(redisModule, key, data[v1], -1)
  93. }
  94. }
  95. }
  96. return data, count
  97. }
  98. func (service *MessageService) CountClassUnread(userId string, groupId int64) (classCount map[string]int64, total int64) {
  99. var (
  100. count int64
  101. )
  102. data := make(map[string]int64)
  103. if _, ok := entity.ClassSearchMap[groupId]; !ok {
  104. return
  105. }
  106. for i := 0; i < len(entity.ClassSearchMap[groupId]); i++ {
  107. msgClass := entity.ClassSearchMap[groupId][i]
  108. key := fmt.Sprintf(MsgClassCountKey, userId, msgClass.MsgType)
  109. if exists, _ := redis.Exists(redisModule, key); exists {
  110. ct := util.Int64All(redis.GetInt(redisModule, key))
  111. data[fmt.Sprintf("%d", msgClass.MsgType)] = ct
  112. count += ct
  113. } else {
  114. q := "select count(*) from message where receive_userid=? and isdel=1 and msg_type=?"
  115. classCount := entity.Mysql.CountBySql(q, userId, msgClass.MsgType)
  116. if classCount != -1 {
  117. redis.Put(redisModule, key, classCount, -1)
  118. data[fmt.Sprintf("%d", msgClass.MsgType)] = classCount
  119. count += classCount
  120. } else {
  121. log.Println("查询classCount失败:", classCount, q)
  122. }
  123. }
  124. }
  125. return data, count
  126. }
  127. // 查询消息详情
  128. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  129. if id > 0 {
  130. msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
  131. } else {
  132. msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
  133. }
  134. if msg != nil && len(*msg) > 0 {
  135. return msg, nil
  136. }
  137. return nil, errors.New("没有查询到消息")
  138. }
  139. // GetMsgType 消息的分类
  140. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  141. types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
  142. if types != nil && len(*types) > 0 {
  143. for _, val := range *types {
  144. data = append(data, &message.MsgTypes{
  145. MsgType: qutil.Int64All(val["group_id"]),
  146. Name: qutil.ObjToString(val["name"]),
  147. Img: qutil.ObjToString(val["img"]),
  148. Code: qutil.ObjToString(val["switch"]),
  149. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  150. })
  151. }
  152. return data, nil
  153. }
  154. return nil, nil
  155. }
  156. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
  157. //判断用户是否已经在pc端打开过
  158. count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
  159. if count <= 0 {
  160. tmp := map[string]interface{}{
  161. "msg_log_id": msgLogId,
  162. "platform": platFrom,
  163. "userid": userId,
  164. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  165. }
  166. SaveCache <- tmp
  167. }
  168. return 0
  169. }