package app

import (
	"bwallet/pkg/errno"
	"bwallet/pkg/handler"
	"bwallet/pkg/util"
	"bwallet/service/live_category_service"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"strconv"
)

func GetLiveCategories(c *gin.Context) {
	platform_id, _ := strconv.Atoi(c.Request.Header.Get("FZM-PLATFORM-ID"))
	valid := validation.Validation{}
	valid.Min(platform_id, 1, "platform_id").Message("平台Id不能为空")
	if valid.HasErrors() {
		handler.SendResponse(c, valid.Errors[0], nil)
		return
	}
	categoryService := live_category_service.LiveCategory{
		PlatformId: int64(platform_id),
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

	total, err := categoryService.Count()
	if err != nil {
		handler.SendResponse(c, errno.ErrCountArticle, nil)
		return
	}

	categories, err := categoryService.GetAll()
	if err != nil {
		handler.SendResponse(c, errno.InternalServerError, nil)
		return
	}

	data := make(map[string]interface{})
	data["items"] = categories
	data["total"] = total

	handler.SendResponse(c, nil, data)
}
