package basic

import (
	"context"
	"time"

	"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 BindPaymentId(ctx context.Context, in *pb.BindPaymentIDRequest) (reply *pb.StatusReply, err error) {
	auth, ok := service.ParseMetaCtx(ctx, nil)
	if ok != nil {
		return nil, ok
	}

	if in.PayType == 0 || in.AuthCode == "" {
		return nil, errcode.ErrInvalidArgument
	}

	var filed string
	//通过code获取微信或者支付宝的openid
	if in.PayType == 1 {
		filed = "wxpay_id"
	}
	if in.PayType == 2 {
		filed = "alipay_id"
	}

	err = impl.DBService.Model(&models.WalletBasic{}).Where("passport_identity=?", auth.Identity).Update(filed, in.AuthCode).Error
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}

	return &pb.StatusReply{
		Message: vars.OK,
		Timeseq: time.Now().UnixMilli(),
	}, nil
}
