package daytask import ( "app/commons/model/entity" "app/commons/services" "github.com/gin-gonic/gin" ) // HelpCategories 帮助分类列表 func (s *Server) HelpCategories(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() var categories []entity.DtHelpCategory db.Where("status = ?", 1). Order("sort ASC, id ASC"). Find(&categories) ctx.OK(gin.H{ "list": categories, }) } // HelpList 帮助文章列表 func (s *Server) HelpList(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() categoryId := ctx.QueryInt64("categoryId", 0) keyword := ctx.QueryString("keyword", "") paging := &services.Pagination{ Current: ctx.QueryInt64("current", 1), Size: ctx.QueryInt64("size", 20), } query := db.Model(&entity.DtHelp{}).Where("status = ?", 1) if categoryId > 0 { query = query.Where("category_id = ?", categoryId) } if keyword != "" { query = query.Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%") } query.Count(&paging.Total) paging.Computer() type HelpInfo struct { Id int64 `json:"id"` CategoryId int64 `json:"categoryId"` Title string `json:"title"` ViewCount int `json:"viewCount"` CreatedAt int64 `json:"createdAt"` } helps := make([]*HelpInfo, 0) query.Select("id, category_id, title, view_count, created_at"). Order("sort ASC, id DESC"). Offset(int(paging.Start)). Limit(int(paging.Size)). Scan(&helps) ctx.OK(gin.H{ "list": helps, "paging": paging, }) } // HelpDetail 帮助文章详情 func (s *Server) HelpDetail(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() 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 } var help entity.DtHelp if err := db.Where("id = ? AND status = ?", req.Id, 1).First(&help).Error; err != nil { ctx.Fail("help_not_found") return } // 更新浏览次数 db.Model(&entity.DtHelp{}).Where("id = ?", req.Id). Update("view_count", help.ViewCount+1) ctx.OK(gin.H{ "id": help.Id, "categoryId": help.CategoryId, "title": help.Title, "content": help.Content, "viewCount": help.ViewCount + 1, "createdAt": help.CreatedAt, }) } // HelpSearch 帮助搜索 func (s *Server) HelpSearch(c *gin.Context) { ctx := s.FromContext(c) db := s.DB() keyword := ctx.QueryString("keyword", "") if keyword == "" { ctx.Fail("keyword_required") return } paging := &services.Pagination{ Current: ctx.QueryInt64("current", 1), Size: ctx.QueryInt64("size", 20), } query := db.Model(&entity.DtHelp{}). Where("status = ?", 1). Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%") query.Count(&paging.Total) paging.Computer() type HelpInfo struct { Id int64 `json:"id"` CategoryId int64 `json:"categoryId"` Title string `json:"title"` ViewCount int `json:"viewCount"` CreatedAt int64 `json:"createdAt"` } helps := make([]*HelpInfo, 0) query.Select("id, category_id, title, view_count, created_at"). Order("view_count DESC, id DESC"). Offset(int(paging.Start)). Limit(int(paging.Size)). Scan(&helps) ctx.OK(gin.H{ "list": helps, "paging": paging, }) }