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