diff --git a/lib/app.dart b/lib/app.dart index 765dadf..82d2d79 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -3,12 +3,40 @@ import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:forui/forui.dart'; import 'package:get/get.dart'; import 'package:ducafe_ui_core/ducafe_ui_core.dart'; +import 'package:flutter_subscreen_plugin/flutter_subscreen_plugin.dart'; import 'package:pj_supermarket_cloud/common/index.dart'; -class MainApp extends StatelessWidget { +class MainApp extends StatefulWidget { const MainApp({super.key}); + @override + State createState() => _MainAppState(); +} + +class _MainAppState extends State with WidgetsBindingObserver { + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addObserver(this); + } + + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); + } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) async { + if (state == AppLifecycleState.paused || + state == AppLifecycleState.detached) { + try { + SubScreenPlugin.doubleScreenCancel(); + } catch (_) {} + } + } + @override Widget build(BuildContext context) { return ScreenUtilInit( diff --git a/lib/common/services/database/database.g.dart b/lib/common/services/database/database.g.dart index 546c2c1..ae8f7ea 100644 --- a/lib/common/services/database/database.g.dart +++ b/lib/common/services/database/database.g.dart @@ -1600,7 +1600,11 @@ class _$UserDao extends UserDao { createTime: _dateTimeEmptyConverter.decode(row['createTime'] as String), updateTime: - _dateTimeEmptyConverter.decode(row['updateTime'] as String)), + _dateTimeEmptyConverter.decode(row['updateTime'] as String), + discountsUpdateTime: _dateTimeEmptyConverter + .decode(row['discountsUpdateTime'] as String), + discountsList: + _listDiscountsConverter.decode(row['discountsList'] as String)), arguments: [userId]); } @@ -1622,7 +1626,11 @@ class _$UserDao extends UserDao { createTime: _dateTimeEmptyConverter.decode(row['createTime'] as String), updateTime: - _dateTimeEmptyConverter.decode(row['updateTime'] as String)), + _dateTimeEmptyConverter.decode(row['updateTime'] as String), + discountsUpdateTime: _dateTimeEmptyConverter + .decode(row['discountsUpdateTime'] as String), + discountsList: + _listDiscountsConverter.decode(row['discountsList'] as String)), arguments: [walletCardNumber]); } diff --git a/lib/common/services/database/entity/user_entity.dart b/lib/common/services/database/entity/user_entity.dart index d503972..01e2171 100644 --- a/lib/common/services/database/entity/user_entity.dart +++ b/lib/common/services/database/entity/user_entity.dart @@ -123,6 +123,8 @@ class UserEntity extends Equatable { this.canteenId, this.createTime, this.updateTime, + this.discountsUpdateTime, + this.discountsList, }); /// 计算优惠后的价格 diff --git a/lib/common/services/database/entity/user_entity.g.dart b/lib/common/services/database/entity/user_entity.g.dart index bea6966..185c3a6 100644 --- a/lib/common/services/database/entity/user_entity.g.dart +++ b/lib/common/services/database/entity/user_entity.g.dart @@ -44,13 +44,13 @@ UserEntity _$UserEntityFromJson(Map json) => UserEntity( updateTime: json['updateTime'] == null ? null : DateTime.parse(json['updateTime'] as String), - ) - ..discountsList = (json['discountsList'] as List?) - ?.map((e) => DiscountsItem.fromJson(e as Map)) - .toList() - ..discountsUpdateTime = json['discountsUpdateTime'] == null + discountsUpdateTime: json['discountsUpdateTime'] == null ? null - : DateTime.parse(json['discountsUpdateTime'] as String); + : DateTime.parse(json['discountsUpdateTime'] as String), + discountsList: (json['discountsList'] as List?) + ?.map((e) => DiscountsItem.fromJson(e as Map)) + .toList(), + ); Map _$UserEntityToJson(UserEntity instance) => { diff --git a/lib/common/services/database/type_converter/type_converter.dart b/lib/common/services/database/type_converter/type_converter.dart index 2ef3dda..30b45a4 100644 --- a/lib/common/services/database/type_converter/type_converter.dart +++ b/lib/common/services/database/type_converter/type_converter.dart @@ -153,21 +153,24 @@ class ListDiscountsConverter extends TypeConverter?, String> { @override List decode(String databaseValue) { - if (databaseValue.isEmpty) { - return []; + if (databaseValue.isEmpty) return []; + final raw = json.decode(databaseValue); + if (raw is List) { + return raw + .map( + (e) => DiscountsItem.fromJson( + Map.from(e as Map), + ), + ) + .toList(); } - return json - .decode(databaseValue) - .map((e) => DiscountsItem.fromJson(e)) - .toList(); + return []; } @override String encode(List? value) { - if (ObjectUtil.isEmptyList(value)) { - return ''; - } - return json.encode(value); + if (ObjectUtil.isEmptyList(value)) return '[]'; + return json.encode(value!.map((e) => e.toJson()).toList()); } } diff --git a/lib/common/utils/discounts/discounts_tool.dart b/lib/common/utils/discounts/discounts_tool.dart index a7b53e0..364a8fe 100644 --- a/lib/common/utils/discounts/discounts_tool.dart +++ b/lib/common/utils/discounts/discounts_tool.dart @@ -39,6 +39,9 @@ class DiscountsTool { required List shopCardList, required String hourType, }) async { + if (ObjectUtil.isEmpty(shopCardList)) { + return 0; + } var discountsItems = await updateUserDiscountsList(userId: userId); discountsItems = discountsItems ?.where((element) => element.hourType?.code == hourType) @@ -51,23 +54,10 @@ class DiscountsTool { final extraPer = item.extraPer; final specialDishEnable = item.specialDishEnable; if (extraPer == null || extraPer <= 1.0) continue; - // 计算总价 - int totalPrice = 0; - // 特色菜不额外收费 - if (!specialDishEnable) { - totalPrice = shopCardList - .where((item) => item.isPriceIncreased) - .map((item) => item.totalPrice) - .reduce((value, element) => value + element); - extraPrice += item.computedDiscountsExtraPrice(totalPrice); - } - // 特色菜额外收费 - if (specialDishEnable) { - totalPrice = shopCardList - .map((item) => item.totalPrice) - .reduce((value, element) => value + element); - extraPrice += item.computedDiscountsExtraPrice(totalPrice); - } + final iterable = specialDishEnable + ? shopCardList + : shopCardList.where((e) => e.isPriceIncreased); + final totalPrice = iterable.fold(0, (sum, e) => sum + e.totalPrice); extraPrice += item.computedDiscountsExtraPrice(totalPrice); } return extraPrice; diff --git a/lib/common/widgets/order/mixin/order_mixin.dart b/lib/common/widgets/order/mixin/order_mixin.dart index 57bc213..e178a41 100644 --- a/lib/common/widgets/order/mixin/order_mixin.dart +++ b/lib/common/widgets/order/mixin/order_mixin.dart @@ -307,6 +307,7 @@ mixin OrderMixin { "productCount": v.productNumber, "productWeight": 0, "productNature": ProductNature.DEFAULT.name, + "isPriceIncreased": v.isPriceIncreased, }; }).toList(); } else if (_sysType == SysType.SUPERMARKET) { @@ -316,6 +317,7 @@ mixin OrderMixin { "productCount": v.productNumber, "productWeight": v.productWidget, "productNature": v.nature.name, + "isPriceIncreased": v.isPriceIncreased, }; }).toList(); } @@ -332,6 +334,7 @@ mixin OrderMixin { "orderSourceTypeId": getOrderSourceTypeId(), "orderPayType": orderPayType.value?.name, "list": shopList, + "extraPrice": amountMealPrice.value, }; if (orderPayType.value != null && appSetting.sysType == SysType.CANTEEN && diff --git a/lib/sub_screen/page/sub_product.dart b/lib/sub_screen/page/sub_product.dart index cacf05f..3f1650f 100644 --- a/lib/sub_screen/page/sub_product.dart +++ b/lib/sub_screen/page/sub_product.dart @@ -131,7 +131,77 @@ class _SubProductPageState extends State { final width = constraints.maxWidth; final height = constraints.maxHeight; final hdHeight = height / 11; - final hdWidth = width / 4.1; + final hdWidth = width / (_sysType == SysType.CANTEEN ? 5.1 : 4.1); + + final columns = [ + DataColumn2( + label: Text("名称", textAlign: TextAlign.center), + headingRowAlignment: MainAxisAlignment.center, + fixedWidth: hdWidth, + ), + DataColumn2( + label: Text("数量", textAlign: TextAlign.center), + headingRowAlignment: MainAxisAlignment.center, + fixedWidth: hdWidth, + ), + DataColumn2( + label: Text("单价", textAlign: TextAlign.center), + headingRowAlignment: MainAxisAlignment.center, + fixedWidth: hdWidth, + ), + DataColumn2( + label: Text("总价", textAlign: TextAlign.center), + headingRowAlignment: MainAxisAlignment.center, + fixedWidth: hdWidth, + ), + ]; + if (_sysType == SysType.CANTEEN) { + columns.insert( + 1, + DataColumn2( + label: Text("是否额外收费", textAlign: TextAlign.center), + headingRowAlignment: MainAxisAlignment.center, + fixedWidth: hdWidth, + ), + ); + } + final rows = _shopList.map((v) { + final cells = [ + DataCell( + Text( + v.productName, + textAlign: TextAlign.center, + ).center(), + ), + DataCell( + Text( + v.nature == ProductNature.DEFAULT + ? v.productNumber.toString() + : "${v.productWidget}/kg", + textAlign: TextAlign.center, + ).center(), + ), + DataCell( + appPrice(context, v.productPrice).center(), + ), + DataCell( + appPrice(context, v.totalPrice).center(), + ), + ]; + if (_sysType == SysType.CANTEEN) { + cells.insert( + 1, + DataCell( + Text( + v.isPriceIncreased ? "是" : "否", + textAlign: TextAlign.center, + ).center(), + ), + ); + } + return DataRow(cells: cells); + }); + return DataTable2( columnSpacing: 12.w, horizontalMargin: 12.w, @@ -140,54 +210,8 @@ class _SubProductPageState extends State { headingTextStyle: theme.typography.lg, dataTextStyle: theme.typography.base, dataRowHeight: hdHeight, - columns: [ - DataColumn2( - label: Text("名称", textAlign: TextAlign.center), - headingRowAlignment: MainAxisAlignment.center, - fixedWidth: hdWidth, - ), - DataColumn2( - label: Text("数量", textAlign: TextAlign.center), - headingRowAlignment: MainAxisAlignment.center, - fixedWidth: hdWidth, - ), - DataColumn2( - label: Text("单价", textAlign: TextAlign.center), - headingRowAlignment: MainAxisAlignment.center, - fixedWidth: hdWidth, - ), - DataColumn2( - label: Text("总价", textAlign: TextAlign.center), - headingRowAlignment: MainAxisAlignment.center, - fixedWidth: hdWidth, - ), - ], - rows: _shopList.map((v) { - return DataRow( - cells: [ - DataCell( - Text( - v.productName, - textAlign: TextAlign.center, - ).center(), - ), - DataCell( - Text( - v.nature == ProductNature.DEFAULT - ? v.productNumber.toString() - : "${v.productWidget}/kg", - textAlign: TextAlign.center, - ).center(), - ), - DataCell( - appPrice(context, v.productPrice).center(), - ), - DataCell( - appPrice(context, v.totalPrice).center(), - ), - ], - ); - }).toList(), + columns: columns, + rows: rows.toList(), ); }, );