commit d0a6f9e0ecc75e1fd8c95be116829fd2a1020536 Author: daniel.w Date: Fri Oct 25 14:18:16 2024 +0800 add trade wallet proto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d179dd3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.idea/ +go.sum +account/ +gen_result/ +etc/order.yaml +client/ +.DS_Store diff --git a/generate/database/mongodb/20241006000001_order.up.mongo b/generate/database/mongodb/20241006000001_order.up.mongo new file mode 100644 index 0000000..e8510aa --- /dev/null +++ b/generate/database/mongodb/20241006000001_order.up.mongo @@ -0,0 +1,13 @@ +use digimon_order; + +db.order.createIndex({ "business_id": 1, "order_status": 1, "create_time": -1 }) +db.orders.createIndex({ "reference_id": 1, "reference_uid": 1, "business_id": 1, "uid": 1 }) +db.orders.createIndex({ "order_type": 1, "direction_type": 1, "order_status": 1 }) +db.orders.createIndex({ "create_time": -1, "update_time": -1, "order_arrival_time": -1, "order_payment_time": -1 }) + +db.order.createIndex({ + "uid": 1, + "order_type": 1, + "order_status": 1, + "create_time": -1, +}) \ No newline at end of file diff --git a/generate/database/mongodb/readme.md b/generate/database/mongodb/readme.md new file mode 100644 index 0000000..5cbb27e --- /dev/null +++ b/generate/database/mongodb/readme.md @@ -0,0 +1 @@ +如果有需要可以把 mongo 放這邊 \ No newline at end of file diff --git a/generate/database/readme.md b/generate/database/readme.md new file mode 100644 index 0000000..4db6e02 --- /dev/null +++ b/generate/database/readme.md @@ -0,0 +1,39 @@ +# migrate +數據庫遷移工具 + +[golang-migrate](https://github.com/golang-migrate/migrate) + +## 安裝 make +```shell +brew install Makefile +``` + +## 安裝 golang-migrate +```shell +brew install golang-migrate +``` + +## 執行刪除 mysql schema + +```shell +migrate -source file://database/migrations/mysql -database 'mysql://account:password@tcp(127.0.0.1:3306)/esc_c2c' down +``` + +## 執行安裝 mysql schema + +```shell +migrate -source file://database/migrations/mysql -database 'mysql://account:password@tcp(127.0.0.1:3306)/esc_c2c' up +``` + +## 執行刪除 mongo schema + +```shell +migrate -source file://database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/esc_c2c' down +``` + +## 執行安裝 mongo schema + +```shell +migrate -source file://database/migrations/mongodb -database 'mongodb://127.0.0.1:27017/esc_c2c' up +``` + diff --git a/generate/protobuf/trade.proto b/generate/protobuf/trade.proto new file mode 100644 index 0000000..7f90723 --- /dev/null +++ b/generate/protobuf/trade.proto @@ -0,0 +1,241 @@ +syntax = "proto3"; +package trade; +option go_package = "./trade"; + +// ========== 基本回應 =========== +message OKResp {} + +// 空的請求 +message NoneReq {} + +// 分頁信息 +message Pager { + int64 total = 1; // 總數量 + int64 size = 2; // 每頁數量 + int64 index = 3; // 當前頁碼 +} + +// ========== 訂單服務 =========== +message CreateOrderReq { + string business_id = 1; // 訂單業務流水號 + int32 order_type = 2; // 訂單類型 + int32 order_status = 3; // 訂單狀態 + string brand = 4; // 下單平台 + string order_uid = 5; // 下單用戶 UID + string reference_id = 6; // 訂單來源 + string count = 7; // 訂單數量 (decimal to string) + string order_fee = 8; // 訂單手續費 (decimal to string) + string amount = 9; // 單價 (decimal to string) + + // 以下是可選字段(擴充用) + optional string reference_brand = 10; // 訂單來源平台 + optional string reference_uid = 11; // 訂單來源用戶 UID + optional int64 wallet_status = 12; // 交易金額狀態 + optional int64 three_party_status = 13; // 三方請求狀態 + optional int64 direction_type = 14; // 交易方向 + optional string crypto_type = 15; // 交易幣種 + optional string third_party_fee = 16; // 第三方手續費 (decimal to string) + optional string crypto_to_usdt_rate = 17; // 交易幣種對 USDT 匯率 (decimal to string) + optional string fiat_to_usd_rate = 18; // 法幣對 USD 匯率 (decimal to string) + optional string fee_crypto_to_usdt_rate = 19; // 手續費幣種對 USDT 匯率 (decimal to string) + optional string usdt_to_crypto_type_rate = 20; // USDT 對交易幣種匯率 (decimal to string) + optional string payment_fiat = 21; // 支付法幣 + optional string payment_unit_price = 22; // crypto 單價 (decimal to string) + optional string payment_template_id = 23; // 支付方式配置 ID + optional int64 order_arrival_time = 24; // 訂單到帳時間 + optional int64 order_payment_time = 25; // 訂單付款時間 + optional int64 unpaid_timeout_second = 26; // 支付期限秒數 + optional string chain_type = 27; // 主網類型 + optional string tx_hash = 28; // 交易哈希 + optional string from_address = 29; // 來源地址 + optional string to_address = 30; // 目標地址 + optional string chain_fee = 31; // 鏈上交易手續費 (decimal to string) + optional string chain_fee_crypto = 32; // 鏈上手續費使用幣別 + optional string memo = 33; // 鏈上備註 + optional string order_note = 34; // 訂單交易備註 +} + +message CancelOrderReq { + string business_id = 1; + int64 status = 2; +} + +message ModifyOrderStatusReq { + string business_id = 1; + int64 status = 2; +} + +message DeleteOrderReq { + string business_id = 1; +} + +message OrderStatusTimeoutReq {} + +message GetOrderReq { + string business_id = 1; // 訂單業務流水號 +} + +message GetOrderResp { + string business_id = 1; // 訂單業務流水號 + int32 order_type = 2; // 訂單類型 + int32 order_status = 3; // 訂單狀態 + string brand = 4; // 下單平台 + string order_uid = 5; // 下單用戶 UID + string reference_id = 6; // 訂單來源 + string count = 7; // 訂單數量 (decimal to string) + string order_fee = 8; // 訂單手續費 (decimal to string) + string amount = 9; // 單價 (decimal to string) + + // 以下是可選字段(擴充用) + optional string reference_brand = 10; // 訂單來源平台 + optional string reference_uid = 11; // 訂單來源用戶 UID + optional int64 wallet_status = 12; // 交易金額狀態 + optional int64 three_party_status = 13; // 三方請求狀態 + optional int64 direction_type = 14; // 交易方向 + optional string crypto_type = 15; // 交易幣種 + optional string third_party_fee = 16; // 第三方手續費 (decimal to string) + optional string crypto_to_usdt_rate = 17; // 交易幣種對 USDT 匯率 (decimal to string) + optional string fiat_to_usd_rate = 18; // 法幣對 USD 匯率 (decimal to string) + optional string fee_crypto_to_usdt_rate = 19; // 手續費幣種對 USDT 匯率 (decimal to string) + optional string usdt_to_crypto_type_rate = 20; // USDT 對交易幣種匯率 (decimal to string) + optional string payment_fiat = 21; // 支付法幣 + optional string payment_unit_price = 22; // crypto 單價 (decimal to string) + optional string payment_template_id = 23; // 支付方式配置 ID + optional int64 order_arrival_time = 24; // 訂單到帳時間 + optional int64 order_payment_time = 25; // 訂單付款時間 + optional int64 unpaid_timeout_second = 26; // 支付期限秒數 + optional string chain_type = 27; // 主網類型 + optional string tx_hash = 28; // 交易哈希 + optional string from_address = 29; // 來源地址 + optional string to_address = 30; // 目標地址 + optional string chain_fee = 31; // 鏈上交易手續費 (decimal to string) + optional string chain_fee_crypto = 32; // 鏈上手續費使用幣別 + optional string memo = 33; // 鏈上備註 + optional string order_note = 34; // 訂單交易備註 + int64 create_time = 35; // 建立時間 + int64 update_time = 36; // 更新時間 +} + +message ListOrderReq { + int64 page_index = 1; // required + int64 page_size = 2; // required + + string reference_id = 3; + string reference_uid = 4; + string business_id = 5; + string uid = 6; + int32 order_type = 7; + repeated int32 direction_type = 8; + repeated int32 order_status = 9; + + int64 start_create_time = 10; + int64 end_create_time = 11; + int64 start_update_time = 12; + int64 end_update_time = 13; + int64 start_order_arrival_time = 14; + int64 end_order_arrival_time = 15; + int64 start_order_payment_time = 16; + int64 end_order_payment_time = 17; + + string crypto_type = 18; + string tx_hash = 19; +} + +message ListOrderResp { + repeated GetOrderResp data = 1; // 訂單列表 + Pager page = 2; +} + +// OrderService 訂單服務(業務邏輯在外面組合) +service OrderService { + // CreateOrder 建立訂單 + rpc CreateOrder(CreateOrderReq) returns (OKResp); + // CancelOrder 取消訂單 + rpc CancelOrder(CancelOrderReq) returns (OKResp); + // ModifyOrderStatus 修改訂單狀態 + rpc ModifyOrderStatus(ModifyOrderStatusReq) returns (OKResp); + // DeleteOrder 刪除訂單(軟刪除) + rpc DeleteOrder(DeleteOrderReq) returns (OKResp); + // GetOrder 取得訂單詳情 + rpc GetOrder(GetOrderReq) returns (GetOrderResp); + // ListOrder 取得訂單列表 + rpc ListOrder(ListOrderReq) returns (ListOrderResp); + // OrderStatusTimeout 訂單超時任務/cron/order-status/timeout + rpc OrderStatusTimeout(OrderStatusTimeoutReq) returns (OKResp); +} + +// ========== 錢包服務 =========== +message CreateWalletReq{ + repeated WalletItem item =1; +} + +message WalletItem { + string source = 1; // 錢包來源 + string uid = 2; // 錢包擁有者的 UID + string currency_type = 3; // 幣別 + string current_balance = 4; // 當前餘額 + int64 wallet_type = 5; // 錢包金額種類 1. 可用餘額 2. 凍結餘額 3. 限制餘額 +} + +message WalletTransactionReq{ + string uid =1; // 錢包擁有者的 UID + optional string order_id =2; // 訂單編號 + optional string business_type =3; // 業務種類 + optional string currency_type = 4; // 幣別 + optional string amount = 5; // 轉帳金額 + optional int64 wallet_type = 6;// 操作金額種類 +} + +message QueryBalanceByDateReq{ + string uid =1; // 錢包擁有者的 UID + string currency_type = 2; // 幣別 +} + +message QueryBalanceByDateResp { + string currency_type = 1; // 幣別 + string available = 2; // 可用餘額 + UnavailableBalance unavailable = 3; // 不可用餘額 + int64 update_time=4; +} + +message UnavailableBalance{ + string freeze = 1; // 凍結餘額 + string unconfirmed = 2; // 限制餘額 +} + +service WalletService { + // CreateWallet 建立錢包 + rpc CreateWallet(CreateWalletReq) returns (OKResp); + // DepositFunds 充值 - 增加 [可用餘額] 或 [限制餘額] + rpc DepositFunds(WalletTransactionReq) returns (OKResp); + // WithdrawFunds 提現 - 減少 [可用餘額] + rpc WithdrawFunds(WalletTransactionReq) returns (OKResp); + // FreezeFunds 凍結 - 將 [可用餘額] 減少,加到 [凍結餘額] + rpc FreezeFunds(WalletTransactionReq) returns (OKResp); + // UnfreezeFunds 解凍 - 減少 [凍結餘額] + rpc UnfreezeFunds(WalletTransactionReq) returns (OKResp); + // AppendToFreeze 追加凍結 - 減少可用餘額,追加到現有的 [凍結餘額] (基於原order_id) + rpc AppendToFreeze(WalletTransactionReq) returns (OKResp); + // RollbackFreeze 凍結回滾 - 減少 [凍結餘額],加回 [可用餘額],不可指定金額,整筆訂單的凍結金額加回 [可用餘額] + rpc RollbackFreeze(WalletTransactionReq) returns (OKResp); + // CancelFreeze 取消凍結 - 減少 [凍結餘額],加回 [可用餘額],可指定金額 + rpc CancelFreeze(WalletTransactionReq) returns (OKResp); + // TransferToRestrictedFunds 限制 - 減少 [凍結餘額],轉移到另一個使用者的 [限制餘額] + rpc TransferToRestrictedFunds(WalletTransactionReq) returns (OKResp); + // SettleRestrictedFunds 結算限制餘額 - 限制餘額在 T+N 天後自動失效,每日只能執行一次 + rpc SettleRestrictedFunds(NoneReq) returns (OKResp); + // QueryBalance 餘額查詢 -> 依照日期查詢 + rpc QueryBalance(QueryBalanceByDateReq) returns (QueryBalanceByDateResp); +} + + + // 餘額查詢-> 依照日期 + // 查詢目前所擁有的資產 + // 依照日期查詢提現金額 + +// ========== 庫存服務 =========== +service InventoryService {} +// ========== 商品服務 =========== +service ProductService {} +// ========== 訂閱服務 =========== +service SubscriptionService {} \ No newline at end of file