feat: 重构局域网数据同步与物理药瓶生命周期(Bottle Entity),实现开新瓶、软删除与多端精确余量核算
This commit is contained in:
@@ -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>);
|
||||
}
|
||||
Reference in New Issue
Block a user