package app

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

func GetOneNews(c *gin.Context) {
	//gredis.ZAdd("sorted_set", 2020716, "二零二零零七一六", 3)
	//gredis.ZAdd("sorted_set", 2020718, "二零二零零七一八", 3)
	//gredis.ZAdd("sorted_set", 2020719, "二零二零零七一九", 3)
	//gredis.ZAdd("sorted_set", 2020717, "二零二零零七一七", 3)

	fmt.Println(gredis.ZRange("sorted_set", 1, 4, 3))
}

func GetNews(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
	}
	newsService := news_service.News{
		PlatformId: platform_id,
		SearchType: -1,
		PageNum:    util.GetPage(c),
		PageSize:   util.GetLimit(c),
	}

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

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

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

	handler.SendResponse(c, nil, data)
}
