import 'dart:async'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../core/api/api_client.dart'; import '../core/auth/auth_state.dart'; import '../core/config/app_constants.dart'; import '../core/models/page_result.dart'; import '../models/stock_in.dart'; import '../models/stock_summary.dart'; import '../repositories/stock_in_repository.dart'; import 'connectivity_provider.dart'; import 'inventory_provider.dart'; final stockInRepositoryProvider = Provider((ref) { return StockInRepository(ref.watch(apiClientProvider)); }); final stockInListProvider = AsyncNotifierProvider>( StockInListNotifier.new, ); /// 入库 KPI 汇总——自然月口径(财务屏「本月采购额」用;换店/网络恢复自动刷新)。 final stockInSummaryProvider = FutureProvider.autoDispose((ref) { ref.watch(authStateProvider.select((s) => s.user?.shopId)); ref.watch(networkRecoveryCountProvider); return ref.read(stockInRepositoryProvider).summary(); }); /// 入库 KPI 汇总——近 30 天滚动窗(入库列表卡片用;月初自然月全 0,用户拍板改滚动)。 final stockInSummary30Provider = FutureProvider.autoDispose((ref) { ref.watch(authStateProvider.select((s) => s.user?.shopId)); ref.watch(networkRecoveryCountProvider); return ref.read(stockInRepositoryProvider).summary(window: 'rolling30'); }); class StockInListNotifier extends AsyncNotifier> { int _page = 1; int _pageSize = AppConstants.defaultPageSize; String _status = ''; String? _startDate; String? _endDate; String _keyword = ''; Map _detail = const {}; PageResult? _cache; Timer? _searchDebounce; @override Future> build() async { ref.onDispose(() => _searchDebounce?.cancel()); ref.watch(authStateProvider.select((s) => s.user?.shopId)); ref.watch(networkRecoveryCountProvider); try { final result = await _fetch(); _cache = result; return result; } catch (_) { if (_cache != null) return _cache!; rethrow; } } Future> _fetch() { return ref.read(stockInRepositoryProvider).list( status: _status.isEmpty ? null : _status, startDate: _startDate, endDate: _endDate, keyword: _keyword.isEmpty ? null : _keyword, detail: _detail.isEmpty ? null : _detail, page: _page, pageSize: _pageSize, ); } Map get currentDetail => _detail; /// 详细搜索多字段(order_no/partner_id/series/spec/category_id/... → 后端组合 AND)。 void setDetail(Map detail) { _detail = detail; _page = 1; reload(); } void setPage(int page) { _page = page; reload(); } void setPageSize(int pageSize) { _pageSize = pageSize; _page = 1; reload(); } /// 当前服务端状态过滤值(供页面进入时对齐标签/下拉,避免重复拉取) String get currentStatus => _status; String get currentKeyword => _keyword; void setStatus(String status) { _status = status; _page = 1; reload(); } void setDateRange(String? startDate, String? endDate) { _startDate = startDate; _endDate = endDate; _page = 1; reload(); } /// 关键词搜索(含酒名模糊反查,单次约 55ms 的明细扫描)。 /// 去重 + 防抖限流:同词不重查;连发回车/狂点合并取最后一次,避免昂贵查询被刷。 void setKeyword(String keyword) { final kw = keyword.trim(); if (kw == _keyword) return; _searchDebounce?.cancel(); _searchDebounce = Timer(const Duration(milliseconds: 350), () { _keyword = kw; _page = 1; reload(); }); } void reload() { // 保留上一次数据(isReloading)→ 屏幕可继续渲染旧内容 + 叠加半透明进度,不白屏。 state = const AsyncValue>.loading() .copyWithPrevious(state); _fetch().then((result) { _cache = result; state = AsyncValue.data(result); }, onError: (e, st) { if (_cache != null) { state = AsyncValue.data(_cache!); } else { state = AsyncValue.error(e, st); } }); } Future createOrder(Map data) async { await ref.read(stockInRepositoryProvider).create(data); reload(); } Future deleteOrder(int id) async { await ref.read(stockInRepositoryProvider).delete(id); reload(); } Future submitOrder(int id) async { await ref.read(stockInRepositoryProvider).submit(id); reload(); } Future approveOrder(int id) async { await ref.read(stockInRepositoryProvider).approve(id); reload(); ref.invalidate(inventoryListProvider); } Future rejectOrder(int id) async { await ref.read(stockInRepositoryProvider).reject(id); reload(); } Future withdrawOrder(int id) async { await ref.read(stockInRepositoryProvider).withdraw(id); reload(); } Future returnItems(int id, List itemIds) async { await ref.read(stockInRepositoryProvider).returnItems(id, itemIds); reload(); ref.invalidate(inventoryListProvider); } }