feat: fix stocktake deletion bug, and add pagination, search, sorting to stocktake lists

This commit is contained in:
DXC
2026-03-19 16:21:09 +08:00
parent 5510bae3b2
commit 53c198f363
4 changed files with 255 additions and 50 deletions

View File

@ -256,12 +256,21 @@ def get_stocks():
@jwt_required()
@permission_required('stock_adjustment:list')
def get_stocktake_discrepancies():
"""获取所有有差异的盘点记录"""
"""获取所有有差异的盘点记录,支持分页、搜索和排序"""
try:
# 查询所有有差异的盘点记录
drafts = StocktakeDraft.query.filter(StocktakeDraft.diff_qty != 0).all()
# 获取分页参数
page = request.args.get('page', 1, type=int)
limit = request.args.get('limit', 20, type=int)
keyword = request.args.get('keyword', '', type=str)
# 先查询有差异的记录
query = StocktakeDraft.query.filter(StocktakeDraft.diff_qty != 0)
# 执行查询(不排序,因为在 Python 中排序)
drafts = query.all()
items = []
for draft in drafts:
diff = float(draft.diff_qty or 0)
if diff == 0:
@ -283,6 +292,11 @@ def get_stocktake_discrepancies():
sku = getattr(stock, 'sku', None) or getattr(stock, 'SKU', '')
warehouse_location = getattr(stock, 'warehouse_location', '')
# 如果有关键词,进行 SKU 模糊匹配
if keyword and sku:
if keyword.lower() not in sku.lower():
continue
# 联表查询 MaterialBase
if base_id:
material = MaterialBase.query.get(base_id)
@ -306,11 +320,22 @@ def get_stocktake_discrepancies():
'remark': draft.remark or ''
})
# 按 SKU 升序排序
items.sort(key=lambda x: (x['sku'] or '').lower())
# 手动分页
total = len(items)
start = (page - 1) * limit
end = start + limit
paginated_items = items[start:end]
return jsonify({
'code': 200,
'data': {
'items': items,
'total': len(items)
'items': paginated_items,
'total': total,
'page': page,
'limit': limit
}
})