package controllers

import (
	"sws_xcx/enums"
	"sws_xcx/service"
)

type ArticleApiController struct {
	BaseApiController
}

// @Title GetArticleList
// @Description 获取文章列表
// @Param   pageNum query int true "当前页(从1开始)"
// @Param   pageSize query int true "分页大小"
// @Param   menuid query int false "分类ID"
// @Param   keyword query string false "搜索关键字"
// @Success 200 {array} models.SyhArticlesVO success
// @Failure 500 error
// @router /getarticlelist [get]
func (c *ArticleApiController) GetArticleList() {
	pageNum, _ := c.GetInt("pageNum", 1)
	pageSize, _ := c.GetInt("pageSize", 30)
	menuid, _ := c.GetInt("menuid", 0)
	keyword := c.GetString("keyword")
	arts, toltal, err := service.NewArticleService().GetArticleList(pageNum, pageSize, menuid, keyword)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return

	}
	c.ServeSuccessPageJSON(arts, toltal)
}

// @Title GetArticleByID
// @Description 获取文章详情
// @Param   id query int true "文章id"
// @Success 200 {object} models.SyhArticlesVO success
// @Failure 500 error
// @router /getarticlebyid [get]
func (c *ArticleApiController) GetArticleByID() {
	id, err := c.GetInt64("id")
	if err != nil || id == 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamFormatWrong)

	}
	art, err := service.NewArticleService().GetArticleByID(id)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return

	}
	c.ServeSuccessJSON(art)
}

// @Title GetArticleMenu
// @Description 获取文章分类
// @Param   type query int true "类型：0全部1主2辅"
// @Success 200 {array} models.SyhArticlesMenu success
// @Failure 500 error
// @router /getarticlemenu [get]
func (c *ArticleApiController) GetArticleMenu() {
	t, _ := c.GetInt("type", 0)
	menus, err := service.NewArticleService().GetArticleMenu(t)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return
	}
	c.ServeSuccessJSON(menus)

}

// @Title GetRamdonArticleList
// @Description 获取随机文章列表
// @Param   limit query int true "文章数量"
// @Success 200 {array} models.SyhArticlesVO success
// @Failure 500 error
// @router /getramdonarticlelist [get]
func (c *ArticleApiController) GetRamdonArticleList() {
	limit, _ := c.GetInt("limit", 20)
	arts, err := service.NewArticleService().GetRamdonArticleList(limit)
	if err != nil {
		c.ServeDynamicFailJsonSend(err.Error())
		return

	}
	c.ServeSuccessJSON(arts)
}
