package match

import (
	"context"

	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"bsm/full/module/social/relation/internal/impl"
	"bsm/full/module/social/relation/internal/logic/common"
	"bsm/full/module/social/relation/internal/models"
	pb "bsm/full/module/social/relation/pb"
)

// 获取匹配列表
func Fetch(ctx context.Context, in *pb.FetchRequest) (reply *pb.FetchRelationItemReply, err error) {
	// parse authorization meta.
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}
	// 处理分页
	if in.PageNo <= 1 {
		in.PageNo = 1
	}
	if in.PageSize <= 1 {
		in.PageSize = 20
	}

	var (
		total int64
	)

	err = impl.DBService.Model(&models.RelationMatch{}).Where("relation_identity=?", auth.Identity).Count(&total).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	// 取得用户信息卡片
	data, err := common.GetrelationInfoDetailCardByMatch(auth.Identity, int(in.PageNo-1)*int(in.PageSize), int(in.PageSize))
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	reply = &pb.FetchRelationItemReply{
		Total: total,
		Data:  data,
	}
	return reply, nil
}
