app-cloudep-trade-service/internal/logic/orderservice/create_order_logic.go

225 lines
10 KiB
Go
Raw Normal View History

2024-10-25 10:17:27 +00:00
package orderservicelogic
import (
2024-10-27 17:27:25 +00:00
"app-cloudep-trade-service/gen_result/pb/trade"
2024-10-27 16:01:55 +00:00
"app-cloudep-trade-service/internal/domain"
"app-cloudep-trade-service/internal/domain/usecase"
2024-10-25 10:17:27 +00:00
"app-cloudep-trade-service/internal/svc"
2024-10-27 17:27:25 +00:00
"context"
2024-10-27 17:54:05 +00:00
ers "code.30cm.net/digimon/library-go/errs"
2024-10-25 10:17:27 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type CreateOrderLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
return &CreateOrderLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
2024-10-27 16:01:55 +00:00
type createOrderReq struct { // 訂單ID
2024-10-27 17:27:25 +00:00
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"`
2024-10-27 16:01:55 +00:00
}
2024-10-27 17:54:05 +00:00
//nolint:gocyclo
func buildCreateOrderReq(in *trade.CreateOrderReq) *createOrderReq {
2024-10-27 16:01:55 +00:00
createOrderReq := &createOrderReq{
BusinessID: in.BusinessId,
OrderType: int8(in.OrderType),
OrderStatus: int8(in.OrderStatus),
Brand: in.Brand,
OrderUID: in.OrderUid,
ReferenceID: in.ReferenceId,
}
// 只有當 Count, OrderFee, Amount 不為空時才進行轉換和設置
if in.Count != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.Count = in.Count
2024-10-27 16:01:55 +00:00
}
if in.OrderFee != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.OrderFee = in.OrderFee
2024-10-27 16:01:55 +00:00
}
if in.Amount != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.Amount = in.Amount
2024-10-27 16:01:55 +00:00
}
// 判斷可選字段,只有不為 nil 才設置
if in.ReferenceBrand != nil {
createOrderReq.ReferenceBrand = in.ReferenceBrand
}
if in.ReferenceUid != nil {
createOrderReq.ReferenceUID = in.ReferenceUid
}
if in.WalletStatus != nil {
createOrderReq.WalletStatus = in.WalletStatus
}
if in.ThreePartyStatus != nil {
createOrderReq.ThreePartyStatus = in.ThreePartyStatus
}
if in.DirectionType != nil {
createOrderReq.DirectionType = in.DirectionType
}
if in.CryptoType != nil {
createOrderReq.CryptoType = in.CryptoType
}
if in.ThirdPartyFee != nil && *in.ThirdPartyFee != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.ThirdPartyFee = in.ThirdPartyFee
2024-10-27 16:01:55 +00:00
}
if in.CryptoToUsdtRate != nil && *in.CryptoToUsdtRate != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.CryptoToUSDTRate = in.CryptoToUsdtRate
2024-10-27 16:01:55 +00:00
}
if in.FiatToUsdRate != nil && *in.FiatToUsdRate != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.FiatToUSDRate = in.FiatToUsdRate
2024-10-27 16:01:55 +00:00
}
if in.FeeCryptoToUsdtRate != nil && *in.FeeCryptoToUsdtRate != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.FeeCryptoToUSDTRate = in.FeeCryptoToUsdtRate
2024-10-27 16:01:55 +00:00
}
if in.UsdtToCryptoTypeRate != nil && *in.UsdtToCryptoTypeRate != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.USDTToCryptoTypeRate = in.UsdtToCryptoTypeRate
2024-10-27 16:01:55 +00:00
}
if in.PaymentFiat != nil {
createOrderReq.PaymentFiat = in.PaymentFiat
}
if in.PaymentUnitPrice != nil && *in.PaymentUnitPrice != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.PaymentUnitPrice = in.PaymentUnitPrice
2024-10-27 16:01:55 +00:00
}
if in.PaymentTemplateId != nil {
createOrderReq.PaymentTemplateID = in.PaymentTemplateId
}
if in.OrderArrivalTime != nil {
createOrderReq.OrderArrivalTime = in.OrderArrivalTime
}
if in.OrderPaymentTime != nil {
createOrderReq.OrderPaymentTime = in.OrderPaymentTime
}
if in.UnpaidTimeoutSecond != nil {
createOrderReq.UnpaidTimeoutSecond = in.UnpaidTimeoutSecond
}
if in.ChainType != nil {
createOrderReq.ChainType = in.ChainType
}
if in.TxHash != nil {
createOrderReq.TxHash = in.TxHash
}
if in.FromAddress != nil {
createOrderReq.FromAddress = in.FromAddress
}
if in.ToAddress != nil {
createOrderReq.ToAddress = in.ToAddress
}
if in.ChainFee != nil && *in.ChainFee != "" {
2024-10-27 17:27:25 +00:00
createOrderReq.ChainFee = in.ChainFee
2024-10-27 16:01:55 +00:00
}
if in.ChainFeeCrypto != nil {
createOrderReq.ChainFeeCrypto = in.ChainFeeCrypto
}
if in.Memo != nil {
createOrderReq.Memo = in.Memo
}
if in.OrderNote != nil {
createOrderReq.OrderNote = in.OrderNote
}
2024-10-27 17:54:05 +00:00
return createOrderReq
2024-10-27 16:01:55 +00:00
}
// toCreateOrderReq 將 createOrderReq 轉換為 CreateOrderReq
2024-10-27 17:27:25 +00:00
// toCreateOrderUseCase 將 createOrderReq 轉換為 CreateOrderReq
2024-10-27 16:01:55 +00:00
func toCreateOrderUseCase(req *createOrderReq) usecase.CreateOrderReq {
return usecase.CreateOrderReq{
BusinessID: req.BusinessID,
OrderType: domain.OrderType(req.OrderType),
OrderStatus: domain.OrderStatus(req.OrderStatus),
Brand: req.Brand,
OrderUID: req.OrderUID,
ReferenceID: req.ReferenceID,
2024-10-27 17:27:25 +00:00
Count: *stringToDecimalPtr(&req.Count),
OrderFee: *stringToDecimalPtr(&req.OrderFee),
Amount: *stringToDecimalPtr(&req.Amount),
WalletStatus: getInt64Value(req.WalletStatus),
DirectionType: getInt64Value(req.DirectionType),
2024-10-27 16:01:55 +00:00
ReferenceBrand: req.ReferenceBrand,
ReferenceUID: req.ReferenceUID,
ThreePartyStatus: req.ThreePartyStatus,
CryptoType: req.CryptoType,
2024-10-27 17:27:25 +00:00
ThirdPartyFee: stringToDecimalPtr(req.ThirdPartyFee),
CryptoToUSDTRate: stringToDecimalPtr(req.CryptoToUSDTRate),
FiatToUSDRate: stringToDecimalPtr(req.FiatToUSDRate),
FeeCryptoToUSDTRate: stringToDecimalPtr(req.FeeCryptoToUSDTRate),
USDTToCryptoTypeRate: stringToDecimalPtr(req.USDTToCryptoTypeRate),
2024-10-27 16:01:55 +00:00
PaymentFiat: req.PaymentFiat,
2024-10-27 17:27:25 +00:00
PaymentUnitPrice: stringToDecimalPtr(req.PaymentUnitPrice),
2024-10-27 16:01:55 +00:00
PaymentTemplateID: req.PaymentTemplateID,
OrderArrivalTime: req.OrderArrivalTime,
OrderPaymentTime: req.OrderPaymentTime,
UnpaidTimeoutSecond: req.UnpaidTimeoutSecond,
ChainType: req.ChainType,
TxHash: req.TxHash,
FromAddress: req.FromAddress,
ToAddress: req.ToAddress,
2024-10-27 17:27:25 +00:00
ChainFee: stringToDecimalPtr(req.ChainFee),
2024-10-27 16:01:55 +00:00
ChainFeeCrypto: req.ChainFeeCrypto,
Memo: req.Memo,
OrderNote: req.OrderNote,
}
}
2024-10-25 10:17:27 +00:00
// CreateOrder 建立訂單
func (l *CreateOrderLogic) CreateOrder(in *trade.CreateOrderReq) (*trade.OKResp, error) {
2024-10-27 17:54:05 +00:00
req := buildCreateOrderReq(in)
2024-10-27 16:01:55 +00:00
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(req); err != nil {
// 錯誤代碼 06-011-00
return nil, ers.InvalidFormat(err.Error())
}
2024-10-27 17:54:05 +00:00
err := l.svcCtx.OrderUseCase.CreateOrder(l.ctx, toCreateOrderUseCase(req))
2024-10-27 16:01:55 +00:00
if err != nil {
return nil, err
}
2024-10-25 10:17:27 +00:00
return &trade.OKResp{}, nil
}