fix(sync): 修复喷数重复扣减与重装恢复,支持余量校准
This commit is contained in:
@@ -108,15 +108,21 @@ class SyncService {
|
||||
final deviceName = await getDeviceName();
|
||||
final deviceId = await getDeviceId();
|
||||
|
||||
final pingMs = await pingServer(baseUrl);
|
||||
if (pingMs == null) {
|
||||
return SyncResult(
|
||||
success: false,
|
||||
message: '无法连接局域网服务器 ($baseUrl),请检查 Wi-Fi 或 IP 地址',
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
final stopwatch = Stopwatch()..start();
|
||||
final ping = await http
|
||||
.get(Uri.parse('$baseUrl/api/v1/ping'))
|
||||
.timeout(const Duration(seconds: 3));
|
||||
final pingMs = stopwatch.elapsedMilliseconds;
|
||||
if (ping.statusCode != 200) {
|
||||
return SyncResult(success: false, message: '服务器连接检查失败');
|
||||
}
|
||||
final capabilities = jsonDecode(utf8.decode(ping.bodyBytes));
|
||||
final syncVersion = capabilities is Map ? capabilities['sync_version'] : null;
|
||||
if (syncVersion is! int || syncVersion < 2) {
|
||||
return SyncResult(success: false, message: '同步服务器需要更新,才能恢复药瓶和完整用药记录');
|
||||
}
|
||||
|
||||
await storageService.recalculateCanisterDoses();
|
||||
final localRecords = storageService.getAllRecords(includeDeleted: true);
|
||||
final localBottles = storageService.getBottles();
|
||||
@@ -145,38 +151,26 @@ class SyncService {
|
||||
final data = json.decode(utf8.decode(res.bodyBytes)) as Map<String, dynamic>;
|
||||
|
||||
// 1. 合并远端药瓶生命周期实体 Bottles
|
||||
final remoteBottlesRaw = data['bottles'] as List<dynamic>? ?? [];
|
||||
final remoteBottles = remoteBottlesRaw
|
||||
final remoteBottles = (data['bottles'] as List<dynamic>)
|
||||
.map((item) => Bottle.fromMap(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
if (remoteBottles.isNotEmpty) {
|
||||
final bottles = storageService.getBottles();
|
||||
final bottleMap = {for (var b in bottles) b.id: b};
|
||||
|
||||
for (var rb in remoteBottles) {
|
||||
final lb = bottleMap[rb.id];
|
||||
if (lb == null) {
|
||||
bottles.add(rb);
|
||||
} else {
|
||||
// LWW 依据 updatedAt 解决冲突
|
||||
if (rb.updatedAt.isAfter(lb.updatedAt) || (rb.isActive != lb.isActive && !rb.isActive)) {
|
||||
final idx = bottles.indexWhere((b) => b.id == rb.id);
|
||||
if (idx != -1) {
|
||||
bottles[idx] = rb;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
await storageService.saveBottles(bottles);
|
||||
}
|
||||
|
||||
// 2. 双向合并全量流水记录 (含软删除墓碑同步)
|
||||
final remoteRecordsRaw = data['records'] as List<dynamic>? ?? [];
|
||||
final remoteRecords = remoteRecordsRaw
|
||||
final remoteRecords = (data['records'] as List<dynamic>)
|
||||
.map((item) => DoseRecord.fromMap(item as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
final sentBottles = {for (final bottle in localBottles) bottle.id: bottle.toJson()};
|
||||
final bottles = {for (final bottle in storageService.getBottles()) bottle.id: bottle};
|
||||
for (final remote in remoteBottles) {
|
||||
final local = bottles[remote.id];
|
||||
// 服务器已合并这次提交。只保留请求发出后才发生的本地换瓶。
|
||||
if (local == null || local.toJson() == sentBottles[remote.id]) {
|
||||
bottles[remote.id] = remote;
|
||||
}
|
||||
}
|
||||
await storageService.saveBottles(bottles.values.toList());
|
||||
|
||||
// 2. 双向合并全量流水记录 (含软删除墓碑同步)
|
||||
final allLocalRecords = storageService.getAllRecords(includeDeleted: true);
|
||||
final localRecordMap = {for (var r in allLocalRecords) r.id: r};
|
||||
int newCount = 0;
|
||||
@@ -214,23 +208,11 @@ class SyncService {
|
||||
.toList();
|
||||
|
||||
if (remoteCanisters.isNotEmpty) {
|
||||
final currentCanisters = storageService.getCanisters();
|
||||
final localMap = {for (var c in currentCanisters) c.id: c};
|
||||
|
||||
for (var rc in remoteCanisters) {
|
||||
final lc = localMap[rc.id];
|
||||
if (lc != null) {
|
||||
lc.name = rc.name;
|
||||
lc.isEnabled = rc.isEnabled;
|
||||
lc.puffsPerSession = rc.puffsPerSession;
|
||||
lc.inhalationSeconds = rc.inhalationSeconds;
|
||||
lc.sortOrder = rc.sortOrder;
|
||||
lc.note = rc.note;
|
||||
} else {
|
||||
currentCanisters.add(rc);
|
||||
}
|
||||
}
|
||||
await storageService.saveCanisters(currentCanisters);
|
||||
final canisters = {
|
||||
for (final local in storageService.getCanisters()) local.id: local,
|
||||
for (final remote in remoteCanisters) remote.id: remote,
|
||||
};
|
||||
await storageService.saveCanisters(canisters.values.toList());
|
||||
}
|
||||
|
||||
// 3.5. 合并远端家庭药箱药品数据 (Medicines)
|
||||
|
||||
Reference in New Issue
Block a user