expense_apply_api.dart 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import 'dart:convert';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:dio/dio.dart';
  4. import 'package:flutter_riverpod/flutter_riverpod.dart';
  5. import '../../core/network/api_client.dart';
  6. import '../../app.dart';
  7. import '../../shared/models/pagination_model.dart';
  8. import '../../shared/models/bill_attachment.dart';
  9. import 'expense_apply_model.dart';
  10. final expenseApplyApiProvider = Provider<ExpenseApplyApi>(
  11. (ref) => ExpenseApplyApi(ref.read(apiClientProvider)),
  12. );
  13. // ═══ 参考数据模型(API 返回) ═══
  14. class CostTypeItem {
  15. final String typeNo;
  16. final String typeName;
  17. final String accNo;
  18. final String accName;
  19. const CostTypeItem({required this.typeNo, required this.typeName, required this.accNo, required this.accName});
  20. factory CostTypeItem.fromJson(Map<String, dynamic> json) => CostTypeItem(
  21. typeNo: json['typeNo'] as String? ?? '',
  22. typeName: json['typeName'] as String? ?? '',
  23. accNo: json['accNo'] as String? ?? '',
  24. accName: json['accName'] as String? ?? '',
  25. );
  26. }
  27. class ProjectCodeItem {
  28. final String objNo;
  29. final String name;
  30. const ProjectCodeItem({required this.objNo, required this.name});
  31. factory ProjectCodeItem.fromJson(Map<String, dynamic> json) => ProjectCodeItem(
  32. objNo: json['objNo'] as String? ?? '',
  33. name: json['name'] as String? ?? '',
  34. );
  35. }
  36. class DepartmentItem {
  37. final String dep;
  38. final String name;
  39. const DepartmentItem({required this.dep, required this.name});
  40. factory DepartmentItem.fromJson(Map<String, dynamic> json) => DepartmentItem(
  41. dep: json['dep'] as String? ?? '',
  42. name: json['name'] as String? ?? '',
  43. );
  44. }
  45. class ExpenseApplyApi {
  46. final ApiClient _client;
  47. ExpenseApplyApi(this._client);
  48. /// 费用申请列表(分页)
  49. Future<PaginatedData<ExpenseApplyModel>> fetchList({
  50. String status = '',
  51. String keyword = '',
  52. String startDate = '',
  53. String endDate = '',
  54. String usr = '',
  55. String sortDir = 'DESC',
  56. int page = 1,
  57. int size = 20,
  58. }) async {
  59. final response = await _client.get<Map<String, dynamic>>(
  60. '/OA/GetExpenseApplies',
  61. queryParameters: {
  62. 'status': status,
  63. 'keyword': keyword,
  64. 'startDate': startDate,
  65. 'endDate': endDate,
  66. 'usr': usr,
  67. 'sortDir': sortDir,
  68. 'page': page,
  69. 'size': size,
  70. },
  71. );
  72. return PaginatedData.fromJson(response.data!, ExpenseApplyModel.fromJson);
  73. }
  74. /// 费用申请详情(主表+明细)
  75. Future<ExpenseApplyModel> fetchDetail(String billNo) async {
  76. final response = await _client.get<Map<String, dynamic>>(
  77. '/OA/GetExpenseApplyDetail',
  78. queryParameters: {'billNo': billNo},
  79. );
  80. return ExpenseApplyModel.fromJson(response.data!);
  81. }
  82. /// 费用类别字典
  83. Future<List<CostTypeItem>> getCostTypes({String keyword = '', String accNo = ''}) async {
  84. final response = await _client.get<Map<String, dynamic>>(
  85. '/OA/GetCostTypes',
  86. queryParameters: {'keyword': keyword, 'accNo': accNo, 'page': 1, 'size': 100},
  87. );
  88. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  89. return list.map((e) => CostTypeItem.fromJson(e as Map<String, dynamic>)).toList();
  90. }
  91. /// 项目代号
  92. Future<List<ProjectCodeItem>> getProjectCodes({String keyword = '', String billDate = ''}) async {
  93. final response = await _client.get<Map<String, dynamic>>(
  94. '/OA/GetProjectCodes',
  95. queryParameters: {'keyword': keyword, 'billDate': billDate, 'page': 1, 'size': 100},
  96. );
  97. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  98. return list.map((e) => ProjectCodeItem.fromJson(e as Map<String, dynamic>)).toList();
  99. }
  100. /// 部门
  101. Future<List<DepartmentItem>> getDepartments({String keyword = '', bool onlyActive = true}) async {
  102. final response = await _client.get<Map<String, dynamic>>(
  103. '/OA/GetDepartments',
  104. queryParameters: {'keyword': keyword, 'onlyActive': onlyActive, 'page': 1, 'size': 100},
  105. );
  106. final list = (response.data?['list'] as List<dynamic>?) ?? [];
  107. return list.map((e) => DepartmentItem.fromJson(e as Map<String, dynamic>)).toList();
  108. }
  109. /// 提交审批,返回申请单号(提取失败时返回 null,不影响主流程)
  110. /// BillSave 返回格式: { callok:true, resultData:{ BIL_NO:"AE20267020005", ... } }
  111. Future<String?> submit(Map<String, dynamic> data) async {
  112. final response = await _client.post<Map<String, dynamic>>('/OA/BillSave', data: {
  113. 'erpCategory': 'MasterService',
  114. 'billId': 'AE',
  115. 'procId': '',
  116. 'data': data,
  117. });
  118. final resData = response.data;
  119. if (resData == null) return null;
  120. // 从 resultData 中取(BillSave 实际返回的嵌套格式)
  121. final resultData = resData['resultData'];
  122. if (resultData is Map<String, dynamic>) {
  123. // BIL_NO 是 BillSave 返回的通用单号字段(不区分 AE/BX)
  124. final bilNo = resultData['BIL_NO'] as String?;
  125. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  126. }
  127. // resultData 可能是 JSON 字符串
  128. if (resultData is String && resultData.isNotEmpty) {
  129. try {
  130. final parsed = json.decode(resultData) as Map<String, dynamic>;
  131. final bilNo = parsed['BIL_NO'] as String?;
  132. if (bilNo != null && bilNo.isNotEmpty) return bilNo;
  133. } catch (_) {}
  134. }
  135. // 兜底: resData 根级 BIL_NO
  136. final rootBilNo = resData['BIL_NO'] as String?;
  137. if (rootBilNo != null && rootBilNo.isNotEmpty) return rootBilNo;
  138. return null;
  139. }
  140. /// 下载附件文件字节
  141. Future<Uint8List?> downloadAttachment(String id) async {
  142. return await _client.downloadFile('/OA/DownloadAttachment', queryParameters: {'id': id});
  143. }
  144. /// 检测附件服务是否可用
  145. Future<bool> checkAttachHealth() async {
  146. try {
  147. final response = await _client.get<Map<String, dynamic>>('/OA/CheckAttachHealth');
  148. debugPrint('[checkAttachHealth] response.data: ${response.data}');
  149. debugPrint('[checkAttachHealth] response.data type: ${response.data.runtimeType}');
  150. final available = response.data?['available'] as bool? ?? false;
  151. debugPrint('[checkAttachHealth] available: $available');
  152. return available;
  153. } catch (e) {
  154. debugPrint('[checkAttachHealth] error: $e');
  155. return false;
  156. }
  157. }
  158. /// 审批进度
  159. Future<Map<String, dynamic>> fetchApprovalTimeline(String bilId, String bilNo, {int bilItm = 0}) async {
  160. final response = await _client.get<Map<String, dynamic>>(
  161. '/OA/GetApprovalTimeline',
  162. queryParameters: {'bilId': bilId, 'bilNo': bilNo, 'bilItm': bilItm},
  163. );
  164. return response.data!;
  165. }
  166. /// 获取单据附件列表
  167. Future<List<BillAttachment>> getAttachments(String bilId, String bilNo, {int? srcItm}) async {
  168. final params = <String, dynamic>{
  169. 'bilId': bilId,
  170. 'bilNo': bilNo,
  171. };
  172. if (srcItm != null) params['srcItm'] = srcItm;
  173. final response = await _client.get<dynamic>(
  174. '/OA/GetAttachments',
  175. queryParameters: params,
  176. );
  177. final body = response.data;
  178. if (body is! Map) return [];
  179. final result = body['Result'];
  180. if (result is! Map) return [];
  181. final documents = result['documents'];
  182. if (documents is! List) return [];
  183. return documents
  184. .whereType<Map<String, dynamic>>()
  185. .map((e) => BillAttachment.fromJson(e))
  186. .toList();
  187. }
  188. /// 上传附件
  189. Future<Map<String, dynamic>> uploadAttachment(
  190. String filePath,
  191. Map<String, dynamic> metadata,
  192. ) async {
  193. final fileName = (metadata['FILENAME'] as String?) ?? filePath.split('/').last;
  194. final response = await _client.uploadMultipart<Map<String, dynamic>>(
  195. '/OA/UploadAttachment',
  196. files: [await MultipartFile.fromFile(filePath, filename: fileName)],
  197. extraFields: {'metadata': json.encode(metadata)},
  198. );
  199. return response.data ?? {};
  200. }
  201. /// 审核执行(通过/驳回/反审核)
  202. Future<Map<String, dynamic>> executeApproval({
  203. required String bilId, required String bilNo, int bilItm = 0,
  204. required String action, String rem = '', String effDd = '',
  205. String reason = '', bool isPreToStart = false, int nodeIndex = -1,
  206. String dataBx = '',
  207. }) async {
  208. final response = await _client.post<Map<String, dynamic>>(
  209. '/OA/ExecuteApproval',
  210. data: {
  211. 'bilId': bilId, 'bilNo': bilNo, 'bilItm': bilItm,
  212. 'action': action, 'rem': rem, 'effDd': effDd,
  213. 'reason': reason, 'isPreToStart': isPreToStart,
  214. 'nodeIndex': nodeIndex, 'dataBx': dataBx,
  215. },
  216. );
  217. return response.data!;
  218. }
  219. }