fix: clean orphaned permission tree nodes and enhance outbound search with material name/spec model

This commit is contained in:
DXC
2026-03-20 09:53:32 +08:00
parent 6336432a5c
commit 74089c7d7d
3 changed files with 42 additions and 7 deletions

View File

@ -195,25 +195,48 @@ class OutboundService:
def get_grouped_list(page=1, per_page=10, keyword=None, start_date=None, end_date=None):
"""
查询出库记录(按出库单号分组),包含详细物品信息
支持跨表搜索单号、领用人、SKU、物料名称、规格型号
"""
# 1. 查询分页单号
# 如果有关键词,需要联表搜索物料名称和规格型号
if keyword:
# 子查询:关联 material_base 表获取物料名称和规格型号
material_join = db.session.query(
TransOutbound.outbound_no
).join(
MaterialBase,
TransOutbound.sku == MaterialBase.sku
).filter(or_(
MaterialBase.name.ilike(f'%{keyword}%'),
MaterialBase.spec_model.ilike(f'%{keyword}%')
)).subquery()
# 主搜索条件单号、领用人、SKU + 物料名称、规格型号
keyword_conditions = or_(
TransOutbound.outbound_no.ilike(f'%{keyword}%'),
TransOutbound.consumer_name.ilike(f'%{keyword}%'),
TransOutbound.sku.ilike(f'%{keyword}%'),
TransOutbound.outbound_no.in_(material_join) # 匹配物料名称/规格型号的单号
)
else:
keyword_conditions = None
stmt = db.session.query(
TransOutbound.outbound_no,
func.max(TransOutbound.outbound_time).label('max_time')
).group_by(TransOutbound.outbound_no)
if keyword:
stmt = stmt.filter(or_(
TransOutbound.outbound_no.ilike(f'%{keyword}%'),
TransOutbound.consumer_name.ilike(f'%{keyword}%'),
TransOutbound.sku.ilike(f'%{keyword}%')
))
if keyword_conditions is not None:
stmt = stmt.filter(keyword_conditions)
if start_date and end_date:
stmt = stmt.filter(TransOutbound.outbound_time.between(start_date, end_date))
stmt = stmt.order_by(desc('max_time'))
# 使用 distinct 确保跨表查询不重复
stmt = stmt.distinct()
pagination = stmt.paginate(page=page, per_page=per_page, error_out=False)
outbound_nos = [row.outbound_no for row in pagination.items]