vehicle_apply_api.dart 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. import 'vehicle_report_model.dart';
  13. final vehicleApplyApiProvider = Provider<VehicleApplyApi>(
  14. (ref) => VehicleApplyApi(ref.read(apiClientProvider)),
  15. );
  16. class VehicleApplyApi {
  17. final ApiClient _client;
  18. final _cache = ApiCache(
  19. keyPrefix:
  20. '${HostAppChannel.sn}_${HostAppChannel.compNo}_${HostAppChannel.usr}',
  21. );
  22. VehicleApplyApi(this._client);
  23. /// 清除所有基础资料缓存(picker 下拉刷新时调用)。
  24. void clearRefCache() => _cache.clear();
  25. /// 用车申请列表(分页)
  26. Future<PaginatedData<VehicleApplyModel>> fetchList({
  27. String status = '',
  28. String keyword = '',
  29. String startDate = '',
  30. String endDate = '',
  31. String usr = '',
  32. String sortDir = 'DESC',
  33. int page = 1,
  34. int size = 20,
  35. }) async {
  36. final response = await _client.get<Map<String, dynamic>>(
  37. '/OA/GetVehicleApplyList',
  38. queryParameters: {
  39. 'status': status,
  40. 'keyword': keyword,
  41. 'startDate': startDate,
  42. 'endDate': endDate,
  43. 'usr': usr,
  44. 'sortDir': sortDir,
  45. 'page': page,
  46. 'size': size,
  47. },
  48. );
  49. return PaginatedData.fromJson(response.data!, VehicleApplyModel.fromJson);
  50. }
  51. /// 用车申请详情
  52. Future<VehicleApplyModel> fetchDetail(String billNo) async {
  53. final response = await _client.get<Map<String, dynamic>>(
  54. '/OA/GetVehicleApplyDetail',
  55. queryParameters: {'billNo': billNo},
  56. );
  57. return VehicleApplyModel.fromJson(response.data!);
  58. }
  59. /// 提交/保存,返回申请单号
  60. Future<String?> submit(Map<String, dynamic> data) async {
  61. final response = await _client.post<Map<String, dynamic>>(
  62. '/OA/BillSave',
  63. data: {
  64. 'erpCategory': 'MasterService',
  65. 'billId': 'YC',
  66. 'procId': '',
  67. 'data': data,
  68. },
  69. );
  70. final resData = response.data;
  71. if (resData == null) return null;
  72. final resultData = resData['resultData'];
  73. if (resultData is Map<String, dynamic>) {
  74. final bilNo = resultData['BIL_NO'] as String?;
  75. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  76. }
  77. if (resultData is String && resultData.isNotEmpty) {
  78. try {
  79. final parsed = json.decode(resultData) as Map<String, dynamic>;
  80. final bilNo = parsed['BIL_NO'] as String?;
  81. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  82. } catch (_) {}
  83. }
  84. final rootBilNo = resData['BIL_NO'] as String?;
  85. if (rootBilNo != null && rootBilNo.isNotEmpty) return rootBilNo;
  86. return null;
  87. }
  88. /// 还车登记(billId="YC",IS_RETURN="T")
  89. Future<String?> submitReturn(
  90. String billNo,
  91. Map<String, dynamic> returnData,
  92. ) async {
  93. final response = await _client.post<Map<String, dynamic>>(
  94. '/OA/BillSave',
  95. data: {
  96. 'erpCategory': 'MasterService',
  97. 'billId': 'YC',
  98. 'procId': '',
  99. 'data': {
  100. 'HeadData': {
  101. 'YC_NO': billNo,
  102. 'IS_RETURN': 'T',
  103. 'RETURN_TIME': returnData['returnTime'],
  104. 'ODOMETER_END': returnData['odometerEnd'],
  105. 'AMTN': returnData['amtn'],
  106. 'REMARK': returnData['remark'],
  107. 'USR_CHECK': returnData['usrCheck'],
  108. },
  109. },
  110. },
  111. );
  112. final resData = response.data;
  113. if (resData == null) return null;
  114. final resultData = resData['resultData'];
  115. if (resultData is Map<String, dynamic>) {
  116. final bilNo = resultData['BIL_NO'] as String?;
  117. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  118. }
  119. if (resultData is String && resultData.isNotEmpty) {
  120. try {
  121. final parsed = json.decode(resultData) as Map<String, dynamic>;
  122. final bilNo = parsed['BIL_NO'] as String?;
  123. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  124. } catch (_) {}
  125. }
  126. return null;
  127. }
  128. /// 提交审核流
  129. Future<bool> shSubmit({
  130. required String bilId,
  131. required String bilNo,
  132. required String bilDd,
  133. String dep = '',
  134. String rem = '',
  135. String usr = '',
  136. bool isCancel = false,
  137. }) async {
  138. try {
  139. final response = await _client.post<Map<String, dynamic>>(
  140. '/OA/SHSubmit',
  141. data: {
  142. 'erpCategory': 'MasterService',
  143. 'bilId': bilId,
  144. 'bilNo': bilNo,
  145. 'bilDd': bilDd,
  146. 'dep': dep,
  147. 'rem': rem,
  148. 'usr': usr,
  149. 'isCancel': isCancel,
  150. },
  151. );
  152. return response.data?['code'] == 0;
  153. } catch (_) {
  154. return false;
  155. }
  156. }
  157. /// 获取单据状态(billId="YC")
  158. Future<Map<String, dynamic>> getBillStatus(String billNo) async {
  159. final response = await _client.get<Map<String, dynamic>>(
  160. '/OA/GetBillStatus',
  161. queryParameters: {'billId': 'YC', 'billNo': billNo},
  162. );
  163. return response.data!;
  164. }
  165. /// 用车报表汇总
  166. Future<VehicleReportSummary> getVehicleReportSummary({
  167. String startDate = '',
  168. String endDate = '',
  169. }) async {
  170. final params = <String, dynamic>{};
  171. if (startDate.isNotEmpty) params['startDate'] = startDate;
  172. if (endDate.isNotEmpty) params['endDate'] = endDate;
  173. final response = await _client.get<Map<String, dynamic>>(
  174. '/OA/GetVehicleReportSummary',
  175. queryParameters: params,
  176. );
  177. return VehicleReportSummary.fromJson(response.data!);
  178. }
  179. /// 用车报表月度数据
  180. Future<List<VehicleMonthlyItem>> getVehicleReportMonthly({
  181. String startDate = '',
  182. String endDate = '',
  183. }) async {
  184. final params = <String, dynamic>{};
  185. if (startDate.isNotEmpty) params['startDate'] = startDate;
  186. if (endDate.isNotEmpty) params['endDate'] = endDate;
  187. final response = await _client.get<dynamic>(
  188. '/OA/GetVehicleReportMonthly',
  189. queryParameters: params,
  190. );
  191. final list =
  192. (response.data as List<dynamic>?)
  193. ?.map((e) => VehicleMonthlyItem.fromJson(e as Map<String, dynamic>))
  194. .toList() ??
  195. [];
  196. return list;
  197. }
  198. /// 用车报表下属对比
  199. Future<List<VehicleSubordinateItem>> getVehicleSubordinateReport({
  200. String startDate = '',
  201. String endDate = '',
  202. }) async {
  203. final params = <String, dynamic>{};
  204. if (startDate.isNotEmpty) params['startDate'] = startDate;
  205. if (endDate.isNotEmpty) params['endDate'] = endDate;
  206. final response = await _client.get<dynamic>(
  207. '/OA/GetVehicleSubordinateReport',
  208. queryParameters: params,
  209. );
  210. final list =
  211. (response.data as List<dynamic>?)
  212. ?.map(
  213. (e) => VehicleSubordinateItem.fromJson(e as Map<String, dynamic>),
  214. )
  215. .toList() ??
  216. [];
  217. return list;
  218. }
  219. /// 用车报表明细(分页)
  220. Future<Map<String, dynamic>> getVehicleReportDetail({
  221. String startDate = '',
  222. String endDate = '',
  223. int page = 1,
  224. int size = 20,
  225. }) async {
  226. final response = await _client.get<Map<String, dynamic>>(
  227. '/OA/GetVehicleReportDetail',
  228. queryParameters: {
  229. if (startDate.isNotEmpty) 'startDate': startDate,
  230. if (endDate.isNotEmpty) 'endDate': endDate,
  231. 'page': page,
  232. 'size': size,
  233. },
  234. );
  235. return response.data!;
  236. }
  237. /// 用车报表表身明细(分页)
  238. Future<Map<String, dynamic>> getVehicleBodyReport({
  239. String startDate = '',
  240. String endDate = '',
  241. int page = 1,
  242. int size = 20,
  243. }) async {
  244. final response = await _client.get<Map<String, dynamic>>(
  245. '/OA/GetVehicleBodyReport',
  246. queryParameters: {
  247. if (startDate.isNotEmpty) 'startDate': startDate,
  248. if (endDate.isNotEmpty) 'endDate': endDate,
  249. 'page': page,
  250. 'size': size,
  251. },
  252. );
  253. return response.data!;
  254. }
  255. /// 部门查询
  256. Future<List<DepartmentItem>> getDepartments({
  257. String keyword = '',
  258. bool onlyActive = true,
  259. int page = 1,
  260. int size = 100,
  261. }) async {
  262. final cacheKey = 'getDepartments_${keyword}_${onlyActive}_$page';
  263. return _cache.getOrFetch(cacheKey, () async {
  264. final response = await _client.get<Map<String, dynamic>>(
  265. '/OA/GetDepartments',
  266. queryParameters: {
  267. 'keyword': keyword,
  268. 'onlyActive': onlyActive,
  269. 'page': page,
  270. 'size': size,
  271. },
  272. );
  273. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  274. return list
  275. .map((e) => DepartmentItem.fromJson(e as Map<String, dynamic>))
  276. .toList();
  277. });
  278. }
  279. /// 员工查询
  280. Future<List<EmployeeItem>> getEmployees({
  281. String keyword = '',
  282. String salNo = '',
  283. int page = 1,
  284. int size = 100,
  285. }) async {
  286. final cacheKey = 'getEmployees_${keyword}_${salNo}_$page';
  287. return _cache.getOrFetch(cacheKey, () async {
  288. final response = await _client.get<Map<String, dynamic>>(
  289. '/OA/GetEmployees',
  290. queryParameters: {
  291. 'keyword': keyword,
  292. 'salNo': salNo,
  293. 'page': page,
  294. 'size': size,
  295. },
  296. );
  297. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  298. return list
  299. .map((e) => EmployeeItem.fromJson(e as Map<String, dynamic>))
  300. .toList();
  301. });
  302. }
  303. }