fix(sync): 修复喷数重复扣减与重装恢复,支持余量校准
This commit is contained in:
@@ -15,6 +15,7 @@ class StorageService {
|
||||
static const String _keyMedicines = 'aerotrack_medicines';
|
||||
|
||||
final SharedPreferences _prefs;
|
||||
Future<void> _recordWrite = Future<void>.value();
|
||||
|
||||
StorageService(this._prefs);
|
||||
|
||||
@@ -72,7 +73,7 @@ class StorageService {
|
||||
openedAt: DateTime(2020, 1, 1).toUtc(),
|
||||
isActive: true,
|
||||
openedByDevice: '系统初始化',
|
||||
updatedAt: DateTime.now().toUtc(),
|
||||
updatedAt: DateTime.utc(2020),
|
||||
);
|
||||
|
||||
final initialVentBottle = Bottle(
|
||||
@@ -84,7 +85,7 @@ class StorageService {
|
||||
openedAt: DateTime(2020, 1, 1).toUtc(),
|
||||
isActive: true,
|
||||
openedByDevice: '系统初始化',
|
||||
updatedAt: DateTime.now().toUtc(),
|
||||
updatedAt: DateTime.utc(2020),
|
||||
);
|
||||
|
||||
await saveBottles([initialFlixBottle, initialVentBottle]);
|
||||
@@ -207,7 +208,12 @@ class StorageService {
|
||||
if (raw == null) return [];
|
||||
try {
|
||||
final list = raw.map((item) => Bottle.fromJson(item)).toList();
|
||||
list.sort((a, b) => a.sequenceNumber.compareTo(b.sequenceNumber));
|
||||
list.sort((a, b) {
|
||||
final sequence = a.sequenceNumber.compareTo(b.sequenceNumber);
|
||||
if (sequence != 0) return sequence;
|
||||
final opening = a.openedAt.compareTo(b.openedAt);
|
||||
return opening != 0 ? opening : a.id.compareTo(b.id);
|
||||
});
|
||||
return list;
|
||||
} catch (_) {
|
||||
return [];
|
||||
@@ -222,7 +228,7 @@ class StorageService {
|
||||
Bottle? getActiveBottle(String canisterId) {
|
||||
final bottles = getBottles();
|
||||
try {
|
||||
return bottles.firstWhere((b) => b.canisterId == canisterId && b.isActive);
|
||||
return bottles.lastWhere((b) => b.canisterId == canisterId && b.isActive);
|
||||
} catch (_) {
|
||||
final matched = bottles.where((b) => b.canisterId == canisterId).toList();
|
||||
return matched.isNotEmpty ? matched.last : null;
|
||||
@@ -347,29 +353,6 @@ class StorageService {
|
||||
await saveCanisters(canisters);
|
||||
}
|
||||
|
||||
Future<void> deductSessionDoses(Map<String, int> deductions) async {
|
||||
final canisters = getCanisters();
|
||||
for (var c in canisters) {
|
||||
if (deductions.containsKey(c.id)) {
|
||||
final used = deductions[c.id]!;
|
||||
c.remainingDoses = (c.remainingDoses - used).clamp(0, c.totalDoses);
|
||||
}
|
||||
}
|
||||
await saveCanisters(canisters);
|
||||
}
|
||||
|
||||
Future<void> deductDoses({int flixotideDoses = 0, int ventolinDoses = 0}) async {
|
||||
final canisters = getCanisters();
|
||||
for (var c in canisters) {
|
||||
if (c.isSteroid && flixotideDoses > 0) {
|
||||
c.remainingDoses = (c.remainingDoses - flixotideDoses).clamp(0, c.totalDoses);
|
||||
} else if (!c.isSteroid && ventolinDoses > 0) {
|
||||
c.remainingDoses = (c.remainingDoses - ventolinDoses).clamp(0, c.totalDoses);
|
||||
}
|
||||
}
|
||||
await saveCanisters(canisters);
|
||||
}
|
||||
|
||||
Future<void> resetCanisterDoses(String canisterId, int newTotal, {String deviceName = '本机'}) async {
|
||||
await openNewBottle(canisterId, deviceName: deviceName);
|
||||
}
|
||||
@@ -386,7 +369,17 @@ class StorageService {
|
||||
int consumedPuffs = 0;
|
||||
|
||||
if (activeBottle != null) {
|
||||
final isInitialBottle = activeBottle.id == 'flix_b1_initial' ||
|
||||
activeBottle.id == 'vent_b1_initial';
|
||||
final calibrationTime = activeBottle.calibratedAt;
|
||||
for (var r in records) {
|
||||
if (calibrationTime != null && !r.startTime.isAfter(calibrationTime)) {
|
||||
continue;
|
||||
}
|
||||
// 旧版本可能已将换瓶前的记录绑定到固定的初始瓶 ID。
|
||||
if (isInitialBottle && r.startTime.isBefore(activeBottle.openedAt)) {
|
||||
continue;
|
||||
}
|
||||
if (c.isSteroid) {
|
||||
// 优先依据明确绑定的 bottleId 核算
|
||||
if (r.flixotideBottleId == activeBottle.id) {
|
||||
@@ -404,7 +397,8 @@ class StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
final correctRemaining = (activeBottle.totalDoses - consumedPuffs).clamp(0, activeBottle.totalDoses);
|
||||
final baseline = activeBottle.calibratedRemainingDoses ?? activeBottle.totalDoses;
|
||||
final correctRemaining = (baseline - consumedPuffs).clamp(0, activeBottle.totalDoses);
|
||||
if (c.remainingDoses != correctRemaining || c.openedAt != activeBottle.openedAt) {
|
||||
c.remainingDoses = correctRemaining;
|
||||
c.openedAt = activeBottle.openedAt;
|
||||
@@ -450,12 +444,25 @@ class StorageService {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> addRecord(DoseRecord record) async {
|
||||
final records = getAllRecords(includeDeleted: true);
|
||||
records.insert(0, record);
|
||||
final list = records.map((r) => r.toJson()).toList();
|
||||
await _prefs.setStringList(_keyRecords, list);
|
||||
await recalculateCanisterDoses();
|
||||
Future<void> addRecord(DoseRecord record) {
|
||||
final write = _recordWrite.then((_) async {
|
||||
final records = getAllRecords(includeDeleted: true);
|
||||
// 同一操作只入账一次,已删除的记录也不能被重试重新启用。
|
||||
if (!records.any((existing) => existing.id == record.id)) {
|
||||
records.insert(0, record);
|
||||
final list = records.map((r) => r.toJson()).toList();
|
||||
if (!await _prefs.setStringList(_keyRecords, list)) {
|
||||
throw StateError('用药记录保存失败');
|
||||
}
|
||||
}
|
||||
await recalculateCanisterDoses();
|
||||
});
|
||||
// 写入失败仍允许后续重试;调用方通过 write 接收原始错误。
|
||||
_recordWrite = write.then<void>(
|
||||
(_) {},
|
||||
onError: (Object error, StackTrace stack) {},
|
||||
);
|
||||
return write;
|
||||
}
|
||||
|
||||
/// 软删除记录(墓碑机制):全网标记废弃,喷数自动准确恢复
|
||||
|
||||
Reference in New Issue
Block a user