facility_booking_view_model.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import 'package:domain/entity/facility_book_entity.dart';
  2. import 'package:domain/repository/facility_repository.dart';
  3. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  4. import 'package:plugin_platform/http/dio/dio_cancelable_mixin.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:router/componentRouter/component_service_manager.dart';
  7. import 'package:shared/utils/log_utils.dart';
  8. import 'package:widgets/load_state_layout.dart';
  9. import '../book_confirm/book_confirm_page.dart';
  10. import '../detail/facility_detail_page.dart';
  11. import 'facility_booking_state.dart';
  12. part 'facility_booking_view_model.g.dart';
  13. @riverpod
  14. class FacilityBookingViewModel extends _$FacilityBookingViewModel with DioCancelableMixin {
  15. late final FacilityRepository _facilityRepository;
  16. @override
  17. FacilityBookingState build() {
  18. _facilityRepository = ref.read(facilityRepositoryProvider);
  19. final state = FacilityBookingState(
  20. selectedDate: DateTime.now(),
  21. );
  22. initListener(state);
  23. registerCancellation(callback: () {
  24. onDispose(state);
  25. });
  26. return state;
  27. }
  28. //设置设施的ID
  29. void setFacilityId(String? facilityId, String? facilityName) {
  30. state = state.copyWith(facilityId: facilityId, facilityName: facilityName);
  31. }
  32. //修改选中的时间
  33. void changeSelectedDate(DateTime dateTime) {
  34. state = state.copyWith(selectedDate: dateTime);
  35. Log.d("当前选中的日期:$dateTime");
  36. fetchListByDate();
  37. }
  38. //失败的重试
  39. void retryRequest() {
  40. fetchListByDate();
  41. }
  42. /// 获取服务器数据
  43. Future fetchListByDate() async {
  44. state = state.copyWith(loadingState: LoadState.State_Loading);
  45. // 获取列表
  46. var result = await _facilityRepository.fetchFacilityDateDetail(
  47. id: state.facilityId,
  48. date: state.selectedDate,
  49. cancelToken: cancelToken,
  50. );
  51. // 处理数据
  52. if (result.isSuccess) {
  53. if (result.data != null) {
  54. //有数据
  55. state = state.copyWith(data: result.data, loadingState: LoadState.State_Success);
  56. } else {
  57. //展示无数据的布局
  58. state = state.copyWith(data: null, loadingState: LoadState.State_Empty, errorMessage: null);
  59. }
  60. } else {
  61. state = state.copyWith(data: null, loadingState: LoadState.State_Error, errorMessage: result.errorMsg);
  62. }
  63. }
  64. void initListener(FacilityBookingState state) {
  65. Log.d("FacilityBookingViewModel - initListener");
  66. }
  67. void onDispose(FacilityBookingState state) {
  68. Log.d("FacilityBookingViewModel - onDispose");
  69. }
  70. //去付款确认页面
  71. void gotoConfirmPage() {
  72. if (state.data == null) return;
  73. //找到数据中的已选中数据
  74. final selectedPeriods = <FacilityBookFacilitiesPeriods>[];
  75. for (var facility in state.data!.facilities!) {
  76. final periods = facility.periods ?? [];
  77. // 筛选出已选中的 periods,并添加到结果列表
  78. selectedPeriods.addAll(
  79. periods.where((period) => period.selected).toList(),
  80. );
  81. }
  82. if (selectedPeriods.isEmpty) {
  83. ToastEngine.show("Select Facility And Date");
  84. } else {
  85. BookConfirmPage.startInstance();
  86. }
  87. }
  88. //选中指定的Task
  89. void selectPeriods(int index, int innerIndex) {
  90. // 创建新的 periods 列表来更新状态
  91. final updatedFacilities = state.data!.facilities!.map((facility) {
  92. // 创建新的 periods 列表
  93. final updatedPeriods = facility.periods!.map((period) {
  94. return FacilityBookFacilitiesPeriods()
  95. ..id = period.id
  96. ..start = period.start
  97. ..end = period.end
  98. ..price = period.price
  99. ..deposit = period.deposit
  100. ..enable = period.enable
  101. ..selected = false; // 设置默认未选中
  102. }).toList();
  103. return FacilityBookFacilities()
  104. ..id = facility.id
  105. ..name = facility.name
  106. ..periods = updatedPeriods;
  107. }).toList();
  108. // 设置指定索引的 selected 为 true
  109. updatedFacilities[index].periods?[innerIndex].selected = true;
  110. // 更新 FacilityBookEntity 的状态
  111. state = state.copyWith(
  112. data: FacilityBookEntity()
  113. ..remainQuota = state.data!.remainQuota
  114. ..quotaResetOn = state.data!.quotaResetOn
  115. ..facilityType = state.data!.facilityType
  116. ..facilities = updatedFacilities, //只更新了facilities对象
  117. index: index,
  118. innerIndex: innerIndex,
  119. );
  120. }
  121. //确认页面执行支付
  122. void doPayment() async {
  123. final result = await _facilityRepository.submitBooking(
  124. periodId: state.data?.facilities?[state.index].periods?[state.innerIndex].id,
  125. date: state.selectedDate,
  126. cancelToken: cancelToken,
  127. );
  128. if (result.isSuccess) {
  129. final bookingId = result.data?.booking?.id;
  130. FacilityDetailPage.startWithPop2Main(bookingId: bookingId);
  131. } else {
  132. ToastEngine.show(result.errorMsg ?? "UnKnow Error");
  133. }
  134. }
  135. //确认页面选择银行卡页面
  136. void gotoChooseCardPage() {
  137. ComponentServiceManager().paymentService.startChooseCardPage();
  138. }
  139. }