package daytask import ( "app/commons/model/entity" "app/commons/services" "github.com/gin-gonic/gin" ) // NoticeList 消息列表 func (s *Server) NoticeList(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() userId := ctx.UserId() noticeType := ctx.QueryString("type", "") // system/task/wallet/team paging := &services.Pagination{ Current: ctx.QueryInt64("current", 1), Size: ctx.QueryInt64("size", 20), } query := db.Model(&entity.DtNotice{}). Where("(user_id = ? OR user_id = 0) AND status = ?", userId, 1) if noticeType != "" { query = query.Where("type = ?", noticeType) } query.Count(&paging.Total) paging.Computer() type NoticeInfo struct { Id int64 `json:"id"` Title string `json:"title"` Content string `json:"content"` Type string `json:"type"` IsRead int8 `json:"isRead"` CreatedAt int64 `json:"createdAt"` } notices := make([]*NoticeInfo, 0) query.Select("id, title, content, type, is_read, created_at"). Order("created_at DESC"). Offset(int(paging.Start)). Limit(int(paging.Size)). Scan(¬ices) ctx.OK(gin.H{ "list": notices, "paging": paging, }) } // NoticeRead 标记消息已读 func (s *Server) NoticeRead(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() userId := ctx.UserId() type Req struct { Id int64 `json:"id" form:"id" binding:"required"` } var req Req if err := c.ShouldBind(&req); err != nil { ctx.Fail("invalid_params") return } db.Model(&entity.DtNotice{}). Where("id = ? AND (user_id = ? OR user_id = 0)", req.Id, userId). Update("is_read", 1) ctx.OK(gin.H{}) } // NoticeReadAll 标记所有消息已读 func (s *Server) NoticeReadAll(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() userId := ctx.UserId() noticeType := ctx.QueryString("type", "") query := db.Model(&entity.DtNotice{}). Where("(user_id = ? OR user_id = 0) AND is_read = ?", userId, 0) if noticeType != "" { query = query.Where("type = ?", noticeType) } query.Update("is_read", 1) ctx.OK(gin.H{}) } // NoticeUnread 未读消息数量 func (s *Server) NoticeUnread(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() userId := ctx.UserId() // 统计各类型未读数量 type UnreadCount struct { Type string `json:"type"` Count int64 `json:"count"` } var counts []UnreadCount db.Model(&entity.DtNotice{}). Select("type, COUNT(*) as count"). Where("(user_id = ? OR user_id = 0) AND is_read = ? AND status = ?", userId, 0, 1). Group("type"). Scan(&counts) // 构建返回结果 result := make(map[string]int64) var total int64 = 0 for _, c := range counts { result[c.Type] = c.Count total += c.Count } result["total"] = total ctx.OK(result) } // NoticeDelete 删除消息 func (s *Server) NoticeDelete(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() userId := ctx.UserId() type Req struct { Id int64 `json:"id" form:"id" binding:"required"` } var req Req if err := c.ShouldBind(&req); err != nil { ctx.Fail("invalid_params") return } // 只能删除自己的消息 db.Where("id = ? AND user_id = ?", req.Id, userId).Delete(&entity.DtNotice{}) ctx.OK(gin.H{}) }