feat: 增加全家庭智能药箱功能,支持扫码识别(追溯码/GS1/条形码)、出厂有效期与开封保质期双轨追踪及局域网多设备同步

This commit is contained in:
shen
2026-10-27 23:56:47 -07:00
parent ba32e29682
commit 113794fa4b
15 changed files with 2934 additions and 9 deletions
+29
View File
@@ -6,6 +6,7 @@ import 'package:uuid/uuid.dart';
import '../models/bottle.dart';
import '../models/canister.dart';
import '../models/dose_record.dart';
import '../models/medicine_item.dart';
import 'storage_service.dart';
class SyncResult {
@@ -120,6 +121,7 @@ class SyncService {
final localRecords = storageService.getAllRecords(includeDeleted: true);
final localBottles = storageService.getBottles();
final localCanisters = storageService.getCanisters();
final localMedicines = storageService.getMedicines(includeDeleted: true);
final lastSync = await getLastSyncTime();
final body = json.encode({
@@ -129,6 +131,7 @@ class SyncService {
'records': localRecords.map((r) => r.toMap()).toList(),
'bottles': localBottles.map((b) => b.toMap()).toList(),
'canisters': localCanisters.map((c) => c.toMap()).toList(),
'medicines': localMedicines.map((m) => m.toJson()).toList(),
});
final syncUri = Uri.parse('$baseUrl/api/v1/sync');
@@ -230,6 +233,32 @@ class SyncService {
await storageService.saveCanisters(currentCanisters);
}
// 3.5. 合并远端家庭药箱药品数据 (Medicines)
final remoteMedicinesRaw = data['medicines'] as List<dynamic>? ?? [];
if (remoteMedicinesRaw.isNotEmpty) {
final remoteMedicines = remoteMedicinesRaw
.map((item) => MedicineItem.fromJson(item as Map<String, dynamic>))
.toList();
final localMedicines = storageService.getMedicines(includeDeleted: true);
final localMedMap = {for (var m in localMedicines) m.id: m};
for (var rm in remoteMedicines) {
final lm = localMedMap[rm.id];
if (lm == null) {
localMedicines.add(rm);
} else {
if (rm.updatedAt.isAfter(lm.updatedAt) || (rm.isDeleted && !lm.isDeleted)) {
final idx = localMedicines.indexWhere((m) => m.id == rm.id);
if (idx != -1) {
localMedicines[idx] = rm;
}
}
}
}
await storageService.saveMedicines(localMedicines);
}
// 4. 核心终极核算:严格由 active 药瓶 UUID 与全量流水确定性重算各药瓶喷数与开封时间
await storageService.recalculateCanisterDoses();