form_repository.dart 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import 'dart:typed_data';
  2. import 'package:domain/entity/feedback_detail_entity.dart';
  3. import 'package:domain/entity/form_option_entity.dart';
  4. import 'package:plugin_platform/platform_export.dart';
  5. import 'package:plugin_platform/http/dio_engine.dart';
  6. import 'package:plugin_platform/http/http_result.dart';
  7. import 'package:riverpod_annotation/riverpod_annotation.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:shared/utils/util.dart';
  10. import '../constants/api_constants.dart';
  11. import 'package:flutter_riverpod/flutter_riverpod.dart';
  12. import 'package:plugin_basic/provider/http_provider/http_provider.dart';
  13. import '../entity/form_content_entity.dart';
  14. import '../entity/form_detail_entity.dart';
  15. import '../entity/form_list_entity.dart';
  16. import '../entity/form_submitted_page_entity.dart';
  17. import '../entity/id_name_entity.dart';
  18. part 'form_repository.g.dart';
  19. @Riverpod(keepAlive: true)
  20. FormRepository formRepository(Ref ref) {
  21. final dioEngine = ref.watch(dioEngineProvider);
  22. return FormRepository(dioEngine: dioEngine);
  23. }
  24. /*
  25. * 表单的数据仓库
  26. */
  27. class FormRepository {
  28. DioEngine dioEngine;
  29. FormRepository({required this.dioEngine});
  30. /// 获取表单的可申请列表
  31. Future<HttpResult<FormListEntity>> fetchFormApplyList({
  32. CancelToken? cancelToken,
  33. }) async {
  34. final result = await dioEngine.requestNetResult(
  35. ApiConstants.apiFormList,
  36. method: HttpMethod.GET,
  37. cancelToken: cancelToken,
  38. );
  39. if (result.isSuccess) {
  40. final jsonList = result.getListJson();
  41. //获取List数据 需要转换一次
  42. var list = jsonList
  43. ?.map((value) {
  44. if (value is Map<String, dynamic>) {
  45. return FormListEntity.fromJson(value);
  46. } else {
  47. return null;
  48. }
  49. })
  50. .where((item) => item != null)
  51. .cast<FormListEntity>()
  52. .toList();
  53. return result.convert<FormListEntity>(list: list);
  54. }
  55. return result.convert<FormListEntity>();
  56. }
  57. /// 已发布的表单列表
  58. Future<HttpResult<FormSubmittedPageEntity>> fetchFormSubmitList({
  59. required int curPage,
  60. CancelToken? cancelToken,
  61. }) async {
  62. Map<String, String> params = {};
  63. params['page'] = curPage.toString();
  64. params['limit'] = "10";
  65. final result = await dioEngine.requestNetResult(
  66. ApiConstants.apiFormSubmittedList,
  67. params: params,
  68. method: HttpMethod.GET,
  69. cancelToken: cancelToken,
  70. );
  71. if (result.isSuccess) {
  72. final json = result.getDataJson();
  73. var data = FormSubmittedPageEntity.fromJson(json!);
  74. return result.convert<FormSubmittedPageEntity>(data: data);
  75. }
  76. return result.convert();
  77. }
  78. /// 已批准的表单列表
  79. Future<HttpResult<FormSubmittedPageEntity>> fetchFormApproveList({
  80. required int curPage,
  81. CancelToken? cancelToken,
  82. }) async {
  83. Map<String, String> params = {};
  84. params['page'] = curPage.toString();
  85. params['limit'] = "10";
  86. final result = await dioEngine.requestNetResult(
  87. ApiConstants.apiFormApprovedList,
  88. params: params,
  89. method: HttpMethod.GET,
  90. cancelToken: cancelToken,
  91. );
  92. if (result.isSuccess) {
  93. final json = result.getDataJson();
  94. var data = FormSubmittedPageEntity.fromJson(json!);
  95. return result.convert<FormSubmittedPageEntity>(data: data);
  96. }
  97. return result.convert();
  98. }
  99. /// 已拒绝的表单列表
  100. Future<HttpResult<FormSubmittedPageEntity>> fetchFormRejectList({
  101. required int curPage,
  102. CancelToken? cancelToken,
  103. }) async {
  104. Map<String, String> params = {};
  105. params['page'] = curPage.toString();
  106. params['limit'] = "10";
  107. final result = await dioEngine.requestNetResult(
  108. ApiConstants.apiFormRejectedList,
  109. params: params,
  110. method: HttpMethod.GET,
  111. cancelToken: cancelToken,
  112. );
  113. if (result.isSuccess) {
  114. final json = result.getDataJson();
  115. var data = FormSubmittedPageEntity.fromJson(json!);
  116. return result.convert<FormSubmittedPageEntity>(data: data);
  117. }
  118. return result.convert();
  119. }
  120. /// 获取表单的选项
  121. Future<HttpResult<FormOptionEntity>> fetchFormOption({
  122. required String? typeId,
  123. CancelToken? cancelToken,
  124. }) async {
  125. Map<String, String> params = {};
  126. params['type_id'] = typeId ?? "";
  127. final result = await dioEngine.requestNetResult(
  128. ApiConstants.apiFormOption,
  129. params: params,
  130. method: HttpMethod.GET,
  131. isShowLoadingDialog: true,
  132. networkDebounce: true,
  133. cancelToken: cancelToken,
  134. );
  135. if (result.isSuccess) {
  136. final json = result.getDataJson();
  137. var data = FormOptionEntity.fromJson(json!);
  138. return result.convert<FormOptionEntity>(data: data);
  139. }
  140. return result.convert();
  141. }
  142. /// 上传表单
  143. Future<HttpResult> submitForm({
  144. required String? estateFormId,
  145. required String? typeId,
  146. required FormContentEntity content,
  147. CancelToken? cancelToken,
  148. }) async {
  149. Map<String, String> params = {};
  150. Map<String, String> paths = {};
  151. params['estate_online_form_id'] = estateFormId ?? "";
  152. //选择参数添加
  153. if (Utils.isNotEmpty(content.typeOfApplication)) {
  154. params["type_of_application"] = content.typeOfApplication!;
  155. }
  156. if (Utils.isNotEmpty(content.notesToRecipient)) {
  157. params["notes_to_recipient"] = content.notesToRecipient!;
  158. }
  159. if (Utils.isNotEmpty(content.notesToManagement)) {
  160. params["notes_to_management"] = content.notesToManagement!;
  161. }
  162. if (Utils.isNotEmpty(content.ownershipStatus)) {
  163. params["ownership_status"] = content.ownershipStatus!;
  164. }
  165. if (Utils.isNotEmpty(content.vehicleNumber)) {
  166. params["vehicle_number"] = content.vehicleNumber!;
  167. }
  168. if (Utils.isNotEmpty(content.iuNumber)) {
  169. params["iu_number"] = content.iuNumber!;
  170. }
  171. if (Utils.isNotEmpty(content.vehicleMakeModelColour)) {
  172. params["vehicle_make_model_colour"] = content.vehicleMakeModelColour!;
  173. }
  174. if (Utils.isNotEmpty(content.startDate)) {
  175. params["start_date"] = content.startDate!;
  176. }
  177. if (Utils.isNotEmpty(content.endDate)) {
  178. params["end_date"] = content.endDate!;
  179. }
  180. if (Utils.isNotEmpty(content.timeOrArrival)) {
  181. params["time_or_arrival"] = "${content.timeOrArrival!}:00";
  182. }
  183. if (Utils.isNotEmpty(content.movingCompany)) {
  184. params["moving_company"] = content.movingCompany!;
  185. }
  186. if (Utils.isNotEmpty(content.personInCharge)) {
  187. params["person_in_charge"] = content.personInCharge!;
  188. }
  189. if (Utils.isNotEmpty(content.mobileNumber)) {
  190. params["mobile_number"] = content.mobileNumber!;
  191. }
  192. if (Utils.isNotEmpty(content.companyAddress)) {
  193. params["company_address"] = content.companyAddress!;
  194. }
  195. if (Utils.isNotEmpty(content.dateOfEntry)) {
  196. params["date_of_entry"] = content.dateOfEntry!;
  197. }
  198. if (Utils.isNotEmpty(content.guestName)) {
  199. params["guest_name"] = content.guestName!;
  200. }
  201. if (Utils.isNotEmpty(content.guestMobileNumber)) {
  202. params["guest_mobile_number"] = content.guestMobileNumber!;
  203. }
  204. if (Utils.isNotEmpty(content.renovationStartDate)) {
  205. params["renovation_start_date"] = content.renovationStartDate!;
  206. }
  207. if (Utils.isNotEmpty(content.renovationEndDate)) {
  208. params["renovation_end_date"] = content.renovationEndDate!;
  209. }
  210. if (Utils.isNotEmpty(content.hackingStartDate)) {
  211. params["hacking_start_date"] = content.hackingStartDate!;
  212. }
  213. if (Utils.isNotEmpty(content.hackingEndDate)) {
  214. params["hacking_end_date"] = content.hackingEndDate!;
  215. }
  216. if (Utils.isNotEmpty(content.renovationCompany)) {
  217. params["renovation_company"] = content.renovationCompany!;
  218. }
  219. if (Utils.isNotEmpty(content.listOfRenovationWorks)) {
  220. params["list_of_renovation_works"] = content.listOfRenovationWorks!;
  221. }
  222. //attachments 添加
  223. if (content.attachments != null && content.attachments!.isNotEmpty) {
  224. content.attachments!.asMap().forEach((index, path) {
  225. paths["attachments[$index]"] = path;
  226. });
  227. }
  228. //signature 添加使用 ByteData 数据
  229. Map<String, Uint8List> streams = {};
  230. if (content.signatureByteData != null && content.signatureByteData is ByteData) {
  231. streams["signature"] = (content.signatureByteData as ByteData).buffer.asUint8List();
  232. }
  233. final result = await dioEngine.requestNetResult(
  234. "${ApiConstants.apiFormSubmit}$typeId",
  235. params: params,
  236. paths: paths,
  237. pathStreams: streams,
  238. method: HttpMethod.POST,
  239. isShowLoadingDialog: true,
  240. networkDebounce: true,
  241. cancelToken: cancelToken,
  242. );
  243. if (result.isSuccess) {
  244. return result.convert();
  245. }
  246. return result.convert();
  247. }
  248. /// 获取表单的详情数据
  249. Future<HttpResult<FormDetailEntity>> fetchFormDetail({
  250. required String? estateFormId,
  251. required String? taskId,
  252. CancelToken? cancelToken,
  253. }) async {
  254. Map<String, String> params = {};
  255. params['estate_online_form_id'] = estateFormId ?? "";
  256. params['id'] = taskId ?? "";
  257. final result = await dioEngine.requestNetResult(
  258. ApiConstants.apiFormDetail,
  259. params: params,
  260. method: HttpMethod.GET,
  261. cancelToken: cancelToken,
  262. );
  263. if (result.isSuccess) {
  264. final json = result.getDataJson();
  265. var data = FormDetailEntity.fromJson(json!);
  266. return result.convert<FormDetailEntity>(data: data);
  267. }
  268. return result.convert();
  269. }
  270. }