messageService.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "errors"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. "log"
  10. "strconv"
  11. )
  12. type MessageService struct {
  13. }
  14. // 修改消息阅读状态
  15. func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusRequest) (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"]), strconv.Itoa(int((*msg)["msg_type"].(int64))), data.Appid)
  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, int64) {
  60. query := map[string]interface{}{
  61. "receive_userid": userId,
  62. "isRead": 0,
  63. "isdel": 1,
  64. "appid": appId,
  65. }
  66. count := entity.Mysql.Count("message", query)
  67. logx.Info("未读消息数量:", count)
  68. return 1, "查询未读消息成功", count
  69. }
  70. // 获取指定用户指定分类最新一条消息
  71. func (service *MessageService) LastMessage(userId string, appId string, msgType int64, isRead int64) (*message.Messages, error) {
  72. query := map[string]interface{}{
  73. "receive_userid": userId,
  74. "isdel": 1,
  75. "appid": appId,
  76. }
  77. if isRead != -1 {
  78. query["isRead"] = isRead
  79. }
  80. if msgType != -1 {
  81. query["msg_type"] = msgType
  82. }
  83. lastMsg := entity.Mysql.FindOne("message", query, "", "createtime desc")
  84. if lastMsg != nil && len(*lastMsg) > 0 {
  85. _id := util.Int64All((*lastMsg)["id"])
  86. id := strconv.FormatInt(_id, 10)
  87. msg := message.Messages{
  88. Id: id,
  89. Appid: qutil.ObjToString((*lastMsg)["appid"]),
  90. ReceiveUserId: qutil.ObjToString((*lastMsg)["receive_userid"]),
  91. ReceiveName: qutil.ObjToString((*lastMsg)["receive_name"]),
  92. SendUserId: qutil.ObjToString((*lastMsg)["send_userid"]),
  93. SendName: qutil.ObjToString((*lastMsg)["send_name"]),
  94. Createtime: qutil.ObjToString((*lastMsg)["createtime"]),
  95. Title: qutil.ObjToString((*lastMsg)["title"]),
  96. MsgType: qutil.Int64All((*lastMsg)["msg_type"]),
  97. Link: qutil.ObjToString((*lastMsg)["link"]),
  98. CiteId: qutil.Int64All((*lastMsg)["cite_id"]),
  99. Content: qutil.ObjToString((*lastMsg)["content"]),
  100. IsRead: qutil.Int64All((*lastMsg)["isRead"]),
  101. MsgLogId: qutil.Int64All((*lastMsg)["msg_log_id"]),
  102. }
  103. return &msg, nil
  104. } else {
  105. return nil, nil
  106. }
  107. }
  108. // 查询消息详情
  109. func FindMessageDetail(id string) (entity.Message, error) {
  110. orm := entity.Engine.NewSession()
  111. defer orm.Close()
  112. mess := entity.Message{}
  113. err := orm.Table("message").Select("*").Where("id = ?", id).Find(&mess)
  114. if err != nil {
  115. return mess, err
  116. }
  117. return mess, nil
  118. }
  119. // 获取用户未读消息分类及数量 及分类下的最新一条消息
  120. func (service *MessageService) ClassCountAndMessage(userId string, appId string) ([]*message.ResCount, error) {
  121. 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)
  122. typeCount := []*message.ResCount{}
  123. // 未读消息分类及数量
  124. if query != nil && len(*query) > 0 {
  125. for _, v := range *query {
  126. typeCount = append(typeCount, &message.ResCount{MsgType: v["msg_type"].(int64), Count: v["count"].(int64)})
  127. }
  128. }
  129. return typeCount, nil
  130. }
  131. // 已接收到消息的分类
  132. func (service *MessageService) ReceiveMsgType(userId string, appId string) ([]int64, error) {
  133. orm := entity.Engine.NewSession()
  134. defer orm.Close()
  135. m := []entity.Message{}
  136. err := orm.Distinct("msg_type").Where("receive_userid=? and isdel=1 and appid=?", userId, appId).Find(&m)
  137. if err != nil {
  138. return nil, err
  139. }
  140. msgTypeList := []int64{}
  141. for _, v := range m {
  142. msgTypeList = append(msgTypeList, int64(v.MsgType))
  143. }
  144. return msgTypeList, nil
  145. }
  146. func (service *MessageService) UpdateMessageReadStatus(msgType int, receiveUserid, appId string) (int, error) {
  147. query := map[string]interface{}{
  148. "receive_userid": receiveUserid,
  149. "msg_type": msgType,
  150. "appid": appId,
  151. }
  152. b := entity.Mysql.Update("message", query, map[string]interface{}{"isRead": 1})
  153. if !b {
  154. return 0, errors.New("修改消息已读出错")
  155. }
  156. MsgCountZero(receiveUserid, strconv.Itoa(msgType), appId)
  157. return 1, nil
  158. }