feat:额外收费
This commit is contained in:
1 parent
2b956342f4
commit
e730b6923c
17 files changed
+461
-153
No files matched your search
@@ -1,9 +1,62 @@
|
||||
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';
|
||||
import 'package:pj_supermarket_cloud/common/models/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;
|
||||
|
||||
/// 打折(8折->0.8)
|
||||
double? extraPer;
|
||||
|
||||
DiscountsItem({
|
||||
required this.name,
|
||||
required this.hourType,
|
||||
required this.type,
|
||||
required this.basePrice,
|
||||
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<String, dynamic> json) =>
|
||||
_$DiscountsItemFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$DiscountsItemToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
@Entity(tableName: "user_record")
|
||||
class UserEntity extends Equatable {
|
||||
@@ -38,6 +91,12 @@ class UserEntity extends Equatable {
|
||||
/// 用户所属的商户id(主要用于机器的离线模式)
|
||||
String? canteenId;
|
||||
|
||||
/// 优惠活动
|
||||
List<DiscountsItem>? discountsList;
|
||||
|
||||
/// 优惠活动更新时间
|
||||
DateTime? discountsUpdateTime;
|
||||
|
||||
/// 创建时间
|
||||
DateTime? createTime;
|
||||
|
||||
@@ -59,6 +118,28 @@ class UserEntity extends Equatable {
|
||||
this.updateTime,
|
||||
});
|
||||
|
||||
/// 计算优惠后的价格
|
||||
/// [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<String, dynamic> json) =>
|
||||
_$UserEntityFromJson(json);
|
||||
|
||||
|
||||
@@ -6,6 +6,24 @@ part of 'user_entity.dart';
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
DiscountsItem _$DiscountsItemFromJson(Map<String, dynamic> json) =>
|
||||
DiscountsItem(
|
||||
name: json['name'] as String,
|
||||
hourType: JsonTool.stringToProductHourType(json['hourType'] as String?),
|
||||
type: JsonTool.stringToDiscountsType(json['type'] as String?),
|
||||
basePrice: (json['basePrice'] as num).toInt(),
|
||||
extraPer: (json['extraPer'] as num?)?.toDouble(),
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$DiscountsItemToJson(DiscountsItem instance) =>
|
||||
<String, dynamic>{
|
||||
'name': instance.name,
|
||||
'hourType': JsonTool.productHourTypeToString(instance.hourType),
|
||||
'type': JsonTool.discountsTypeToString(instance.type),
|
||||
'basePrice': instance.basePrice,
|
||||
'extraPer': instance.extraPer,
|
||||
};
|
||||
|
||||
UserEntity _$UserEntityFromJson(Map<String, dynamic> json) => UserEntity(
|
||||
id: (json['id'] as num?)?.toInt(),
|
||||
userId: json['userId'] as String,
|
||||
@@ -23,7 +41,13 @@ UserEntity _$UserEntityFromJson(Map<String, dynamic> json) => UserEntity(
|
||||
updateTime: json['updateTime'] == null
|
||||
? null
|
||||
: DateTime.parse(json['updateTime'] as String),
|
||||
);
|
||||
)
|
||||
..discountsList = (json['discountsList'] as List<dynamic>?)
|
||||
?.map((e) => DiscountsItem.fromJson(e as Map<String, dynamic>))
|
||||
.toList()
|
||||
..discountsUpdateTime = json['discountsUpdateTime'] == null
|
||||
? null
|
||||
: DateTime.parse(json['discountsUpdateTime'] as String);
|
||||
|
||||
Map<String, dynamic> _$UserEntityToJson(UserEntity instance) =>
|
||||
<String, dynamic>{
|
||||
@@ -37,6 +61,8 @@ Map<String, dynamic> _$UserEntityToJson(UserEntity instance) =>
|
||||
'walletTypeId': instance.walletTypeId,
|
||||
'walletBalance': instance.walletBalance,
|
||||
'canteenId': instance.canteenId,
|
||||
'discountsList': instance.discountsList,
|
||||
'discountsUpdateTime': instance.discountsUpdateTime?.toIso8601String(),
|
||||
'createTime': instance.createTime?.toIso8601String(),
|
||||
'updateTime': instance.updateTime?.toIso8601String(),
|
||||
};
|
||||
Reference in new issue
Block a user