Files
platforms/apps/user_app/lib/domain/models/cart_item.dart
czl231 3ef33b531d 已完成用户APP首期功能开发
交付用户端首期页面、配套接口、后台资源及测试文档。用户APP构建、静态分析和三个管理后台构建通过;完整测试仍有2项失败,后端模型注释检查未通过,详见交付记录。
2026-09-13 00:57:32 +08:00

54 lines
1.8 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 功能描述购物车条目、并发版本和结算快照版本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? ?? '',
);
}
}