messageService.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "app.yhyue.com/moapp/MessageCenter/rpc/message"
  5. "app.yhyue.com/moapp/MessageCenter/util"
  6. qutil "app.yhyue.com/moapp/jybase/common"
  7. "errors"
  8. "github.com/tal-tech/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))))
  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. m := entity.Message{}
  33. m.Isdel = -1
  34. count, err := orm.Where("appid=?", appId).In("id", id).Cols("isdel").Update(&m)
  35. if err != nil || count == 0 {
  36. log.Println(err)
  37. orm.Rollback()
  38. return 0, "删除消息失败"
  39. }
  40. err2 := orm.Commit()
  41. if err2 != nil {
  42. return 0, "删除消息失败"
  43. }
  44. return 1, "删除消息成功"
  45. }
  46. // 未读消息合计
  47. func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int64) {
  48. query := map[string]interface{}{
  49. "receive_userid": userId,
  50. "isRead": 0,
  51. "isdel": 1,
  52. "appid": appId,
  53. }
  54. count := entity.Mysql.Count("message", query)
  55. logx.Info("未读消息数量:", count)
  56. return 1, "查询未读消息成功", count
  57. }
  58. // 获取指定用户指定分类最新一条消息
  59. func (service *MessageService) LastMessage(userId string, appId string, msgType int64, isRead int64) (*message.Messages, error) {
  60. sql := "SELECT * FROM message receive_userid=? and isdel=1 and appid=? and msg_type=?"
  61. valueList := []interface{}{userId, appId, msgType}
  62. if isRead != -1 {
  63. sql = sql + " and isRead=?"
  64. valueList = append(valueList, isRead)
  65. }
  66. query := map[string]interface{}{
  67. "receive_userid": userId,
  68. "isdel": 1,
  69. "appid": appId,
  70. "msg_type": msgType,
  71. }
  72. lastMsg := entity.Mysql.FindOne("message", query, "", "createtime desc")
  73. if lastMsg != nil && len(*lastMsg) > 0 {
  74. _id := util.Int64All((*lastMsg)["id"])
  75. id := strconv.FormatInt(_id, 10)
  76. msg := message.Messages{
  77. Id: id,
  78. Appid: qutil.ObjToString((*lastMsg)["appid"]),
  79. ReceiveUserId: qutil.ObjToString((*lastMsg)["receive_userid"]),
  80. ReceiveName: qutil.ObjToString((*lastMsg)["receive_name"]),
  81. SendUserId: qutil.ObjToString((*lastMsg)["send_userid"]),
  82. SendName: qutil.ObjToString((*lastMsg)["send_name"]),
  83. Createtime: qutil.ObjToString((*lastMsg)["createtime"]),
  84. Title: qutil.ObjToString((*lastMsg)["title"]),
  85. MsgType: qutil.Int64All((*lastMsg)["msg_type"]),
  86. Link: qutil.ObjToString((*lastMsg)["link"]),
  87. CiteId: qutil.Int64All((*lastMsg)["cite_id"]),
  88. Content: qutil.ObjToString((*lastMsg)["content"]),
  89. IsRead: qutil.Int64All((*lastMsg)["isRead"]),
  90. MsgLogId: qutil.Int64All((*lastMsg)["msg_log_id"]),
  91. }
  92. return &msg, nil
  93. } else {
  94. return nil, nil
  95. }
  96. }
  97. //查询消息详情
  98. func FindMessageDetail(id string) (entity.Message, error) {
  99. orm := entity.Engine.NewSession()
  100. defer orm.Close()
  101. mess := entity.Message{}
  102. err := orm.Table("message").Select("*").Where("id = ?", id).Find(&mess)
  103. if err != nil {
  104. return mess, err
  105. }
  106. return mess, nil
  107. }
  108. // 获取用户未读消息分类及数量 及分类下的最新一条消息
  109. func (service *MessageService) ClassCountAndMessage(userId string, appId string) ([]*message.ResCount, error) {
  110. 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)
  111. typeCount := []*message.ResCount{}
  112. // 未读消息分类及数量
  113. if query != nil && len(*query) > 0 {
  114. for _, v := range *query {
  115. typeCount = append(typeCount, &message.ResCount{MsgType: v["msg_type"].(int64), Count: v["count"].(int64)})
  116. }
  117. }
  118. return typeCount, nil
  119. }
  120. // 已接收到消息的分类
  121. func (service *MessageService) ReceiveMsgType(userId string, appId string) ([]int64, error) {
  122. orm := entity.Engine.NewSession()
  123. defer orm.Close()
  124. m := []entity.Message{}
  125. err := orm.Distinct("msg_type").Where("receive_userid=? and isdel=1 and appid=?", userId, appId).Find(&m)
  126. if err != nil {
  127. return nil, err
  128. }
  129. msgTypeList := []int64{}
  130. for _, v := range m {
  131. msgTypeList = append(msgTypeList, int64(v.MsgType))
  132. }
  133. return msgTypeList, nil
  134. }
  135. func (service *MessageService) UpdateMessageReadStatus(msgType int, receiveUserid, appId string) (int, error) {
  136. query := map[string]interface{}{
  137. "receive_userid": receiveUserid,
  138. "msg_type": msgType,
  139. "appid": appId,
  140. }
  141. b := entity.Mysql.Update("message", query, map[string]interface{}{"isRead": 1})
  142. if !b {
  143. return 0, errors.New("修改消息已读出错")
  144. }
  145. MsgCountZero(receiveUserid, strconv.Itoa(msgType))
  146. return 1, nil
  147. }