messageService.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. "strconv"
  12. )
  13. type MessageService struct{}
  14. // 修改消息阅读状态
  15. func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) (int64, string) {
  16. msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": data.Id, "isdel": 1, "appid": data.Appid}, "", "")
  17. //log.Println("查询到消息:", msg)
  18. if msg == nil {
  19. return 0, "该消息不存在"
  20. }
  21. b := entity.Mysql.Update("message", map[string]interface{}{"id": data.Id, "isdel": 1}, map[string]interface{}{"isRead": int(data.ReadStatus)})
  22. if !b {
  23. return 0, "修改消息阅读状态失败"
  24. }
  25. MsgCountMinusOne(qutil.ObjToString((*msg)["receive_userid"]), data.Appid, qutil.Int64All((*msg)["msg_type"]))
  26. return 1, "修改消息阅读状态成功"
  27. }
  28. // 删除消息
  29. func (service *MessageService) DeleteMessage(id []string, appId string) (int64, string) {
  30. orm := entity.Engine.NewSession()
  31. defer orm.Close()
  32. msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": id, "appid": appId}, "", "")
  33. if msg == nil {
  34. return 0, "该消息不存在"
  35. }
  36. m := entity.Message{}
  37. m.Isdel = -1
  38. count, err := orm.Where("appid=?", appId).In("id", id).Cols("isdel").Update(&m)
  39. if err != nil || count == 0 {
  40. log.Println(err)
  41. orm.Rollback()
  42. return 0, "删除消息失败"
  43. }
  44. err2 := orm.Commit()
  45. if err2 != nil {
  46. return 0, "删除消息失败"
  47. }
  48. FindUserMsg(message.FindUserMsgReq{
  49. UserId: qutil.ObjToString((*msg)["receive_userid"]),
  50. Appid: appId,
  51. OffSet: 1,
  52. PageSize: 5,
  53. MsgType: -1,
  54. Read: 0,
  55. }, true)
  56. return 1, "删除消息成功"
  57. }
  58. // 未读消息合计
  59. func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int) {
  60. count := 0
  61. types := entity.Mysql.Find("message_column", map[string]interface{}{}, `"msg_type"`, "", -1, -1)
  62. if types != nil && len(*types) > 0 {
  63. for _, v := range *types {
  64. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["msg_type"]))
  65. if exists, _ := redis.Exists(redisModule, key); exists {
  66. count += redis.GetInt(redisModule, key)
  67. }
  68. }
  69. }
  70. return 1, "查询未读消息成功", count
  71. }
  72. // 获取指定用户指定分类最新一条消息
  73. func (service *MessageService) LastMessage(userId string, appId string, msgType int64, isRead int64) (*message.Messages, error) {
  74. query := map[string]interface{}{
  75. "receive_userid": userId,
  76. "isdel": 1,
  77. "appid": appId,
  78. }
  79. if isRead != -1 {
  80. query["isRead"] = isRead
  81. }
  82. if msgType != -1 {
  83. query["msg_type"] = msgType
  84. }
  85. lastMsg := entity.Mysql.FindOne("message", query, "", "createtime desc")
  86. if lastMsg != nil && len(*lastMsg) > 0 {
  87. _id := util.Int64All((*lastMsg)["id"])
  88. id := strconv.FormatInt(_id, 10)
  89. msg := message.Messages{
  90. Id: id,
  91. Appid: qutil.ObjToString((*lastMsg)["appid"]),
  92. ReceiveUserId: qutil.ObjToString((*lastMsg)["receive_userid"]),
  93. ReceiveName: qutil.ObjToString((*lastMsg)["receive_name"]),
  94. SendUserId: qutil.ObjToString((*lastMsg)["send_userid"]),
  95. SendName: qutil.ObjToString((*lastMsg)["send_name"]),
  96. Createtime: qutil.ObjToString((*lastMsg)["createtime"]),
  97. Title: qutil.ObjToString((*lastMsg)["title"]),
  98. MsgType: qutil.Int64All((*lastMsg)["msg_type"]),
  99. Link: qutil.ObjToString((*lastMsg)["link"]),
  100. CiteId: qutil.Int64All((*lastMsg)["cite_id"]),
  101. Content: qutil.ObjToString((*lastMsg)["content"]),
  102. IsRead: qutil.Int64All((*lastMsg)["isRead"]),
  103. MsgLogId: qutil.Int64All((*lastMsg)["msg_log_id"]),
  104. }
  105. return &msg, nil
  106. } else {
  107. return nil, nil
  108. }
  109. }
  110. // 查询消息详情
  111. func FindMessageDetail(id string) (entity.Message, error) {
  112. orm := entity.Engine.NewSession()
  113. defer orm.Close()
  114. mess := entity.Message{}
  115. err := orm.Table("message").Select("*").Where("id = ?", id).Find(&mess)
  116. if err != nil {
  117. return mess, err
  118. }
  119. return mess, nil
  120. }
  121. // 获取用户未读消息分类及数量 及分类下的最新一条消息
  122. func (service *MessageService) ClassCountAndMessage(userId string, appId string) ([]*message.ResCount, error) {
  123. query := entity.Mysql.SelectBySql("SELECT msg_type,COUNT(CASE WHEN isRead=0 THEN 1 END) as count FROM message where receive_userid=? and isdel=1 and appid=? GROUP BY msg_type ORDER BY FIELD(`msg_type`,\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\")", userId, appId)
  124. typeCount := []*message.ResCount{}
  125. // 未读消息分类及数量
  126. if query != nil && len(*query) > 0 {
  127. for _, v := range *query {
  128. typeCount = append(typeCount, &message.ResCount{MsgType: v["msg_type"].(int64), Count: v["count"].(int64)})
  129. }
  130. }
  131. return typeCount, nil
  132. }
  133. // 已接收到消息的分类
  134. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  135. types := entity.Mysql.SelectBySql("SELECT * FROM `message_column` WHERE msg_type > 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["msg_type"]),
  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) UpdateMessageReadStatus(msgType int, receiveUserid, appId string) (int, error) {
  151. query := map[string]interface{}{
  152. "receive_userid": receiveUserid,
  153. "msg_type": msgType,
  154. "appid": appId,
  155. }
  156. b := entity.Mysql.Update("message", query, map[string]interface{}{"isRead": 1})
  157. if !b {
  158. return 0, errors.New("修改消息已读出错")
  159. }
  160. MsgCountZero(receiveUserid, appId, qutil.Int64All(msgType))
  161. return 1, nil
  162. }