feat: initial commit and ignore qwen files
This commit is contained in:
60
backend/app/routers/bom_targets.py
Normal file
60
backend/app/routers/bom_targets.py
Normal file
@ -0,0 +1,60 @@
|
||||
"""BOM 成品搜索 API
|
||||
|
||||
注意:prefix 必须唯一,不能与 deduce_bom.py 的 /api/pms 冲突
|
||||
"""
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.models import MaterialBase, BomTable
|
||||
|
||||
# 修改 prefix 为唯一值,避免与 deduce_bom.py 的 /api/pms 冲突
|
||||
router = APIRouter(prefix="/api/pms/bom", tags=["BOM 成品搜索"])
|
||||
|
||||
|
||||
class BomTargetItem(BaseModel):
|
||||
"""成品下拉项"""
|
||||
id: int
|
||||
name: str
|
||||
spec_model: str | None
|
||||
|
||||
|
||||
@router.get("/targets", response_model=list[BomTargetItem])
|
||||
async def search_bom_targets(q: str = Query("", description="搜索词")):
|
||||
"""
|
||||
成品模糊搜索接口
|
||||
|
||||
- 仅返回在 bom_table 中作为 parent_id 出现过的物料(即真正的成品)
|
||||
- 支持按 name 或 spec_model 模糊匹配
|
||||
"""
|
||||
from app.database import SessionLocalInventory
|
||||
db = SessionLocalInventory()
|
||||
|
||||
try:
|
||||
# 子查询:找出所有作为 parent_id 的物料ID
|
||||
parent_ids = db.query(BomTable.parent_id).distinct().subquery()
|
||||
|
||||
# 基础查询:仅成品
|
||||
query = db.query(MaterialBase).filter(
|
||||
MaterialBase.id.in_(parent_ids)
|
||||
)
|
||||
|
||||
# 如果有搜索词,添加模糊匹配
|
||||
if q:
|
||||
search_pattern = f"%{q}%"
|
||||
query = query.filter(
|
||||
(MaterialBase.name.ilike(search_pattern)) |
|
||||
(MaterialBase.spec_model.ilike(search_pattern))
|
||||
)
|
||||
|
||||
materials = query.limit(50).all()
|
||||
|
||||
return [
|
||||
BomTargetItem(
|
||||
id=m.id,
|
||||
name=m.name,
|
||||
spec_model=m.spec_model
|
||||
)
|
||||
for m in materials
|
||||
]
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user