vehicle_api.dart 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import 'dart:convert';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../../core/network/api_client.dart';
  4. import '../../shared/models/pagination_model.dart';
  5. import '../../app.dart';
  6. import 'vehicle_model.dart';
  7. final vehicleApiProvider = Provider<VehicleApi>(
  8. (ref) => VehicleApi(ref.read(apiClientProvider)),
  9. );
  10. /// 部门项(复用自 expense_apply_api)
  11. class DepartmentItem {
  12. final String dep;
  13. final String name;
  14. const DepartmentItem({required this.dep, required this.name});
  15. factory DepartmentItem.fromJson(Map<String, dynamic> json) =>
  16. DepartmentItem(dep: json['dep'] as String? ?? '', name: json['name'] as String? ?? '');
  17. }
  18. /// 员工项
  19. class EmployeeItem {
  20. final String salNo;
  21. final String name;
  22. const EmployeeItem({required this.salNo, required this.name});
  23. factory EmployeeItem.fromJson(Map<String, dynamic> json) =>
  24. EmployeeItem(salNo: json['salNo'] as String? ?? '', name: json['name'] as String? ?? '');
  25. }
  26. class VehicleApi {
  27. final ApiClient _client;
  28. VehicleApi(this._client);
  29. /// 用车申请列表(分页)
  30. Future<PaginatedData<VehicleModel>> fetchList({
  31. String status = '',
  32. String keyword = '',
  33. String startDate = '',
  34. String endDate = '',
  35. String usr = '',
  36. String sortDir = 'DESC',
  37. int page = 1,
  38. int size = 20,
  39. }) async {
  40. final response = await _client.get<Map<String, dynamic>>(
  41. '/OA/GetVehicleApplyList',
  42. queryParameters: {
  43. 'status': status,
  44. 'keyword': keyword,
  45. 'startDate': startDate,
  46. 'endDate': endDate,
  47. 'usr': usr,
  48. 'sortDir': sortDir,
  49. 'page': page,
  50. 'size': size,
  51. },
  52. );
  53. return PaginatedData.fromJson(response.data!, VehicleModel.fromJson);
  54. }
  55. /// 用车申请详情
  56. Future<VehicleModel> fetchDetail(String billNo) async {
  57. final response = await _client.get<Map<String, dynamic>>(
  58. '/OA/GetVehicleApplyDetail',
  59. queryParameters: {'billNo': billNo},
  60. );
  61. return VehicleModel.fromJson(response.data!);
  62. }
  63. /// 提交审批,返回申请单号
  64. Future<String?> submit(Map<String, dynamic> data) async {
  65. final response = await _client.post<Map<String, dynamic>>(
  66. '/OA/BillSave',
  67. data: {
  68. 'erpCategory': 'MasterService',
  69. 'billId': 'VC',
  70. 'procId': '',
  71. 'data': data,
  72. },
  73. );
  74. final resData = response.data;
  75. if (resData == null) return null;
  76. final resultData = resData['resultData'];
  77. if (resultData is Map<String, dynamic>) {
  78. final bilNo = resultData['BIL_NO'] as String?;
  79. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  80. }
  81. if (resultData is String && resultData.isNotEmpty) {
  82. try {
  83. final parsed = json.decode(resultData) as Map<String, dynamic>;
  84. final bilNo = parsed['BIL_NO'] as String?;
  85. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  86. } catch (_) {}
  87. }
  88. final rootBilNo = resData['BIL_NO'] as String?;
  89. if (rootBilNo != null && rootBilNo.isNotEmpty) return rootBilNo;
  90. return null;
  91. }
  92. /// 还车登记
  93. Future<void> submitReturn(String billNo, Map<String, dynamic> returnData) async {
  94. await _client.post<Map<String, dynamic>>(
  95. '/OA/BillSave',
  96. data: {
  97. 'erpCategory': 'MasterService',
  98. 'billId': 'VC',
  99. 'procId': '',
  100. 'data': {
  101. 'HeadData': {
  102. 'YC_NO': billNo,
  103. 'IS_RETURN': 'T',
  104. 'RETURN_TIME': returnData['returnTime'],
  105. 'ODOMETER': returnData['odometer'],
  106. 'AMTN': returnData['amtn'],
  107. 'REMARK': returnData['remark'],
  108. },
  109. },
  110. },
  111. );
  112. }
  113. /// 获取单据状态
  114. Future<Map<String, dynamic>> getBillStatus(String billNo) async {
  115. final response = await _client.get<Map<String, dynamic>>(
  116. '/OA/GetBillStatus',
  117. queryParameters: {'billId': 'VC', 'billNo': billNo},
  118. );
  119. return response.data!;
  120. }
  121. /// 部门查询
  122. Future<List<DepartmentItem>> getDepartments({
  123. String keyword = '',
  124. bool onlyActive = true,
  125. int page = 1,
  126. int size = 100,
  127. }) async {
  128. final response = await _client.get<Map<String, dynamic>>(
  129. '/OA/GetDepartments',
  130. queryParameters: {
  131. 'keyword': keyword,
  132. 'onlyActive': onlyActive,
  133. 'page': page,
  134. 'size': size,
  135. },
  136. );
  137. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  138. return list
  139. .map((e) => DepartmentItem.fromJson(e as Map<String, dynamic>))
  140. .toList();
  141. }
  142. /// 员工查询
  143. Future<List<EmployeeItem>> getEmployees({
  144. String keyword = '',
  145. String salNo = '',
  146. int page = 1,
  147. int size = 100,
  148. }) async {
  149. final response = await _client.get<Map<String, dynamic>>(
  150. '/OA/GetEmployees',
  151. queryParameters: {
  152. 'keyword': keyword,
  153. 'salNo': salNo,
  154. 'page': page,
  155. 'size': size,
  156. },
  157. );
  158. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  159. return list
  160. .map((e) => EmployeeItem.fromJson(e as Map<String, dynamic>))
  161. .toList();
  162. }
  163. }