79 lines
2.9 KiB
Dart
79 lines
2.9 KiB
Dart
// 功能描述:定义用气统计的服务端事实模型;版本:1.0.0。
|
||
|
||
class UsagePoint {
|
||
const UsagePoint({required this.date, required this.usage});
|
||
final DateTime date;
|
||
final double usage;
|
||
|
||
factory UsagePoint.fromJson(Map<String, Object?> json) => UsagePoint(
|
||
date: DateTime.parse(json['date'] as String),
|
||
usage: (json['usage'] as num).toDouble(),
|
||
);
|
||
}
|
||
|
||
class UsageComposition {
|
||
const UsageComposition({required this.breakfast, required this.lunch, required this.dinner});
|
||
final double breakfast, lunch, dinner;
|
||
|
||
factory UsageComposition.fromJson(Map<String, Object?> json) => UsageComposition(
|
||
breakfast: (json['breakfast'] as num? ?? 0).toDouble(),
|
||
lunch: (json['lunch'] as num? ?? 0).toDouble(),
|
||
dinner: (json['dinner'] as num? ?? 0).toDouble(),
|
||
);
|
||
}
|
||
|
||
class UsageStatistics {
|
||
const UsageStatistics({
|
||
required this.available,
|
||
required this.deviceIdentity,
|
||
required this.deviceName,
|
||
required this.deviceCode,
|
||
required this.period,
|
||
required this.periodStart,
|
||
required this.periodEnd,
|
||
required this.unit,
|
||
required this.points,
|
||
required this.totalUsage,
|
||
required this.averageUsage,
|
||
required this.composition,
|
||
required this.source,
|
||
required this.calcVersion,
|
||
required this.updatedAt,
|
||
required this.unavailableReason,
|
||
});
|
||
|
||
final bool available;
|
||
final String deviceIdentity, deviceName, deviceCode, period, unit;
|
||
final DateTime periodStart, periodEnd;
|
||
final List<UsagePoint> points;
|
||
final double totalUsage, averageUsage;
|
||
final UsageComposition composition;
|
||
final String source, calcVersion, unavailableReason;
|
||
final DateTime? updatedAt;
|
||
|
||
factory UsageStatistics.fromJson(Map<String, Object?> json) {
|
||
final device = json['device'] as Map<String, Object?>? ?? const {};
|
||
final composition = json['composition'] as Map<String, Object?>? ?? const {};
|
||
return UsageStatistics(
|
||
available: json['available'] == true,
|
||
deviceIdentity: device['identity'] as String? ?? '',
|
||
deviceName: device['name'] as String? ?? '',
|
||
deviceCode: device['code'] as String? ?? '',
|
||
period: json['period'] as String? ?? 'month',
|
||
periodStart: DateTime.parse(json['period_start'] as String),
|
||
periodEnd: DateTime.parse(json['period_end'] as String),
|
||
unit: json['unit'] as String? ?? 'kg',
|
||
points: (json['points'] as List<Object?>? ?? const [])
|
||
.map((item) => UsagePoint.fromJson(item as Map<String, Object?>))
|
||
.toList(),
|
||
totalUsage: (json['total_usage'] as num? ?? 0).toDouble(),
|
||
averageUsage: (json['average_usage'] as num? ?? 0).toDouble(),
|
||
composition: UsageComposition.fromJson(composition),
|
||
source: json['source'] as String? ?? '',
|
||
calcVersion: json['calc_version'] as String? ?? '',
|
||
updatedAt: DateTime.tryParse(json['updated_at']?.toString() ?? '')?.toLocal(),
|
||
unavailableReason: json['unavailable_reason'] as String? ?? '',
|
||
);
|
||
}
|
||
}
|