messageService.go 5.3 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. sql := "SELECT * FROM message receive_userid=? and isdel=1 and appid=? and msg_type=?"
  60. valueList := []interface{}{userId, appId, msgType}
  61. if isRead != -1 {
  62. sql = sql + " and isRead=?"
  63. valueList = append(valueList, isRead)
  64. }
  65. query := map[string]interface{}{
  66. "receive_userid": userId,
  67. "isdel": 1,
  68. "appid": appId,
  69. "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. }
  90. return &msg, nil
  91. } else {
  92. return nil, nil
  93. }
  94. }
  95. //查询消息详情
  96. func FindMessageDetail(id string) (entity.Message, error) {
  97. orm := entity.Engine.NewSession()
  98. defer orm.Close()
  99. mess := entity.Message{}
  100. err := orm.Table("message").Select("*").Where("id = ?", id).Find(&mess)
  101. if err != nil {
  102. return mess, err
  103. }
  104. return mess, nil
  105. }
  106. // 获取用户未读消息分类及数量 及分类下的最新一条消息
  107. func (service *MessageService) ClassCountAndMessage(userId string, appId string) ([]*message.ResCount, error) {
  108. 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)
  109. typeCount := []*message.ResCount{}
  110. // 未读消息分类及数量
  111. if query != nil && len(*query) > 0 {
  112. for _, v := range *query {
  113. typeCount = append(typeCount, &message.ResCount{MsgType: v["msg_type"].(int64), Count: v["count"].(int64)})
  114. }
  115. }
  116. return typeCount, nil
  117. }
  118. // 已接收到消息的分类
  119. func (service *MessageService) ReceiveMsgType(userId string, appId string) ([]int64, error) {
  120. orm := entity.Engine.NewSession()
  121. defer orm.Close()
  122. m := []entity.Message{}
  123. err := orm.Distinct("msg_type").Where("receive_userid=? and isdel=1 and appid=?", userId, appId).Find(&m)
  124. if err != nil {
  125. return nil, err
  126. }
  127. msgTypeList := []int64{}
  128. for _, v := range m {
  129. msgTypeList = append(msgTypeList, int64(v.MsgType))
  130. }
  131. return msgTypeList, nil
  132. }
  133. func (service *MessageService) UpdateMessageReadStatus(msgType int, receiveUserid, appId string) (int, error) {
  134. query := map[string]interface{}{
  135. "receive_userid": receiveUserid,
  136. "msg_type": msgType,
  137. "appid": appId,
  138. }
  139. b := entity.Mysql.Update("message", query, map[string]interface{}{"isRead": 1})
  140. if !b {
  141. return 0, errors.New("修改消息已读出错")
  142. }
  143. EtcdSetCountZero(receiveUserid, strconv.Itoa(msgType))
  144. return 1, nil
  145. }