Files
Aq-Accounting-Flutter/lib/models/budget.dart
T
2026-09-21 17:37:53 +08:00

55 lines
1.5 KiB
Dart

import 'json_util.dart';
/// 预算信息(对应后端 BudgetResponse,仅当月有效)
class BudgetInfo {
final int? id;
final int? year;
final int? month;
final double amount; // 预算总额
final double usedAmount; // 已用
final double remainingAmount; // 剩余
final double remainingDaily; // 日均可用
const BudgetInfo({
this.id,
this.year,
this.month,
required this.amount,
required this.usedAmount,
required this.remainingAmount,
required this.remainingDaily,
});
bool get hasBudget => amount > 0;
bool get isOverBudget => amount > 0 && usedAmount > amount;
/// 已用占比(0~1),未设预算时为 0
double get usedRatio {
if (amount <= 0 || !usedAmount.isFinite) return 0;
return (usedAmount / amount).clamp(0.0, 1.0);
}
factory BudgetInfo.fromJson(Map<String, dynamic> json) {
final amount = asDouble(json['amount']);
final used = asDouble(json['usedAmount']);
return BudgetInfo(
id: json['id'] == null ? null : asInt(json['id']),
year: json['year'] == null ? null : asInt(json['year']),
month: json['month'] == null ? null : asInt(json['month']),
amount: amount,
usedAmount: used,
remainingAmount:
json['remainingAmount'] == null ? amount - used : asDouble(json['remainingAmount']),
remainingDaily: asDouble(json['remainingDaily']),
);
}
static const empty = BudgetInfo(
amount: 0,
usedAmount: 0,
remainingAmount: 0,
remainingDaily: 0,
);
}