messageService.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "time"
  11. )
  12. type MessageService struct{}
  13. // 修改消息阅读状态
  14. func (service *MessageService) ChangeReadStatus(data *message.ChangeReadStatusReq) (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. MsgCountMinusOne(qutil.ObjToString((*msg)["receive_userid"]), data.Appid, qutil.Int64All((*msg)["msg_type"]))
  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. msg := entity.Mysql.FindOne("message", map[string]interface{}{"id": id, "appid": appId}, "", "")
  32. if msg == nil {
  33. return 0, "该消息不存在"
  34. }
  35. m := entity.Message{}
  36. m.Isdel = -1
  37. count, err := orm.Where("appid=?", appId).In("id", id).Cols("isdel").Update(&m)
  38. if err != nil || count == 0 {
  39. log.Println(err)
  40. orm.Rollback()
  41. return 0, "删除消息失败"
  42. }
  43. err2 := orm.Commit()
  44. if err2 != nil {
  45. return 0, "删除消息失败"
  46. }
  47. FindUserMsg(message.FindUserMsgReq{
  48. UserId: qutil.ObjToString((*msg)["receive_userid"]),
  49. Appid: appId,
  50. OffSet: 1,
  51. PageSize: 5,
  52. MsgType: -1,
  53. Read: 0,
  54. }, true)
  55. return 1, "删除消息成功"
  56. }
  57. // 未读消息合计
  58. func (service *MessageService) CountUnread(userId string, appId string) (int64, string, int) {
  59. count := 0
  60. types := entity.Mysql.Find("message_column", map[string]interface{}{}, `"msg_type"`, "", -1, -1)
  61. if types != nil && len(*types) > 0 {
  62. for _, v := range *types {
  63. key := fmt.Sprintf(MsgCountKey, userId, qutil.IntAll(v["msg_type"]))
  64. if exists, _ := redis.Exists(redisModule, key); exists {
  65. count += redis.GetInt(redisModule, key)
  66. }
  67. }
  68. }
  69. return 1, "查询未读消息成功", count
  70. }
  71. // 查询消息详情
  72. func FindMessageDetail(id, msgLogId int64, userId string) (msg *map[string]interface{}, err error) {
  73. if id > 0 {
  74. msg = entity.Mysql.FindOne("message", map[string]interface{}{"id": id}, "", "")
  75. } else {
  76. msg = entity.Mysql.FindOne("message", map[string]interface{}{"receive_userid": userId, "msg_log_id": msgLogId}, "", "")
  77. }
  78. if msg != nil && len(*msg) > 0 {
  79. return msg, nil
  80. }
  81. return nil, errors.New("没有查询到消息")
  82. }
  83. // GetMsgType 消息的分类
  84. func (service *MessageService) GetMsgType() (data []*message.MsgTypes, err error) {
  85. types := entity.Mysql.SelectBySql("SELECT * FROM `message_column` WHERE msg_type > 0 ORDER BY sequence ASC")
  86. if types != nil && len(*types) > 0 {
  87. for _, val := range *types {
  88. data = append(data, &message.MsgTypes{
  89. MsgType: qutil.Int64All(val["msg_type"]),
  90. Name: qutil.ObjToString(val["name"]),
  91. Img: qutil.ObjToString(val["img"]),
  92. Code: qutil.ObjToString(val["switch"]),
  93. DisplayPlatform: qutil.ObjToString(val["display_platform"]),
  94. })
  95. }
  96. return data, nil
  97. }
  98. return nil, nil
  99. }
  100. func (service *MessageService) MsgOpenLog(platFrom, msgLogId int64, userId string) int64 {
  101. //判断用户是否已经在pc端打开过
  102. count := entity.Mysql.CountBySql("SELECT COUNT(*) FROM message_open_log WHERE msg_log_id = ? and platform = ? and userid = ?", msgLogId, platFrom, userId)
  103. if count <= 0 {
  104. tmp := map[string]interface{}{
  105. "msg_log_id": msgLogId,
  106. "platform": platFrom,
  107. "userid": userId,
  108. "createtime": time.Now().Format("2006-01-02 15:04:05"),
  109. }
  110. SaveCache <- tmp
  111. }
  112. return 0
  113. }