messageService.go 5.7 KB

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