fix(stocktake): 盘库扫码改为精确匹配,修复漏匹配与性能问题
问题:
1. 准确性 bug: 前端用 getStockList({pageSize:10, keyword}) 模糊搜索 + find()
当 SKU 前缀相同、目标不在前10条时,误报'未找到该物料库存'
2. 性能: 每次扫码全表 ilike %x% 搜索 3 张表,不走索引
修复:
- 后端 get_stock_info 改为精确匹配(==)优先,未命中再回退模糊搜索
- 新增 GET /inbound/stock/scan 扫码精确匹配接口,返回唯一命中
- 前端 onScanSuccess/handleManualInput 改用 scanStockByBarcode
一次请求直接命中,不再依赖 pageSize:10 + find()
This commit is contained in:
@ -90,54 +90,70 @@ def get_stock_record(source_table, stock_id, for_update=False):
|
||||
|
||||
def get_stock_info(uuid_or_barcode):
|
||||
"""
|
||||
根据 uuid 或 barcode 查询库存信息
|
||||
返回: (item, source_table, stock_id)
|
||||
根据 uuid 或 barcode 查询库存信息(★ 精确匹配优先,性能与准确性兼顾)
|
||||
|
||||
修复: 原来用 ilike %x% 全表模糊搜索 + .first(),
|
||||
在 SKU 前缀相同的场景会命中错误记录或漏匹配。
|
||||
改为: 精确匹配(==)优先,命中即返回;无精确命中再回退模糊搜索。
|
||||
|
||||
返回: (item, source_table, stock_id) 或 (None, None, None)
|
||||
"""
|
||||
# 清洗输入:去掉前后空格和换行符
|
||||
uuid_or_barcode = str(uuid_or_barcode).strip()
|
||||
code = str(uuid_or_barcode).strip()
|
||||
if not code:
|
||||
return None, None, None
|
||||
|
||||
# 1. 成品
|
||||
if StockProduct:
|
||||
print(f"🔍 [QUERY DEBUG] 正在成品表搜关键词: {uuid_or_barcode}")
|
||||
item = StockProduct.query.filter(
|
||||
db.or_(
|
||||
StockProduct.barcode.ilike(f"%{uuid_or_barcode}%"),
|
||||
StockProduct.sku.ilike(f"%{uuid_or_barcode}%"),
|
||||
StockProduct.serial_number.ilike(f"%{uuid_or_barcode}%")
|
||||
)
|
||||
).first()
|
||||
if item:
|
||||
print(f"✅ [QUERY DEBUG] 命中成品! ID={item.id}, SKU={item.sku}")
|
||||
return (item, 'stock_product', item.id)
|
||||
else:
|
||||
print(f"❌ [QUERY DEBUG] 成品表查询结束,无匹配项")
|
||||
# ===== 精确匹配优先(走索引,快且准) =====
|
||||
exact_checks = [
|
||||
(StockProduct, lambda c: db.or_(
|
||||
StockProduct.barcode == c,
|
||||
StockProduct.sku == c,
|
||||
StockProduct.serial_number == c
|
||||
), 'stock_product'),
|
||||
(StockSemi, lambda c: db.or_(
|
||||
StockSemi.barcode == c,
|
||||
StockSemi.sku == c,
|
||||
StockSemi.serial_number == c
|
||||
), 'stock_semi'),
|
||||
(StockBuy, lambda c: db.or_(
|
||||
StockBuy.barcode == c,
|
||||
StockBuy.sku == c
|
||||
), 'stock_buy'),
|
||||
]
|
||||
|
||||
# 2. 半成品
|
||||
if StockSemi:
|
||||
print(f"🔍 [QUERY DEBUG] 正在半成品表搜关键词: {uuid_or_barcode}")
|
||||
item = StockSemi.query.filter(
|
||||
db.or_(
|
||||
StockSemi.barcode.ilike(f"%{uuid_or_barcode}%"),
|
||||
StockSemi.sku.ilike(f"%{uuid_or_barcode}%"),
|
||||
StockSemi.serial_number.ilike(f"%{uuid_or_barcode}%")
|
||||
)
|
||||
).first()
|
||||
for model, cond_fn, table_name in exact_checks:
|
||||
if not model:
|
||||
continue
|
||||
item = model.query.filter(cond_fn(code)).first()
|
||||
if item:
|
||||
print(f"✅ [QUERY DEBUG] 命中半成品! ID={item.id}, SKU={item.sku}")
|
||||
return (item, 'stock_semi', item.id)
|
||||
return (item, table_name, item.id)
|
||||
|
||||
# 3. 采购件
|
||||
if StockBuy:
|
||||
print(f"🔍 [QUERY DEBUG] 正在采购件表搜关键词: {uuid_or_barcode}")
|
||||
item = StockBuy.query.filter(
|
||||
db.or_(
|
||||
StockBuy.barcode.ilike(f"%{uuid_or_barcode}%"),
|
||||
StockBuy.sku.ilike(f"%{uuid_or_barcode}%")
|
||||
)
|
||||
).first()
|
||||
# ===== 精确未命中 → 回退模糊搜索(保留旧行为兜底) =====
|
||||
fuzzy_checks = [
|
||||
(StockProduct, lambda c: db.or_(
|
||||
StockProduct.barcode.ilike(f"%{c}%"),
|
||||
StockProduct.sku.ilike(f"%{c}%"),
|
||||
StockProduct.serial_number.ilike(f"%{c}%")
|
||||
), 'stock_product'),
|
||||
(StockSemi, lambda c: db.or_(
|
||||
StockSemi.barcode.ilike(f"%{c}%"),
|
||||
StockSemi.sku.ilike(f"%{c}%"),
|
||||
StockSemi.serial_number.ilike(f"%{c}%")
|
||||
), 'stock_semi'),
|
||||
(StockBuy, lambda c: db.or_(
|
||||
StockBuy.barcode.ilike(f"%{c}%"),
|
||||
StockBuy.sku.ilike(f"%{c}%")
|
||||
), 'stock_buy'),
|
||||
]
|
||||
|
||||
for model, cond_fn, table_name in fuzzy_checks:
|
||||
if not model:
|
||||
continue
|
||||
item = model.query.filter(cond_fn(code)).first()
|
||||
if item:
|
||||
print(f"✅ [QUERY DEBUG] 命中采购件! ID={item.id}, SKU={item.sku}")
|
||||
return (item, 'stock_buy', item.id)
|
||||
return (item, table_name, item.id)
|
||||
|
||||
return None, None, None
|
||||
|
||||
return (None, None, None)
|
||||
|
||||
@ -411,6 +427,42 @@ def get_stock_list():
|
||||
return _do_get_stock_list(permission_prefix='outbound_selection')
|
||||
|
||||
|
||||
# --------------------------------------------------------
|
||||
# 盘库/出库/借库 扫码精确匹配接口
|
||||
# GET /api/v1/inbound/stock/scan?barcode=xxx
|
||||
# 精确匹配优先,替代前端 pageSize:10 模糊搜索 + find() 的漏匹配问题
|
||||
# --------------------------------------------------------
|
||||
@bp.route('/scan', methods=['GET'])
|
||||
@jwt_required()
|
||||
def scan_stock_by_barcode():
|
||||
"""根据条码精确匹配库存记录(一次返回唯一命中,性能好且准确)"""
|
||||
try:
|
||||
barcode = request.args.get('barcode', '').strip()
|
||||
if not barcode:
|
||||
return jsonify({'code': 400, 'msg': 'barcode 不能为空'}), 400
|
||||
|
||||
item, source_table, stock_id = get_stock_info(barcode)
|
||||
if not item:
|
||||
return jsonify({'code': 404, 'msg': f'未找到该物料库存: {barcode}'}), 404
|
||||
|
||||
d = item.to_dict()
|
||||
d['stock_type'] = source_table.replace('stock_', '')
|
||||
d['type'] = source_table.replace('stock_', '')
|
||||
d['source_table'] = source_table
|
||||
d['stock_id'] = stock_id
|
||||
# 兼容前端字段
|
||||
if hasattr(item, 'base') and item.base:
|
||||
d['name'] = d.get('material_name') or item.base.name or ''
|
||||
d['standard'] = d.get('spec_model') or item.base.spec_model or ''
|
||||
d['stock_quantity'] = float(d.get('stock_quantity') or d.get('qty_stock') or 0)
|
||||
d['available_quantity'] = float(d.get('available_quantity') or d.get('qty_available') or 0)
|
||||
|
||||
return jsonify({'code': 200, 'msg': 'success', 'data': d}), 200
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return jsonify({'code': 500, 'msg': str(e)}), 500
|
||||
|
||||
|
||||
# --- 草稿箱接口 ---
|
||||
|
||||
@bp.route('/draft/list', methods=['GET'])
|
||||
|
||||
@ -19,6 +19,15 @@ export function getStockList(params: { page?: number; pageSize?: number; keyword
|
||||
})
|
||||
}
|
||||
|
||||
// 扫码精确匹配库存(盘库/出库/借库通用,替代模糊搜索漏匹配问题)
|
||||
export function scanStockByBarcode(barcode: string) {
|
||||
return request({
|
||||
url: '/v1/inbound/stock/scan',
|
||||
method: 'get',
|
||||
params: { barcode }
|
||||
})
|
||||
}
|
||||
|
||||
// 打印出库选单
|
||||
// 修改后: 去掉开头的 /api
|
||||
export function printSelectionList(items: any[]) {
|
||||
|
||||
@ -413,7 +413,7 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, onUnmounted, nextTick } from 'vue'
|
||||
import { getStockList, getAllStocktakeItems, getDraftMergedList, updateStocktakeQuantity } from '@/api/inbound/stock'
|
||||
import { getAllStocktakeItems, getDraftMergedList, updateStocktakeQuantity, scanStockByBarcode } from '@/api/inbound/stock'
|
||||
import QrScanner from '@/components/QrScanner/index.vue'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { Search, VideoPlay, VideoPause, List, Checked, Download, ArrowRight, Cloudy, Edit, EditPen, CameraFilled, Close, WarningFilled } from '@element-plus/icons-vue'
|
||||
@ -736,58 +736,51 @@ const onScanSuccess = async (code: string) => {
|
||||
return
|
||||
}
|
||||
|
||||
// 实时查询后端匹配
|
||||
// ★ 精确匹配查询后端(替代 pageSize:10 模糊搜索 + find(),避免漏匹配)
|
||||
loading.value = true
|
||||
try {
|
||||
const res: any = await getStockList({
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
keyword: trimCode
|
||||
})
|
||||
const res: any = await scanStockByBarcode(trimCode)
|
||||
|
||||
if (!res || !res.data || !res.data.list || res.data.list.length === 0) {
|
||||
ElMessage.error(`未找到该物料库存: ${trimCode}`)
|
||||
if (navigator.vibrate) navigator.vibrate([200, 50, 200])
|
||||
return
|
||||
}
|
||||
|
||||
// 查找匹配的物料
|
||||
const foundItem = res.data.list.find((i: any) =>
|
||||
i.uuid === trimCode || i.sku === trimCode || i.barcode === trimCode || i.bar_code === trimCode
|
||||
)
|
||||
|
||||
if (!foundItem) {
|
||||
if (!res || !res.data) {
|
||||
ElMessage.error(`未找到该物料库存: ${trimCode}`)
|
||||
if (navigator.vibrate) navigator.vibrate([200, 50, 200])
|
||||
return
|
||||
}
|
||||
|
||||
const foundItem = res.data
|
||||
if (navigator.vibrate) navigator.vibrate(100)
|
||||
|
||||
// 关闭全屏扫码,弹出填数对话框
|
||||
showCamera.value = false
|
||||
|
||||
// 处理数据格式
|
||||
// 处理数据格式(后端已返回 source_table / stock_id / name / standard)
|
||||
const stock = parseFloat(foundItem.stock_quantity || foundItem.qty_stock || 0)
|
||||
const type = foundItem.stock_type || foundItem.type || 'material'
|
||||
const sourceTable = foundItem.source_table || typeToSourceTable(type)
|
||||
const stockId = foundItem.stock_id || foundItem.id
|
||||
const item: StockItem = {
|
||||
...foundItem,
|
||||
name: foundItem.name || foundItem.material_name || foundItem.product_name || '未知物品',
|
||||
standard: foundItem.spec_model || foundItem.standard || foundItem.model || '',
|
||||
standard: foundItem.standard || foundItem.spec_model || foundItem.model || '',
|
||||
sku: foundItem.sku || '',
|
||||
uuid: foundItem.uuid || foundItem.sku || '',
|
||||
bar_code: foundItem.bar_code || foundItem.barcode || '',
|
||||
qty_stock: stock,
|
||||
qty_actual: 1,
|
||||
scanned: true,
|
||||
uniqueKey: `${type}_${foundItem.id}`,
|
||||
source_table: typeToSourceTable(type),
|
||||
stock_id: foundItem.id
|
||||
uniqueKey: `${type}_${stockId}`,
|
||||
source_table: sourceTable,
|
||||
stock_id: stockId
|
||||
}
|
||||
|
||||
openQtyDialog(item)
|
||||
} catch (e) {
|
||||
ElMessage.error('查询库存失败')
|
||||
} catch (e: any) {
|
||||
const msg = e?.msg || e?.message || '查询库存失败'
|
||||
if (msg.includes('未找到')) {
|
||||
ElMessage.error(`未找到该物料库存: ${trimCode}`)
|
||||
} else {
|
||||
ElMessage.error(msg)
|
||||
}
|
||||
console.error(e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
@ -820,48 +813,44 @@ const handleManualInput = async () => {
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
const res: any = await getStockList({
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
keyword: code
|
||||
})
|
||||
// ★ 精确匹配查询后端(替代 pageSize:10 模糊搜索 + find())
|
||||
const res: any = await scanStockByBarcode(code)
|
||||
|
||||
if (!res || !res.data || !res.data.list || res.data.list.length === 0) {
|
||||
ElMessage.error(`未找到该物料库存: ${code}`)
|
||||
return
|
||||
}
|
||||
|
||||
const foundItem = res.data.list.find((i: any) =>
|
||||
i.uuid === code || i.sku === code || i.barcode === code || i.bar_code === code
|
||||
)
|
||||
|
||||
if (!foundItem) {
|
||||
if (!res || !res.data) {
|
||||
ElMessage.error(`未找到该物料库存: ${code}`)
|
||||
return
|
||||
}
|
||||
|
||||
const foundItem = res.data
|
||||
if (navigator.vibrate) navigator.vibrate(100)
|
||||
|
||||
const stock = parseFloat(foundItem.stock_quantity || foundItem.qty_stock || 0)
|
||||
const type = foundItem.stock_type || foundItem.type || 'material'
|
||||
const sourceTable = foundItem.source_table || typeToSourceTable(type)
|
||||
const stockId = foundItem.stock_id || foundItem.id
|
||||
const item: StockItem = {
|
||||
...foundItem,
|
||||
name: foundItem.name || foundItem.material_name || foundItem.product_name || '未知物品',
|
||||
standard: foundItem.spec_model || foundItem.standard || foundItem.model || '',
|
||||
standard: foundItem.standard || foundItem.spec_model || foundItem.model || '',
|
||||
sku: foundItem.sku || '',
|
||||
uuid: foundItem.uuid || foundItem.sku || '',
|
||||
bar_code: foundItem.bar_code || foundItem.barcode || '',
|
||||
qty_stock: stock,
|
||||
qty_actual: 1,
|
||||
scanned: true,
|
||||
uniqueKey: `${type}_${foundItem.id}`,
|
||||
source_table: typeToSourceTable(type),
|
||||
stock_id: foundItem.id
|
||||
uniqueKey: `${type}_${stockId}`,
|
||||
source_table: sourceTable,
|
||||
stock_id: stockId
|
||||
}
|
||||
|
||||
openQtyDialog(item)
|
||||
} catch (e) {
|
||||
ElMessage.error('查询库存失败')
|
||||
} catch (e: any) {
|
||||
const msg = e?.msg || e?.message || '查询库存失败'
|
||||
if (msg.includes('未找到')) {
|
||||
ElMessage.error(`未找到该物料库存: ${code}`)
|
||||
} else {
|
||||
ElMessage.error(msg)
|
||||
}
|
||||
console.error(e)
|
||||
} finally {
|
||||
loading.value = false
|
||||
|
||||
Reference in New Issue
Block a user