messageService.go 5.2 KB

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