chooseVisitTimeContent_vm.dart 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 'chooseVisitTimeContent_state.dart';
  14. part 'chooseVisitTimeContent_vm.g.dart';
  15. List<Map<String, dynamic>> visitTimeList = [
  16. // {
  17. // "time_text": "07:00 AM",
  18. // "time": "2025-01-16 07:00:00",
  19. // "urgent": false,
  20. // "holiday": false,
  21. // "night": false,
  22. // "enable": false
  23. // },
  24. ];
  25. @riverpod
  26. class ChooseVisitTimeContentVm extends _$ChooseVisitTimeContentVm {
  27. late ServicesRespository serviceRespositoryInstance;
  28. late BuildContext _context;
  29. late int _id;
  30. late String _cleanServiceType;
  31. ChooseVisitTimeContentState initState(){
  32. Log.d("--------------------------initState---------------------");
  33. List<VisitTimeContentItem> visitTimeListNew = [];
  34. visitTimeList.forEach((item) {
  35. VisitTimeContentItem newItem = VisitTimeContentItem()
  36. ..id = null
  37. ..urgent = item['urgent']
  38. ..holiday = item['holiday']
  39. ..night = item['night']
  40. ..time = item['time']
  41. ..timeText = item['time_text']
  42. ..enable = item['enable']
  43. ..isChecked = false;
  44. visitTimeListNew.add(newItem);
  45. });
  46. return ChooseVisitTimeContentState(
  47. visitTimeList: visitTimeListNew,
  48. currentDate: DateTimeUtils.formatDate(DateTime.now(), format: "yyyy-MM-dd"),
  49. );
  50. }
  51. @override
  52. ChooseVisitTimeContentState build(){
  53. // 引入数据仓库
  54. serviceRespositoryInstance = ref.read(servicesRespositoryProvider);
  55. state = initState();
  56. return state;
  57. }
  58. void changeLoadingState(LoadState loadState, String? errorMsg) {
  59. state = state.copyWith(
  60. loadingState: loadState,
  61. errorMessage: errorMsg
  62. );
  63. // Log.d("87338 ${state.currentDate}");
  64. Log.d("87338 ${state.visitTimeList}");
  65. }
  66. setInitPageData(BuildContext context, int id, String cleanServiceType){
  67. _context = context;
  68. _id = id;
  69. _cleanServiceType = cleanServiceType;
  70. }
  71. initPageData(BuildContext context){
  72. // changeLoadingState(LoadState.State_Success, null);
  73. getVisitTimeList(context, DateTimeUtils.formatDate(DateTime.now(), format: "yyyy-MM-dd"));
  74. }
  75. // 获取指定 日期下面的所有 时间段
  76. Future getVisitTimeList(BuildContext context, String date) async{
  77. Log.d("--------------------------getVisitTimeList---------------------");
  78. // 获取数据
  79. changeLoadingState(LoadState.State_Loading, null);
  80. try {
  81. //请求网络
  82. Map<String, dynamic> params = {
  83. "id": _id,
  84. "date": date,
  85. };
  86. Log.d("请求参数------$params");
  87. final result = await serviceRespositoryInstance.fetchPaidServiceTimePeriod(params);
  88. //校验成功失败
  89. if (result.isSuccess) {
  90. List<ServiceTimePeriodEntity>? resultEntityList = result.list as List<ServiceTimePeriodEntity>;
  91. handlerResultList(resultEntityList);
  92. } else {
  93. String errorMessage = result.errorMsg!;
  94. changeLoadingState(LoadState.State_Error, errorMessage);
  95. ToastEngine.show(result.errorMsg ?? "Network Load Error");
  96. }
  97. } catch (e) {
  98. ToastEngine.show("Error: $e");
  99. }
  100. }
  101. void handlerResultList(List<ServiceTimePeriodEntity>? list) {
  102. if(list != null && list.isNotEmpty){
  103. // 需要模型转换 将 ServiceTimePeriodEntity 转换为 VisitTimeContentItem
  104. List<VisitTimeContentItem> newList = [];
  105. list.asMap().forEach((index,item) {
  106. VisitTimeContentItem newVisitTimeContentItem = VisitTimeContentItem()
  107. ..id = index
  108. ..urgent = item.urgent
  109. ..holiday = item.holiday
  110. ..night = item.night
  111. ..time = item.time
  112. ..timeText = item.timeText
  113. ..enable = item.enable
  114. ..isChecked = false;
  115. newList.add(newVisitTimeContentItem);
  116. });
  117. state.visitTimeList!.clear();
  118. state.visitTimeList!.addAll(newList);
  119. //更新展示的状态
  120. changeLoadingState(LoadState.State_Success, null);
  121. }
  122. }
  123. handlerChangeNum(BuildContext context, int num, int index){
  124. // state.visitTimeList[index].tu = num;
  125. // state = state.copyWith(
  126. // visitTimeList: state.visitTimeList,
  127. // );
  128. }
  129. // 点击 时间段
  130. handlerTimeItemClick(BuildContext context, int index, bool changetoValue){
  131. Log.d("index:${index} 的 isChecked属性 变换成: $changetoValue");
  132. // 修改 状态
  133. if(state.isMultiSelect??false){
  134. // 可多选
  135. state.visitTimeList[index].isChecked = changetoValue;
  136. }else {
  137. // 不可多选
  138. state.visitTimeList.asMap().forEach((i,item) {
  139. if(i == index){
  140. item.isChecked = changetoValue;
  141. }else {
  142. item.isChecked = false;
  143. }
  144. });
  145. }
  146. state = state.copyWith(
  147. visitTimeList: state.visitTimeList,
  148. );
  149. final serviceOrderConfirmVm = ref.read(serviceOrderConfirmVmProvider.notifier);
  150. serviceOrderConfirmVm?.setConfirmOrderTotalPrice(context);
  151. }
  152. }