feat: 增加全家庭智能药箱功能,支持扫码识别(追溯码/GS1/条形码)、出厂有效期与开封保质期双轨追踪及局域网多设备同步
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user