import 'package:common_utils/common_utils.dart'; import 'package:equatable/equatable.dart'; import 'package:floor/floor.dart'; import 'package:json_annotation/json_annotation.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; part 'user_entity.g.dart'; // 优惠 @JsonSerializable() class DiscountsItem { /// 优惠活动名称 String name; /// 用餐时间段类型 @JsonKey( fromJson: JsonTool.stringToProductHourType, toJson: JsonTool.productHourTypeToString, ) ProductHourType? hourType; /// 优惠类型 @JsonKey( fromJson: JsonTool.stringToDiscountsType, toJson: JsonTool.discountsTypeToString, ) DiscountsType? type; /// 基础金额(分),超过了这个基数`extraPer`就生效 int basePrice; /// 特色菜是否额外收费 @JsonKey( fromJson: JsonTool.stringToBoolean, toJson: JsonTool.booleanToString, ) bool specialDishEnable; /// 打折(8折->0.8) double? extraPer; DiscountsItem({ required this.name, required this.hourType, required this.type, required this.basePrice, required this.specialDishEnable, required this.extraPer, }); /// 计算优惠后的价格 /// [totalPrice] 总金额(分) /// [return] 优惠的价格(分) int computedDiscountsExtraPrice(int totalPrice) { if (totalPrice <= basePrice) return 0; final rate = extraPer ?? 1.0; if (rate == 1.0) return 0; return (totalPrice * (rate - 1)).round(); } factory DiscountsItem.fromJson(Map json) => _$DiscountsItemFromJson(json); Map toJson() => _$DiscountsItemToJson(this); } @JsonSerializable() @Entity(tableName: "user_record") class UserEntity extends Equatable { /// 自增ID @PrimaryKey(autoGenerate: true) int? id; /// 用户id String userId; /// 用户名 String nickname; /// 手机号码 String? mobile; /// 用户头像 String? avatar; /// 钱包卡号 String? walletCardNumber; /// 钱包id String? walletId; /// 钱包类型id String? walletTypeId; /// 钱包余额 int? walletBalance; /// 用户所属的商户id(主要用于机器的离线模式) String? canteenId; /// 优惠活动 List? discountsList; /// 优惠活动更新时间 DateTime? discountsUpdateTime; /// 创建时间 DateTime? createTime; /// 同步时间 DateTime? updateTime; UserEntity({ this.id, required this.userId, required this.nickname, this.mobile, this.avatar, this.walletCardNumber, this.walletId, this.walletTypeId, this.walletBalance, this.canteenId, this.createTime, this.updateTime, this.discountsUpdateTime, this.discountsList, }); /// 计算优惠后的价格 /// [totalPrice] 总金额(分) /// [return] 优惠的价格(分) int computedDiscountsPrice(int totalPrice) { if (ObjectUtil.isEmpty(discountsList)) { return 0; } var discountsPrice = 0; for (final item in discountsList!) { discountsPrice += item.computedDiscountsExtraPrice(totalPrice); } return discountsPrice; } /// 根据`discountsUpdateTime`来计算,最小更新时间30s bool isUpdateDiscounts() { if (ObjectUtil.isEmpty(discountsUpdateTime)) { return true; } return DateTime.now().difference(discountsUpdateTime!).inSeconds > 30; } factory UserEntity.fromJson(Map json) => _$UserEntityFromJson(json); Map toJson() => _$UserEntityToJson(this); @override List get props => [ userId, nickname, mobile, walletCardNumber, walletId, walletTypeId, walletBalance, ]; }