fix(sync): 修复喷数重复扣减与重装恢复,支持余量校准

This commit is contained in:
shen
2026-11-25 03:43:24 -08:00
parent d322ae4d87
commit daccb4c04c
10 changed files with 576 additions and 123 deletions
+8
View File
@@ -10,6 +10,8 @@ class Bottle {
DateTime? finishedAt; // 严格 UTC 用尽/归档时间
bool isActive; // 是否为当前正在使用的活跃药瓶
final String openedByDevice; // 开瓶操作的设备名称
final int? calibratedRemainingDoses;
final DateTime? calibratedAt;
DateTime updatedAt; // 严格 UTC 更新时间戳 (用于多端 LWW 冲突解决)
Bottle({
@@ -20,6 +22,8 @@ class Bottle {
required this.totalDoses,
required this.openedAt,
this.finishedAt,
this.calibratedRemainingDoses,
this.calibratedAt,
this.isActive = true,
this.openedByDevice = '本机',
DateTime? updatedAt,
@@ -36,6 +40,8 @@ class Bottle {
'finishedAt': finishedAt?.toUtc().toIso8601String(),
'isActive': isActive,
'openedByDevice': openedByDevice,
'calibratedRemainingDoses': calibratedRemainingDoses,
'calibratedAt': calibratedAt?.toUtc().toIso8601String(),
'updatedAt': updatedAt.toUtc().toIso8601String(),
};
}
@@ -53,6 +59,8 @@ class Bottle {
: (map['finished_at'] != null ? DateTime.parse(map['finished_at'] as String).toUtc() : null),
isActive: map['isActive'] as bool? ?? ((map['is_active'] as int? ?? 1) == 1),
openedByDevice: map['openedByDevice'] as String? ?? (map['opened_by_device'] as String? ?? '家庭设备'),
calibratedRemainingDoses: map['calibratedRemainingDoses'] as int?,
calibratedAt: DateTime.tryParse(map['calibratedAt'] as String? ?? '')?.toUtc(),
updatedAt: map['updatedAt'] != null
? DateTime.parse(map['updatedAt'] as String).toUtc()
: (map['updated_at'] != null ? DateTime.parse(map['updated_at'] as String).toUtc() : DateTime.now().toUtc()),