import 'package:dio/dio.dart'; import '../core/api/api_client.dart'; import '../core/exceptions.dart'; import '../core/models/page_result.dart'; import '../models/finance.dart'; class FinanceRepository { final ApiClient _client; const FinanceRepository(this._client); Future> listRecords({ String? type, String? month, int page = 1, int pageSize = 50, }) async { try { final params = { 'page': page, 'page_size': pageSize, if (type != null && type.isNotEmpty) 'type': type, if (month != null && month.isNotEmpty) 'month': month, }; final resp = await _client.get('/finance/records', params: params); return PageResult.fromJson( resp.data as Map, FinanceRecord.fromJson, ); } on DioException catch (e) { throw AppException( e.response?.data?['error'] as String? ?? '获取财务记录失败', statusCode: e.response?.statusCode, ); } } Future create(Map body) async { try { await _client.post('/finance/records', data: body); } on DioException catch (e) { throw AppException( e.response?.data?['error'] as String? ?? '创建记录失败', statusCode: e.response?.statusCode, ); } } Future close(int id) async { try { await _client.put('/finance/records/$id/close'); } on DioException catch (e) { throw AppException( e.response?.data?['error'] as String? ?? '操作失败', statusCode: e.response?.statusCode, ); } } Future closeByRef(String refType, int refId) async { try { await _client.put( '/finance/records/close-by-ref?ref_type=$refType&ref_id=$refId'); } on DioException catch (e) { throw AppException( e.response?.data?['error'] as String? ?? '结清失败', statusCode: e.response?.statusCode, ); } } }