package category

import (
	"context"

	"bsm/full/module/ec/mall/internal/models"
	pb "bsm/full/module/ec/mall/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
)

// 获取分类数据
func Fetch(ctx context.Context, in *pb.CategoryFetchRequest) (reply *pb.CategoryReply, err error) {
	if in.GetStoreIdentity() == "" {
		return nil, errcode.ErrInvalidArgument
	}

	var (
		category = "all" // all全部，first1级
	)

	if in.GetTypes() != "" {
		category = in.GetTypes()
	}
	data, err := models.GetCategoryList(in.GetKeywords(), in.GetStoreIdentity(), in.GetId())
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.CategoryReply{
		Data:  refList(data, category),
		Count: int64(len(data)),
	}, nil
}
