notice.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. ctx.OK(gin.H{
  39. "list": notices,
  40. "paging": paging,
  41. })
  42. }
  43. // NoticeRead 标记消息已读
  44. func (s *Server) NoticeRead(c *gin.Context) {
  45. ctx := s.FromContext(c)
  46. db := s.DB()
  47. userId := ctx.UserId()
  48. type Req struct {
  49. Id int64 `json:"id" form:"id" binding:"required"`
  50. }
  51. var req Req
  52. if err := c.ShouldBind(&req); err != nil {
  53. ctx.Fail("invalid_params")
  54. return
  55. }
  56. db.Model(&entity.DtNotice{}).
  57. Where("id = ? AND (user_id = ? OR user_id = 0)", req.Id, userId).
  58. Update("is_read", 1)
  59. ctx.OK(gin.H{})
  60. }
  61. // NoticeReadAll 标记所有消息已读
  62. func (s *Server) NoticeReadAll(c *gin.Context) {
  63. ctx := s.FromContext(c)
  64. db := s.DB()
  65. userId := ctx.UserId()
  66. noticeType := ctx.QueryString("type", "")
  67. query := db.Model(&entity.DtNotice{}).
  68. Where("(user_id = ? OR user_id = 0) AND is_read = ?", userId, 0)
  69. if noticeType != "" {
  70. query = query.Where("type = ?", noticeType)
  71. }
  72. query.Update("is_read", 1)
  73. ctx.OK(gin.H{})
  74. }
  75. // NoticeUnread 未读消息数量
  76. func (s *Server) NoticeUnread(c *gin.Context) {
  77. ctx := s.FromContext(c)
  78. db := s.DB()
  79. userId := ctx.UserId()
  80. // 统计各类型未读数量
  81. type UnreadCount struct {
  82. Type string `json:"type"`
  83. Count int64 `json:"count"`
  84. }
  85. var counts []UnreadCount
  86. db.Model(&entity.DtNotice{}).
  87. Select("type, COUNT(*) as count").
  88. Where("(user_id = ? OR user_id = 0) AND is_read = ? AND status = ?", userId, 0, 1).
  89. Group("type").
  90. Scan(&counts)
  91. // 构建返回结果
  92. result := make(map[string]int64)
  93. var total int64 = 0
  94. for _, c := range counts {
  95. result[c.Type] = c.Count
  96. total += c.Count
  97. }
  98. result["total"] = total
  99. ctx.OK(result)
  100. }
  101. // NoticeDelete 删除消息
  102. func (s *Server) NoticeDelete(c *gin.Context) {
  103. ctx := s.FromContext(c)
  104. db := s.DB()
  105. userId := ctx.UserId()
  106. type Req struct {
  107. Id int64 `json:"id" form:"id" binding:"required"`
  108. }
  109. var req Req
  110. if err := c.ShouldBind(&req); err != nil {
  111. ctx.Fail("invalid_params")
  112. return
  113. }
  114. // 只能删除自己的消息
  115. db.Where("id = ? AND user_id = ?", req.Id, userId).Delete(&entity.DtNotice{})
  116. ctx.OK(gin.H{})
  117. }