54 lines
1.8 KiB
Dart
54 lines
1.8 KiB
Dart
|
|
// 功能描述:购物车条目、并发版本和结算快照;版本:1.0.0。
|
|||
|
|
import 'primary_models.dart';
|
|||
|
|
|
|||
|
|
/// 服务端购物车快照;revision 用于阻止过期操作覆盖其他设备的修改。
|
|||
|
|
class CartItem {
|
|||
|
|
const CartItem({
|
|||
|
|
required this.product,
|
|||
|
|
required this.quantity,
|
|||
|
|
required this.selected,
|
|||
|
|
required this.available,
|
|||
|
|
required this.revision,
|
|||
|
|
this.specification = '',
|
|||
|
|
});
|
|||
|
|
final ProductSummary product;
|
|||
|
|
final int quantity;
|
|||
|
|
final bool selected, available;
|
|||
|
|
final String revision;
|
|||
|
|
final String specification;
|
|||
|
|
bool get purchasable => available && quantity > 0 && quantity <= product.stock;
|
|||
|
|
int get amount => product.price * quantity;
|
|||
|
|
|
|||
|
|
factory CartItem.fromJson(Map<String, Object?> json, String Function(String) resolve) {
|
|||
|
|
final price = json['price_amount'], stock = json['stock_quantity'], quantity = json['quantity'];
|
|||
|
|
if (price is! int ||
|
|||
|
|
price < 0 ||
|
|||
|
|
stock is! int ||
|
|||
|
|
quantity is! int ||
|
|||
|
|
quantity < 0 ||
|
|||
|
|
quantity > 999 ||
|
|||
|
|
json['product_identity'] is! String ||
|
|||
|
|
(json['product_identity'] as String).isEmpty ||
|
|||
|
|
json['revision'] is! String ||
|
|||
|
|
json['selected'] is! bool ||
|
|||
|
|
json['available'] is! bool) {
|
|||
|
|
throw const FormatException('购物车数据异常');
|
|||
|
|
}
|
|||
|
|
return CartItem(
|
|||
|
|
product: ProductSummary(
|
|||
|
|
identity: json['product_identity'] as String,
|
|||
|
|
name: json['name'] as String? ?? '',
|
|||
|
|
price: price,
|
|||
|
|
stock: stock,
|
|||
|
|
imageUrl: resolve(json['image_url'] as String? ?? ''),
|
|||
|
|
category: json['category_name'] as String? ?? '',
|
|||
|
|
),
|
|||
|
|
quantity: quantity,
|
|||
|
|
selected: json['selected'] as bool,
|
|||
|
|
available: json['available'] as bool,
|
|||
|
|
revision: json['revision'] as String,
|
|||
|
|
specification: json['specification'] as String? ?? '',
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
}
|