package logic

import (
	"context"
	"github.com/gogf/gf/v2/errors/gcode"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	v1 "practice_Go/api/v1/shows"
	"practice_Go/internal/dao"
	"practice_Go/internal/model/entity"
	show "practice_Go/internal/service"
)

type sShows struct{}

func init() {
	show.RegisterShows(NewShows())
}

func NewShows() *sShows {
	return &sShows{}
}

const (
	QueryRecommend = iota + 1 // 推荐界面
	QueryVideo                // 精选视频
	QueryClub                 // 博股俱乐部
	QueryChannel              // homilyLink频道
)

// 实现 IShows 接口中的 GetShowsList 方法
func (s *sShows) GetShowsList(ctx context.Context, req *v1.ShowReq) (res v1.GetShowsListRes, err error) {
	model := dao.Shows.Ctx(ctx)

	// 根据请求类型动态调整查询条件
	switch req.QueryType {
	case QueryVideo:
		model = model.Where("type", 2).OrderAsc("publish_time") // 视频类型正序
	case QueryClub:
		model = model.Where("club_id", 1).OrderDesc("publish_time") // 俱乐部倒序
	case QueryChannel:
		model = model.Where("publisher_name", "homilyLink").OrderDesc("publish_time")
	default:
		model = model.OrderDesc("publish_time") // 默认推荐倒序
	}

	// 查询数据并填充到结果列表
	var list []*entity.Shows
	if err := model.Scan(&list); err != nil {
		g.Log().Errorf(ctx, "内容查询异常|类型:%d|错误:%v", req.QueryType, err)
		return res, gerror.WrapCode(gcode.CodeDbOperationError, err, "数据获取失败")
	}

	// 将查询结果赋值给返回值
	res.List = list
	return res, nil
}