fix: 创建产品时自动将当前位置设为当前登录用户(谁创建谁持有)

This commit is contained in:
2026-08-07 15:42:10 +08:00
parent bc43b5a732
commit beea543fbd
2 changed files with 16 additions and 4 deletions

View File

@ -147,8 +147,8 @@ async def get_product(db: AsyncSession, product_id: uuid.UUID) -> Product:
return product
async def create_product(db: AsyncSession, data: ProductCreate) -> ProductResponse:
"""创建产品 — 自动生成 16 位 HEX 序列号"""
async def create_product(db: AsyncSession, data: ProductCreate, creator_username: str = "") -> ProductResponse:
"""创建产品 — 自动生成 16 位 HEX 序列号,初始位置设为创建者"""
from app.services.counter_service import ensure_sequence, next_hex_id
from app.models.production_order import ProductionOrder
@ -180,10 +180,18 @@ async def create_product(db: AsyncSession, data: ProductCreate) -> ProductRespon
material_type=data.material_type or None,
external_serial=data.external_serial,
parent_product_id=data.parent_product_id,
current_location_id=creator_username or None, # 谁创建,初始位置就是谁
)
db.add(product)
await db.commit()
await db.refresh(product, ["order"])
# 查创建者的真实姓名
creator_display_name = ""
if creator_username:
name_map = _lookup_display_names([creator_username])
creator_display_name = name_map.get(creator_username, "")
return ProductResponse(
id=product.id,
serial_number=product.serial_number,
@ -197,6 +205,7 @@ async def create_product(db: AsyncSession, data: ProductCreate) -> ProductRespon
material_type=product.material_type,
parent_product_id=product.parent_product_id,
current_location_id=product.current_location_id,
current_location_name=creator_display_name or None,
overall_status=product.overall_status,
status=product.status,
created_at=product.created_at,