import 'package:flutter/material.dart'; enum MedicineCategory { oralLiquid, // 口服混悬液/糖浆 eyeDrops, // 滴眼液/滴耳液/滴鼻液 inhaler, // 吸入剂/气雾剂 topical, // 外用乳膏/软膏 tablets, // 片剂/胶囊 spray, // 喷雾剂 other, // 其他 } extension MedicineCategoryExt on MedicineCategory { String get label { switch (this) { case MedicineCategory.oralLiquid: return '口服混悬/糖浆'; case MedicineCategory.eyeDrops: return '滴眼/滴鼻液'; case MedicineCategory.inhaler: return '吸入/气雾剂'; case MedicineCategory.topical: return '外用乳膏/软膏'; case MedicineCategory.tablets: return '片剂/胶囊'; case MedicineCategory.spray: return '外用喷雾'; case MedicineCategory.other: return '其他药品'; } } IconData get icon { switch (this) { case MedicineCategory.oralLiquid: return Icons.water_drop_outlined; case MedicineCategory.eyeDrops: return Icons.remove_red_eye_outlined; case MedicineCategory.inhaler: return Icons.air; case MedicineCategory.topical: return Icons.clean_hands_outlined; case MedicineCategory.tablets: return Icons.medication_outlined; case MedicineCategory.spray: return Icons.scatter_plot_outlined; case MedicineCategory.other: return Icons.medical_services_outlined; } } /// 临床常见开封后推荐保质期天数 int? get defaultShelfLifeDaysAfterOpening { switch (this) { case MedicineCategory.oralLiquid: return 60; // 混悬液开封后一般建议 1~2 个月内用完 case MedicineCategory.eyeDrops: return 28; // 绝大多数眼药水开封 4 周后必须丢弃 case MedicineCategory.inhaler: return 60; // 辅舒酮等拆封后通常建议 60 天 case MedicineCategory.topical: return 90; // 软膏开封后一般 3 个月 case MedicineCategory.spray: return 60; case MedicineCategory.tablets: return null; // 独立泡罩铝箔通常跟随出厂保质期 case MedicineCategory.other: return null; } } } enum MedicineHealthStatus { safe, // 🟢 安全正常 expiringSoon, // 🟡 临期预警 (30天内到期或开封7天内超期) overdueInUse, // 🔴 开封超期 (开封天数超过建议保质期) expired, // 🔴 已过期 (出厂有效期已过) } class MedicineItem { final String id; final String name; final MedicineCategory category; final String spec; // 如 "100ml", "60喷", "24片" final String storageLocation; // 如 "常温药箱", "冰箱冷藏(2-8℃)", "随身包" final String targetUser; // 如 "宝宝", "全家", "爸爸", "妈妈" final String? barcode; // 条码/追溯码 final String? batchNumber; // 批号 final DateTime? productionDate; // 生产日期 final DateTime expiryDate; // 出厂有效期至 (必选) final int? shelfLifeDaysAfterOpening; // 开封后有效天数 final bool isOpened; // 是否已开封 final DateTime? openedAt; // 开封时间 final String? openedByDevice; // 开封设备标识 final String notes; // 适应症/用法备注 final bool isDeleted; // 墓碑标记软删除 final DateTime updatedAt; // 更新时间 UTC const MedicineItem({ required this.id, required this.name, this.category = MedicineCategory.other, this.spec = '', this.storageLocation = '常温药箱', this.targetUser = '全家', this.barcode, this.batchNumber, this.productionDate, required this.expiryDate, this.shelfLifeDaysAfterOpening, this.isOpened = false, this.openedAt, this.openedByDevice, this.notes = '', this.isDeleted = false, required this.updatedAt, }); /// 距出厂到期天数 int get daysUntilExpiry { final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); final exp = DateTime(expiryDate.year, expiryDate.month, expiryDate.day); return exp.difference(today).inDays; } /// 已开封天数 int? get daysSinceOpened { if (!isOpened || openedAt == null) return null; final now = DateTime.now(); final today = DateTime(now.year, now.month, now.day); final op = DateTime(openedAt!.year, openedAt!.month, openedAt!.day); final days = today.difference(op).inDays; return days < 0 ? 0 : days; } /// 距开封后保质期截止天数 (剩余可用天数) int? get daysUntilInUseExpiry { if (!isOpened || openedAt == null || shelfLifeDaysAfterOpening == null) { return null; } final daysUsed = daysSinceOpened ?? 0; return shelfLifeDaysAfterOpening! - daysUsed; } /// 综合健康状态 MedicineHealthStatus get healthStatus { if (daysUntilExpiry < 0) { return MedicineHealthStatus.expired; } if (isOpened && daysUntilInUseExpiry != null && daysUntilInUseExpiry! < 0) { return MedicineHealthStatus.overdueInUse; } final inUseSoon = isOpened && daysUntilInUseExpiry != null && daysUntilInUseExpiry! <= 7; final expSoon = daysUntilExpiry <= 30; if (expSoon || inUseSoon) { return MedicineHealthStatus.expiringSoon; } return MedicineHealthStatus.safe; } String get healthStatusLabel { switch (healthStatus) { case MedicineHealthStatus.safe: return isOpened ? '在用正常' : '未开封'; case MedicineHealthStatus.expiringSoon: if (daysUntilExpiry <= 30) { return '即将到期 ($daysUntilExpiry天)'; } return '开封快到期 ($daysUntilInUseExpiry天)'; case MedicineHealthStatus.overdueInUse: return '⚠️ 开封已超期'; case MedicineHealthStatus.expired: return '❌ 出厂已过期'; } } Color get healthStatusColor { switch (healthStatus) { case MedicineHealthStatus.safe: return isOpened ? const Color(0xFF2563EB) : const Color(0xFF10B981); case MedicineHealthStatus.expiringSoon: return const Color(0xFFF59E0B); case MedicineHealthStatus.overdueInUse: case MedicineHealthStatus.expired: return const Color(0xFFEF4444); } } MedicineItem copyWith({ String? id, String? name, MedicineCategory? category, String? spec, String? storageLocation, String? targetUser, String? barcode, String? batchNumber, DateTime? productionDate, DateTime? expiryDate, int? shelfLifeDaysAfterOpening, bool? isOpened, DateTime? openedAt, String? openedByDevice, String? notes, bool? isDeleted, DateTime? updatedAt, }) { return MedicineItem( id: id ?? this.id, name: name ?? this.name, category: category ?? this.category, spec: spec ?? this.spec, storageLocation: storageLocation ?? this.storageLocation, targetUser: targetUser ?? this.targetUser, barcode: barcode ?? this.barcode, batchNumber: batchNumber ?? this.batchNumber, productionDate: productionDate ?? this.productionDate, expiryDate: expiryDate ?? this.expiryDate, shelfLifeDaysAfterOpening: shelfLifeDaysAfterOpening ?? this.shelfLifeDaysAfterOpening, isOpened: isOpened ?? this.isOpened, openedAt: openedAt ?? this.openedAt, openedByDevice: openedByDevice ?? this.openedByDevice, notes: notes ?? this.notes, isDeleted: isDeleted ?? this.isDeleted, updatedAt: updatedAt ?? this.updatedAt, ); } Map toJson() { return { 'id': id, 'name': name, 'category': category.name, 'spec': spec, 'storageLocation': storageLocation, 'targetUser': targetUser, 'barcode': barcode, 'batchNumber': batchNumber, 'productionDate': productionDate?.toUtc().toIso8601String(), 'expiryDate': expiryDate.toUtc().toIso8601String(), 'shelfLifeDaysAfterOpening': shelfLifeDaysAfterOpening, 'isOpened': isOpened, 'openedAt': openedAt?.toUtc().toIso8601String(), 'openedByDevice': openedByDevice, 'notes': notes, 'isDeleted': isDeleted, 'updatedAt': updatedAt.toUtc().toIso8601String(), }; } factory MedicineItem.fromJson(Map json) { MedicineCategory cat = MedicineCategory.other; final catStr = json['category'] as String?; if (catStr != null) { for (final c in MedicineCategory.values) { if (c.name == catStr) { cat = c; break; } } } return MedicineItem( id: json['id'] as String, name: json['name'] as String, category: cat, spec: (json['spec'] as String?) ?? '', storageLocation: (json['storageLocation'] as String?) ?? '常温药箱', targetUser: (json['targetUser'] as String?) ?? '全家', barcode: json['barcode'] as String?, batchNumber: json['batchNumber'] as String?, productionDate: json['productionDate'] != null ? DateTime.parse(json['productionDate'] as String).toLocal() : null, expiryDate: json['expiryDate'] != null ? DateTime.parse(json['expiryDate'] as String).toLocal() : DateTime.now().add(const Duration(days: 365)), shelfLifeDaysAfterOpening: json['shelfLifeDaysAfterOpening'] as int?, isOpened: (json['isOpened'] as bool?) ?? false, openedAt: json['openedAt'] != null ? DateTime.parse(json['openedAt'] as String).toLocal() : null, openedByDevice: json['openedByDevice'] as String?, notes: (json['notes'] as String?) ?? '', isDeleted: (json['isDeleted'] as bool?) ?? false, updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt'] as String).toUtc() : DateTime.now().toUtc(), ); } }