70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
"""
|
|
扫码查库存接口(移动端专用)
|
|
GET /api/v1/scan/inventory?barcode=xxx
|
|
"""
|
|
from flask import Blueprint, jsonify, request
|
|
from app.extensions import db
|
|
from app.models.base import MaterialBase
|
|
from app.models.inbound.buy import StockBuy
|
|
from app.models.inbound.product import StockProduct
|
|
from app.models.inbound.semi import StockSemi
|
|
|
|
scan_bp = Blueprint('scan', __name__, url_prefix='/scan')
|
|
|
|
|
|
def _build_response(stock_record, stock_type: str) -> dict:
|
|
"""联表 MaterialBase 提取物料信息并组装返回结构"""
|
|
material = MaterialBase.query.get(stock_record.base_id)
|
|
return {
|
|
'code': 200,
|
|
'data': {
|
|
'materialName': material.name if material else '未知物料',
|
|
'spec': material.spec_model if material else '',
|
|
'location': stock_record.warehouse_location or '',
|
|
'quantity': float(stock_record.available_quantity) if stock_record.available_quantity else 0.0,
|
|
'stockType': stock_type
|
|
}
|
|
}
|
|
|
|
|
|
@scan_bp.route('/inventory', methods=['GET'])
|
|
def scan_inventory():
|
|
"""
|
|
扫码精确查找库存
|
|
入参: barcode (query string)
|
|
逻辑: 在 StockBuy / StockProduct / StockSemi 三表中精确匹配,只要命中一张即返回
|
|
"""
|
|
barcode = (request.args.get('barcode') or '').strip()
|
|
if not barcode:
|
|
return jsonify({'code': 400, 'msg': 'barcode 参数不能为空'}), 400
|
|
|
|
# 1. 采购库
|
|
buy = StockBuy.query.filter(
|
|
StockBuy.barcode == barcode,
|
|
StockBuy.stock_quantity > 0
|
|
).first()
|
|
if buy:
|
|
return jsonify(_build_response(buy, '采购库'))
|
|
|
|
# 2. 成品库
|
|
product = StockProduct.query.filter(
|
|
StockProduct.barcode == barcode,
|
|
StockProduct.stock_quantity > 0
|
|
).first()
|
|
if product:
|
|
return jsonify(_build_response(product, '成品库'))
|
|
|
|
# 3. 半成品库
|
|
semi = StockSemi.query.filter(
|
|
StockSemi.barcode == barcode,
|
|
StockSemi.stock_quantity > 0
|
|
).first()
|
|
if semi:
|
|
return jsonify(_build_response(semi, '半成品库'))
|
|
|
|
# 4. 全部未命中
|
|
return jsonify({
|
|
'code': 404,
|
|
'msg': f'未找到条码 [{barcode}] 对应的库存记录,或该物料当前库存为零'
|
|
}), 404
|