package basic

import (
	"context"

	"bsm/full/module/finance/wallet/internal/impl"
	"bsm/full/module/finance/wallet/internal/models"
	pb "bsm/full/module/finance/wallet/pb"
	"git.apinb.com/bsm-sdk/core/errcode"
	"git.apinb.com/bsm-sdk/core/printer"
	"git.apinb.com/bsm-sdk/core/service"
	"git.apinb.com/bsm-sdk/core/vars"
)

// 获取用户银行卡列表
func GetBankCard(ctx context.Context, in *pb.FinanceEmpty) (reply *pb.GetBankCardReply, err error) {
	auth, ok := service.ParseMetaCtx(ctx, nil)
	if ok != nil {
		return nil, ok
	}

	myWallet, err := models.GetWalletByPassportIdentity(auth.ID, auth.Identity)
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	result := make([]*models.WalletBank, 0)
	err = impl.DBService.Where("wallet_identity=?", myWallet.Identity).Find(&result).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	data := make([]*pb.BankCardInfo, 0)
	for _, card := range result {
		data = append(data, &pb.BankCardInfo{
			Id:         int64(card.ID),
			Bank:       card.Bank,
			BankName:   card.BankName,
			BankType:   card.BankType,
			BankNumber: card.CardNo,
			BindId:     card.BindID,
			Phone:      card.Phone,
			CardOwner:  card.CardOwner,
			IdCard:     card.IDCard,
			Created:    card.CreatedAt.Format(vars.YYYY_MM_DD),
		})
	}

	return &pb.GetBankCardReply{
		Data: data,
	}, nil
}
