import '../../models/trade_order_model.dart'; import '../../models/price_info_model.dart'; import '../../models/trading_account_model.dart'; import '../../models/market_overview_model.dart'; import '../../models/asset_display_model.dart'; import '../../../core/network/api_client.dart'; import '../../../core/network/api_endpoints.dart'; import '../../../core/error/exceptions.dart'; abstract class TradingRemoteDataSource { /// 获取当前价格信息 Future getCurrentPrice(); /// 获取市场概览 Future getMarketOverview(); /// 获取交易账户信息 Future getTradingAccount(String accountSequence); /// 创建订单 Future> createOrder({ required String type, required String price, required String quantity, }); /// 取消订单 Future cancelOrder(String orderNo); /// 获取用户订单列表 Future getOrders({ int page = 1, int pageSize = 50, }); /// 预估卖出收益 Future> estimateSell(String quantity); /// 划入积分股 (从挖矿账户到交易账户) Future> transferIn(String amount); /// 划出积分股 (从交易账户到挖矿账户) Future> transferOut(String amount); /// 获取我的资产显示信息 Future getMyAsset({String? dailyAllocation}); /// 获取指定账户资产显示信息 Future getAccountAsset(String accountSequence, {String? dailyAllocation}); } class TradingRemoteDataSourceImpl implements TradingRemoteDataSource { final ApiClient client; TradingRemoteDataSourceImpl({required this.client}); @override Future getCurrentPrice() async { try { final response = await client.get(ApiEndpoints.currentPrice); return PriceInfoModel.fromJson(response.data); } catch (e) { throw ServerException(e.toString()); } } @override Future getMarketOverview() async { try { final response = await client.get(ApiEndpoints.marketOverview); return MarketOverviewModel.fromJson(response.data); } catch (e) { throw ServerException(e.toString()); } } @override Future getTradingAccount(String accountSequence) async { try { final response = await client.get(ApiEndpoints.tradingAccount(accountSequence)); return TradingAccountModel.fromJson(response.data); } catch (e) { throw ServerException(e.toString()); } } @override Future> createOrder({ required String type, required String price, required String quantity, }) async { try { final response = await client.post( ApiEndpoints.createOrder, data: { 'type': type, 'price': price, 'quantity': quantity, }, ); return response.data; } catch (e) { throw ServerException(e.toString()); } } @override Future cancelOrder(String orderNo) async { try { await client.post(ApiEndpoints.cancelOrder(orderNo)); } catch (e) { throw ServerException(e.toString()); } } @override Future getOrders({ int page = 1, int pageSize = 50, }) async { try { final response = await client.get( ApiEndpoints.orders, queryParameters: {'page': page, 'pageSize': pageSize}, ); return OrdersPageModel.fromJson(response.data); } catch (e) { throw ServerException(e.toString()); } } @override Future> estimateSell(String quantity) async { try { final response = await client.get( ApiEndpoints.estimateSell, queryParameters: {'quantity': quantity}, ); return response.data; } catch (e) { throw ServerException(e.toString()); } } @override Future> transferIn(String amount) async { try { final response = await client.post( ApiEndpoints.transferIn, data: {'amount': amount}, ); return response.data; } catch (e) { throw ServerException(e.toString()); } } @override Future> transferOut(String amount) async { try { final response = await client.post( ApiEndpoints.transferOut, data: {'amount': amount}, ); return response.data; } catch (e) { throw ServerException(e.toString()); } } @override Future getMyAsset({String? dailyAllocation}) async { try { final queryParams = {}; if (dailyAllocation != null) { queryParams['dailyAllocation'] = dailyAllocation; } final response = await client.get( ApiEndpoints.myAsset, queryParameters: queryParams.isNotEmpty ? queryParams : null, ); return AssetDisplayModel.fromJson(response.data); } catch (e) { throw ServerException(e.toString()); } } @override Future getAccountAsset(String accountSequence, {String? dailyAllocation}) async { try { final queryParams = {}; if (dailyAllocation != null) { queryParams['dailyAllocation'] = dailyAllocation; } final response = await client.get( ApiEndpoints.accountAsset(accountSequence), queryParameters: queryParams.isNotEmpty ? queryParams : null, ); return AssetDisplayModel.fromJson(response.data); } catch (e) { throw ServerException(e.toString()); } } }