From cf55c948263ada60c995370f639784d8c02a1024 Mon Sep 17 00:00:00 2001 From: DXC Date: Tue, 19 May 2026 09:53:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BA=93=E5=AD=98=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20ai=5Fmode=3Dtrue=20=E6=9E=81=E7=AE=80?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=A8=A1=E5=BC=8F=EF=BC=8C=E9=94=AE=E5=90=8D?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9=E4=B8=BA=20n/s/c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inventory-backend/app/api/v1/inbound/stock.py | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/inventory-backend/app/api/v1/inbound/stock.py b/inventory-backend/app/api/v1/inbound/stock.py index 1296e98..d7c3a3d 100644 --- a/inventory-backend/app/api/v1/inbound/stock.py +++ b/inventory-backend/app/api/v1/inbound/stock.py @@ -137,34 +137,70 @@ def get_stock_info(uuid_or_barcode): def get_all_stock(): """ 获取所有库存 > 0 的物品 + 支持 AI 极简模式: ?ai_mode=true + - 只返回 name / spec / availableQuantity 三个字段 + - 键名压缩为 n / s / c """ + ai_mode = request.args.get('ai_mode', '').lower() == 'true' + try: + all_items = [] + # 1. 采购件 - materials = [] if StockBuy: - materials = StockBuy.query.filter(StockBuy.stock_quantity > 0).all() + rows = StockBuy.query.filter( + StockBuy.stock_quantity > 0 + ).options(joinedload(StockBuy.base)).all() + for item in rows: + if ai_mode: + b = item.base + all_items.append({ + 'n': b.name if b else '', + 's': b.spec_model if b else '', + 'c': float(item.available_quantity or 0) + }) + else: + all_items.append(item.to_dict()) # 2. 半成品 - semis = [] if StockSemi: try: - semis = StockSemi.query.filter(StockSemi.stock_quantity > 0).all() + rows = StockSemi.query.filter( + StockSemi.stock_quantity > 0 + ).options(joinedload(StockSemi.base)).all() + for item in rows: + if ai_mode: + b = item.base + all_items.append({ + 'n': b.name if b else '', + 's': b.spec_model if b else '', + 'c': float(item.available_quantity or 0) + }) + else: + all_items.append(item.to_dict()) except Exception: - semis = [] + pass # 3. 成品 - products = [] if StockProduct: try: - products = StockProduct.query.filter(StockProduct.stock_quantity > 0).all() + rows = StockProduct.query.filter( + StockProduct.stock_quantity > 0 + ).options(joinedload(StockProduct.base)).all() + for item in rows: + if ai_mode: + b = item.base + all_items.append({ + 'n': b.name if b else '', + 's': b.spec_model if b else '', + 'c': float(item.available_quantity or 0) + }) + else: + all_items.append(item.to_dict()) except Exception: - products = [] + pass - return jsonify({ - "materials": [item.to_dict() for item in materials], - "semis": [item.to_dict() for item in semis], - "products": [item.to_dict() for item in products] - }), 200 + return jsonify(all_items), 200 except Exception as e: print(f"Error: {e}") return jsonify({"message": f"查询库存失败: {str(e)}"}), 500