messageService.go 5.1 KB

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