notice.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package daytask
  2. import (
  3. "app/commons/model/entity"
  4. "app/commons/services"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // NoticeList 消息列表
  8. func (s *Server) NoticeList(c *gin.Context) {
  9. ctx := s.FromContext(c)
  10. db := s.DB()
  11. userId := ctx.UserId()
  12. noticeType := ctx.QueryString("type", "") // system/task/wallet/team
  13. paging := &services.Pagination{
  14. Current: ctx.QueryInt64("current", 1),
  15. Size: ctx.QueryInt64("size", 20),
  16. }
  17. query := db.Model(&entity.DtNotice{}).
  18. Where("(user_id = ? OR user_id = 0) AND status = ?", userId, 1)
  19. if noticeType != "" {
  20. query = query.Where("type = ?", noticeType)
  21. }
  22. query.Count(&paging.Total)
  23. paging.Computer()
  24. type NoticeInfo struct {
  25. Id int64 `json:"id"`
  26. Title string `json:"title"`
  27. Content string `json:"content"`
  28. Type string `json:"type"`
  29. IsRead int8 `json:"isRead"`
  30. CreatedAt int64 `json:"createdAt"`
  31. }
  32. notices := make([]*NoticeInfo, 0)
  33. query.Select("id, title, content, type, is_read, created_at").
  34. Order("created_at DESC").
  35. Offset(int(paging.Start)).
  36. Limit(int(paging.Size)).
  37. Scan(&notices)
  38. // 对于全局通知(user_id=0),批量查询已读状态(避免N+1)
  39. unreadIds := make([]int64, 0)
  40. for _, n := range notices {
  41. if n.IsRead == 0 {
  42. unreadIds = append(unreadIds, n.Id)
  43. }
  44. }
  45. if len(unreadIds) > 0 {
  46. var readRecords []entity.DtNoticeRead
  47. db.Where("user_id = ? AND notice_id IN ?", userId, unreadIds).Find(&readRecords)
  48. readMap := make(map[int64]bool)
  49. for _, r := range readRecords {
  50. readMap[r.NoticeId] = true
  51. }
  52. for _, n := range notices {
  53. if n.IsRead == 0 && readMap[n.Id] {
  54. n.IsRead = 1
  55. }
  56. }
  57. }
  58. ctx.OK(gin.H{
  59. "list": notices,
  60. "paging": paging,
  61. })
  62. }
  63. // NoticeRead 标记消息已读
  64. func (s *Server) NoticeRead(c *gin.Context) {
  65. ctx := s.FromContext(c)
  66. db := s.DB()
  67. userId := ctx.UserId()
  68. type Req struct {
  69. Id int64 `json:"id" form:"id" binding:"required"`
  70. }
  71. var req Req
  72. if err := c.ShouldBind(&req); err != nil {
  73. ctx.Fail("invalid_params")
  74. return
  75. }
  76. // 查询通知
  77. var notice entity.DtNotice
  78. if err := db.Where("id = ?", req.Id).First(&notice).Error; err != nil {
  79. ctx.Fail("notice_not_found")
  80. return
  81. }
  82. if notice.UserId == 0 {
  83. // 全局通知:写入关联表记录已读
  84. readRecord := &entity.DtNoticeRead{
  85. UserId: userId,
  86. NoticeId: req.Id,
  87. }
  88. db.Where("user_id = ? AND notice_id = ?", userId, req.Id).
  89. FirstOrCreate(readRecord)
  90. } else {
  91. // 个人通知:直接更新
  92. db.Model(&entity.DtNotice{}).
  93. Where("id = ? AND user_id = ?", req.Id, userId).
  94. Update("is_read", 1)
  95. }
  96. ctx.OK(gin.H{})
  97. }
  98. // NoticeReadAll 标记所有消息已读
  99. func (s *Server) NoticeReadAll(c *gin.Context) {
  100. ctx := s.FromContext(c)
  101. db := s.DB()
  102. userId := ctx.UserId()
  103. noticeType := ctx.QueryString("type", "")
  104. // 1. 更新个人通知
  105. personalQuery := db.Model(&entity.DtNotice{}).
  106. Where("user_id = ? AND is_read = ?", userId, 0)
  107. if noticeType != "" {
  108. personalQuery = personalQuery.Where("type = ?", noticeType)
  109. }
  110. personalQuery.Update("is_read", 1)
  111. // 2. 全局通知:查出未读的全局通知,批量写入关联表
  112. globalQuery := db.Model(&entity.DtNotice{}).
  113. Where("user_id = 0 AND status = 1")
  114. if noticeType != "" {
  115. globalQuery = globalQuery.Where("type = ?", noticeType)
  116. }
  117. var globalNotices []entity.DtNotice
  118. globalQuery.Select("id").Find(&globalNotices)
  119. for _, n := range globalNotices {
  120. readRecord := &entity.DtNoticeRead{
  121. UserId: userId,
  122. NoticeId: n.Id,
  123. }
  124. db.Where("user_id = ? AND notice_id = ?", userId, n.Id).
  125. FirstOrCreate(readRecord)
  126. }
  127. ctx.OK(gin.H{})
  128. }
  129. // NoticeUnread 未读消息数量
  130. func (s *Server) NoticeUnread(c *gin.Context) {
  131. ctx := s.FromContext(c)
  132. db := s.DB()
  133. userId := ctx.UserId()
  134. // 个人未读通知数
  135. var personalCount int64
  136. db.Model(&entity.DtNotice{}).
  137. Where("user_id = ? AND is_read = ? AND status = ?", userId, 0, 1).
  138. Count(&personalCount)
  139. // 全局未读通知数(排除已在关联表中标记已读的)
  140. var globalCount int64
  141. db.Model(&entity.DtNotice{}).
  142. Where("user_id = 0 AND status = 1").
  143. Where("id NOT IN (SELECT notice_id FROM dt_notice_read WHERE user_id = ?)", userId).
  144. Count(&globalCount)
  145. total := personalCount + globalCount
  146. ctx.OK(gin.H{
  147. "personal": personalCount,
  148. "global": globalCount,
  149. "total": total,
  150. })
  151. }
  152. // NoticeDelete 删除消息
  153. func (s *Server) NoticeDelete(c *gin.Context) {
  154. ctx := s.FromContext(c)
  155. db := s.DB()
  156. userId := ctx.UserId()
  157. type Req struct {
  158. Id int64 `json:"id" form:"id" binding:"required"`
  159. }
  160. var req Req
  161. if err := c.ShouldBind(&req); err != nil {
  162. ctx.Fail("invalid_params")
  163. return
  164. }
  165. // 只能删除自己的消息(全局通知不可删除)
  166. db.Where("id = ? AND user_id = ?", req.Id, userId).Delete(&entity.DtNotice{})
  167. ctx.OK(gin.H{})
  168. }