package friend

import (
	"context"

	"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"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
)

// 获取标签成员
func TagMemberFetch(ctx context.Context, in *pb.IdentRequest) (reply *pb.PartFriendReply, err error) {
	auth, err := service.ParseMetaCtx(ctx, nil)
	if err != nil {
		return nil, err
	}

	if in.Identity == "" {
		return nil, errcode.ErrInvalidArgument
	}

	var ids []uint
	err = impl.DBService.Model(models.RelationFriend{}).Where("relation_id=? and group_identity=?", auth.ID, in.Identity).Pluck("relation_id", ids).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	data, err := common.GetrelationInfoDetailCardById(ids)
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	return &pb.PartFriendReply{
		Total:   int32(len(data)),
		Friends: data,
	}, nil
}
