From 8596d56fc7a2ec037acea757fe6580efb975ff83 Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Mon, 28 Oct 2024 00:07:24 +0800 Subject: [PATCH] add order service logic --- .golangci.yaml | 5 +++++ internal/domain/usecase/order.go | 10 ++++----- .../logic/orderservice/cancel_order_logic.go | 3 ++- .../logic/orderservice/create_order_logic.go | 3 ++- .../logic/orderservice/delete_order_logic.go | 3 ++- .../logic/orderservice/get_order_logic.go | 13 ++++++----- .../logic/orderservice/list_order_logic.go | 13 ++++++----- .../orderservice/modify_order_status_logic.go | 3 ++- .../order_status_timeout_logic.go | 2 +- internal/usecase/order.go | 22 ++++++++++--------- internal/usecase/order_test.go | 13 ++++++----- internal/usecase/utils_test.go | 3 ++- 12 files changed, 54 insertions(+), 39 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index c9726ca..4bb078b 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -119,6 +119,11 @@ issues: exclude-dirs: - internal/model + - internal/logic/couponservice + - internal/logic/inventoryservice + - internal/logic/productservice + - internal/logic/subscriptionservice + - internal/logic/walletservice exclude-files: - .*_test.go diff --git a/internal/domain/usecase/order.go b/internal/domain/usecase/order.go index 6e6760b..6d1db4f 100644 --- a/internal/domain/usecase/order.go +++ b/internal/domain/usecase/order.go @@ -52,17 +52,17 @@ type GetOrderQuery struct { } type GetOrderResp struct { - BusinessId string // 訂單業務流水號 + BusinessID string // 訂單業務流水號 OrderType domain.OrderType `json:"order_type"` // 訂單類型 OrderStatus domain.OrderStatus `json:"order_status"` // 訂單狀態 Brand string `json:"brand"` // 下單平台 - OrderUid string `json:"order_uid"` // 下單用戶 UID - ReferenceId string `json:"reference_id"` // 訂單來源 + OrderUID string `json:"order_uid"` // 下單用戶 UID + ReferenceID string `json:"reference_id"` // 訂單來源 Count string `json:"count"` // 訂單數量 (decimal to string) OrderFee string `json:"order_fee"` // 訂單手續費 (decimal to string) Amount string `json:"amount"` // 單價 (decimal to string) ReferenceBrand *string `json:"reference_brand,omitempty"` // 訂單來源平台 - ReferenceUid *string `json:"reference_uid,omitempty"` // 訂單來源用戶 UID + ReferenceUID *string `json:"reference_uid,omitempty"` // 訂單來源用戶 UID WalletStatus *int64 `json:"wallet_status,omitempty"` // 交易金額狀態 ThreePartyStatus *int64 `json:"three_party_status,omitempty"` // 三方請求狀態 DirectionType *int64 `json:"direction_type,omitempty"` // 交易方向 @@ -74,7 +74,7 @@ type GetOrderResp struct { UsdtToCryptoTypeRate *string `json:"usdt_to_crypto_type_rate,omitempty"` // USDT 對交易幣種匯率 (decimal to string) PaymentFiat *string `json:"payment_fiat,omitempty"` // 支付法幣 PaymentUnitPrice *string `json:"payment_unit_price,omitempty"` // crypto 單價 (decimal to string) - PaymentTemplateId *string `json:"payment_template_id,omitempty"` // 支付方式配置 ID + PaymentTemplateID *string `json:"payment_template_id,omitempty"` // 支付方式配置 ID OrderArrivalTime *int64 `json:"order_arrival_time,omitempty"` // 訂單到帳時間 OrderPaymentTime *int64 `json:"order_payment_time,omitempty"` // 訂單付款時間 UnpaidTimeoutSecond *int64 `json:"unpaid_timeout_second,omitempty"` // 支付期限秒數 diff --git a/internal/logic/orderservice/cancel_order_logic.go b/internal/logic/orderservice/cancel_order_logic.go index 76e6b5a..cd499a6 100644 --- a/internal/logic/orderservice/cancel_order_logic.go +++ b/internal/logic/orderservice/cancel_order_logic.go @@ -3,9 +3,10 @@ package orderservicelogic import ( "app-cloudep-trade-service/internal/domain" "app-cloudep-trade-service/internal/domain/usecase" - ers "code.30cm.net/digimon/library-go/errs" "context" + ers "code.30cm.net/digimon/library-go/errs" + "app-cloudep-trade-service/gen_result/pb/trade" "app-cloudep-trade-service/internal/svc" diff --git a/internal/logic/orderservice/create_order_logic.go b/internal/logic/orderservice/create_order_logic.go index 0cb2546..c95a3d6 100644 --- a/internal/logic/orderservice/create_order_logic.go +++ b/internal/logic/orderservice/create_order_logic.go @@ -3,8 +3,9 @@ package orderservicelogic import ( "app-cloudep-trade-service/internal/domain" "app-cloudep-trade-service/internal/domain/usecase" - ers "code.30cm.net/digimon/library-go/errs" "context" + + ers "code.30cm.net/digimon/library-go/errs" "github.com/shopspring/decimal" "app-cloudep-trade-service/gen_result/pb/trade" diff --git a/internal/logic/orderservice/delete_order_logic.go b/internal/logic/orderservice/delete_order_logic.go index c58e4fc..2b804e5 100644 --- a/internal/logic/orderservice/delete_order_logic.go +++ b/internal/logic/orderservice/delete_order_logic.go @@ -2,9 +2,10 @@ package orderservicelogic import ( "app-cloudep-trade-service/internal/domain/usecase" - ers "code.30cm.net/digimon/library-go/errs" "context" + ers "code.30cm.net/digimon/library-go/errs" + "app-cloudep-trade-service/gen_result/pb/trade" "app-cloudep-trade-service/internal/svc" diff --git a/internal/logic/orderservice/get_order_logic.go b/internal/logic/orderservice/get_order_logic.go index 6afabf7..967fa81 100644 --- a/internal/logic/orderservice/get_order_logic.go +++ b/internal/logic/orderservice/get_order_logic.go @@ -2,9 +2,10 @@ package orderservicelogic import ( "app-cloudep-trade-service/internal/domain/usecase" - ers "code.30cm.net/digimon/library-go/errs" "context" + ers "code.30cm.net/digimon/library-go/errs" + "app-cloudep-trade-service/gen_result/pb/trade" "app-cloudep-trade-service/internal/svc" @@ -49,18 +50,18 @@ func (l *GetOrderLogic) GetOrder(in *trade.GetOrderReq) (*trade.GetOrderResp, er return &trade.GetOrderResp{ UpdateTime: o.UpdateTime, CreateTime: o.CreateTime, - BusinessId: o.BusinessId, + BusinessId: o.BusinessID, OrderType: int32(o.OrderType), OrderStatus: int32(o.OrderStatus), Brand: o.Brand, - OrderUid: o.OrderUid, - ReferenceId: o.ReferenceId, + OrderUid: o.OrderUID, + ReferenceId: o.ReferenceID, Count: o.Count, OrderFee: o.OrderFee, Amount: o.Amount, // 下面的為未來擴充用的欄位 ReferenceBrand: o.ReferenceBrand, - ReferenceUid: o.ReferenceUid, + ReferenceUid: o.ReferenceUID, WalletStatus: o.WalletStatus, ThreePartyStatus: o.ThreePartyStatus, DirectionType: o.DirectionType, @@ -72,7 +73,7 @@ func (l *GetOrderLogic) GetOrder(in *trade.GetOrderReq) (*trade.GetOrderResp, er UsdtToCryptoTypeRate: o.UsdtToCryptoTypeRate, PaymentFiat: o.PaymentFiat, PaymentUnitPrice: o.PaymentUnitPrice, - PaymentTemplateId: o.PaymentTemplateId, + PaymentTemplateId: o.PaymentTemplateID, OrderArrivalTime: o.OrderArrivalTime, OrderPaymentTime: o.OrderPaymentTime, UnpaidTimeoutSecond: o.UnpaidTimeoutSecond, diff --git a/internal/logic/orderservice/list_order_logic.go b/internal/logic/orderservice/list_order_logic.go index ec5eb50..b68bfcd 100644 --- a/internal/logic/orderservice/list_order_logic.go +++ b/internal/logic/orderservice/list_order_logic.go @@ -3,9 +3,10 @@ package orderservicelogic import ( "app-cloudep-trade-service/internal/domain" "app-cloudep-trade-service/internal/domain/usecase" - ers "code.30cm.net/digimon/library-go/errs" "context" + ers "code.30cm.net/digimon/library-go/errs" + "app-cloudep-trade-service/gen_result/pb/trade" "app-cloudep-trade-service/internal/svc" @@ -107,17 +108,17 @@ func (l *ListOrderLogic) ListOrder(in *trade.ListOrderReq) (*trade.ListOrderResp data := make([]*trade.GetOrderResp, 0, len(order.Data)) for _, item := range order.Data { data = append(data, &trade.GetOrderResp{ - BusinessId: item.BusinessId, + BusinessId: item.BusinessID, OrderType: int32(item.OrderType), OrderStatus: int32(item.OrderStatus), Brand: item.Brand, - OrderUid: item.OrderUid, - ReferenceId: item.ReferenceId, + OrderUid: item.OrderUID, + ReferenceId: item.ReferenceID, Count: item.Count, OrderFee: item.OrderFee, Amount: item.Amount, ReferenceBrand: item.ReferenceBrand, - ReferenceUid: item.ReferenceUid, + ReferenceUid: item.ReferenceUID, WalletStatus: item.WalletStatus, ThreePartyStatus: item.ThreePartyStatus, DirectionType: item.DirectionType, @@ -129,7 +130,7 @@ func (l *ListOrderLogic) ListOrder(in *trade.ListOrderReq) (*trade.ListOrderResp UsdtToCryptoTypeRate: item.UsdtToCryptoTypeRate, PaymentFiat: item.PaymentFiat, PaymentUnitPrice: item.PaymentUnitPrice, - PaymentTemplateId: item.PaymentTemplateId, + PaymentTemplateId: item.PaymentTemplateID, OrderArrivalTime: item.OrderArrivalTime, OrderPaymentTime: item.OrderPaymentTime, UnpaidTimeoutSecond: item.UnpaidTimeoutSecond, diff --git a/internal/logic/orderservice/modify_order_status_logic.go b/internal/logic/orderservice/modify_order_status_logic.go index a2cad15..47c6b1e 100644 --- a/internal/logic/orderservice/modify_order_status_logic.go +++ b/internal/logic/orderservice/modify_order_status_logic.go @@ -2,9 +2,10 @@ package orderservicelogic import ( "app-cloudep-trade-service/internal/domain/usecase" - ers "code.30cm.net/digimon/library-go/errs" "context" + ers "code.30cm.net/digimon/library-go/errs" + "app-cloudep-trade-service/gen_result/pb/trade" "app-cloudep-trade-service/internal/svc" diff --git a/internal/logic/orderservice/order_status_timeout_logic.go b/internal/logic/orderservice/order_status_timeout_logic.go index 7282fb9..ae15f9c 100644 --- a/internal/logic/orderservice/order_status_timeout_logic.go +++ b/internal/logic/orderservice/order_status_timeout_logic.go @@ -24,7 +24,7 @@ func NewOrderStatusTimeoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) } // OrderStatusTimeout 訂單超時任務/cron/order-status/timeout -func (l *OrderStatusTimeoutLogic) OrderStatusTimeout(in *trade.OrderStatusTimeoutReq) (*trade.OKResp, error) { +func (l *OrderStatusTimeoutLogic) OrderStatusTimeout(_ *trade.OrderStatusTimeoutReq) (*trade.OKResp, error) { err := l.svcCtx.OrderUseCase.OrderStatusTimeout(l.ctx) if err != nil { return nil, err diff --git a/internal/usecase/order.go b/internal/usecase/order.go index be34a79..63b9ada 100644 --- a/internal/usecase/order.go +++ b/internal/usecase/order.go @@ -126,23 +126,24 @@ func (o *OrderUseCase) GetOrder(ctx context.Context, param usecase.GetOrderQuery if errors.Is(mon.ErrNotFound, err) { return nil, domain.NotFoundError(domain.DataNotFoundErrorCode, "failed to get this order id:", param.BusinessID) } + return nil, err } resp := &usecase.GetOrderResp{ UpdateTime: order.UpdateTime, CreateTime: order.CreateTime, - BusinessId: order.BusinessID, + BusinessID: order.BusinessID, OrderType: order.OrderType, OrderStatus: order.OrderStatus, Brand: order.Brand, - OrderUid: order.OrderUID, - ReferenceId: order.ReferenceID, + OrderUID: order.OrderUID, + ReferenceID: order.ReferenceID, Count: order.Count.String(), OrderFee: order.OrderFee.String(), Amount: order.Amount.String(), ReferenceBrand: order.ReferenceBrand, - ReferenceUid: order.ReferenceUID, + ReferenceUID: order.ReferenceUID, WalletStatus: Int64Ptr(order.WalletStatus), ThreePartyStatus: order.ThreePartyStatus, DirectionType: Int64Ptr(order.DirectionType), @@ -154,7 +155,7 @@ func (o *OrderUseCase) GetOrder(ctx context.Context, param usecase.GetOrderQuery UsdtToCryptoTypeRate: DecimalToStringPtr(order.USDTToCryptoTypeRate), PaymentFiat: order.PaymentFiat, PaymentUnitPrice: DecimalToStringPtr(order.PaymentUnitPrice), - PaymentTemplateId: order.PaymentTemplateID, + PaymentTemplateID: order.PaymentTemplateID, OrderArrivalTime: order.OrderArrivalTime, OrderPaymentTime: order.OrderPaymentTime, UnpaidTimeoutSecond: order.UnpaidTimeoutSecond, @@ -266,17 +267,17 @@ func (o *OrderUseCase) convertOrdersToResponses(orders []model.Order) []*usecase resp := &usecase.GetOrderResp{ UpdateTime: order.UpdateTime, CreateTime: order.CreateTime, - BusinessId: order.BusinessID, + BusinessID: order.BusinessID, OrderType: order.OrderType, OrderStatus: order.OrderStatus, Brand: order.Brand, - OrderUid: order.OrderUID, - ReferenceId: order.ReferenceID, + OrderUID: order.OrderUID, + ReferenceID: order.ReferenceID, Count: order.Count.String(), OrderFee: order.OrderFee.String(), Amount: order.Amount.String(), ReferenceBrand: order.ReferenceBrand, - ReferenceUid: order.ReferenceUID, + ReferenceUID: order.ReferenceUID, WalletStatus: Int64Ptr(order.WalletStatus), ThreePartyStatus: order.ThreePartyStatus, DirectionType: Int64Ptr(order.DirectionType), @@ -288,7 +289,7 @@ func (o *OrderUseCase) convertOrdersToResponses(orders []model.Order) []*usecase UsdtToCryptoTypeRate: DecimalToStringPtr(order.USDTToCryptoTypeRate), PaymentFiat: order.PaymentFiat, PaymentUnitPrice: DecimalToStringPtr(order.PaymentUnitPrice), - PaymentTemplateId: order.PaymentTemplateID, + PaymentTemplateID: order.PaymentTemplateID, OrderArrivalTime: order.OrderArrivalTime, OrderPaymentTime: order.OrderPaymentTime, UnpaidTimeoutSecond: order.UnpaidTimeoutSecond, @@ -303,5 +304,6 @@ func (o *OrderUseCase) convertOrdersToResponses(orders []model.Order) []*usecase } res = append(res, resp) } + return res } diff --git a/internal/usecase/order_test.go b/internal/usecase/order_test.go index dde64d7..846d3ce 100644 --- a/internal/usecase/order_test.go +++ b/internal/usecase/order_test.go @@ -7,11 +7,12 @@ import ( model "app-cloudep-trade-service/internal/model/mongo" "context" "errors" + "time" + "github.com/go-playground/assert/v2" "github.com/shopspring/decimal" "github.com/zeromicro/go-zero/core/stores/mon" "go.uber.org/mock/gomock" - "time" "reflect" "testing" @@ -266,23 +267,23 @@ func TestOrderUseCase_GetOrder(t *testing.T) { wantResp: &usecase.GetOrderResp{ // 构建预期响应 UpdateTime: mockOrder.UpdateTime, CreateTime: mockOrder.CreateTime, - BusinessId: mockOrder.BusinessID, + BusinessID: mockOrder.BusinessID, OrderType: mockOrder.OrderType, OrderStatus: mockOrder.OrderStatus, Brand: mockOrder.Brand, - OrderUid: mockOrder.OrderUID, - ReferenceId: mockOrder.ReferenceID, + OrderUID: mockOrder.OrderUID, + ReferenceID: mockOrder.ReferenceID, Count: mockOrder.Count.String(), OrderFee: mockOrder.OrderFee.String(), Amount: mockOrder.Amount.String(), ReferenceBrand: mockOrder.ReferenceBrand, - ReferenceUid: mockOrder.ReferenceUID, + ReferenceUID: mockOrder.ReferenceUID, WalletStatus: Int64Ptr(mockOrder.WalletStatus), ThreePartyStatus: mockOrder.ThreePartyStatus, DirectionType: Int64Ptr(mockOrder.DirectionType), CryptoType: mockOrder.CryptoType, PaymentFiat: mockOrder.PaymentFiat, - PaymentTemplateId: mockOrder.PaymentTemplateID, + PaymentTemplateID: mockOrder.PaymentTemplateID, OrderArrivalTime: mockOrder.OrderArrivalTime, OrderPaymentTime: mockOrder.OrderPaymentTime, UnpaidTimeoutSecond: mockOrder.UnpaidTimeoutSecond, diff --git a/internal/usecase/utils_test.go b/internal/usecase/utils_test.go index f4fa7e4..f18c5a1 100644 --- a/internal/usecase/utils_test.go +++ b/internal/usecase/utils_test.go @@ -1,9 +1,10 @@ package usecase import ( + "testing" + "github.com/go-playground/assert/v2" "github.com/shopspring/decimal" - "testing" ) // 測試 Int64Ptr 函數