102ace42ac
- 搜索框 hint 改为「单号/往来单位/酒名」 - setKeyword 加去重+350ms 防抖:同词不重查、连发回车/狂点合并取最后一次, 避免酒名模糊查询(单次约 55ms 明细扫描)被刷 - Timer 在 provider dispose 时取消 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
158 lines
4.2 KiB
Dart
158 lines
4.2 KiB
Dart
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_out.dart';
|
|
import '../repositories/stock_out_repository.dart';
|
|
import 'connectivity_provider.dart';
|
|
import 'inventory_provider.dart';
|
|
|
|
final stockOutRepositoryProvider = Provider<StockOutRepository>((ref) {
|
|
return StockOutRepository(ref.watch(apiClientProvider));
|
|
});
|
|
|
|
final stockOutListProvider =
|
|
AsyncNotifierProvider<StockOutListNotifier, PageResult<StockOutOrder>>(
|
|
StockOutListNotifier.new,
|
|
);
|
|
|
|
class StockOutListNotifier extends AsyncNotifier<PageResult<StockOutOrder>> {
|
|
int _page = 1;
|
|
int _pageSize = AppConstants.defaultPageSize;
|
|
String _status = '';
|
|
String? _startDate;
|
|
String? _endDate;
|
|
String _keyword = '';
|
|
String _productCode = '';
|
|
PageResult<StockOutOrder>? _cache;
|
|
Timer? _searchDebounce;
|
|
|
|
@override
|
|
Future<PageResult<StockOutOrder>> 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<PageResult<StockOutOrder>> _fetch() {
|
|
return ref.read(stockOutRepositoryProvider).list(
|
|
status: _status.isEmpty ? null : _status,
|
|
startDate: _startDate,
|
|
endDate: _endDate,
|
|
keyword: _keyword.isEmpty ? null : _keyword,
|
|
productCode: _productCode.isEmpty ? null : _productCode,
|
|
page: _page,
|
|
pageSize: _pageSize,
|
|
);
|
|
}
|
|
|
|
void setPage(int page) {
|
|
_page = page;
|
|
reload();
|
|
}
|
|
|
|
void setPageSize(int pageSize) {
|
|
_pageSize = pageSize;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
/// 当前服务端状态过滤值(供页面进入时对齐标签/下拉,避免重复拉取)
|
|
String get currentStatus => _status;
|
|
|
|
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();
|
|
});
|
|
}
|
|
|
|
/// 按商品编码精确反查(独立于 keyword)
|
|
void setProductCode(String code) {
|
|
_productCode = code;
|
|
_page = 1;
|
|
reload();
|
|
}
|
|
|
|
void reload() {
|
|
state = const AsyncValue.loading();
|
|
_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<void> createOrder(Map<String, dynamic> data) async {
|
|
await ref.read(stockOutRepositoryProvider).create(data);
|
|
reload();
|
|
}
|
|
|
|
Future<void> deleteOrder(int id) async {
|
|
await ref.read(stockOutRepositoryProvider).delete(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> submitOrder(int id) async {
|
|
await ref.read(stockOutRepositoryProvider).submit(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> approveOrder(int id) async {
|
|
await ref.read(stockOutRepositoryProvider).approve(id);
|
|
reload();
|
|
ref.invalidate(inventoryListProvider);
|
|
}
|
|
|
|
Future<void> rejectOrder(int id) async {
|
|
await ref.read(stockOutRepositoryProvider).reject(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> withdrawOrder(int id) async {
|
|
await ref.read(stockOutRepositoryProvider).withdraw(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> returnItems(int id, List<int> itemIds) async {
|
|
await ref.read(stockOutRepositoryProvider).returnItems(id, itemIds);
|
|
reload();
|
|
ref.invalidate(inventoryListProvider);
|
|
}
|
|
}
|