messageService.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  5. qutil "app.yhyue.com/moapp/jybase/common"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. "errors"
  8. "fmt"
  9. "log"
  10. "strings"
  11. "time"
  12. )
  13. type MessageService struct{}
  14. // 修改消息阅读状态
  15. func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) (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"]), data.Appid, qutil.Int64All((*msg)["group_id"]))
  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: 1,
  52. PageSize: 5,
  53. MsgType: -1,
  54. Read: 0,
  55. }, true)
  56. return 1, "删除消息成功"
  57. }
  58. // 未读消息合计
  59. func (service *MessageService) CountUnread(userId string) int64 {
  60. var count int64
  61. var msgTypes []string
  62. for _, v := range entity.MessageColumn {
  63. if qutil.IntAll(v["group_id"]) > 0 && qutil.IntAll(v["group_id"]) < 999 {
  64. msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
  65. }
  66. }
  67. if len(msgTypes) > 0 {
  68. query := entity.Mysql.SelectBySql(fmt.Sprintf("SELECT group_id,COUNT(CASE WHEN isRead=0 THEN 1 END) as count FROM message where receive_userid=? and isdel=1 GROUP BY group_id ORDER BY FIELD(`group_id`,%s)", strings.Join(msgTypes, ",")), userId)
  69. if query != nil && len(*query) > 0 {
  70. for _, v := range *query {
  71. count += qutil.Int64All(v["count"])
  72. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
  73. redis.Put(redisModule, key, qutil.Int64All(v["count"]), -1)
  74. }
  75. }
  76. }
  77. return count
  78. }
  79. // 查询消息详情
  80. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  81. if id > 0 {
  82. msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
  83. } else {
  84. msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
  85. }
  86. if msg != nil && len(*msg) > 0 {
  87. return msg, nil
  88. }
  89. return nil, errors.New("没有查询到消息")
  90. }
  91. // GetMsgType 消息的分类
  92. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  93. types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
  94. if types != nil && len(*types) > 0 {
  95. for _, val := range *types {
  96. data = append(data, &message.MsgTypes{
  97. MsgType: qutil.Int64All(val["group_id"]),
  98. Name: qutil.ObjToString(val["name"]),
  99. Img: qutil.ObjToString(val["img"]),
  100. Code: qutil.ObjToString(val["switch"]),
  101. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  102. })
  103. }
  104. return data, nil
  105. }
  106. return nil, nil
  107. }
  108. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
  109. //判断用户是否已经在pc端打开过
  110. count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
  111. if count <= 0 {
  112. tmp := map[string]interface{}{
  113. "msg_log_id": msgLogId,
  114. "platform": platFrom,
  115. "userid": userId,
  116. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  117. }
  118. SaveCache <- tmp
  119. }
  120. return 0
  121. }