feat: 增加全家庭智能药箱功能,支持扫码识别(追溯码/GS1/条形码)、出厂有效期与开封保质期双轨追踪及局域网多设备同步
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../models/bottle.dart';
|
||||
import '../models/canister.dart';
|
||||
import '../models/dose_record.dart';
|
||||
import '../models/app_settings.dart';
|
||||
import '../models/medicine_item.dart';
|
||||
|
||||
class StorageService {
|
||||
static const String _keySettings = 'aerotrack_settings';
|
||||
static const String _keyCanisters = 'aerotrack_canisters';
|
||||
static const String _keyRecords = 'aerotrack_records';
|
||||
static const String _keyBottles = 'aerotrack_bottles';
|
||||
static const String _keyMedicines = 'aerotrack_medicines';
|
||||
|
||||
final SharedPreferences _prefs;
|
||||
|
||||
@@ -130,6 +133,57 @@ class StorageService {
|
||||
await _prefs.setStringList(_keyRecords, list);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 家庭药箱初始默认药品播种
|
||||
if (!_prefs.containsKey(_keyMedicines)) {
|
||||
final nowUtc = DateTime.now().toUtc();
|
||||
final initialMedicines = [
|
||||
MedicineItem(
|
||||
id: 'med_flixotide_seed',
|
||||
name: '辅舒酮 (丙酸氟替卡松吸入气雾剂)',
|
||||
category: MedicineCategory.inhaler,
|
||||
spec: '50μg/喷 × 60喷',
|
||||
storageLocation: '常温药箱',
|
||||
targetUser: '宝宝',
|
||||
barcode: '6921147600010',
|
||||
expiryDate: DateTime.now().add(const Duration(days: 365 * 2)).toUtc(),
|
||||
shelfLifeDaysAfterOpening: 60,
|
||||
isOpened: true,
|
||||
openedAt: DateTime.now().subtract(const Duration(days: 10)).toUtc(),
|
||||
notes: '拆开铝箔袋后 60 天内用完,吸入后温水漱口',
|
||||
updatedAt: nowUtc,
|
||||
),
|
||||
MedicineItem(
|
||||
id: 'med_meilin_seed',
|
||||
name: '美林 (布洛芬混悬液)',
|
||||
category: MedicineCategory.oralLiquid,
|
||||
spec: '100ml:2g',
|
||||
storageLocation: '常温药箱 (避光)',
|
||||
targetUser: '宝宝',
|
||||
barcode: '6925567800018',
|
||||
expiryDate: DateTime.now().add(const Duration(days: 365 * 2)).toUtc(),
|
||||
shelfLifeDaysAfterOpening: 60,
|
||||
isOpened: false,
|
||||
notes: '发热 38.5℃ 以上使用,间隔 4~6 小时',
|
||||
updatedAt: nowUtc,
|
||||
),
|
||||
MedicineItem(
|
||||
id: 'med_tobramycin_seed',
|
||||
name: '托百士 (妥布霉素滴眼液)',
|
||||
category: MedicineCategory.eyeDrops,
|
||||
spec: '5ml:15mg',
|
||||
storageLocation: '常温药箱',
|
||||
targetUser: '宝宝',
|
||||
barcode: '6936577800015',
|
||||
expiryDate: DateTime.now().add(const Duration(days: 300)).toUtc(),
|
||||
shelfLifeDaysAfterOpening: 28,
|
||||
isOpened: false,
|
||||
notes: '抗生素眼药水,开封后 4 周必须丢弃',
|
||||
updatedAt: nowUtc,
|
||||
),
|
||||
];
|
||||
await saveMedicines(initialMedicines);
|
||||
}
|
||||
}
|
||||
|
||||
// ========== AppSettings ==========
|
||||
@@ -439,4 +493,73 @@ class StorageService {
|
||||
await recalculateCanisterDoses();
|
||||
}
|
||||
}
|
||||
|
||||
// ========== Family Medicine Cabinet (家庭药箱) ==========
|
||||
/// 获取未被软删除的有效药品列表(按有效期升序排序)
|
||||
List<MedicineItem> getMedicines({bool includeDeleted = false}) {
|
||||
final raw = _prefs.getStringList(_keyMedicines);
|
||||
if (raw == null) return [];
|
||||
try {
|
||||
final list = raw
|
||||
.map((item) => MedicineItem.fromJson(jsonDecode(item) as Map<String, dynamic>))
|
||||
.toList();
|
||||
final filtered = includeDeleted ? list : list.where((m) => !m.isDeleted).toList();
|
||||
filtered.sort((a, b) => a.expiryDate.compareTo(b.expiryDate));
|
||||
return filtered;
|
||||
} catch (_) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/// 保存完整药品列表
|
||||
Future<void> saveMedicines(List<MedicineItem> medicines) async {
|
||||
final list = medicines.map((m) => jsonEncode(m.toJson())).toList();
|
||||
await _prefs.setStringList(_keyMedicines, list);
|
||||
}
|
||||
|
||||
/// 新增或更新单个药品
|
||||
Future<void> saveMedicine(MedicineItem medicine) async {
|
||||
final list = getMedicines(includeDeleted: true);
|
||||
final index = list.indexWhere((m) => m.id == medicine.id);
|
||||
if (index >= 0) {
|
||||
list[index] = medicine;
|
||||
} else {
|
||||
list.add(medicine);
|
||||
}
|
||||
await saveMedicines(list);
|
||||
}
|
||||
|
||||
/// 软删除药品(墓碑标记,支持多端同步自动下架)
|
||||
Future<void> deleteMedicine(String id) async {
|
||||
final list = getMedicines(includeDeleted: true);
|
||||
final nowUtc = DateTime.now().toUtc();
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
if (list[i].id == id) {
|
||||
list[i] = list[i].copyWith(
|
||||
isDeleted: true,
|
||||
updatedAt: nowUtc,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
await saveMedicines(list);
|
||||
}
|
||||
|
||||
/// 一键打卡开封
|
||||
Future<void> openMedicine(String id, {String? deviceName}) async {
|
||||
final list = getMedicines(includeDeleted: true);
|
||||
final nowUtc = DateTime.now().toUtc();
|
||||
for (int i = 0; i < list.length; i++) {
|
||||
if (list[i].id == id) {
|
||||
list[i] = list[i].copyWith(
|
||||
isOpened: true,
|
||||
openedAt: nowUtc,
|
||||
openedByDevice: deviceName ?? '本机',
|
||||
updatedAt: nowUtc,
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
await saveMedicines(list);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user