feat: 重构局域网数据同步与物理药瓶生命周期(Bottle Entity),实现开新瓶、软删除与多端精确余量核算

This commit is contained in:
shen
2026-10-27 22:42:44 -07:00
parent 065e219a83
commit 89ab13ecf5
9 changed files with 699 additions and 208 deletions
+65
View File
@@ -0,0 +1,65 @@
import 'dart:convert';
class Bottle {
final String id; // 全局唯一 UUID (如: flix_b1_xxxx)
final String canisterId; // 关联的药品ID (如: flixotide_primary / ventolin_primary)
final String name; // 药品展示名称
final int sequenceNumber; // 第几瓶 (1, 2, 3...)
final int totalDoses; // 规格总喷数 (如 60, 200)
final DateTime openedAt; // 严格 UTC 开封时间
DateTime? finishedAt; // 严格 UTC 用尽/归档时间
bool isActive; // 是否为当前正在使用的活跃药瓶
final String openedByDevice; // 开瓶操作的设备名称
DateTime updatedAt; // 严格 UTC 更新时间戳 (用于多端 LWW 冲突解决)
Bottle({
required this.id,
required this.canisterId,
required this.name,
this.sequenceNumber = 1,
required this.totalDoses,
required this.openedAt,
this.finishedAt,
this.isActive = true,
this.openedByDevice = '本机',
DateTime? updatedAt,
}) : updatedAt = updatedAt ?? DateTime.now().toUtc();
Map<String, dynamic> toMap() {
return {
'id': id,
'canisterId': canisterId,
'name': name,
'sequenceNumber': sequenceNumber,
'totalDoses': totalDoses,
'openedAt': openedAt.toUtc().toIso8601String(),
'finishedAt': finishedAt?.toUtc().toIso8601String(),
'isActive': isActive,
'openedByDevice': openedByDevice,
'updatedAt': updatedAt.toUtc().toIso8601String(),
};
}
factory Bottle.fromMap(Map<String, dynamic> map) {
return Bottle(
id: map['id'] as String,
canisterId: map['canisterId'] as String? ?? (map['canister_id'] as String? ?? ''),
name: map['name'] as String? ?? '',
sequenceNumber: map['sequenceNumber'] as int? ?? (map['sequence_number'] as int? ?? 1),
totalDoses: map['totalDoses'] as int? ?? (map['total_doses'] as int? ?? 60),
openedAt: DateTime.parse(map['openedAt'] as String? ?? (map['opened_at'] as String? ?? DateTime.now().toUtc().toIso8601String())).toUtc(),
finishedAt: map['finishedAt'] != null
? DateTime.parse(map['finishedAt'] as String).toUtc()
: (map['finished_at'] != null ? DateTime.parse(map['finished_at'] as String).toUtc() : null),
isActive: map['isActive'] as bool? ?? ((map['is_active'] as int? ?? 1) == 1),
openedByDevice: map['openedByDevice'] as String? ?? (map['opened_by_device'] as String? ?? '家庭设备'),
updatedAt: map['updatedAt'] != null
? DateTime.parse(map['updatedAt'] as String).toUtc()
: (map['updated_at'] != null ? DateTime.parse(map['updated_at'] as String).toUtc() : DateTime.now().toUtc()),
);
}
String toJson() => json.encode(toMap());
factory Bottle.fromJson(String source) =>
Bottle.fromMap(json.decode(source) as Map<String, dynamic>);
}
+2 -2
View File
@@ -39,7 +39,7 @@ class Canister {
'name': name,
'totalDoses': totalDoses,
'remainingDoses': remainingDoses,
'openedAt': openedAt.toIso8601String(),
'openedAt': openedAt.toUtc().toIso8601String(),
'isEnabled': isEnabled,
'puffsPerSession': puffsPerSession,
'inhalationSeconds': inhalationSeconds,
@@ -55,7 +55,7 @@ class Canister {
name: map['name'] as String,
totalDoses: map['totalDoses'] as int,
remainingDoses: map['remainingDoses'] as int,
openedAt: DateTime.parse(map['openedAt'] as String),
openedAt: DateTime.parse(map['openedAt'] as String).toUtc(),
isEnabled: map['isEnabled'] as bool? ?? true,
puffsPerSession: map['puffsPerSession'] as int? ?? 2,
inhalationSeconds: map['inhalationSeconds'] as int? ?? 15,
+33 -15
View File
@@ -4,12 +4,16 @@ class DoseRecord {
final String id;
final DateTime startTime;
final DateTime endTime;
final int flixotidePuffs; // 辅舒酮喷数
final int ventolinPuffs; // 万托林喷数
final bool rinsedMouth; // 是否漱口
final bool washedFace; // 是否洗脸
final bool isPrime; // 是否为试喷扣除
final int flixotidePuffs; // 辅舒酮喷数
final int ventolinPuffs; // 万托林喷数
final String? flixotideBottleId; // 关联的辅舒酮药瓶 UUID
final String? ventolinBottleId; // 关联的万托林药瓶 UUID
final bool rinsedMouth; // 是否漱口
final bool washedFace; // 是否洗脸
final bool isPrime; // 是否为试喷扣除
final bool isDeleted; // 软删除墓碑标记
final String? note;
final DateTime updatedAt; // UTC 更新时间
DoseRecord({
required this.id,
@@ -17,39 +21,53 @@ class DoseRecord {
required this.endTime,
this.flixotidePuffs = 0,
this.ventolinPuffs = 0,
this.flixotideBottleId,
this.ventolinBottleId,
this.rinsedMouth = true,
this.washedFace = true,
this.isPrime = false,
this.isDeleted = false,
this.note,
});
DateTime? updatedAt,
}) : updatedAt = updatedAt ?? DateTime.now().toUtc();
Duration get duration => endTime.difference(startTime);
Map<String, dynamic> toMap() {
return {
'id': id,
'startTime': startTime.toIso8601String(),
'endTime': endTime.toIso8601String(),
'startTime': startTime.toUtc().toIso8601String(),
'endTime': endTime.toUtc().toIso8601String(),
'flixotidePuffs': flixotidePuffs,
'ventolinPuffs': ventolinPuffs,
'flixotideBottleId': flixotideBottleId,
'ventolinBottleId': ventolinBottleId,
'rinsedMouth': rinsedMouth,
'washedFace': washedFace,
'isPrime': isPrime,
'isDeleted': isDeleted,
'note': note,
'updatedAt': updatedAt.toUtc().toIso8601String(),
};
}
factory DoseRecord.fromMap(Map<String, dynamic> map) {
return DoseRecord(
id: map['id'] as String,
startTime: DateTime.parse(map['startTime'] as String),
endTime: DateTime.parse(map['endTime'] as String),
flixotidePuffs: map['flixotidePuffs'] as int? ?? 0,
ventolinPuffs: map['ventolinPuffs'] as int? ?? 0,
rinsedMouth: map['rinsedMouth'] as bool? ?? true,
washedFace: map['washedFace'] as bool? ?? true,
isPrime: map['isPrime'] as bool? ?? false,
startTime: DateTime.parse(map['startTime'] as String? ?? (map['start_time'] as String)).toUtc(),
endTime: DateTime.parse(map['endTime'] as String? ?? (map['end_time'] as String)).toUtc(),
flixotidePuffs: map['flixotidePuffs'] as int? ?? (map['flixotide_puffs'] as int? ?? 0),
ventolinPuffs: map['ventolinPuffs'] as int? ?? (map['ventolin_puffs'] as int? ?? 0),
flixotideBottleId: map['flixotideBottleId'] as String? ?? (map['flixotide_bottle_id'] as String?),
ventolinBottleId: map['ventolinBottleId'] as String? ?? (map['ventolin_bottle_id'] as String?),
rinsedMouth: map['rinsedMouth'] as bool? ?? ((map['rinsed_mouth'] as int? ?? 1) == 1),
washedFace: map['washedFace'] as bool? ?? ((map['washed_face'] as int? ?? 1) == 1),
isPrime: map['isPrime'] as bool? ?? ((map['is_prime'] as int? ?? 0) == 1),
isDeleted: map['isDeleted'] as bool? ?? ((map['is_deleted'] as int? ?? 0) == 1),
note: map['note'] as String?,
updatedAt: map['updatedAt'] != null
? DateTime.parse(map['updatedAt'] as String).toUtc()
: (map['updated_at'] != null ? DateTime.parse(map['updated_at'] as String).toUtc() : DateTime.now().toUtc()),
);
}