fix(scrap): resolve material names, specs and operator names in list query
This commit is contained in:
@ -8,6 +8,8 @@ from app.models.transaction import TransScrap, TransRepair
|
|||||||
from app.models.inbound.buy import StockBuy
|
from app.models.inbound.buy import StockBuy
|
||||||
from app.models.inbound.semi import StockSemi
|
from app.models.inbound.semi import StockSemi
|
||||||
from app.models.inbound.product import StockProduct
|
from app.models.inbound.product import StockProduct
|
||||||
|
from app.models.base import MaterialBase
|
||||||
|
from app.models.system import SysUser
|
||||||
import traceback
|
import traceback
|
||||||
import math
|
import math
|
||||||
|
|
||||||
@ -324,8 +326,63 @@ class ScrapService:
|
|||||||
total = query.count()
|
total = query.count()
|
||||||
records = query.offset((page - 1) * page_size).limit(page_size).all()
|
records = query.offset((page - 1) * page_size).limit(page_size).all()
|
||||||
|
|
||||||
|
# 遍历结果,补充操作人姓名、物料名称、规格
|
||||||
|
result_list = []
|
||||||
|
for r in records:
|
||||||
|
item = r.to_dict()
|
||||||
|
|
||||||
|
# 1. 解析操作人姓名
|
||||||
|
if r.operator_name:
|
||||||
|
# operator_name 可能是用户ID或用户名,尝试解析为真实姓名
|
||||||
|
try:
|
||||||
|
# 尝试将 operator_name 当作用户ID查询
|
||||||
|
user_id = int(r.operator_name)
|
||||||
|
user = SysUser.query.get(user_id)
|
||||||
|
if user:
|
||||||
|
# 解析存储格式: "张三/zhangsan"
|
||||||
|
raw_name = user.username
|
||||||
|
if '/' in raw_name:
|
||||||
|
item['operator_name'] = raw_name.split('/')[0]
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
# 如果不是数字ID,保持原值
|
||||||
|
pass
|
||||||
|
|
||||||
|
# 2. 多态解析物料名称与规格
|
||||||
|
material_name = ''
|
||||||
|
spec_model = ''
|
||||||
|
|
||||||
|
if r.source_table == 'trans_repair':
|
||||||
|
# 维修单
|
||||||
|
repair = TransRepair.query.get(r.stock_id)
|
||||||
|
if repair:
|
||||||
|
material_name = repair.material_name or ''
|
||||||
|
spec_model = ''
|
||||||
|
elif r.source_table in ['stock_buy', 'stock_semi', 'stock_product']:
|
||||||
|
# 常规库存表
|
||||||
|
stock_model = None
|
||||||
|
if r.source_table == 'stock_buy':
|
||||||
|
stock_model = StockBuy.query.get(r.stock_id)
|
||||||
|
elif r.source_table == 'stock_semi':
|
||||||
|
stock_model = StockSemi.query.get(r.stock_id)
|
||||||
|
elif r.source_table == 'stock_product':
|
||||||
|
stock_model = StockProduct.query.get(r.stock_id)
|
||||||
|
|
||||||
|
if stock_model and hasattr(stock_model, 'base_id') and stock_model.base_id:
|
||||||
|
base = MaterialBase.query.get(stock_model.base_id)
|
||||||
|
if base:
|
||||||
|
material_name = base.name or ''
|
||||||
|
spec_model = base.spec_model or ''
|
||||||
|
elif stock_model and hasattr(stock_model, 'base') and stock_model.base:
|
||||||
|
material_name = stock_model.base.name or ''
|
||||||
|
spec_model = stock_model.base.spec_model or ''
|
||||||
|
|
||||||
|
item['material_name'] = material_name
|
||||||
|
item['spec_model'] = spec_model
|
||||||
|
|
||||||
|
result_list.append(item)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
'list': [r.to_dict() for r in records],
|
'list': result_list,
|
||||||
'total': total,
|
'total': total,
|
||||||
'page': page,
|
'page': page,
|
||||||
'pageSize': page_size
|
'pageSize': page_size
|
||||||
|
|||||||
@ -30,7 +30,7 @@
|
|||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="规格型号" min-width="140">
|
<el-table-column label="规格型号" min-width="140">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
{{ row.material_spec || '-' }}
|
{{ row.spec_model || '-' }}
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column prop="quantity" label="报废数量" width="100" align="right" />
|
<el-table-column prop="quantity" label="报废数量" width="100" align="right" />
|
||||||
|
|||||||
Reference in New Issue
Block a user