messageService.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. msgTypes = append(msgTypes, fmt.Sprintf(`"%s"`, qutil.InterfaceToStr(v["group_id"])))
  64. }
  65. 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)
  66. if query != nil && len(*query) > 0 {
  67. for _, v := range *query {
  68. count += qutil.Int64All(v["count"])
  69. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["group_id"]))
  70. redis.Put(redisModule, key, qutil.Int64All(v["count"]), -1)
  71. }
  72. }
  73. return count
  74. }
  75. // 查询消息详情
  76. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  77. if id > 0 {
  78. msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
  79. } else {
  80. msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
  81. }
  82. if msg != nil && len(*msg) > 0 {
  83. return msg, nil
  84. }
  85. return nil, errors.New("没有查询到消息")
  86. }
  87. // GetMsgType 消息的分类
  88. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  89. types := entity.Mysql.SelectBySql("SELECT * FROM `message_group` WHERE group_id > 0 ORDER BY sequence ASC")
  90. if types != nil && len(*types) > 0 {
  91. for _, val := range *types {
  92. data = append(data, &message.MsgTypes{
  93. MsgType: qutil.Int64All(val["group_id"]),
  94. Name: qutil.ObjToString(val["name"]),
  95. Img: qutil.ObjToString(val["img"]),
  96. Code: qutil.ObjToString(val["switch"]),
  97. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  98. })
  99. }
  100. return data, nil
  101. }
  102. return nil, nil
  103. }
  104. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
  105. //判断用户是否已经在pc端打开过
  106. count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
  107. if count <= 0 {
  108. tmp := map[string]interface{}{
  109. "msg_log_id": msgLogId,
  110. "platform": platFrom,
  111. "userid": userId,
  112. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  113. }
  114. SaveCache <- tmp
  115. }
  116. return 0
  117. }