messageService.go 5.2 KB

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