messageService.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 classMsgType_, _ := range entity.ClassSearchMap[groupId] {
  107. key := fmt.Sprintf(MsgClassCountKey, userId, classMsgType_)
  108. if exists, _ := redis.Exists(MsgClassCountKey, key); exists {
  109. ct := util.Int64All(redis.GetInt(MsgClassCountKey, key))
  110. data[fmt.Sprintf("%d", classMsgType_)] = ct
  111. count += ct
  112. } else {
  113. // todo
  114. entity.Mysql.CountBySql("select * ")
  115. //key := fmt.Sprintf(MsgClassCountKey, userId,classMsgType_)
  116. //redis.Put(redisModule, key, , -1)
  117. }
  118. }
  119. return data, count
  120. }
  121. // 查询消息详情
  122. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  123. if id > 0 {
  124. msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
  125. } else {
  126. msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
  127. }
  128. if msg != nil && len(*msg) > 0 {
  129. return msg, nil
  130. }
  131. return nil, errors.New("没有查询到消息")
  132. }
  133. // GetMsgType 消息的分类
  134. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  135. types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
  136. if types != nil && len(*types) > 0 {
  137. for _, val := range *types {
  138. data = append(data, &message.MsgTypes{
  139. MsgType: qutil.Int64All(val["group_id"]),
  140. Name: qutil.ObjToString(val["name"]),
  141. Img: qutil.ObjToString(val["img"]),
  142. Code: qutil.ObjToString(val["switch"]),
  143. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  144. })
  145. }
  146. return data, nil
  147. }
  148. return nil, nil
  149. }
  150. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
  151. //判断用户是否已经在pc端打开过
  152. count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
  153. if count <= 0 {
  154. tmp := map[string]interface{}{
  155. "msg_log_id": msgLogId,
  156. "platform": platFrom,
  157. "userid": userId,
  158. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  159. }
  160. SaveCache <- tmp
  161. }
  162. return 0
  163. }