vehicle_apply_api.dart 10.0 KB

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