package basic

import (
	"context"

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

	if in.Page <= 1 {
		in.Page = 1
	}
	if in.PageSize <= 1 {
		in.PageSize = 20
	}
	list, total, err := models.FindWalletRecords(auth.Identity, in.Start, in.End, in.TransType, in.TradeType, in.Page, in.PageSize)
	if err != nil {
		printer.Error(err.Error())
		return nil, errcode.ErrDB
	}
	res := &pb.TransactionsReply{
		Total: total,
	}
	data := make([]*pb.Transaction, 0)
	if len(list) > 0 {
		for _, item := range list {
			data = append(data, &pb.Transaction{
				TransType:  int32(item.TransType),
				TradeType:  int32(item.TradeType),
				InTradeNo:  item.InTradeNo,
				OutTradeNo: item.OutTradeNo,
				Money:      item.Money,
				Fee:        item.Fee,
				PayChannel: int32(item.PayChannel),
				PayType:    item.PayType,
				Remark:     item.Remark,
				Created:    item.CreatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS),
			})
		}
	}
	res.Records = data
	return res, nil
}
