3888cf2d65
- SlowRequestInterceptor:单次 API 调用超 500ms 即 debugPrint long-request 日志并经 ErrorReporter 上报(error_type=slow_api,按 method+归一路径 5 分钟 节流)。服务端 GIN 日志只见自身处理耗时,网络往返段只有客户端可观测。 - 拆除 10 个列表 provider 的 _cache 失败兜底:实测线上接口客户端视角典型 50-120ms、最坏约 270ms(列表类全部 <500ms),失败静默端旧数据弊大于利 (曾放大跨账号残留问题),改为明确进入错误态由用户重试。 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
168 lines
5.1 KiB
Dart
168 lines
5.1 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_in.dart';
|
|
import '../models/stock_summary.dart';
|
|
import '../repositories/stock_in_repository.dart';
|
|
import 'connectivity_provider.dart';
|
|
import 'inventory_provider.dart';
|
|
|
|
final stockInRepositoryProvider = Provider<StockInRepository>((ref) {
|
|
return StockInRepository(ref.watch(apiClientProvider));
|
|
});
|
|
|
|
final stockInListProvider =
|
|
AsyncNotifierProvider<StockInListNotifier, PageResult<StockInOrder>>(
|
|
StockInListNotifier.new,
|
|
);
|
|
|
|
/// 入库 KPI 汇总——自然月口径(财务屏「本月采购额」用;换店/网络恢复自动刷新)。
|
|
final stockInSummaryProvider = FutureProvider.autoDispose<StockSummary>((ref) {
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
ref.watch(networkRecoveryCountProvider);
|
|
return ref.read(stockInRepositoryProvider).summary();
|
|
});
|
|
|
|
/// 入库 KPI 汇总——近 30 天滚动窗(入库列表卡片用;月初自然月全 0,用户拍板改滚动)。
|
|
final stockInSummary30Provider =
|
|
FutureProvider.autoDispose<StockSummary>((ref) {
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
ref.watch(networkRecoveryCountProvider);
|
|
return ref.read(stockInRepositoryProvider).summary(window: 'rolling30');
|
|
});
|
|
|
|
class StockInListNotifier extends AsyncNotifier<PageResult<StockInOrder>> {
|
|
int _page = 1;
|
|
int _pageSize = AppConstants.defaultPageSize;
|
|
// 默认「草稿+待审核」(2026-07-14 用户拍板,状态筛选改多选后的默认态)。
|
|
String _status = 'draft,pending';
|
|
String? _startDate;
|
|
String? _endDate;
|
|
String _keyword = '';
|
|
Map<String, String> _detail = const {};
|
|
Timer? _searchDebounce;
|
|
|
|
@override
|
|
Future<PageResult<StockInOrder>> build() async {
|
|
ref.onDispose(() => _searchDebounce?.cancel());
|
|
ref.watch(authStateProvider.select((s) => s.user?.shopId));
|
|
ref.watch(networkRecoveryCountProvider);
|
|
return _fetch();
|
|
}
|
|
|
|
Future<PageResult<StockInOrder>> _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<String, String> get currentDetail => _detail;
|
|
|
|
/// 详细搜索多字段(order_no/partner_id/series/spec/category_id/... → 后端组合 AND)。
|
|
void setDetail(Map<String, String> 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<PageResult<StockInOrder>>.loading()
|
|
.copyWithPrevious(state);
|
|
_fetch().then((result) {
|
|
state = AsyncValue.data(result);
|
|
}, onError: (e, st) {
|
|
state = AsyncValue.error(e, st);
|
|
});
|
|
}
|
|
|
|
Future<void> createOrder(Map<String, dynamic> data) async {
|
|
await ref.read(stockInRepositoryProvider).create(data);
|
|
reload();
|
|
}
|
|
|
|
Future<void> deleteOrder(int id) async {
|
|
await ref.read(stockInRepositoryProvider).delete(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> submitOrder(int id) async {
|
|
await ref.read(stockInRepositoryProvider).submit(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> approveOrder(int id) async {
|
|
await ref.read(stockInRepositoryProvider).approve(id);
|
|
reload();
|
|
ref.invalidate(inventoryListProvider);
|
|
}
|
|
|
|
Future<void> rejectOrder(int id) async {
|
|
await ref.read(stockInRepositoryProvider).reject(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> withdrawOrder(int id) async {
|
|
await ref.read(stockInRepositoryProvider).withdraw(id);
|
|
reload();
|
|
}
|
|
|
|
Future<void> returnItems(int id, List<int> itemIds) async {
|
|
await ref.read(stockInRepositoryProvider).returnItems(id, itemIds);
|
|
reload();
|
|
ref.invalidate(inventoryListProvider);
|
|
}
|
|
}
|