blockchain/internal/logic/blockchainservice/list_candle_data_logic.go

73 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package blockchainservicelogic
import (
"blockchain/internal/domain/usecase"
"context"
"fmt"
"time"
"blockchain/gen_result/pb/code.30cm.net/digimon/app-cloudep-blockchain"
"blockchain/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type ListCandleDataLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewListCandleDataLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCandleDataLogic {
return &ListCandleDataLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *ListCandleDataLogic) ListCandleData(in *app_cloudep_blockchain.HistoryReq) (*app_cloudep_blockchain.ListCandleDataResp, error) {
// 1) 先驗證輸入(避免把錯誤時間丟進背景 goroutine
st, err := time.Parse(time.RFC3339, in.GetStartTime())
if err != nil {
return nil, fmt.Errorf("invalid startTime (RFC3339): %w", err)
}
et, err := time.Parse(time.RFC3339, in.GetEndTime())
if err != nil {
return nil, fmt.Errorf("invalid endTime (RFC3339): %w", err)
}
kline, err := l.svcCtx.BinanceDataSource.ListKline(l.ctx, usecase.QueryKline{
Symbol: in.GetSymbol(),
Interval: in.GetInterval(),
StartTime: st.UnixMilli(),
EndTime: et.UnixMilli(),
})
if err != nil {
return nil, err
}
res := make([]*app_cloudep_blockchain.Kline, 0, len(kline))
for _, item := range kline {
res = append(res, &app_cloudep_blockchain.Kline{
OpenTime: item.OpenTime,
Open: item.Open,
High: item.High,
Low: item.Low,
Close: item.Close,
Volume: item.Volume,
CloseTime: item.OpenTime,
QuoteAssetVolume: item.QuoteAssetVolume,
NumberOfTrades: int32(item.NumberOfTrades),
TakerBuyBaseAssetVolume: item.TakerBuyBaseAssetVolume,
TakerBuyQuoteAssetVolume: item.TakerBuyQuoteAssetVolume,
Symbol: item.Symbol,
Interval: item.Interval,
})
}
return &app_cloudep_blockchain.ListCandleDataResp{
Data: res,
}, nil
}