vehicle_apply_api.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /// 获取用户对指定程序的权限
  158. Future<Map<String, dynamic>> getUserRights(String pgm) async {
  159. final response = await _client.get<Map<String, dynamic>>(
  160. '/OA/GetUserRights',
  161. queryParameters: {
  162. 'compNo': HostAppChannel.compNo,
  163. 'usrId': HostAppChannel.usr,
  164. 'pgm': pgm,
  165. },
  166. );
  167. return response.data!;
  168. }
  169. /// 获取审核配置
  170. Future<Map<String, dynamic>> getBillAuditConfig(String bilId) async {
  171. final response = await _client.get<Map<String, dynamic>>(
  172. '/OA/GetBillAuditConfig',
  173. queryParameters: {'bilId': bilId},
  174. );
  175. return response.data!;
  176. }
  177. /// 获取单据状态(billId="YC")
  178. Future<Map<String, dynamic>> getBillStatus(String billNo) async {
  179. final response = await _client.get<Map<String, dynamic>>(
  180. '/OA/GetBillStatus',
  181. queryParameters: {'billId': 'YC', 'billNo': billNo},
  182. );
  183. return response.data!;
  184. }
  185. /// 用车报表汇总
  186. Future<VehicleReportSummary> getVehicleReportSummary({
  187. String startDate = '',
  188. String endDate = '',
  189. }) async {
  190. final params = <String, dynamic>{};
  191. if (startDate.isNotEmpty) params['startDate'] = startDate;
  192. if (endDate.isNotEmpty) params['endDate'] = endDate;
  193. final response = await _client.get<Map<String, dynamic>>(
  194. '/OA/GetVehicleReportSummary',
  195. queryParameters: params,
  196. );
  197. return VehicleReportSummary.fromJson(response.data!);
  198. }
  199. /// 用车报表月度数据
  200. Future<List<VehicleMonthlyItem>> getVehicleReportMonthly({
  201. String startDate = '',
  202. String endDate = '',
  203. }) async {
  204. final params = <String, dynamic>{};
  205. if (startDate.isNotEmpty) params['startDate'] = startDate;
  206. if (endDate.isNotEmpty) params['endDate'] = endDate;
  207. final response = await _client.get<dynamic>(
  208. '/OA/GetVehicleReportMonthly',
  209. queryParameters: params,
  210. );
  211. final list =
  212. (response.data as List<dynamic>?)
  213. ?.map((e) => VehicleMonthlyItem.fromJson(e as Map<String, dynamic>))
  214. .toList() ??
  215. [];
  216. return list;
  217. }
  218. /// 用车报表下属对比
  219. Future<List<VehicleSubordinateItem>> getVehicleSubordinateReport({
  220. String startDate = '',
  221. String endDate = '',
  222. }) async {
  223. final params = <String, dynamic>{};
  224. if (startDate.isNotEmpty) params['startDate'] = startDate;
  225. if (endDate.isNotEmpty) params['endDate'] = endDate;
  226. final response = await _client.get<dynamic>(
  227. '/OA/GetVehicleSubordinateReport',
  228. queryParameters: params,
  229. );
  230. final list =
  231. (response.data as List<dynamic>?)
  232. ?.map(
  233. (e) => VehicleSubordinateItem.fromJson(e as Map<String, dynamic>),
  234. )
  235. .toList() ??
  236. [];
  237. return list;
  238. }
  239. /// 用车报表明细(分页)
  240. Future<Map<String, dynamic>> getVehicleReportDetail({
  241. String startDate = '',
  242. String endDate = '',
  243. int page = 1,
  244. int size = 20,
  245. }) async {
  246. final response = await _client.get<Map<String, dynamic>>(
  247. '/OA/GetVehicleReportDetail',
  248. queryParameters: {
  249. if (startDate.isNotEmpty) 'startDate': startDate,
  250. if (endDate.isNotEmpty) 'endDate': endDate,
  251. 'page': page,
  252. 'size': size,
  253. },
  254. );
  255. return response.data!;
  256. }
  257. /// 用车报表表身明细(分页)
  258. Future<Map<String, dynamic>> getVehicleBodyReport({
  259. String startDate = '',
  260. String endDate = '',
  261. int page = 1,
  262. int size = 20,
  263. }) async {
  264. final response = await _client.get<Map<String, dynamic>>(
  265. '/OA/GetVehicleBodyReport',
  266. queryParameters: {
  267. if (startDate.isNotEmpty) 'startDate': startDate,
  268. if (endDate.isNotEmpty) 'endDate': endDate,
  269. 'page': page,
  270. 'size': size,
  271. },
  272. );
  273. return response.data!;
  274. }
  275. /// 部门查询
  276. Future<List<DepartmentItem>> getDepartments({
  277. String keyword = '',
  278. bool onlyActive = true,
  279. int page = 1,
  280. int size = 100,
  281. }) async {
  282. final cacheKey = 'getDepartments_${keyword}_${onlyActive}_$page';
  283. return _cache.getOrFetch(cacheKey, () async {
  284. final response = await _client.get<Map<String, dynamic>>(
  285. '/OA/GetDepartments',
  286. queryParameters: {
  287. 'keyword': keyword,
  288. 'onlyActive': onlyActive,
  289. 'page': page,
  290. 'size': size,
  291. },
  292. );
  293. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  294. return list
  295. .map((e) => DepartmentItem.fromJson(e as Map<String, dynamic>))
  296. .toList();
  297. });
  298. }
  299. /// 员工查询
  300. Future<List<EmployeeItem>> getEmployees({
  301. String keyword = '',
  302. String salNo = '',
  303. int page = 1,
  304. int size = 100,
  305. }) async {
  306. final cacheKey = 'getEmployees_${keyword}_${salNo}_$page';
  307. return _cache.getOrFetch(cacheKey, () async {
  308. final response = await _client.get<Map<String, dynamic>>(
  309. '/OA/GetEmployees',
  310. queryParameters: {
  311. 'keyword': keyword,
  312. 'salNo': salNo,
  313. 'page': page,
  314. 'size': size,
  315. },
  316. );
  317. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  318. return list
  319. .map((e) => EmployeeItem.fromJson(e as Map<String, dynamic>))
  320. .toList();
  321. });
  322. }
  323. }