chooseVisitTimeContent_vm.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import 'package:cpt_services/modules/services/service_order_confirm/service_order_confirm_vm.dart';
  2. import 'package:domain/entity/service_time_period_entity.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  6. import 'package:riverpod_annotation/riverpod_annotation.dart';
  7. import 'package:router/ext/auto_router_extensions.dart';
  8. import 'package:shared/utils/date_time_utils.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:widgets/dialog/dialog_content_wrap.dart';
  11. import 'package:widgets/load_state_layout.dart';
  12. import '../../../respository/services_respository.dart';
  13. import '../modules/services/service_clean_detail/service_clean_detail_vm.dart';
  14. import 'chooseVisitTimeContent_state.dart';
  15. part 'chooseVisitTimeContent_vm.g.dart';
  16. List<Map<String, dynamic>> visitTimeList = [
  17. // {
  18. // "time_text": "07:00 AM",
  19. // "time": "2025-01-16 07:00:00",
  20. // "urgent": false,
  21. // "holiday": false,
  22. // "night": false,
  23. // "enable": false
  24. // },
  25. ];
  26. @riverpod
  27. class ChooseVisitTimeContentVm extends _$ChooseVisitTimeContentVm {
  28. late ServicesRespository serviceRespositoryInstance;
  29. late BuildContext _context;
  30. late int _id;
  31. late String _cleanServiceType;
  32. ChooseVisitTimeContentState initState(){
  33. Log.d("--------------------------initState---------------------");
  34. List<VisitTimeContentItem> visitTimeListNew = [];
  35. visitTimeList.forEach((item) {
  36. VisitTimeContentItem newItem = VisitTimeContentItem()
  37. ..id = null
  38. ..urgent = item['urgent']
  39. ..holiday = item['holiday']
  40. ..night = item['night']
  41. ..time = item['time']
  42. ..timeText = item['time_text']
  43. ..enable = item['enable']
  44. ..isChecked = false;
  45. visitTimeListNew.add(newItem);
  46. });
  47. return ChooseVisitTimeContentState(
  48. visitTimeList: visitTimeListNew,
  49. currentDate: DateTimeUtils.formatDate(DateTime.now(), format: "yyyy-MM-dd"),
  50. );
  51. }
  52. @override
  53. ChooseVisitTimeContentState build(){
  54. // 引入数据仓库
  55. serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
  56. state = initState();
  57. return state;
  58. }
  59. void changeLoadingState(LoadState loadState, String? errorMsg) {
  60. state = state.copyWith(
  61. loadingState: loadState,
  62. errorMessage: errorMsg
  63. );
  64. // Log.d("87338 ${state.currentDate}");
  65. Log.d("87338 ${state.visitTimeList}");
  66. }
  67. setInitPageData(BuildContext context, int id, String cleanServiceType){
  68. _context = context;
  69. _id = id;
  70. _cleanServiceType = cleanServiceType;
  71. }
  72. initPageData(BuildContext context){
  73. // changeLoadingState(LoadState.State_Success, null);
  74. getVisitTimeList(context, DateTimeUtils.formatDate(DateTime.now(), format: "yyyy-MM-dd"));
  75. }
  76. // 获取指定 日期下面的所有 时间段
  77. Future getVisitTimeList(BuildContext context, String date) async{
  78. Log.d("--------------------------getVisitTimeList---------------------");
  79. // 获取数据
  80. changeLoadingState(LoadState.State_Loading, null);
  81. try {
  82. //请求网络
  83. Map<String, dynamic> params = {
  84. "id": _id,
  85. "date": date,
  86. };
  87. Log.d("请求参数------$params");
  88. final result = await serviceRespositoryInstance.fetchPaidServiceTimePeriod(params);
  89. //校验成功失败
  90. if (result.isSuccess) {
  91. List<ServiceTimePeriodEntity>? resultEntityList = result.list as List<ServiceTimePeriodEntity>;
  92. handlerResultList(resultEntityList);
  93. } else {
  94. String errorMessage = result.errorMsg!;
  95. changeLoadingState(LoadState.State_Error, errorMessage);
  96. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  97. }
  98. } catch (e) {
  99. ToastEngine.show("Error: $e");
  100. }
  101. }
  102. void handlerResultList(List<ServiceTimePeriodEntity>? list) {
  103. if(list != null && list.isNotEmpty){
  104. // 需要模型转换 将 ServiceTimePeriodEntity 转换为 VisitTimeContentItem
  105. List<VisitTimeContentItem> newList = [];
  106. list.asMap().forEach((index,item) {
  107. VisitTimeContentItem newVisitTimeContentItem = VisitTimeContentItem()
  108. ..id = index
  109. ..urgent = item.urgent
  110. ..holiday = item.holiday
  111. ..night = item.night
  112. ..time = item.time
  113. ..timeText = item.timeText
  114. ..enable = item.enable
  115. ..isChecked = false;
  116. newList.add(newVisitTimeContentItem);
  117. });
  118. state.visitTimeList!.clear();
  119. state.visitTimeList!.addAll(newList);
  120. //更新展示的状态
  121. changeLoadingState(LoadState.State_Success, null);
  122. }
  123. }
  124. handlerChangeNum(BuildContext context, int num, int index){
  125. // state.visitTimeList[index].tu = num;
  126. // state = state.copyWith(
  127. // visitTimeList: state.visitTimeList,
  128. // );
  129. }
  130. // 点击 时间段
  131. handlerTimeItemClick(BuildContext context, int index, bool changetoValue){
  132. Log.d("index:${index} 的 isChecked属性 变换成: $changetoValue");
  133. // 修改 状态
  134. if(state.isMultiSelect??false){
  135. // 可多选
  136. state.visitTimeList[index].isChecked = changetoValue;
  137. }else {
  138. // 不可多选
  139. state.visitTimeList.asMap().forEach((i,item) {
  140. if(i == index){
  141. item.isChecked = changetoValue;
  142. }else {
  143. item.isChecked = false;
  144. }
  145. });
  146. }
  147. final cleanDetailVm = ref.read(serviceCleanDetailVmProvider.notifier);
  148. // 结算当前所 预约的价格 (加急/节假日/夜间 等)所有选中的时间段 如果有 紧急、节假日、夜间 则需要加上相应价格
  149. final extraCostItemList = state.allSelectedTimeList?.map((item) {
  150. if (item.urgent ?? false) {
  151. return cleanDetailVm?.state.urgentPrice ?? 0.0;
  152. } else if (item.holiday ?? false) {
  153. return cleanDetailVm?.state.holidayPrice ?? 0.0;
  154. } else if (item.night ?? false) {
  155. return cleanDetailVm?.state.nightPrice ?? 0.0;
  156. } else {
  157. return 0.0;
  158. }
  159. }).toList();
  160. state.extraCostList = extraCostItemList;
  161. Log.d("chooseVisitTimeContent中选择了预约的时间段后 state.extraCostList: ${state.extraCostList}");
  162. state = state.copyWith(
  163. visitTimeList: state.visitTimeList,
  164. extraCostList: extraCostItemList,
  165. );
  166. // 设置 订单确认页面的 总价格 (当点击了 Confirm 按钮后再去设置 订单页面的总价格)
  167. // final serviceOrderConfirmVm = ref.read(serviceOrderConfirmVmProvider.notifier);
  168. // serviceOrderConfirmVm?.setConfirmOrderTotalPrice(context);
  169. }
  170. }