feat: 首页全局搜索支持按 SKU 查库位——采购/半成品/成品库存纳入搜索并显示库位
This commit is contained in:
@ -3,6 +3,8 @@ from flask import jsonify, request
|
||||
from . import common_bp
|
||||
from app.models import MaterialBase
|
||||
from app.models.inbound.buy import StockBuy
|
||||
from app.models.inbound.semi import StockSemi
|
||||
from app.models.inbound.product import StockProduct
|
||||
from app.models.bom import BomTable
|
||||
from app.extensions import db
|
||||
|
||||
@ -46,31 +48,38 @@ def global_search():
|
||||
"extra": {"category": b.category or ''}
|
||||
})
|
||||
|
||||
# ── 2. 采购库 (StockBuy) ─────────────────────────────────
|
||||
# 真实字段: barcode, sku (通过 join 搜索关联的 MaterialBase.name)
|
||||
stock_conditions = []
|
||||
for kw in keywords:
|
||||
kw_term = f'%{kw}%'
|
||||
stock_conditions.append(
|
||||
db.or_(
|
||||
MaterialBase.name.ilike(kw_term),
|
||||
StockBuy.barcode.ilike(kw_term),
|
||||
StockBuy.sku.ilike(kw_term)
|
||||
# ── 2. 库存三表(采购/半成品/成品):按 SKU/条码/名称命中,显示所在库位 ──
|
||||
def _append_stock_items(model, type_key, badge):
|
||||
conds = []
|
||||
for kw in keywords:
|
||||
t = f'%{kw}%'
|
||||
c = db.or_(
|
||||
MaterialBase.name.ilike(t),
|
||||
model.sku.ilike(t),
|
||||
model.barcode.ilike(t),
|
||||
)
|
||||
)
|
||||
stocks = StockBuy.query.join(MaterialBase, StockBuy.base_id == MaterialBase.id).filter(
|
||||
db.and_(*stock_conditions)
|
||||
).all()
|
||||
for s in stocks:
|
||||
merged_list.append({
|
||||
"id": s.base_id,
|
||||
"stock_id": s.id,
|
||||
"type": "stock_buy",
|
||||
"title": s.base.name if s.base else '未知物料',
|
||||
"subtitle": f"条码: {s.barcode or '无'} | 库存: {s.stock_quantity}",
|
||||
"badge": "采购库",
|
||||
"extra": {"barcode": s.barcode or '', "status": s.status or ''}
|
||||
})
|
||||
if hasattr(model, 'serial_number'):
|
||||
c = db.or_(c, model.serial_number.ilike(t))
|
||||
conds.append(c)
|
||||
rows = (model.query.join(MaterialBase, model.base_id == MaterialBase.id)
|
||||
.filter(db.and_(*conds)).order_by(model.id.desc()).limit(50).all())
|
||||
for s in rows:
|
||||
merged_list.append({
|
||||
"id": s.base_id,
|
||||
"stock_id": s.id,
|
||||
"type": type_key,
|
||||
"title": s.base.name if s.base else '未知物料',
|
||||
"subtitle": f"SKU: {s.sku or '-'} | 库位: {s.warehouse_location or '-'} | 库存: {s.stock_quantity or 0}",
|
||||
"badge": badge,
|
||||
"extra": {
|
||||
"sku": s.sku or '', "warehouse_location": s.warehouse_location or '',
|
||||
"stock_id": s.id, "source_table": model.__tablename__,
|
||||
}
|
||||
})
|
||||
|
||||
_append_stock_items(StockBuy, 'stock_buy', '采购库')
|
||||
_append_stock_items(StockSemi, 'stock_semi', '半成品')
|
||||
_append_stock_items(StockProduct, 'stock_product', '成品')
|
||||
|
||||
# ── 3. BOM 配方 (BomTable) ──────────────────────────────
|
||||
# 真实字段: bom_no, version
|
||||
|
||||
Reference in New Issue
Block a user