message.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package message
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/request/message"
  7. "hongze/hongze_yb/models/response"
  8. "hongze/hongze_yb/models/tables/yb_message"
  9. "hongze/hongze_yb/services/user"
  10. "hongze/hongze_yb/utils"
  11. "time"
  12. )
  13. // GetList 查询消息列表
  14. func GetList(user user.UserInfo, msgType, pageIndex, pageSize int) (ret response.RespMessageList, err error) {
  15. var errMsg string
  16. defer func() {
  17. if err != nil {
  18. global.LOG.Critical(fmt.Sprintf("message GetList: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  19. }
  20. }()
  21. //校验入参
  22. //查询消息列表
  23. offset := (pageIndex - 1) * pageSize
  24. msgList, err := yb_message.GetListByType(user.UserID, msgType, offset, pageSize)
  25. if err != nil {
  26. errMsg = err.Error()
  27. err = errors.New("查询消息列表出错")
  28. return
  29. }
  30. total, err := yb_message.GetListTotalByType(user.UserID, msgType)
  31. if err != nil {
  32. errMsg = err.Error()
  33. err = errors.New("查询消息列表总数出错")
  34. return
  35. }
  36. //处理列表返回值
  37. var list []*response.RespMessageItem
  38. for _, v := range msgList {
  39. tmp := new(response.RespMessageItem)
  40. tmp.MsgId = v.MsgId
  41. tmp.IsRead = v.IsRead
  42. tmp.UserId = v.UserId
  43. tmp.Type = v.Type
  44. tmp.CreateTime = v.CreateTime
  45. tmp.ReportChapterId = v.ReportChapterId
  46. tmp.ReportId = v.ReportId
  47. tmp.OldReportId = v.OldReportId
  48. tmp.OldReportChapterId = v.OldReportChapterId
  49. tmp.ContentFirst = v.ContentFirst
  50. tmp.ContentSecond = v.ContentSecond
  51. tmp.Img = utils.DEFAULT_HONGZE_SYS_LOGO
  52. list = append(list, tmp)
  53. }
  54. ret.List = list
  55. ret.Paging = response.GetPaging(pageIndex, pageSize, int(total))
  56. return
  57. }
  58. // BatchRead 一键已读
  59. func BatchRead(user user.UserInfo, req message.ReqMessageBatch) (err error) {
  60. var errMsg string
  61. defer func() {
  62. if err != nil {
  63. global.LOG.Critical(fmt.Sprintf("message BatchRead: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  64. }
  65. }()
  66. //根据类型设置一键已读
  67. err = yb_message.SetBatchRead(user.UserID, req.Type)
  68. if err != nil {
  69. errMsg = err.Error()
  70. err = errors.New("一键已读设置失败")
  71. return
  72. }
  73. return
  74. }
  75. // SingleRead 单条消息设置已读
  76. func SingleRead(user user.UserInfo, req message.ReqMessage) (err error) {
  77. var errMsg string
  78. defer func() {
  79. if err != nil {
  80. global.LOG.Critical(fmt.Sprintf("message SingleRead: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  81. }
  82. }()
  83. if req.MsgId <=0 {
  84. err = errors.New("请输入消息序号")
  85. return
  86. }
  87. //查询当前消息
  88. msgInfo, err := yb_message.GetMsgByMsgId(user.UserID, req.MsgId)
  89. if err != nil {
  90. errMsg = err.Error()
  91. err = errors.New("查询消息出错")
  92. return
  93. }
  94. if msgInfo.MsgId <= 0 {
  95. err = errors.New("消息不存在")
  96. return
  97. }
  98. if msgInfo.IsRead == 1 {
  99. return
  100. }
  101. msgInfo.IsRead = 1
  102. msgInfo.ModifyTime = time.Now()
  103. //根据类型设置一键已读
  104. err = msgInfo.Update([]string{"is_read","modify_time"})
  105. if err != nil {
  106. errMsg = err.Error()
  107. err = errors.New("已读设置失败")
  108. return
  109. }
  110. return
  111. }
  112. // SingleDelete 单条消息删除
  113. func SingleDelete(user user.UserInfo, req message.ReqMessage) (err error) {
  114. var errMsg string
  115. defer func() {
  116. if err != nil {
  117. global.LOG.Critical(fmt.Sprintf("message SingleDelete: userId=%d, err:%s, errMsg:%s", user.UserID, err.Error(), errMsg))
  118. }
  119. }()
  120. if req.MsgId <=0 {
  121. err = errors.New("请输入消息序号")
  122. return
  123. }
  124. //查询当前消息
  125. msgInfo, err := yb_message.GetMsgByMsgId(user.UserID, req.MsgId)
  126. if err != nil {
  127. errMsg = err.Error()
  128. err = errors.New("查询消息出错")
  129. return
  130. }
  131. if msgInfo.MsgId <= 0 {
  132. err = errors.New("消息不存在")
  133. return
  134. }
  135. if msgInfo.Enabled == 0 {
  136. return
  137. }
  138. msgInfo.Enabled = 0
  139. msgInfo.ModifyTime = time.Now()
  140. //根据类型设置一键已读
  141. err = msgInfo.Update([]string{"enabled","modify_time"})
  142. if err != nil {
  143. errMsg = err.Error()
  144. err = errors.New("删除消息失败")
  145. return
  146. }
  147. return
  148. }