feat: 产品管理 — 当前位置显示真实姓名 + 编辑弹窗 + 删除确认

This commit is contained in:
2026-08-07 15:38:01 +08:00
parent d3d501dfeb
commit bc43b5a732
5 changed files with 254 additions and 180 deletions

View File

@ -278,6 +278,35 @@ async def update_overall_status(db: AsyncSession, serial_number: str, status_val
return await get_product_by_serial(db, serial_number)
def _lookup_display_names(location_ids: list[str]) -> dict[str, str]:
"""批量查询 MOM sys_user将 username 映射为真实姓名"""
if not location_ids:
return {}
from app.core.mom_database import MomSessionLocal
from sqlalchemy import text
db = MomSessionLocal()
try:
# 过滤掉特殊值
real_ids = [uid for uid in location_ids if uid and uid != "virtual_warehouse"]
if not real_ids:
return {}
# 用 LIKE 模糊匹配批量查出
conditions = " OR ".join([f"username LIKE '%/{uid}'" for uid in real_ids])
result = db.execute(
text(f"SELECT username, SPLIT_PART(username, '/', 1) as display_name FROM sys_user WHERE {conditions}")
)
mapping = {}
for row in result:
full_username = row[0]
display_name = row[1]
# 从 full_username 末尾提取短用户名: "张三/zhangsan01" → "zhangsan01"
short = full_username.split("/")[-1] if "/" in full_username else full_username
mapping[short] = display_name
return mapping
finally:
db.close()
async def get_all_products(
db: AsyncSession,
skip: int = 0,
@ -341,6 +370,11 @@ async def get_all_products(
result = await db.execute(stmt)
products = result.scalars().all()
# 批量查询当前位置对应的真实姓名
location_ids = [p.current_location_id for p in products if p.current_location_id]
name_map = _lookup_display_names(location_ids)
return [
ProductResponse(
id=p.id,
@ -355,9 +389,37 @@ async def get_all_products(
material_type=p.material_type,
parent_product_id=p.parent_product_id,
current_location_id=p.current_location_id,
current_location_name=(
"仓库" if p.current_location_id == "virtual_warehouse"
else name_map.get(p.current_location_id) if p.current_location_id
else None
),
overall_status=p.overall_status,
status=p.status,
created_at=p.created_at,
)
for p in products
]
async def delete_product(db: AsyncSession, product_id: uuid.UUID) -> None:
"""删除产品及其关联任务"""
product = await get_product(db, product_id)
# 删除关联任务记录
from app.models.task import TaskRecord
tasks_result = await db.execute(
select(Task).where(Task.product_id == product_id)
)
tasks = tasks_result.scalars().all()
for task in tasks:
await db.execute(
select(TaskRecord).where(TaskRecord.task_id == task.id)
)
# 级联删除已在模型中定义,直接删任务
# 删除产品task 有外键 CASCADE检查模型
# 手动删关联任务确保完整
for task in tasks:
await db.delete(task)
await db.delete(product)
await db.commit()