feature/order_service #1

Merged
daniel.w merged 8 commits from feature/order_service into main 2024-10-27 17:28:27 +00:00
4 changed files with 84 additions and 101 deletions
Showing only changes of commit ea419a9943 - Show all commits

View File

@ -1,16 +1,12 @@
package orderservicelogic
import (
"app-cloudep-trade-service/gen_result/pb/trade"
"app-cloudep-trade-service/internal/domain"
"app-cloudep-trade-service/internal/domain/usecase"
"context"
ers "code.30cm.net/digimon/library-go/errs"
"github.com/shopspring/decimal"
"app-cloudep-trade-service/gen_result/pb/trade"
"app-cloudep-trade-service/internal/svc"
ers "code.30cm.net/digimon/library-go/errs"
"context"
"github.com/zeromicro/go-zero/core/logx"
)
@ -29,40 +25,40 @@ func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Creat
}
type createOrderReq struct { // 訂單ID
BusinessID string `json:"business_id" validate:"required"` // 訂單業務流水號
OrderType int8 `json:"order_type" validate:"required"` // 訂單類型
OrderStatus int8 `json:"order_status" validate:"oneof=0 1 2 3 4 5 6 7 8 9 10 11"` // 訂單狀態
Brand string `json:"brand" validate:"required"` // 下單平台
OrderUID string `json:"order_uid" validate:"required"` // 下單用戶 UID
ReferenceID string `json:"reference_id" validate:"required"` // 訂單來源
Count decimal.Decimal `json:"count" validate:"required,decimalGt=0"` // 訂單數量
OrderFee decimal.Decimal `json:"order_fee" validate:"required,decimalGte=0"` // 訂單手續費
Amount decimal.Decimal `json:"amount" validate:"required,decimalGte=0"` // 單價
ReferenceBrand *string `json:"reference_brand,omitempty" validate:"omitempty"` // 訂單來源平台
ReferenceUID *string `json:"reference_uid,omitempty" validate:"omitempty"` // 訂單來源用戶 UID
WalletStatus *int64 `json:"wallet_status,omitempty" validate:"omitempty,oneof=1 2 3 4 5 6 7"` // 交易金額狀態
ThreePartyStatus *int64 `json:"three_party_status,omitempty" validate:"omitempty,oneof=1 2 3"` // 三方請求狀態
DirectionType *int64 `json:"direction_type,omitempty" validate:"omitempty,oneof=1 2"` // 交易方向
CryptoType *string `json:"crypto_type,omitempty" validate:"omitempty"` // 交易幣種
ThirdPartyFee *decimal.Decimal `json:"third_party_fee,omitempty" validate:"omitempty,decimalGte=0"` // 第三方手續費
CryptoToUSDTRate *decimal.Decimal `json:"crypto_to_usdt_rate,omitempty" validate:"omitempty,decimalGte=0"` // 交易幣種對 USDT 匯率
FiatToUSDRate *decimal.Decimal `json:"fiat_to_usd_rate,omitempty" validate:"omitempty,decimalGte=0"` // 法幣對 USD 匯率
FeeCryptoToUSDTRate *decimal.Decimal `json:"fee_crypto_to_usdt_rate,omitempty" validate:"omitempty,decimalGte=0"` // 手續費幣種對 USDT 匯率
USDTToCryptoTypeRate *decimal.Decimal `json:"usdt_to_crypto_type_rate,omitempty" validate:"omitempty,decimalGte=0"` // USDT 對交易幣種匯率
PaymentFiat *string `json:"payment_fiat,omitempty" validate:"omitempty"` // 支付法幣
PaymentUnitPrice *decimal.Decimal `json:"payment_unit_price,omitempty" validate:"omitempty,decimalGte=0"` // crypto 單價
PaymentTemplateID *string `json:"payment_template_id,omitempty" validate:"omitempty"` // 支付方式配置 ID
OrderArrivalTime *int64 `json:"order_arrival_time,omitempty" validate:"omitempty"` // 訂單到帳時間
OrderPaymentTime *int64 `json:"order_payment_time,omitempty" validate:"omitempty"` // 訂單付款時間
UnpaidTimeoutSecond *int64 `json:"unpaid_timeout_second,omitempty" validate:"omitempty,decimalGte=0"` // 支付期限秒數
ChainType *string `json:"chain_type,omitempty" validate:"omitempty"` // 主網類型
TxHash *string `json:"tx_hash,omitempty" validate:"omitempty"` // 交易哈希
FromAddress *string `json:"from_address,omitempty" validate:"omitempty"` // 來源地址
ToAddress *string `json:"to_address,omitempty" validate:"omitempty"` // 目標地址
ChainFee *decimal.Decimal `json:"chain_fee,omitempty" validate:"omitempty,decimalGte=0"` // 鏈上交易手續費
ChainFeeCrypto *string `json:"chain_fee_crypto,omitempty" validate:"omitempty"` // 鏈上手續費使用幣別
Memo *string `json:"memo,omitempty" validate:"omitempty"` // 鏈上備註
OrderNote *string `json:"order_note,omitempty" validate:"omitempty"`
BusinessID string `json:"business_id" validate:"required"` // 訂單業務流水號
OrderType int8 `json:"order_type" validate:"required"` // 訂單類型
OrderStatus int8 `json:"order_status" validate:"oneof=0 1 2 3 4 5 6 7 8 9 10 11"` // 訂單狀態
Brand string `json:"brand" validate:"required"` // 下單平台
OrderUID string `json:"order_uid" validate:"required"` // 下單用戶 UID
ReferenceID string `json:"reference_id" validate:"required"` // 訂單來源
Count string `json:"count" validate:"required,decimalGt=0"` // 訂單數量
OrderFee string `json:"order_fee" validate:"required,decimalGte=0"` // 訂單手續費
Amount string `json:"amount" validate:"required,decimalGte=0"` // 單價
ReferenceBrand *string `json:"reference_brand,omitempty" validate:"omitempty"` // 訂單來源平台
ReferenceUID *string `json:"reference_uid,omitempty" validate:"omitempty"` // 訂單來源用戶 UID
WalletStatus *int64 `json:"wallet_status,omitempty" validate:"omitempty,oneof=1 2 3 4 5 6 7"` // 交易金額狀態
ThreePartyStatus *int64 `json:"three_party_status,omitempty" validate:"omitempty,oneof=1 2 3"` // 三方請求狀態
DirectionType *int64 `json:"direction_type,omitempty" validate:"omitempty,oneof=1 2"` // 交易方向
CryptoType *string `json:"crypto_type,omitempty" validate:"omitempty"` // 交易幣種
ThirdPartyFee *string `json:"third_party_fee,omitempty" validate:"omitempty,decimalGte=0"` // 第三方手續費
CryptoToUSDTRate *string `json:"crypto_to_usdt_rate,omitempty" validate:"omitempty,decimalGte=0"` // 交易幣種對 USDT 匯率
FiatToUSDRate *string `json:"fiat_to_usd_rate,omitempty" validate:"omitempty,decimalGte=0"` // 法幣對 USD 匯率
FeeCryptoToUSDTRate *string `json:"fee_crypto_to_usdt_rate,omitempty" validate:"omitempty,decimalGte=0"` // 手續費幣種對 USDT 匯率
USDTToCryptoTypeRate *string `json:"usdt_to_crypto_type_rate,omitempty" validate:"omitempty,decimalGte=0"` // USDT 對交易幣種匯率
PaymentFiat *string `json:"payment_fiat,omitempty" validate:"omitempty"` // 支付法幣
PaymentUnitPrice *string `json:"payment_unit_price,omitempty" validate:"omitempty,decimalGte=0"` // crypto 單價
PaymentTemplateID *string `json:"payment_template_id,omitempty" validate:"omitempty"` // 支付方式配置 ID
OrderArrivalTime *int64 `json:"order_arrival_time,omitempty" validate:"omitempty,gt=1"` // 訂單到帳時間
OrderPaymentTime *int64 `json:"order_payment_time,omitempty" validate:"omitempty,gt=1"` // 訂單付款時間
UnpaidTimeoutSecond *int64 `json:"unpaid_timeout_second,omitempty" validate:"omitempty,gt=1"` // 支付期限秒數
ChainType *string `json:"chain_type,omitempty" validate:"omitempty"` // 主網類型
TxHash *string `json:"tx_hash,omitempty" validate:"omitempty"` // 交易哈希
FromAddress *string `json:"from_address,omitempty" validate:"omitempty"` // 來源地址
ToAddress *string `json:"to_address,omitempty" validate:"omitempty"` // 目標地址
ChainFee *string `json:"chain_fee,omitempty" validate:"omitempty,decimalGte=0"` // 鏈上交易手續費
ChainFeeCrypto *string `json:"chain_fee_crypto,omitempty" validate:"omitempty"` // 鏈上手續費使用幣別
Memo *string `json:"memo,omitempty" validate:"omitempty"` // 鏈上備註
OrderNote *string `json:"order_note,omitempty" validate:"omitempty"`
}
//nolint:gocyclo,gocognit
@ -76,25 +72,15 @@ func buildCreateOrderReq(in *trade.CreateOrderReq) (*createOrderReq, error) {
ReferenceID: in.ReferenceId,
}
var err error
// 只有當 Count, OrderFee, Amount 不為空時才進行轉換和設置
if in.Count != "" {
createOrderReq.Count, err = decimal.NewFromString(in.Count)
if err != nil {
return nil, err
}
createOrderReq.Count = in.Count
}
if in.OrderFee != "" {
createOrderReq.OrderFee, err = decimal.NewFromString(in.OrderFee)
if err != nil {
return nil, err
}
createOrderReq.OrderFee = in.OrderFee
}
if in.Amount != "" {
createOrderReq.Amount, err = decimal.NewFromString(in.Amount)
if err != nil {
return nil, err
}
createOrderReq.Amount = in.Amount
}
// 判斷可選字段,只有不為 nil 才設置
@ -117,25 +103,25 @@ func buildCreateOrderReq(in *trade.CreateOrderReq) (*createOrderReq, error) {
createOrderReq.CryptoType = in.CryptoType
}
if in.ThirdPartyFee != nil && *in.ThirdPartyFee != "" {
createOrderReq.ThirdPartyFee = decimalPtrFromString(*in.ThirdPartyFee)
createOrderReq.ThirdPartyFee = in.ThirdPartyFee
}
if in.CryptoToUsdtRate != nil && *in.CryptoToUsdtRate != "" {
createOrderReq.CryptoToUSDTRate = decimalPtrFromString(*in.CryptoToUsdtRate)
createOrderReq.CryptoToUSDTRate = in.CryptoToUsdtRate
}
if in.FiatToUsdRate != nil && *in.FiatToUsdRate != "" {
createOrderReq.FiatToUSDRate = decimalPtrFromString(*in.FiatToUsdRate)
createOrderReq.FiatToUSDRate = in.FiatToUsdRate
}
if in.FeeCryptoToUsdtRate != nil && *in.FeeCryptoToUsdtRate != "" {
createOrderReq.FeeCryptoToUSDTRate = decimalPtrFromString(*in.FeeCryptoToUsdtRate)
createOrderReq.FeeCryptoToUSDTRate = in.FeeCryptoToUsdtRate
}
if in.UsdtToCryptoTypeRate != nil && *in.UsdtToCryptoTypeRate != "" {
createOrderReq.USDTToCryptoTypeRate = decimalPtrFromString(*in.UsdtToCryptoTypeRate)
createOrderReq.USDTToCryptoTypeRate = in.UsdtToCryptoTypeRate
}
if in.PaymentFiat != nil {
createOrderReq.PaymentFiat = in.PaymentFiat
}
if in.PaymentUnitPrice != nil && *in.PaymentUnitPrice != "" {
createOrderReq.PaymentUnitPrice = decimalPtrFromString(*in.PaymentUnitPrice)
createOrderReq.PaymentUnitPrice = in.PaymentUnitPrice
}
if in.PaymentTemplateId != nil {
createOrderReq.PaymentTemplateID = in.PaymentTemplateId
@ -162,7 +148,7 @@ func buildCreateOrderReq(in *trade.CreateOrderReq) (*createOrderReq, error) {
createOrderReq.ToAddress = in.ToAddress
}
if in.ChainFee != nil && *in.ChainFee != "" {
createOrderReq.ChainFee = decimalPtrFromString(*in.ChainFee)
createOrderReq.ChainFee = in.ChainFee
}
if in.ChainFeeCrypto != nil {
createOrderReq.ChainFeeCrypto = in.ChainFeeCrypto
@ -178,6 +164,7 @@ func buildCreateOrderReq(in *trade.CreateOrderReq) (*createOrderReq, error) {
}
// toCreateOrderReq 將 createOrderReq 轉換為 CreateOrderReq
// toCreateOrderUseCase 將 createOrderReq 轉換為 CreateOrderReq
func toCreateOrderUseCase(req *createOrderReq) usecase.CreateOrderReq {
return usecase.CreateOrderReq{
BusinessID: req.BusinessID,
@ -186,22 +173,22 @@ func toCreateOrderUseCase(req *createOrderReq) usecase.CreateOrderReq {
Brand: req.Brand,
OrderUID: req.OrderUID,
ReferenceID: req.ReferenceID,
Count: req.Count,
OrderFee: req.OrderFee,
Amount: req.Amount,
WalletStatus: *req.WalletStatus,
DirectionType: *req.DirectionType,
Count: *stringToDecimalPtr(&req.Count),
OrderFee: *stringToDecimalPtr(&req.OrderFee),
Amount: *stringToDecimalPtr(&req.Amount),
WalletStatus: getInt64Value(req.WalletStatus),
DirectionType: getInt64Value(req.DirectionType),
ReferenceBrand: req.ReferenceBrand,
ReferenceUID: req.ReferenceUID,
ThreePartyStatus: req.ThreePartyStatus,
CryptoType: req.CryptoType,
ThirdPartyFee: req.ThirdPartyFee,
CryptoToUSDTRate: req.CryptoToUSDTRate,
FiatToUSDRate: req.FiatToUSDRate,
FeeCryptoToUSDTRate: req.FeeCryptoToUSDTRate,
USDTToCryptoTypeRate: req.USDTToCryptoTypeRate,
ThirdPartyFee: stringToDecimalPtr(req.ThirdPartyFee),
CryptoToUSDTRate: stringToDecimalPtr(req.CryptoToUSDTRate),
FiatToUSDRate: stringToDecimalPtr(req.FiatToUSDRate),
FeeCryptoToUSDTRate: stringToDecimalPtr(req.FeeCryptoToUSDTRate),
USDTToCryptoTypeRate: stringToDecimalPtr(req.USDTToCryptoTypeRate),
PaymentFiat: req.PaymentFiat,
PaymentUnitPrice: req.PaymentUnitPrice,
PaymentUnitPrice: stringToDecimalPtr(req.PaymentUnitPrice),
PaymentTemplateID: req.PaymentTemplateID,
OrderArrivalTime: req.OrderArrivalTime,
OrderPaymentTime: req.OrderPaymentTime,
@ -210,7 +197,7 @@ func toCreateOrderUseCase(req *createOrderReq) usecase.CreateOrderReq {
TxHash: req.TxHash,
FromAddress: req.FromAddress,
ToAddress: req.ToAddress,
ChainFee: req.ChainFee,
ChainFee: stringToDecimalPtr(req.ChainFee),
ChainFeeCrypto: req.ChainFeeCrypto,
Memo: req.Memo,
OrderNote: req.OrderNote,
@ -219,6 +206,7 @@ func toCreateOrderUseCase(req *createOrderReq) usecase.CreateOrderReq {
// CreateOrder 建立訂單
func (l *CreateOrderLogic) CreateOrder(in *trade.CreateOrderReq) (*trade.OKResp, error) {
req, err := buildCreateOrderReq(in)
if err != nil {
// 錯誤代碼 06-011-00

View File

@ -18,3 +18,25 @@ func decimalPtrFromString(val string) *decimal.Decimal {
return &dec
}
// getInt64Value 將 *int64 的值返回,如果為 nil 則返回 0
func getInt64Value(val *int64) int64 {
if val == nil {
return 0
}
return *val
}
// stringToDecimalPtr 將 *string 轉換為 *decimal.Decimal如果解析失敗或值為 nil 則返回 nil
func stringToDecimalPtr(s *string) *decimal.Decimal {
if s == nil {
return nil
}
dec, err := decimal.NewFromString(*s)
if err != nil {
return nil // 解析失敗時返回 nil或根據需求記錄錯誤
}
return &dec
}

View File

@ -67,6 +67,7 @@ func (o *OrderUseCase) CreateOrder(ctx context.Context, param usecase.CreateOrde
Memo: param.Memo,
OrderNote: param.OrderNote,
}
err := o.OrderModel.Insert(ctx, order)
if err != nil {

View File

@ -14,7 +14,6 @@ import (
"github.com/zeromicro/go-zero/core/stores/mon"
"go.uber.org/mock/gomock"
"reflect"
"testing"
)
@ -499,30 +498,3 @@ func TestOrderUseCase_OrderStatusTimeout(t *testing.T) {
})
}
}
func TestOrderUseCase_convertOrdersToResponses(t *testing.T) {
type fields struct {
OrderModel model.OrderModel
}
type args struct {
orders []model.Order
}
tests := []struct {
name string
fields fields
args args
want []*usecase.GetOrderResp
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &OrderUseCase{
OrderModel: tt.fields.OrderModel,
}
if got := o.convertOrdersToResponses(tt.args.orders); !reflect.DeepEqual(got, tt.want) {
t.Errorf("convertOrdersToResponses() = %v, want %v", got, tt.want)
}
})
}
}