feat: 库存接口增加 ai_mode=true 极简返回模式,键名压缩为 n/s/c
This commit is contained in:
@ -137,34 +137,70 @@ def get_stock_info(uuid_or_barcode):
|
|||||||
def get_all_stock():
|
def get_all_stock():
|
||||||
"""
|
"""
|
||||||
获取所有库存 > 0 的物品
|
获取所有库存 > 0 的物品
|
||||||
|
支持 AI 极简模式: ?ai_mode=true
|
||||||
|
- 只返回 name / spec / availableQuantity 三个字段
|
||||||
|
- 键名压缩为 n / s / c
|
||||||
"""
|
"""
|
||||||
|
ai_mode = request.args.get('ai_mode', '').lower() == 'true'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
all_items = []
|
||||||
|
|
||||||
# 1. 采购件
|
# 1. 采购件
|
||||||
materials = []
|
|
||||||
if StockBuy:
|
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. 半成品
|
# 2. 半成品
|
||||||
semis = []
|
|
||||||
if StockSemi:
|
if StockSemi:
|
||||||
try:
|
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:
|
except Exception:
|
||||||
semis = []
|
pass
|
||||||
|
|
||||||
# 3. 成品
|
# 3. 成品
|
||||||
products = []
|
|
||||||
if StockProduct:
|
if StockProduct:
|
||||||
try:
|
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:
|
except Exception:
|
||||||
products = []
|
pass
|
||||||
|
|
||||||
return jsonify({
|
return jsonify(all_items), 200
|
||||||
"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
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
return jsonify({"message": f"查询库存失败: {str(e)}"}), 500
|
return jsonify({"message": f"查询库存失败: {str(e)}"}), 500
|
||||||
|
|||||||
Reference in New Issue
Block a user