package tags

import (
	"context"
	"fmt"

	"bsm/full/module/base/cms/internal/impl"
	"bsm/full/module/base/cms/internal/models"
	pb "bsm/full/module/base/cms/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
)

// 标签列表
func Fetch(ctx context.Context, in *pb.IdentRequest) (reply *pb.TagsListReply, err error) {

	var (
		tags       = make([]*models.CmsTags, 0)
		cnt  int64 = 0
	)
	if err := impl.DBService.Model(&models.CmsTags{}).Order("created_at desc").Count(&cnt).Find(&tags).Error; err != nil {
		fmt.Println("err = ", err)
		return nil, errcode.ErrDB
	}

	var res = &pb.TagsListReply{
		Count: cnt,
		Data:  make([]*pb.TagsItem, 0, len(tags)),
	}
	for _, v := range tags {
		res.Data = append(res.Data, &pb.TagsItem{
			Id:        int64(v.ID),
			Identity:  v.Identity,
			Title:     v.Title,
			Intro:     v.Intro,
			CoverPath: v.CoverPath,
		})
	}
	return res, nil
}
