feat: 商品追踪页面 + 出库提交库存校验

后端:
- feat(backend): 重命名 Batches→Products 接口,新增库存状态(在售/已卖出)和买家信息
- feat(backend): 出库单创建/提交时校验仓库库存,不足则返回明确错误信息
- fix(backend): 出库创建 status 判断逻辑修复(空值默认 draft)

前端:
- feat(client): 批次追踪改为商品追踪,新增状态列(在售/已卖出)和买家/时间列
- fix(client): 无批次号时显示"无批次"而非空
- refactor(client): BatchRecord → ProductTrackingRecord,repository 接口更新

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-04-10 22:26:42 +08:00
parent ce1cbf404c
commit 66e54af8c6
12 changed files with 513 additions and 68 deletions
+22 -5
View File
@@ -43,8 +43,8 @@ class Inventory {
}
}
// 批次追踪:已审核入库单的明细行
class BatchRecord {
// 商品追踪:已审核入库单的明细行,含库存状态和买家信息
class ProductTrackingRecord {
final int id;
final int productId;
final String? productName;
@@ -58,8 +58,13 @@ class BatchRecord {
final String? orderDate;
final String? warehouseName;
final String? supplierName;
// 库存状态
final double currentQty;
final String status; // in_stock | sold_out
final String? buyerName;
final String? soldAt;
const BatchRecord({
const ProductTrackingRecord({
required this.id,
required this.productId,
this.productName,
@@ -73,14 +78,20 @@ class BatchRecord {
this.orderDate,
this.warehouseName,
this.supplierName,
required this.currentQty,
required this.status,
this.buyerName,
this.soldAt,
});
factory BatchRecord.fromJson(Map<String, dynamic> json) {
bool get isSoldOut => status == 'sold_out';
factory ProductTrackingRecord.fromJson(Map<String, dynamic> json) {
final product = json['product'] as Map<String, dynamic>?;
final order = json['order'] as Map<String, dynamic>?;
final warehouse = order?['warehouse'] as Map<String, dynamic>?;
final partner = order?['partner'] as Map<String, dynamic>?;
return BatchRecord(
return ProductTrackingRecord(
id: (json['id'] as num).toInt(),
productId: (json['product_id'] as num).toInt(),
productName: product?['name'] as String?,
@@ -94,6 +105,12 @@ class BatchRecord {
orderDate: order?['order_date'] as String?,
warehouseName: warehouse?['name'] as String?,
supplierName: partner?['name'] as String?,
currentQty: json['current_qty'] != null
? (json['current_qty'] as num).toDouble()
: 0,
status: json['status'] as String? ?? 'in_stock',
buyerName: json['buyer_name'] as String?,
soldAt: json['sold_at'] as String?,
);
}
}
@@ -47,7 +47,7 @@ class InventoryRepository {
}
}
Future<PageResult<BatchRecord>> listBatches({
Future<PageResult<ProductTrackingRecord>> listProducts({
int? productId,
int? warehouseId,
int page = 1,
@@ -60,14 +60,14 @@ class InventoryRepository {
if (productId != null) 'product_id': productId,
if (warehouseId != null) 'warehouse_id': warehouseId,
};
final resp = await _client.get('/inventory/batches', params: params);
final resp = await _client.get('/inventory/products', params: params);
return PageResult.fromJson(
resp.data as Map<String, dynamic>,
BatchRecord.fromJson,
ProductTrackingRecord.fromJson,
);
} on DioException catch (e) {
throw AppException(
e.response?.data?['error'] as String? ?? '获取批次数据失败',
e.response?.data?['error'] as String? ?? '获取商品追踪数据失败',
statusCode: e.response?.statusCode,
);
}
@@ -15,7 +15,8 @@ class BatchTrackingScreen extends ConsumerStatefulWidget {
class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
int _page = 1;
late Future<List<BatchRecord>> _future;
int _total = 0;
late Future<List<ProductTrackingRecord>> _future;
@override
void initState() {
@@ -26,17 +27,18 @@ class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
void _fetch() {
_future = ref
.read(inventoryRepositoryProvider)
.listBatches(page: _page, pageSize: 20)
.then((r) => r.data);
.listProducts(page: _page, pageSize: 20)
.then((r) {
_total = r.total;
return r.data;
});
}
void _refetch() {
setState(() => _fetch());
}
void _refetch() => setState(() => _fetch());
@override
Widget build(BuildContext context) {
return FutureBuilder<List<BatchRecord>>(
return FutureBuilder<List<ProductTrackingRecord>>(
future: _future,
builder: (context, snap) {
if (snap.connectionState == ConnectionState.waiting) {
@@ -61,30 +63,26 @@ class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
);
}
Widget _buildTable(List<BatchRecord> records) {
Widget _buildTable(List<ProductTrackingRecord> records) {
return DataTableCard(
totalCount: records.length,
totalCount: _total,
page: _page,
onPageChanged: (p) {
setState(() {
_page = p;
_fetch();
});
},
onPageChanged: (p) => setState(() {
_page = p;
_fetch();
}),
toolbar: Row(
children: [
const Text('已审核入库批次',
const Text('已审核入库商品(含库存与销售状态)',
style: TextStyle(
fontSize: 13, color: AppTheme.textSecondary)),
const Spacer(),
IconButton(
icon: const Icon(Icons.refresh, size: 18),
onPressed: () {
setState(() {
_page = 1;
_fetch();
});
},
onPressed: () => setState(() {
_page = 1;
_fetch();
}),
tooltip: '刷新',
),
],
@@ -99,12 +97,14 @@ class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
DataColumn(label: Text('入库日期')),
DataColumn(label: Text('数量'), numeric: true),
DataColumn(label: Text('单价'), numeric: true),
DataColumn(label: Text('状态')),
DataColumn(label: Text('买家/时间')),
],
rows: records.isEmpty
? [
const DataRow(cells: [
DataCell(SizedBox()),
DataCell(Text('暂无批次记录',
DataCell(Text('暂无记录',
style: TextStyle(color: AppTheme.textSecondary))),
DataCell(SizedBox()),
DataCell(SizedBox()),
@@ -113,33 +113,34 @@ class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
DataCell(SizedBox()),
DataCell(SizedBox()),
DataCell(SizedBox()),
DataCell(SizedBox()),
DataCell(SizedBox()),
])
]
: records.map((r) {
final hasBatch =
r.batchNo != null && r.batchNo!.isNotEmpty;
final batchText =
(r.batchNo != null && r.batchNo!.isNotEmpty)
? r.batchNo!
: null;
return DataRow(cells: [
DataCell(
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(r.productName ?? '-',
DataCell(Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(r.productName ?? '-',
style: const TextStyle(
fontSize: 13, fontWeight: FontWeight.w500)),
if (r.productCode != null)
Text(r.productCode!,
style: const TextStyle(
fontSize: 13,
fontWeight: FontWeight.w500)),
if (r.productCode != null)
Text(r.productCode!,
style: const TextStyle(
fontSize: 11,
color: AppTheme.textSecondary,
fontFamily: 'monospace')),
],
),
),
fontSize: 11,
color: AppTheme.textSecondary,
fontFamily: 'monospace')),
],
)),
DataCell(Text(r.productSpec ?? '-',
style: const TextStyle(fontSize: 12))),
DataCell(hasBatch
DataCell(batchText != null
? Container(
padding: const EdgeInsets.symmetric(
horizontal: 6, vertical: 2),
@@ -147,18 +148,19 @@ class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
color: AppTheme.primary.withOpacity(0.08),
borderRadius: BorderRadius.circular(3),
),
child: Text(r.batchNo!,
child: Text(batchText,
style: const TextStyle(
fontSize: 12,
color: AppTheme.primary,
fontFamily: 'monospace')),
)
: const Text('-',
: const Text('无批次',
style: TextStyle(
fontSize: 12,
color: AppTheme.textSecondary))),
DataCell(Text(r.orderNo ?? '-',
style: const TextStyle(
fontSize: 12,
fontSize: 11,
color: AppTheme.primary,
fontFamily: 'monospace'))),
DataCell(Text(r.supplierName ?? '-',
@@ -174,8 +176,60 @@ class _BatchTrackingScreenState extends ConsumerState<BatchTrackingScreen> {
DataCell(Text(
'¥${r.unitPrice.toStringAsFixed(2)}',
style: const TextStyle(fontSize: 13))),
DataCell(_StatusBadge(r.isSoldOut)),
DataCell(r.isSoldOut
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
r.buyerName?.isNotEmpty == true
? r.buyerName!
: '未知买家',
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500),
),
if (r.soldAt != null)
Text(r.soldAt!.length > 10
? r.soldAt!.substring(0, 10)
: r.soldAt!,
style: const TextStyle(
fontSize: 11,
color: AppTheme.textSecondary)),
],
)
: const Text('-',
style:
TextStyle(color: AppTheme.textSecondary))),
]);
}).toList(),
);
}
}
class _StatusBadge extends StatelessWidget {
final bool isSoldOut;
const _StatusBadge(this.isSoldOut);
@override
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
decoration: BoxDecoration(
color: isSoldOut
? AppTheme.textSecondary.withOpacity(0.12)
: AppTheme.success.withOpacity(0.12),
borderRadius: BorderRadius.circular(3),
),
child: Text(
isSoldOut ? '已卖出' : '在售',
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: isSoldOut ? AppTheme.textSecondary : AppTheme.success,
),
),
);
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ class _AppShellState extends ConsumerState<AppShell> {
_NavItem(icon: Icons.input, label: '入库管理', path: '/stock-in'),
_NavItem(icon: Icons.output, label: '出库管理', path: '/stock-out'),
_NavItem(icon: Icons.inventory_2, label: '库存管理', path: '/inventory'),
_NavItem(icon: Icons.track_changes, label: '批次追踪', path: '/batches'),
_NavItem(icon: Icons.track_changes, label: '商品追踪', path: '/batches'),
_NavItem(
icon: Icons.account_balance_wallet,
label: '财务管理',