181 lines
4.9 KiB
Dart
181 lines
4.9 KiB
Dart
/// 资产总览模型
|
||
class AssetOverview {
|
||
final String totalAsset;
|
||
final String fundBalance;
|
||
final String tradeBalance;
|
||
final String totalProfit;
|
||
|
||
AssetOverview({
|
||
required this.totalAsset,
|
||
required this.fundBalance,
|
||
required this.tradeBalance,
|
||
required this.totalProfit,
|
||
});
|
||
|
||
factory AssetOverview.fromJson(Map<String, dynamic> json) {
|
||
return AssetOverview(
|
||
totalAsset: json['totalAsset']?.toString() ?? '0.00',
|
||
fundBalance: json['fundBalance']?.toString() ?? '0.00',
|
||
tradeBalance: json['tradeBalance']?.toString() ?? '0.00',
|
||
totalProfit: json['totalProfit']?.toString() ?? '0.00',
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 资金账户模型
|
||
class AccountFund {
|
||
final int id;
|
||
final int userId;
|
||
final String balance;
|
||
final String frozenBalance;
|
||
final DateTime? updateTime;
|
||
|
||
AccountFund({
|
||
required this.id,
|
||
required this.userId,
|
||
required this.balance,
|
||
required this.frozenBalance,
|
||
this.updateTime,
|
||
});
|
||
|
||
factory AccountFund.fromJson(Map<String, dynamic> json) {
|
||
return AccountFund(
|
||
id: json['id'] as int? ?? 0,
|
||
userId: json['userId'] as int? ?? 0,
|
||
balance: json['balance']?.toString() ?? '0.00',
|
||
frozenBalance: json['frozenBalance']?.toString() ?? '0.00',
|
||
updateTime: json['updateTime'] != null
|
||
? DateTime.tryParse(json['updateTime'])
|
||
: null,
|
||
);
|
||
}
|
||
}
|
||
|
||
/// 交易账户模型(持仓)
|
||
class AccountTrade {
|
||
final int id;
|
||
final int userId;
|
||
final String coinCode;
|
||
final String quantity;
|
||
final String avgPrice;
|
||
final String totalCost;
|
||
final String currentValue;
|
||
final String profit;
|
||
final double profitRate;
|
||
final DateTime? updateTime;
|
||
|
||
AccountTrade({
|
||
required this.id,
|
||
required this.userId,
|
||
required this.coinCode,
|
||
required this.quantity,
|
||
required this.avgPrice,
|
||
required this.totalCost,
|
||
required this.currentValue,
|
||
required this.profit,
|
||
required this.profitRate,
|
||
this.updateTime,
|
||
});
|
||
|
||
factory AccountTrade.fromJson(Map<String, dynamic> json) {
|
||
// 后端返回 value(当前价值),前端用 currentValue
|
||
final quantityNum =
|
||
double.tryParse(json['quantity']?.toString() ?? '0') ?? 0;
|
||
final avgPriceNum =
|
||
double.tryParse(json['avgPrice']?.toString() ?? '0') ?? 0;
|
||
final currentValueNum =
|
||
double.tryParse(json['value']?.toString() ?? '0') ??
|
||
double.tryParse(json['currentValue']?.toString() ?? '0') ??
|
||
0;
|
||
final totalCostNum = quantityNum * avgPriceNum;
|
||
final profitNum = currentValueNum - totalCostNum;
|
||
final double profitRateNum =
|
||
totalCostNum > 0 ? (profitNum / totalCostNum) * 100.0 : 0.0;
|
||
|
||
return AccountTrade(
|
||
id: json['id'] as int? ?? 0,
|
||
userId: json['userId'] as int? ?? 0,
|
||
coinCode: json['coinCode'] as String? ?? '',
|
||
quantity: json['quantity']?.toString() ?? '0',
|
||
avgPrice: json['avgPrice']?.toString() ?? '0.00',
|
||
totalCost: totalCostNum.toStringAsFixed(2),
|
||
currentValue: currentValueNum.toStringAsFixed(2),
|
||
profit: profitNum.toStringAsFixed(2),
|
||
profitRate: profitRateNum,
|
||
updateTime: json['updateTime'] != null
|
||
? DateTime.tryParse(json['updateTime'])
|
||
: null,
|
||
);
|
||
}
|
||
|
||
/// 格式化盈亏率
|
||
String get formattedProfitRate {
|
||
final prefix = profitRate >= 0 ? '+' : '';
|
||
return '$prefix${profitRate.toStringAsFixed(2)}%';
|
||
}
|
||
|
||
/// 是否盈利
|
||
bool get isProfit => profitRate >= 0;
|
||
}
|
||
|
||
/// 资金流水模型
|
||
class AccountFlow {
|
||
final int id;
|
||
final int userId;
|
||
final String flowType;
|
||
final String amount;
|
||
final String balanceBefore;
|
||
final String balanceAfter;
|
||
final String remark;
|
||
final DateTime? createTime;
|
||
|
||
AccountFlow({
|
||
required this.id,
|
||
required this.userId,
|
||
required this.flowType,
|
||
required this.amount,
|
||
required this.balanceBefore,
|
||
required this.balanceAfter,
|
||
required this.remark,
|
||
this.createTime,
|
||
});
|
||
|
||
factory AccountFlow.fromJson(Map<String, dynamic> json) {
|
||
return AccountFlow(
|
||
id: json['id'] as int? ?? 0,
|
||
userId: json['userId'] as int? ?? 0,
|
||
flowType: json['flowType']?.toString() ?? '',
|
||
amount: json['amount']?.toString() ?? '0.00',
|
||
balanceBefore: json['balanceBefore']?.toString() ?? '0.00',
|
||
balanceAfter: json['balanceAfter']?.toString() ?? '0.00',
|
||
remark: json['remark']?.toString() ?? '',
|
||
createTime: json['createTime'] != null
|
||
? DateTime.tryParse(json['createTime'])
|
||
: null,
|
||
);
|
||
}
|
||
|
||
/// 流水类型文字
|
||
String get flowTypeText {
|
||
switch (flowType) {
|
||
case '1':
|
||
return '充值';
|
||
case '2':
|
||
return '提现';
|
||
case '3':
|
||
return '转入交易账户';
|
||
case '4':
|
||
return '从交易账户转出';
|
||
case '5':
|
||
return '卖出收入';
|
||
case '6':
|
||
return '买入支出';
|
||
default:
|
||
return '未知';
|
||
}
|
||
}
|
||
|
||
/// 是否为收入
|
||
bool get isIncome => ['1', '3', '5'].contains(flowType);
|
||
}
|