vehicle_apply_api.dart 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import 'dart:convert';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../../core/data/api_cache.dart';
  4. import '../../core/navigation/host_app_channel.dart';
  5. import '../../core/network/api_client.dart';
  6. import '../../shared/models/pagination_model.dart';
  7. import '../../app.dart';
  8. import '../expense_apply/expense_apply_api.dart';
  9. export '../expense_apply/expense_apply_api.dart'
  10. show DepartmentItem, EmployeeItem;
  11. import 'vehicle_apply_model.dart';
  12. final vehicleApplyApiProvider = Provider<VehicleApplyApi>(
  13. (ref) => VehicleApplyApi(ref.read(apiClientProvider)),
  14. );
  15. class VehicleApplyApi {
  16. final ApiClient _client;
  17. final _cache = ApiCache(
  18. keyPrefix:
  19. '${HostAppChannel.sn}_${HostAppChannel.compNo}_${HostAppChannel.usr}',
  20. );
  21. VehicleApplyApi(this._client);
  22. /// 清除所有基础资料缓存(picker 下拉刷新时调用)。
  23. void clearRefCache() => _cache.clear();
  24. /// 用车申请列表(分页)
  25. Future<PaginatedData<VehicleApplyModel>> fetchList({
  26. String status = '',
  27. String keyword = '',
  28. String startDate = '',
  29. String endDate = '',
  30. String usr = '',
  31. String sortDir = 'DESC',
  32. int page = 1,
  33. int size = 20,
  34. }) async {
  35. final response = await _client.get<Map<String, dynamic>>(
  36. '/OA/GetVehicleApplyList',
  37. queryParameters: {
  38. 'status': status,
  39. 'keyword': keyword,
  40. 'startDate': startDate,
  41. 'endDate': endDate,
  42. 'usr': usr,
  43. 'sortDir': sortDir,
  44. 'page': page,
  45. 'size': size,
  46. },
  47. );
  48. return PaginatedData.fromJson(response.data!, VehicleApplyModel.fromJson);
  49. }
  50. /// 用车申请详情
  51. Future<VehicleApplyModel> fetchDetail(String billNo) async {
  52. final response = await _client.get<Map<String, dynamic>>(
  53. '/OA/GetVehicleApplyDetail',
  54. queryParameters: {'billNo': billNo},
  55. );
  56. return VehicleApplyModel.fromJson(response.data!);
  57. }
  58. /// 提交/保存,返回申请单号
  59. Future<String?> submit(Map<String, dynamic> data) async {
  60. final response = await _client.post<Map<String, dynamic>>(
  61. '/OA/BillSave',
  62. data: {
  63. 'erpCategory': 'MasterService',
  64. 'billId': 'YC',
  65. 'procId': '',
  66. 'data': data,
  67. },
  68. );
  69. final resData = response.data;
  70. if (resData == null) return null;
  71. final resultData = resData['resultData'];
  72. if (resultData is Map<String, dynamic>) {
  73. final bilNo = resultData['BIL_NO'] as String?;
  74. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  75. }
  76. if (resultData is String && resultData.isNotEmpty) {
  77. try {
  78. final parsed = json.decode(resultData) as Map<String, dynamic>;
  79. final bilNo = parsed['BIL_NO'] as String?;
  80. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  81. } catch (_) {}
  82. }
  83. final rootBilNo = resData['BIL_NO'] as String?;
  84. if (rootBilNo != null && rootBilNo.isNotEmpty) return rootBilNo;
  85. return null;
  86. }
  87. /// 还车登记(billId="YC",IS_RETURN="T")
  88. Future<String?> submitReturn(
  89. String billNo,
  90. Map<String, dynamic> returnData,
  91. ) async {
  92. final response = await _client.post<Map<String, dynamic>>(
  93. '/OA/BillSave',
  94. data: {
  95. 'erpCategory': 'MasterService',
  96. 'billId': 'YC',
  97. 'procId': '',
  98. 'data': {
  99. 'HeadData': {
  100. 'YC_NO': billNo,
  101. 'IS_RETURN': 'T',
  102. 'RETURN_TIME': returnData['returnTime'],
  103. 'ODOMETER_END': returnData['odometerEnd'],
  104. 'AMTN': returnData['amtn'],
  105. 'REMARK': returnData['remark'],
  106. 'USR_CHECK': returnData['usrCheck'],
  107. },
  108. },
  109. },
  110. );
  111. final resData = response.data;
  112. if (resData == null) return null;
  113. final resultData = resData['resultData'];
  114. if (resultData is Map<String, dynamic>) {
  115. final bilNo = resultData['BIL_NO'] as String?;
  116. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  117. }
  118. if (resultData is String && resultData.isNotEmpty) {
  119. try {
  120. final parsed = json.decode(resultData) as Map<String, dynamic>;
  121. final bilNo = parsed['BIL_NO'] as String?;
  122. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  123. } catch (_) {}
  124. }
  125. return null;
  126. }
  127. /// 提交审核流
  128. Future<bool> shSubmit({
  129. required String bilId,
  130. required String bilNo,
  131. required String bilDd,
  132. String dep = '',
  133. String rem = '',
  134. String usr = '',
  135. }) async {
  136. try {
  137. final response = await _client.post<Map<String, dynamic>>(
  138. '/OA/SHSubmit',
  139. data: {
  140. 'erpCategory': 'MasterService',
  141. 'bilId': bilId,
  142. 'bilNo': bilNo,
  143. 'bilDd': bilDd,
  144. 'dep': dep,
  145. 'rem': rem,
  146. 'usr': usr,
  147. },
  148. );
  149. return response.data?['code'] == 0;
  150. } catch (_) {
  151. return false;
  152. }
  153. }
  154. /// 获取单据状态(billId="YC")
  155. Future<Map<String, dynamic>> getBillStatus(String billNo) async {
  156. final response = await _client.get<Map<String, dynamic>>(
  157. '/OA/GetBillStatus',
  158. queryParameters: {'billId': 'YC', 'billNo': billNo},
  159. );
  160. return response.data!;
  161. }
  162. /// 部门查询
  163. Future<List<DepartmentItem>> getDepartments({
  164. String keyword = '',
  165. bool onlyActive = true,
  166. int page = 1,
  167. int size = 100,
  168. }) async {
  169. final cacheKey = 'getDepartments_${keyword}_${onlyActive}_$page';
  170. return _cache.getOrFetch(cacheKey, () async {
  171. final response = await _client.get<Map<String, dynamic>>(
  172. '/OA/GetDepartments',
  173. queryParameters: {
  174. 'keyword': keyword,
  175. 'onlyActive': onlyActive,
  176. 'page': page,
  177. 'size': size,
  178. },
  179. );
  180. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  181. return list
  182. .map((e) => DepartmentItem.fromJson(e as Map<String, dynamic>))
  183. .toList();
  184. });
  185. }
  186. /// 员工查询
  187. Future<List<EmployeeItem>> getEmployees({
  188. String keyword = '',
  189. String salNo = '',
  190. int page = 1,
  191. int size = 100,
  192. }) async {
  193. final cacheKey = 'getEmployees_${keyword}_${salNo}_$page';
  194. return _cache.getOrFetch(cacheKey, () async {
  195. final response = await _client.get<Map<String, dynamic>>(
  196. '/OA/GetEmployees',
  197. queryParameters: {
  198. 'keyword': keyword,
  199. 'salNo': salNo,
  200. 'page': page,
  201. 'size': size,
  202. },
  203. );
  204. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  205. return list
  206. .map((e) => EmployeeItem.fromJson(e as Map<String, dynamic>))
  207. .toList();
  208. });
  209. }
  210. }