From 73f91be6497432316130f3c637867b5e8f8013ee Mon Sep 17 00:00:00 2001 From: "daniel.w" Date: Sat, 12 Oct 2024 21:56:59 +0800 Subject: [PATCH] gplint --- generate/protobuf/order.proto | 4 +- .../logic/orderservice/cancel_order_logic.go | 6 +- .../logic/orderservice/create_order_logic.go | 89 +++++++++--------- .../logic/orderservice/delete_order_logic.go | 6 +- .../logic/orderservice/get_order_logic.go | 80 ++++++++-------- .../logic/orderservice/list_order_logic.go | 94 +++++++++---------- .../logic/orderservice/modify_order_logic.go | 6 +- .../orderservice/modify_order_status_logic.go | 6 +- .../order_status_timeout_logic.go | 6 +- internal/mock/model/order_model.go | 90 ++++++++++++++++-- internal/mock/model/order_model_gen.go | 14 +-- .../orderservice/order_service_server.go | 20 ++-- internal/svc/init_validate.go | 55 +++++++++++ internal/svc/service_context.go | 2 +- order.go | 4 +- 15 files changed, 309 insertions(+), 173 deletions(-) create mode 100644 internal/svc/init_validate.go diff --git a/generate/protobuf/order.proto b/generate/protobuf/order.proto index c4a9792..0b04a0d 100644 --- a/generate/protobuf/order.proto +++ b/generate/protobuf/order.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -package tweeting; -option go_package = "./tweeting"; +package order; +option go_package = "./order"; // ========== 基本回應 =========== message OKResp {} diff --git a/internal/logic/orderservice/cancel_order_logic.go b/internal/logic/orderservice/cancel_order_logic.go index 80b242e..d2c8e56 100644 --- a/internal/logic/orderservice/cancel_order_logic.go +++ b/internal/logic/orderservice/cancel_order_logic.go @@ -1,13 +1,13 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "app-cloudep-order-server/internal/domain" model "app-cloudep-order-server/internal/model/mongo" "context" ers "code.30cm.net/digimon/library-go/errs" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -34,7 +34,7 @@ type CancelOrderQuery struct { } // CancelOrder 取消訂單 -func (l *CancelOrderLogic) CancelOrder(in *tweeting.CancelOrderReq) (*tweeting.OKResp, error) { +func (l *CancelOrderLogic) CancelOrder(in *order.CancelOrderReq) (*order.OKResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&CancelOrderQuery{ BusinessID: in.GetBusinessId(), @@ -64,5 +64,5 @@ func (l *CancelOrderLogic) CancelOrder(in *tweeting.CancelOrderReq) (*tweeting.O return nil, e } - return &tweeting.OKResp{}, nil + return &order.OKResp{}, nil } diff --git a/internal/logic/orderservice/create_order_logic.go b/internal/logic/orderservice/create_order_logic.go index ccc8046..d415f7a 100644 --- a/internal/logic/orderservice/create_order_logic.go +++ b/internal/logic/orderservice/create_order_logic.go @@ -1,15 +1,16 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "app-cloudep-order-server/internal/domain" model "app-cloudep-order-server/internal/model/mongo" "context" + "fmt" "time" ers "code.30cm.net/digimon/library-go/errs" "github.com/shopspring/decimal" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -30,55 +31,59 @@ 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:"required,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,gt=0"` // 訂單數量 - OrderFee decimal.Decimal `json:"order_fee" validate:"required,gte=0"` // 訂單手續費 - Amount decimal.Decimal `json:"amount" validate:"required,gte=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,gte=0"` // 第三方手續費 - CryptoToUSDTRate *decimal.Decimal `json:"crypto_to_usdt_rate,omitempty" validate:"omitempty,gte=0"` // 交易幣種對 USDT 匯率 - FiatToUSDRate *decimal.Decimal `json:"fiat_to_usd_rate,omitempty" validate:"omitempty,gte=0"` // 法幣對 USD 匯率 - FeeCryptoToUSDTRate *decimal.Decimal `json:"fee_crypto_to_usdt_rate,omitempty" validate:"omitempty,gte=0"` // 手續費幣種對 USDT 匯率 - USDTToCryptoTypeRate *decimal.Decimal `json:"usdt_to_crypto_type_rate,omitempty" validate:"omitempty,gte=0"` // USDT 對交易幣種匯率 - PaymentFiat *string `json:"payment_fiat,omitempty" validate:"omitempty"` // 支付法幣 - PaymentUnitPrice *decimal.Decimal `json:"payment_unit_price,omitempty" validate:"omitempty,gte=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,gte=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,gte=0"` // 鏈上交易手續費 - ChainFeeCrypto *string `json:"chain_fee_crypto,omitempty" validate:"omitempty"` // 鏈上手續費使用幣別 - Memo *string `json:"memo,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 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"` } // CreateOrder 建立訂單 -func (l *CreateOrderLogic) CreateOrder(in *tweeting.CreateOrderReq) (*tweeting.OKResp, error) { +func (l *CreateOrderLogic) CreateOrder(in *order.CreateOrderReq) (*order.OKResp, error) { req, err := buildCreateOrderReq(in) if err != nil { // 錯誤代碼 06-011-00 - return nil, ers.InvalidFormat(err.Error()) - } - // 驗證資料 - if err := l.svcCtx.Validate.ValidateAll(&req); err != nil { - // 錯誤代碼 06-011-00 + fmt.Println("1", err) return nil, ers.InvalidFormat(err.Error()) } + fmt.Println(req) + // 驗證資料 + if err := l.svcCtx.Validate.ValidateAll(req); err != nil { + // 錯誤代碼 06-011-00 + fmt.Println("2", err) + return nil, ers.InvalidFormat(err.Error()) + } + fmt.Println(req) now := time.Now().UTC().UnixNano() // 插入資料庫 o := &model.Order{ @@ -137,11 +142,11 @@ func (l *CreateOrderLogic) CreateOrder(in *tweeting.CreateOrderReq) (*tweeting.O return nil, e } - return &tweeting.OKResp{}, nil + return &order.OKResp{}, nil } //nolint:gocyclo,gocognit -func buildCreateOrderReq(in *tweeting.CreateOrderReq) (*createOrderReq, error) { +func buildCreateOrderReq(in *order.CreateOrderReq) (*createOrderReq, error) { createOrderReq := &createOrderReq{ BusinessID: in.BusinessId, OrderType: int8(in.OrderType), diff --git a/internal/logic/orderservice/delete_order_logic.go b/internal/logic/orderservice/delete_order_logic.go index 89c0fa0..a678a02 100644 --- a/internal/logic/orderservice/delete_order_logic.go +++ b/internal/logic/orderservice/delete_order_logic.go @@ -1,11 +1,11 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "context" ers "code.30cm.net/digimon/library-go/errs" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -31,7 +31,7 @@ type DeleteOrderQuery struct { } // DeleteOrder 刪除訂單(軟刪除) -func (l *DeleteOrderLogic) DeleteOrder(in *tweeting.DeleteOrderReq) (*tweeting.OKResp, error) { +func (l *DeleteOrderLogic) DeleteOrder(in *order.DeleteOrderReq) (*order.OKResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&DeleteOrderQuery{ BusinessID: in.GetBusinessId(), @@ -45,5 +45,5 @@ func (l *DeleteOrderLogic) DeleteOrder(in *tweeting.DeleteOrderReq) (*tweeting.O return nil, err } - return &tweeting.OKResp{}, nil + return &order.OKResp{}, nil } diff --git a/internal/logic/orderservice/get_order_logic.go b/internal/logic/orderservice/get_order_logic.go index 58e823f..779acb2 100644 --- a/internal/logic/orderservice/get_order_logic.go +++ b/internal/logic/orderservice/get_order_logic.go @@ -1,12 +1,12 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "context" ers "code.30cm.net/digimon/library-go/errs" "github.com/shopspring/decimal" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -32,7 +32,7 @@ type GetOrderQuery struct { } // GetOrder 取得訂單詳情 -func (l *GetOrderLogic) GetOrder(in *tweeting.GetOrderReq) (*tweeting.GetOrderResp, error) { +func (l *GetOrderLogic) GetOrder(in *order.GetOrderReq) (*order.GetOrderResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&GetOrderQuery{ BusinessID: in.GetBusinessId(), @@ -41,49 +41,49 @@ func (l *GetOrderLogic) GetOrder(in *tweeting.GetOrderReq) (*tweeting.GetOrderRe return nil, ers.InvalidFormat(err.Error()) } - order, err := l.svcCtx.OrderModel.FindOneBusinessID(l.ctx, in.GetBusinessId()) + o, err := l.svcCtx.OrderModel.FindOneBusinessID(l.ctx, in.GetBusinessId()) if err != nil { return nil, err } - return &tweeting.GetOrderResp{ - UpdateTime: order.UpdateTime, - CreateTime: order.CreateTime, - BusinessId: order.BusinessID, - OrderType: int32(order.OrderType), - OrderStatus: int32(order.OrderStatus), - Brand: order.Brand, - OrderUid: order.OrderUID, - ReferenceId: order.ReferenceID, - Count: order.Count.String(), - OrderFee: order.OrderFee.String(), - Amount: order.Amount.String(), + return &order.GetOrderResp{ + UpdateTime: o.UpdateTime, + CreateTime: o.CreateTime, + BusinessId: o.BusinessID, + OrderType: int32(o.OrderType), + OrderStatus: int32(o.OrderStatus), + Brand: o.Brand, + OrderUid: o.OrderUID, + ReferenceId: o.ReferenceID, + Count: o.Count.String(), + OrderFee: o.OrderFee.String(), + Amount: o.Amount.String(), // 下面的是未來擴充用,加密貨幣用,或者幣別轉換用,普通訂單用不到 - ReferenceBrand: order.ReferenceBrand, - ReferenceUid: order.ReferenceUID, - WalletStatus: order.WalletStatus, - ThreePartyStatus: order.ThreePartyStatus, - DirectionType: order.DirectionType, - CryptoType: order.CryptoType, - ThirdPartyFee: decimalToString(*order.ThirdPartyFee), - CryptoToUsdtRate: decimalToString(*order.CryptoToUSDTRate), - FiatToUsdRate: decimalToString(*order.FiatToUSDRate), - FeeCryptoToUsdtRate: decimalToString(*order.FeeCryptoToUSDTRate), - UsdtToCryptoTypeRate: decimalToString(*order.USDTToCryptoTypeRate), - PaymentFiat: order.PaymentFiat, - PaymentUnitPrice: decimalToString(*order.PaymentUnitPrice), - PaymentTemplateId: order.PaymentTemplateID, - OrderArrivalTime: order.OrderArrivalTime, - OrderPaymentTime: order.OrderPaymentTime, - UnpaidTimeoutSecond: order.UnpaidTimeoutSecond, - ChainType: order.ChainType, - TxHash: order.TxHash, - FromAddress: order.FromAddress, - ToAddress: order.ToAddress, - ChainFee: decimalToString(*order.ChainFee), - ChainFeeCrypto: order.ChainFeeCrypto, - Memo: order.Memo, - OrderNote: order.OrderNote, + ReferenceBrand: o.ReferenceBrand, + ReferenceUid: o.ReferenceUID, + WalletStatus: o.WalletStatus, + ThreePartyStatus: o.ThreePartyStatus, + DirectionType: o.DirectionType, + CryptoType: o.CryptoType, + ThirdPartyFee: decimalToString(*o.ThirdPartyFee), + CryptoToUsdtRate: decimalToString(*o.CryptoToUSDTRate), + FiatToUsdRate: decimalToString(*o.FiatToUSDRate), + FeeCryptoToUsdtRate: decimalToString(*o.FeeCryptoToUSDTRate), + UsdtToCryptoTypeRate: decimalToString(*o.USDTToCryptoTypeRate), + PaymentFiat: o.PaymentFiat, + PaymentUnitPrice: decimalToString(*o.PaymentUnitPrice), + PaymentTemplateId: o.PaymentTemplateID, + OrderArrivalTime: o.OrderArrivalTime, + OrderPaymentTime: o.OrderPaymentTime, + UnpaidTimeoutSecond: o.UnpaidTimeoutSecond, + ChainType: o.ChainType, + TxHash: o.TxHash, + FromAddress: o.FromAddress, + ToAddress: o.ToAddress, + ChainFee: decimalToString(*o.ChainFee), + ChainFeeCrypto: o.ChainFeeCrypto, + Memo: o.Memo, + OrderNote: o.OrderNote, }, nil } diff --git a/internal/logic/orderservice/list_order_logic.go b/internal/logic/orderservice/list_order_logic.go index dd418a7..ebe68e7 100644 --- a/internal/logic/orderservice/list_order_logic.go +++ b/internal/logic/orderservice/list_order_logic.go @@ -1,13 +1,13 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" model "app-cloudep-order-server/internal/model/mongo" "context" ers "code.30cm.net/digimon/library-go/errs" "github.com/shopspring/decimal" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -53,7 +53,7 @@ type GetOrderListReq struct { } // ListOrder 取得訂單列表 -func (l *ListOrderLogic) ListOrder(in *tweeting.ListOrderReq) (*tweeting.ListOrderResp, error) { +func (l *ListOrderLogic) ListOrder(in *order.ListOrderReq) (*order.ListOrderResp, error) { // 驗證資料,目前只有 Page 必帶,其他要驗證在驗證 if err := l.svcCtx.Validate.ValidateAll(&GetOrderListReq{ PageIndex: in.GetPageIndex(), @@ -68,14 +68,14 @@ func (l *ListOrderLogic) ListOrder(in *tweeting.ListOrderReq) (*tweeting.ListOrd return nil, err } - res := make([]*tweeting.GetOrderResp, 0, len(orders)) + res := make([]*order.GetOrderResp, 0, len(orders)) for _, item := range orders { res = append(res, ConvertOrderToGetOrderResp(item)) } - return &tweeting.ListOrderResp{ + return &order.ListOrderResp{ Data: res, - Page: &tweeting.Pager{ + Page: &order.Pager{ Total: total, Index: in.GetPageIndex(), Size: in.GetPageSize(), @@ -84,44 +84,44 @@ func (l *ListOrderLogic) ListOrder(in *tweeting.ListOrderReq) (*tweeting.ListOrd } // ConvertOrderToGetOrderResp 將 Order 結構轉換為 GetOrderResp -func ConvertOrderToGetOrderResp(order model.Order) *tweeting.GetOrderResp { - return &tweeting.GetOrderResp{ - BusinessId: order.BusinessID, - OrderType: int32(order.OrderType), - OrderStatus: int32(order.OrderStatus), - Brand: order.Brand, - OrderUid: order.OrderUID, - ReferenceId: order.ReferenceID, - Count: order.Count.String(), - OrderFee: order.OrderFee.String(), - Amount: order.Amount.String(), - ReferenceBrand: optionalString(order.ReferenceBrand), - ReferenceUid: optionalString(order.ReferenceUID), - WalletStatus: optionalInt64(order.WalletStatus), - ThreePartyStatus: optionalInt64(order.ThreePartyStatus), - DirectionType: optionalInt64(order.DirectionType), - CryptoType: optionalString(order.CryptoType), - ThirdPartyFee: optionalDecimalToString(order.ThirdPartyFee), - CryptoToUsdtRate: optionalDecimalToString(order.CryptoToUSDTRate), - FiatToUsdRate: optionalDecimalToString(order.FiatToUSDRate), - FeeCryptoToUsdtRate: optionalDecimalToString(order.FeeCryptoToUSDTRate), - UsdtToCryptoTypeRate: optionalDecimalToString(order.USDTToCryptoTypeRate), - PaymentFiat: optionalString(order.PaymentFiat), - PaymentUnitPrice: optionalDecimalToString(order.PaymentUnitPrice), - PaymentTemplateId: optionalString(order.PaymentTemplateID), - OrderArrivalTime: optionalInt64(order.OrderArrivalTime), - OrderPaymentTime: optionalInt64(order.OrderPaymentTime), - UnpaidTimeoutSecond: optionalInt64(order.UnpaidTimeoutSecond), - ChainType: optionalString(order.ChainType), - TxHash: optionalString(order.TxHash), - FromAddress: optionalString(order.FromAddress), - ToAddress: optionalString(order.ToAddress), - ChainFee: optionalDecimalToString(order.ChainFee), - ChainFeeCrypto: optionalString(order.ChainFeeCrypto), - Memo: optionalString(order.Memo), - OrderNote: optionalString(order.OrderNote), - CreateTime: order.CreateTime, - UpdateTime: order.UpdateTime, +func ConvertOrderToGetOrderResp(o model.Order) *order.GetOrderResp { + return &order.GetOrderResp{ + BusinessId: o.BusinessID, + OrderType: int32(o.OrderType), + OrderStatus: int32(o.OrderStatus), + Brand: o.Brand, + OrderUid: o.OrderUID, + ReferenceId: o.ReferenceID, + Count: o.Count.String(), + OrderFee: o.OrderFee.String(), + Amount: o.Amount.String(), + ReferenceBrand: optionalString(o.ReferenceBrand), + ReferenceUid: optionalString(o.ReferenceUID), + WalletStatus: optionalInt64(o.WalletStatus), + ThreePartyStatus: optionalInt64(o.ThreePartyStatus), + DirectionType: optionalInt64(o.DirectionType), + CryptoType: optionalString(o.CryptoType), + ThirdPartyFee: optionalDecimalToString(o.ThirdPartyFee), + CryptoToUsdtRate: optionalDecimalToString(o.CryptoToUSDTRate), + FiatToUsdRate: optionalDecimalToString(o.FiatToUSDRate), + FeeCryptoToUsdtRate: optionalDecimalToString(o.FeeCryptoToUSDTRate), + UsdtToCryptoTypeRate: optionalDecimalToString(o.USDTToCryptoTypeRate), + PaymentFiat: optionalString(o.PaymentFiat), + PaymentUnitPrice: optionalDecimalToString(o.PaymentUnitPrice), + PaymentTemplateId: optionalString(o.PaymentTemplateID), + OrderArrivalTime: optionalInt64(o.OrderArrivalTime), + OrderPaymentTime: optionalInt64(o.OrderPaymentTime), + UnpaidTimeoutSecond: optionalInt64(o.UnpaidTimeoutSecond), + ChainType: optionalString(o.ChainType), + TxHash: optionalString(o.TxHash), + FromAddress: optionalString(o.FromAddress), + ToAddress: optionalString(o.ToAddress), + ChainFee: optionalDecimalToString(o.ChainFee), + ChainFeeCrypto: optionalString(o.ChainFeeCrypto), + Memo: optionalString(o.Memo), + OrderNote: optionalString(o.OrderNote), + CreateTime: o.CreateTime, + UpdateTime: o.UpdateTime, } } @@ -152,10 +152,10 @@ func optionalDecimalToString(d *decimal.Decimal) *string { return nil } -func ConvertOrdersToGetOrderResp(orders []model.Order) []*tweeting.GetOrderResp { - res := make([]*tweeting.GetOrderResp, 0, len(orders)) - for _, order := range orders { - res = append(res, ConvertOrderToGetOrderResp(order)) +func ConvertOrdersToGetOrderResp(orders []model.Order) []*order.GetOrderResp { + res := make([]*order.GetOrderResp, 0, len(orders)) + for _, o := range orders { + res = append(res, ConvertOrderToGetOrderResp(o)) } return res diff --git a/internal/logic/orderservice/modify_order_logic.go b/internal/logic/orderservice/modify_order_logic.go index a838866..1c2e399 100644 --- a/internal/logic/orderservice/modify_order_logic.go +++ b/internal/logic/orderservice/modify_order_logic.go @@ -1,9 +1,9 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "context" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -24,8 +24,8 @@ func NewModifyOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Modif } // ModifyOrder 修改訂單 -func (l *ModifyOrderLogic) ModifyOrder(in *tweeting.ModifyOrderReq) (*tweeting.OKResp, error) { +func (l *ModifyOrderLogic) ModifyOrder(in *order.ModifyOrderReq) (*order.OKResp, error) { // todo: add your logic here and delete this line - return &tweeting.OKResp{}, nil + return &order.OKResp{}, nil } diff --git a/internal/logic/orderservice/modify_order_status_logic.go b/internal/logic/orderservice/modify_order_status_logic.go index 02a204c..9167370 100644 --- a/internal/logic/orderservice/modify_order_status_logic.go +++ b/internal/logic/orderservice/modify_order_status_logic.go @@ -1,13 +1,13 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "app-cloudep-order-server/internal/domain" model "app-cloudep-order-server/internal/model/mongo" "context" ers "code.30cm.net/digimon/library-go/errs" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -38,7 +38,7 @@ type ModifyOrderQuery struct { } // ModifyOrderStatus 修改訂單狀態 -func (l *ModifyOrderStatusLogic) ModifyOrderStatus(in *tweeting.ModifyOrderStatusReq) (*tweeting.OKResp, error) { +func (l *ModifyOrderStatusLogic) ModifyOrderStatus(in *order.ModifyOrderStatusReq) (*order.OKResp, error) { // 驗證資料 if err := l.svcCtx.Validate.ValidateAll(&ModifyOrderQuery{ BusinessID: in.GetBusinessId(), @@ -68,5 +68,5 @@ func (l *ModifyOrderStatusLogic) ModifyOrderStatus(in *tweeting.ModifyOrderStatu return nil, e } - return &tweeting.OKResp{}, nil + return &order.OKResp{}, nil } diff --git a/internal/logic/orderservice/order_status_timeout_logic.go b/internal/logic/orderservice/order_status_timeout_logic.go index ca4359c..62d7fee 100644 --- a/internal/logic/orderservice/order_status_timeout_logic.go +++ b/internal/logic/orderservice/order_status_timeout_logic.go @@ -1,12 +1,12 @@ package orderservicelogic import ( + "app-cloudep-order-server/gen_result/pb/order" "app-cloudep-order-server/internal/domain" model "app-cloudep-order-server/internal/model/mongo" "context" "time" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/svc" "github.com/zeromicro/go-zero/core/logx" @@ -27,7 +27,7 @@ func NewOrderStatusTimeoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) } // OrderStatusTimeout 訂單超時任務/cron/order-status/timeout -func (l *OrderStatusTimeoutLogic) OrderStatusTimeout(_ *tweeting.OrderStatusTimeoutReq) (*tweeting.OKResp, error) { +func (l *OrderStatusTimeoutLogic) OrderStatusTimeout(_ *order.OrderStatusTimeoutReq) (*order.OKResp, error) { now := time.Now().UTC().UnixNano() _, err := l.svcCtx.OrderModel.UpdateTimeoutOrder(l.ctx, model.UpdateTimeoutReq{ CreateTimeBefore: now, @@ -47,5 +47,5 @@ func (l *OrderStatusTimeoutLogic) OrderStatusTimeout(_ *tweeting.OrderStatusTime return nil, e } - return &tweeting.OKResp{}, nil + return &order.OKResp{}, nil } diff --git a/internal/mock/model/order_model.go b/internal/mock/model/order_model.go index fb24d12..914b226 100644 --- a/internal/mock/model/order_model.go +++ b/internal/mock/model/order_model.go @@ -10,11 +10,11 @@ package mock import ( - mongo "app-cloudep-order-server/internal/model/mongo" + model "app-cloudep-order-server/internal/model/mongo" context "context" reflect "reflect" - mongo0 "go.mongodb.org/mongo-driver/mongo" + mongo "go.mongodb.org/mongo-driver/mongo" gomock "go.uber.org/mock/gomock" ) @@ -56,11 +56,26 @@ func (mr *MockOrderModelMockRecorder) Delete(ctx, id any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockOrderModel)(nil).Delete), ctx, id) } +// DeleteByBusinessID mocks base method. +func (m *MockOrderModel) DeleteByBusinessID(ctx context.Context, id string) (*mongo.UpdateResult, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteByBusinessID", ctx, id) + ret0, _ := ret[0].(*mongo.UpdateResult) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// DeleteByBusinessID indicates an expected call of DeleteByBusinessID. +func (mr *MockOrderModelMockRecorder) DeleteByBusinessID(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteByBusinessID", reflect.TypeOf((*MockOrderModel)(nil).DeleteByBusinessID), ctx, id) +} + // FindOne mocks base method. -func (m *MockOrderModel) FindOne(ctx context.Context, id string) (*mongo.Order, error) { +func (m *MockOrderModel) FindOne(ctx context.Context, id string) (*model.Order, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindOne", ctx, id) - ret0, _ := ret[0].(*mongo.Order) + ret0, _ := ret[0].(*model.Order) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -71,8 +86,23 @@ func (mr *MockOrderModelMockRecorder) FindOne(ctx, id any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOne", reflect.TypeOf((*MockOrderModel)(nil).FindOne), ctx, id) } +// FindOneBusinessID mocks base method. +func (m *MockOrderModel) FindOneBusinessID(ctx context.Context, id string) (*model.Order, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindOneBusinessID", ctx, id) + ret0, _ := ret[0].(*model.Order) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// FindOneBusinessID indicates an expected call of FindOneBusinessID. +func (mr *MockOrderModelMockRecorder) FindOneBusinessID(ctx, id any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindOneBusinessID", reflect.TypeOf((*MockOrderModel)(nil).FindOneBusinessID), ctx, id) +} + // Insert mocks base method. -func (m *MockOrderModel) Insert(ctx context.Context, data *mongo.Order) error { +func (m *MockOrderModel) Insert(ctx context.Context, data *model.Order) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Insert", ctx, data) ret0, _ := ret[0].(error) @@ -85,11 +115,27 @@ func (mr *MockOrderModelMockRecorder) Insert(ctx, data any) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Insert", reflect.TypeOf((*MockOrderModel)(nil).Insert), ctx, data) } +// ListOrder mocks base method. +func (m *MockOrderModel) ListOrder(ctx context.Context, req model.GetOrderListReq) ([]model.Order, int64, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListOrder", ctx, req) + ret0, _ := ret[0].([]model.Order) + ret1, _ := ret[1].(int64) + ret2, _ := ret[2].(error) + return ret0, ret1, ret2 +} + +// ListOrder indicates an expected call of ListOrder. +func (mr *MockOrderModelMockRecorder) ListOrder(ctx, req any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListOrder", reflect.TypeOf((*MockOrderModel)(nil).ListOrder), ctx, req) +} + // Update mocks base method. -func (m *MockOrderModel) Update(ctx context.Context, data *mongo.Order) (*mongo0.UpdateResult, error) { +func (m *MockOrderModel) Update(ctx context.Context, data *model.Order) (*mongo.UpdateResult, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", ctx, data) - ret0, _ := ret[0].(*mongo0.UpdateResult) + ret0, _ := ret[0].(*mongo.UpdateResult) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -99,3 +145,33 @@ func (mr *MockOrderModelMockRecorder) Update(ctx, data any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Update", reflect.TypeOf((*MockOrderModel)(nil).Update), ctx, data) } + +// UpdateStatus mocks base method. +func (m *MockOrderModel) UpdateStatus(ctx context.Context, data model.UpdateStatusReq) (*mongo.UpdateResult, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateStatus", ctx, data) + ret0, _ := ret[0].(*mongo.UpdateResult) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateStatus indicates an expected call of UpdateStatus. +func (mr *MockOrderModelMockRecorder) UpdateStatus(ctx, data any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateStatus", reflect.TypeOf((*MockOrderModel)(nil).UpdateStatus), ctx, data) +} + +// UpdateTimeoutOrder mocks base method. +func (m *MockOrderModel) UpdateTimeoutOrder(ctx context.Context, req model.UpdateTimeoutReq) (*mongo.UpdateResult, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateTimeoutOrder", ctx, req) + ret0, _ := ret[0].(*mongo.UpdateResult) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateTimeoutOrder indicates an expected call of UpdateTimeoutOrder. +func (mr *MockOrderModelMockRecorder) UpdateTimeoutOrder(ctx, req any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateTimeoutOrder", reflect.TypeOf((*MockOrderModel)(nil).UpdateTimeoutOrder), ctx, req) +} diff --git a/internal/mock/model/order_model_gen.go b/internal/mock/model/order_model_gen.go index f3fb4da..5052926 100644 --- a/internal/mock/model/order_model_gen.go +++ b/internal/mock/model/order_model_gen.go @@ -10,11 +10,11 @@ package mock import ( - mongo "app-cloudep-order-server/internal/model/mongo" + model "app-cloudep-order-server/internal/model/mongo" context "context" reflect "reflect" - mongo0 "go.mongodb.org/mongo-driver/mongo" + mongo "go.mongodb.org/mongo-driver/mongo" gomock "go.uber.org/mock/gomock" ) @@ -57,10 +57,10 @@ func (mr *MockorderModelMockRecorder) Delete(ctx, id any) *gomock.Call { } // FindOne mocks base method. -func (m *MockorderModel) FindOne(ctx context.Context, id string) (*mongo.Order, error) { +func (m *MockorderModel) FindOne(ctx context.Context, id string) (*model.Order, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindOne", ctx, id) - ret0, _ := ret[0].(*mongo.Order) + ret0, _ := ret[0].(*model.Order) ret1, _ := ret[1].(error) return ret0, ret1 } @@ -72,7 +72,7 @@ func (mr *MockorderModelMockRecorder) FindOne(ctx, id any) *gomock.Call { } // Insert mocks base method. -func (m *MockorderModel) Insert(ctx context.Context, data *mongo.Order) error { +func (m *MockorderModel) Insert(ctx context.Context, data *model.Order) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Insert", ctx, data) ret0, _ := ret[0].(error) @@ -86,10 +86,10 @@ func (mr *MockorderModelMockRecorder) Insert(ctx, data any) *gomock.Call { } // Update mocks base method. -func (m *MockorderModel) Update(ctx context.Context, data *mongo.Order) (*mongo0.UpdateResult, error) { +func (m *MockorderModel) Update(ctx context.Context, data *model.Order) (*mongo.UpdateResult, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Update", ctx, data) - ret0, _ := ret[0].(*mongo0.UpdateResult) + ret0, _ := ret[0].(*mongo.UpdateResult) ret1, _ := ret[1].(error) return ret0, ret1 } diff --git a/internal/server/orderservice/order_service_server.go b/internal/server/orderservice/order_service_server.go index bae682a..f362241 100644 --- a/internal/server/orderservice/order_service_server.go +++ b/internal/server/orderservice/order_service_server.go @@ -6,14 +6,14 @@ package server import ( "context" - "app-cloudep-order-server/gen_result/pb/tweeting" + "app-cloudep-order-server/gen_result/pb/order" orderservicelogic "app-cloudep-order-server/internal/logic/orderservice" "app-cloudep-order-server/internal/svc" ) type OrderServiceServer struct { svcCtx *svc.ServiceContext - tweeting.UnimplementedOrderServiceServer + order.UnimplementedOrderServiceServer } func NewOrderServiceServer(svcCtx *svc.ServiceContext) *OrderServiceServer { @@ -23,49 +23,49 @@ func NewOrderServiceServer(svcCtx *svc.ServiceContext) *OrderServiceServer { } // CreateOrder 建立訂單 -func (s *OrderServiceServer) CreateOrder(ctx context.Context, in *tweeting.CreateOrderReq) (*tweeting.OKResp, error) { +func (s *OrderServiceServer) CreateOrder(ctx context.Context, in *order.CreateOrderReq) (*order.OKResp, error) { l := orderservicelogic.NewCreateOrderLogic(ctx, s.svcCtx) return l.CreateOrder(in) } // CancelOrder 取消訂單 -func (s *OrderServiceServer) CancelOrder(ctx context.Context, in *tweeting.CancelOrderReq) (*tweeting.OKResp, error) { +func (s *OrderServiceServer) CancelOrder(ctx context.Context, in *order.CancelOrderReq) (*order.OKResp, error) { l := orderservicelogic.NewCancelOrderLogic(ctx, s.svcCtx) return l.CancelOrder(in) } // ModifyOrder 修改訂單 -func (s *OrderServiceServer) ModifyOrder(ctx context.Context, in *tweeting.ModifyOrderReq) (*tweeting.OKResp, error) { +func (s *OrderServiceServer) ModifyOrder(ctx context.Context, in *order.ModifyOrderReq) (*order.OKResp, error) { l := orderservicelogic.NewModifyOrderLogic(ctx, s.svcCtx) return l.ModifyOrder(in) } // ModifyOrderStatus 修改訂單狀態 -func (s *OrderServiceServer) ModifyOrderStatus(ctx context.Context, in *tweeting.ModifyOrderStatusReq) (*tweeting.OKResp, error) { +func (s *OrderServiceServer) ModifyOrderStatus(ctx context.Context, in *order.ModifyOrderStatusReq) (*order.OKResp, error) { l := orderservicelogic.NewModifyOrderStatusLogic(ctx, s.svcCtx) return l.ModifyOrderStatus(in) } // DeleteOrder 刪除訂單(軟刪除) -func (s *OrderServiceServer) DeleteOrder(ctx context.Context, in *tweeting.DeleteOrderReq) (*tweeting.OKResp, error) { +func (s *OrderServiceServer) DeleteOrder(ctx context.Context, in *order.DeleteOrderReq) (*order.OKResp, error) { l := orderservicelogic.NewDeleteOrderLogic(ctx, s.svcCtx) return l.DeleteOrder(in) } // GetOrder 取得訂單詳情 -func (s *OrderServiceServer) GetOrder(ctx context.Context, in *tweeting.GetOrderReq) (*tweeting.GetOrderResp, error) { +func (s *OrderServiceServer) GetOrder(ctx context.Context, in *order.GetOrderReq) (*order.GetOrderResp, error) { l := orderservicelogic.NewGetOrderLogic(ctx, s.svcCtx) return l.GetOrder(in) } // ListOrder 取得訂單列表 -func (s *OrderServiceServer) ListOrder(ctx context.Context, in *tweeting.ListOrderReq) (*tweeting.ListOrderResp, error) { +func (s *OrderServiceServer) ListOrder(ctx context.Context, in *order.ListOrderReq) (*order.ListOrderResp, error) { l := orderservicelogic.NewListOrderLogic(ctx, s.svcCtx) return l.ListOrder(in) } // OrderStatusTimeout 訂單超時任務/cron/order-status/timeout -func (s *OrderServiceServer) OrderStatusTimeout(ctx context.Context, in *tweeting.OrderStatusTimeoutReq) (*tweeting.OKResp, error) { +func (s *OrderServiceServer) OrderStatusTimeout(ctx context.Context, in *order.OrderStatusTimeoutReq) (*order.OKResp, error) { l := orderservicelogic.NewOrderStatusTimeoutLogic(ctx, s.svcCtx) return l.OrderStatusTimeout(in) } diff --git a/internal/svc/init_validate.go b/internal/svc/init_validate.go new file mode 100644 index 0000000..f5104d5 --- /dev/null +++ b/internal/svc/init_validate.go @@ -0,0 +1,55 @@ +package svc + +import ( + vi "code.30cm.net/digimon/library-go/validator" + "github.com/go-playground/validator/v10" + "github.com/shopspring/decimal" +) + +// WithDecimalGt 是否大於等於 +func WithDecimalGt() vi.Option { + return vi.Option{ + ValidatorName: "decimalGt", + ValidatorFunc: func(fl validator.FieldLevel) bool { + if val, ok := fl.Field().Interface().(string); ok { + value, err := decimal.NewFromString(val) + if err != nil { + return false + } + + conditionValue, err := decimal.NewFromString(fl.Param()) + if err != nil { + return false + } + + return value.GreaterThan(conditionValue) + } + + return true + }, + } +} + +// WithDecimalGte 是否大於等於 +func WithDecimalGte() vi.Option { + return vi.Option{ + ValidatorName: "decimalGte", + ValidatorFunc: func(fl validator.FieldLevel) bool { + if val, ok := fl.Field().Interface().(string); ok { + value, err := decimal.NewFromString(val) + if err != nil { + return false + } + + conditionValue, err := decimal.NewFromString(fl.Param()) + if err != nil { + return false + } + + return value.GreaterThanOrEqual(conditionValue) + } + + return true + }, + } +} diff --git a/internal/svc/service_context.go b/internal/svc/service_context.go index 1c91e31..ee1c4d1 100644 --- a/internal/svc/service_context.go +++ b/internal/svc/service_context.go @@ -21,7 +21,7 @@ func NewServiceContext(c config.Config) *ServiceContext { return &ServiceContext{ Config: c, - Validate: vi.MustValidator(), + Validate: vi.MustValidator(WithDecimalGt(), WithDecimalGte()), OrderModel: MustOrderModel(c), } } diff --git a/order.go b/order.go index c5aeb4f..815f0cd 100644 --- a/order.go +++ b/order.go @@ -1,10 +1,10 @@ package main import ( + "app-cloudep-order-server/gen_result/pb/order" "flag" "fmt" - "app-cloudep-order-server/gen_result/pb/tweeting" "app-cloudep-order-server/internal/config" orderserviceServer "app-cloudep-order-server/internal/server/orderservice" "app-cloudep-order-server/internal/svc" @@ -26,7 +26,7 @@ func main() { ctx := svc.NewServiceContext(c) s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { - tweeting.RegisterOrderServiceServer(grpcServer, orderserviceServer.NewOrderServiceServer(ctx)) + order.RegisterOrderServiceServer(grpcServer, orderserviceServer.NewOrderServiceServer(ctx)) if c.Mode == service.DevMode || c.Mode == service.TestMode { reflection.Register(grpcServer)