From e23e8c6a9e8ed523f1d993dcbf848119465fb11e Mon Sep 17 00:00:00 2001 From: DXC Date: Thu, 9 Apr 2026 17:49:51 +0800 Subject: [PATCH] fix(scrap): resolve material names, specs and operator names in list query --- inventory-backend/app/api/v1/scrap.py | 59 ++++++++++++++++++- .../src/views/operation/scrap/index.vue | 2 +- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/inventory-backend/app/api/v1/scrap.py b/inventory-backend/app/api/v1/scrap.py index 0a81639..a647b0e 100644 --- a/inventory-backend/app/api/v1/scrap.py +++ b/inventory-backend/app/api/v1/scrap.py @@ -8,6 +8,8 @@ from app.models.transaction import TransScrap, TransRepair from app.models.inbound.buy import StockBuy from app.models.inbound.semi import StockSemi from app.models.inbound.product import StockProduct +from app.models.base import MaterialBase +from app.models.system import SysUser import traceback import math @@ -324,8 +326,63 @@ class ScrapService: total = query.count() 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 { - 'list': [r.to_dict() for r in records], + 'list': result_list, 'total': total, 'page': page, 'pageSize': page_size diff --git a/inventory-web/src/views/operation/scrap/index.vue b/inventory-web/src/views/operation/scrap/index.vue index 8a1b4d5..42c7bb1 100644 --- a/inventory-web/src/views/operation/scrap/index.vue +++ b/inventory-web/src/views/operation/scrap/index.vue @@ -30,7 +30,7 @@