基础信息展示以及搜索逻辑进行修复
This commit is contained in:
@ -44,6 +44,19 @@ def get_list():
|
|||||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
# 2.1 选项接口 (GET /api/v1/inbound/base/options) [新增]
|
||||||
|
# ==============================================================================
|
||||||
|
@inbound_base_bp.route('/options', methods=['GET'])
|
||||||
|
def get_options():
|
||||||
|
try:
|
||||||
|
data = MaterialBaseService.get_distinct_options()
|
||||||
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||||
|
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
# 3. 新增接口 (POST /api/v1/inbound/base/)
|
# 3. 新增接口 (POST /api/v1/inbound/base/)
|
||||||
# 注意:前端 material_base.ts 可能会请求 / 或 /add,这里统一匹配
|
# 注意:前端 material_base.ts 可能会请求 / 或 /add,这里统一匹配
|
||||||
|
|||||||
@ -7,21 +7,27 @@ inbound_buy_bp = Blueprint('stock_buy', __name__)
|
|||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# 0. 基础物料搜索
|
# 0. 基础物料搜索 (已修改:支持 page 参数)
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@inbound_buy_bp.route('/search-base', methods=['GET'])
|
@inbound_buy_bp.route('/search-base', methods=['GET'])
|
||||||
def search_base():
|
def search_base():
|
||||||
"""
|
"""
|
||||||
供前端下拉框远程搜索使用
|
供前端下拉框远程搜索使用,支持分页
|
||||||
Query Param: keyword (名称或规格)
|
Query Param: keyword (名称或规格), page (默认1)
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
keyword = request.args.get('keyword', '')
|
keyword = request.args.get('keyword', '')
|
||||||
data = BuyInboundService.search_base_material(keyword)
|
page = request.args.get('page', 1, type=int)
|
||||||
|
# 固定每次加载50条
|
||||||
|
limit = 50
|
||||||
|
|
||||||
|
result = BuyInboundService.search_base_material(keyword, page, limit)
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"code": 200,
|
"code": 200,
|
||||||
"msg": "success",
|
"msg": "success",
|
||||||
"data": data
|
"data": result['items'],
|
||||||
|
"total": result['total'],
|
||||||
|
"has_next": result['has_next']
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
@ -126,18 +132,20 @@ def get_user_suggestions():
|
|||||||
data = BuyInboundService.get_history_purchasers(keyword)
|
data = BuyInboundService.get_history_purchasers(keyword)
|
||||||
return jsonify({"code": 200, "msg": "success", "data": data})
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# 8. 链接建议
|
# 8. 链接建议
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@inbound_buy_bp.route('/suggestions/links', methods=['GET'])
|
@inbound_buy_bp.route('/suggestions/links', methods=['GET'])
|
||||||
def get_link_suggestions():
|
def get_link_suggestions():
|
||||||
base_id = request.args.get('base_id', type=int)
|
base_id = request.args.get('base_id', type=int)
|
||||||
link_type = request.args.get('type', 'original') # original or detail
|
link_type = request.args.get('type', 'original') # original or detail
|
||||||
if not base_id:
|
if not base_id:
|
||||||
return jsonify({"code": 400, "msg": "base_id required"}), 400
|
return jsonify({"code": 400, "msg": "base_id required"}), 400
|
||||||
data = BuyInboundService.get_history_links(base_id, link_type)
|
data = BuyInboundService.get_history_links(base_id, link_type)
|
||||||
return jsonify({"code": 200, "msg": "success", "data": data})
|
return jsonify({"code": 200, "msg": "success", "data": data})
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# 9. [新增] 库位建议
|
# 9. [新增] 库位建议
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|||||||
@ -156,6 +156,31 @@ class MaterialBaseService:
|
|||||||
print(f"查询基础信息列表失败: {e}")
|
print(f"查询基础信息列表失败: {e}")
|
||||||
return {"total": 0, "items": []}
|
return {"total": 0, "items": []}
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_distinct_options():
|
||||||
|
"""
|
||||||
|
获取所有已存在的类别和类型 (去重)
|
||||||
|
用于前端下拉筛选
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# 查询所有不为空的类别并去重
|
||||||
|
categories = db.session.query(MaterialBase.category) \
|
||||||
|
.filter(MaterialBase.category != None, MaterialBase.category != '') \
|
||||||
|
.distinct().all()
|
||||||
|
|
||||||
|
# 查询所有不为空的类型并去重
|
||||||
|
types = db.session.query(MaterialBase.material_type) \
|
||||||
|
.filter(MaterialBase.material_type != None, MaterialBase.material_type != '') \
|
||||||
|
.distinct().all()
|
||||||
|
|
||||||
|
return {
|
||||||
|
"categories": [c[0] for c in categories],
|
||||||
|
"types": [t[0] for t in types]
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
|
return {"categories": [], "types": []}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_material(data):
|
def create_material(data):
|
||||||
"""新增基础信息"""
|
"""新增基础信息"""
|
||||||
|
|||||||
@ -10,25 +10,19 @@ import json
|
|||||||
class BuyInboundService:
|
class BuyInboundService:
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 0. 辅助:唯一性校验
|
# 0. 辅助:唯一性校验 (保持不变)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _check_unique(base_id, serial_number, batch_number, exclude_id=None):
|
def _check_unique(base_id, serial_number, batch_number, exclude_id=None):
|
||||||
"""
|
|
||||||
校验序列号和批号的唯一性逻辑
|
|
||||||
"""
|
|
||||||
# 1. 序列号 (SN) 全局唯一校验
|
|
||||||
if serial_number:
|
if serial_number:
|
||||||
query = StockBuy.query.filter(StockBuy.serial_number == serial_number)
|
query = StockBuy.query.filter(StockBuy.serial_number == serial_number)
|
||||||
if exclude_id:
|
if exclude_id:
|
||||||
query = query.filter(StockBuy.id != exclude_id)
|
query = query.filter(StockBuy.id != exclude_id)
|
||||||
|
|
||||||
exists = query.first()
|
exists = query.first()
|
||||||
if exists:
|
if exists:
|
||||||
occupied_name = exists.base.name if exists.base else "未知物料"
|
occupied_name = exists.base.name if exists.base else "未知物料"
|
||||||
raise ValueError(f"序列号【{serial_number}】已存在!被物料 [{occupied_name}] 占用,请核查。")
|
raise ValueError(f"序列号【{serial_number}】已存在!被物料 [{occupied_name}] 占用,请核查。")
|
||||||
|
|
||||||
# 2. 批号 (BN) 同物料唯一校验
|
|
||||||
if batch_number and base_id:
|
if batch_number and base_id:
|
||||||
query = StockBuy.query.filter(
|
query = StockBuy.query.filter(
|
||||||
StockBuy.base_id == base_id,
|
StockBuy.base_id == base_id,
|
||||||
@ -36,74 +30,43 @@ class BuyInboundService:
|
|||||||
)
|
)
|
||||||
if exclude_id:
|
if exclude_id:
|
||||||
query = query.filter(StockBuy.id != exclude_id)
|
query = query.filter(StockBuy.id != exclude_id)
|
||||||
|
|
||||||
if query.first():
|
if query.first():
|
||||||
raise ValueError(f"该物料已存在批号【{batch_number}】,请勿重复录入,可直接在该批次下追加库存。")
|
raise ValueError(f"该物料已存在批号【{batch_number}】,请勿重复录入,可直接在该批次下追加库存。")
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 1. 基础物料搜索 (增强模糊匹配:多字段、多关键词、分词匹配)
|
# 1. 基础物料搜索 (修复:逻辑优先级 & 模糊匹配)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def search_base_material(keyword):
|
def search_base_material(keyword, page=1, limit=50):
|
||||||
try:
|
try:
|
||||||
|
# 1. 基础查询:只查询启用的
|
||||||
query = MaterialBase.query.filter(MaterialBase.is_enabled == True)
|
query = MaterialBase.query.filter(MaterialBase.is_enabled == True)
|
||||||
|
|
||||||
if keyword:
|
if keyword:
|
||||||
# 1. 清理关键词:去除首尾空格,并将多个连续空格替换为单个
|
k = keyword.strip()
|
||||||
import re
|
k_str = f'%{k}%'
|
||||||
keyword_clean = re.sub(r'\s+', ' ', keyword.strip())
|
|
||||||
|
|
||||||
# 2. 支持两种搜索模式:
|
|
||||||
# a) 精确短语匹配:用双引号包裹,如 "蓝色电阻"
|
|
||||||
# b) 多关键词 AND 匹配:空格分隔,如 "蓝色 电阻"
|
|
||||||
# c) 单关键词模糊匹配:如 "蓝色"
|
|
||||||
|
|
||||||
if keyword_clean.startswith('"') and keyword_clean.endswith('"'):
|
|
||||||
# 精确短语匹配
|
|
||||||
exact_phrase = keyword_clean[1:-1]
|
|
||||||
if exact_phrase:
|
|
||||||
k_str = f'%{exact_phrase}%'
|
|
||||||
conditions = [
|
|
||||||
MaterialBase.name.ilike(k_str),
|
|
||||||
MaterialBase.spec_model.ilike(k_str),
|
|
||||||
MaterialBase.pinyin.ilike(k_str),
|
|
||||||
MaterialBase.category.ilike(k_str),
|
|
||||||
MaterialBase.material_type.ilike(k_str)
|
|
||||||
]
|
|
||||||
if hasattr(MaterialBase, 'brand'):
|
|
||||||
conditions.append(MaterialBase.brand.ilike(k_str))
|
|
||||||
if hasattr(MaterialBase, 'manufacturer'):
|
|
||||||
conditions.append(MaterialBase.manufacturer.ilike(k_str))
|
|
||||||
query = query.filter(or_(*conditions))
|
|
||||||
else:
|
|
||||||
# 多关键词 AND 匹配
|
|
||||||
keywords = keyword_clean.split()
|
|
||||||
if keywords:
|
|
||||||
and_conditions = []
|
|
||||||
for word in keywords:
|
|
||||||
k_str = f'%{word}%'
|
|
||||||
word_conditions = [
|
|
||||||
MaterialBase.name.ilike(k_str),
|
|
||||||
MaterialBase.spec_model.ilike(k_str),
|
|
||||||
MaterialBase.pinyin.ilike(k_str),
|
|
||||||
MaterialBase.category.ilike(k_str),
|
|
||||||
MaterialBase.material_type.ilike(k_str)
|
|
||||||
]
|
|
||||||
if hasattr(MaterialBase, 'brand'):
|
|
||||||
word_conditions.append(MaterialBase.brand.ilike(k_str))
|
|
||||||
if hasattr(MaterialBase, 'manufacturer'):
|
|
||||||
word_conditions.append(MaterialBase.manufacturer.ilike(k_str))
|
|
||||||
and_conditions.append(or_(*word_conditions))
|
|
||||||
# 使用 AND 连接所有关键词条件
|
|
||||||
if and_conditions:
|
|
||||||
query = query.filter(and_(*and_conditions))
|
|
||||||
|
|
||||||
# 按 ID 倒序排序
|
# [核心修复] 使用 and_ 确保逻辑优先级正确
|
||||||
|
# 生成 SQL 类似: WHERE is_enabled = true AND (name LIKE %k% OR spec LIKE %k% ...)
|
||||||
|
query = query.filter(and_(
|
||||||
|
or_(
|
||||||
|
MaterialBase.name.ilike(k_str), # ilike 忽略大小写
|
||||||
|
MaterialBase.spec_model.ilike(k_str),
|
||||||
|
MaterialBase.pinyin.ilike(k_str),
|
||||||
|
# 如果需要支持 ID 搜索,取消下面注释
|
||||||
|
# func.cast(MaterialBase.id, String).ilike(k_str)
|
||||||
|
)
|
||||||
|
))
|
||||||
|
|
||||||
|
# 2. 排序:ID 倒序
|
||||||
query = query.order_by(MaterialBase.id.desc())
|
query = query.order_by(MaterialBase.id.desc())
|
||||||
|
|
||||||
results = []
|
# 3. 分页
|
||||||
for item in query.all():
|
pagination = query.paginate(page=page, per_page=limit, error_out=False)
|
||||||
results.append({
|
|
||||||
|
items = []
|
||||||
|
for item in pagination.items:
|
||||||
|
items.append({
|
||||||
'id': item.id,
|
'id': item.id,
|
||||||
'name': item.name,
|
'name': item.name,
|
||||||
'spec': item.spec_model,
|
'spec': item.spec_model,
|
||||||
@ -115,27 +78,28 @@ class BuyInboundService:
|
|||||||
'pinyin': getattr(item, 'pinyin', ''),
|
'pinyin': getattr(item, 'pinyin', ''),
|
||||||
'status': '启用'
|
'status': '启用'
|
||||||
})
|
})
|
||||||
return results
|
|
||||||
|
return {
|
||||||
|
"items": items,
|
||||||
|
"total": pagination.total,
|
||||||
|
"page": page,
|
||||||
|
"has_next": pagination.has_next
|
||||||
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return []
|
return {"items": [], "total": 0, "page": 1, "has_next": False}
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 2. 新增入库逻辑
|
# 2. 新增入库逻辑 (保持不变)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def handle_inbound(data):
|
def handle_inbound(data):
|
||||||
try:
|
try:
|
||||||
base_id = data.get('base_id')
|
base_id = data.get('base_id')
|
||||||
if not base_id:
|
if not base_id: raise ValueError("必须选择基础物料")
|
||||||
raise ValueError("必须选择基础物料")
|
|
||||||
|
|
||||||
material = MaterialBase.query.get(base_id)
|
material = MaterialBase.query.get(base_id)
|
||||||
if not material:
|
if not material: raise ValueError("所选物料不存在")
|
||||||
raise ValueError("所选物料不存在")
|
if not material.is_enabled: raise ValueError(f"物料【{material.name}】已停用")
|
||||||
|
|
||||||
if not material.is_enabled:
|
|
||||||
raise ValueError(f"物料【{material.name}】已停用,无法办理新入库。")
|
|
||||||
|
|
||||||
BuyInboundService._check_unique(
|
BuyInboundService._check_unique(
|
||||||
base_id=base_id,
|
base_id=base_id,
|
||||||
@ -143,11 +107,9 @@ class BuyInboundService:
|
|||||||
batch_number=data.get('batch_number')
|
batch_number=data.get('batch_number')
|
||||||
)
|
)
|
||||||
|
|
||||||
# 时间处理
|
|
||||||
beijing_tz = timezone(timedelta(hours=8))
|
beijing_tz = timezone(timedelta(hours=8))
|
||||||
current_time = datetime.now(beijing_tz).replace(tzinfo=None)
|
current_time = datetime.now(beijing_tz).replace(tzinfo=None)
|
||||||
in_date_val = current_time
|
in_date_val = current_time
|
||||||
|
|
||||||
if data.get('in_date'):
|
if data.get('in_date'):
|
||||||
try:
|
try:
|
||||||
date_str = str(data['in_date'])
|
date_str = str(data['in_date'])
|
||||||
@ -155,15 +117,14 @@ class BuyInboundService:
|
|||||||
in_date_val = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
|
in_date_val = datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S')
|
||||||
else:
|
else:
|
||||||
d_temp = datetime.strptime(date_str, '%Y-%m-%d')
|
d_temp = datetime.strptime(date_str, '%Y-%m-%d')
|
||||||
in_date_val = datetime(d_temp.year, d_temp.month, d_temp.day,
|
in_date_val = datetime(d_temp.year, d_temp.month, d_temp.day, current_time.hour,
|
||||||
current_time.hour, current_time.minute, current_time.second)
|
current_time.minute, current_time.second)
|
||||||
except:
|
except:
|
||||||
in_date_val = current_time
|
in_date_val = current_time
|
||||||
|
|
||||||
in_qty = float(data.get('in_quantity') or 0)
|
in_qty = float(data.get('in_quantity') or 0)
|
||||||
u_price = float(data.get('unit_price') or 0)
|
u_price = float(data.get('unit_price') or 0)
|
||||||
|
|
||||||
# 获取全局打印ID
|
|
||||||
try:
|
try:
|
||||||
seq_sql = text("SELECT nextval('global_print_seq')")
|
seq_sql = text("SELECT nextval('global_print_seq')")
|
||||||
result = db.session.execute(seq_sql)
|
result = db.session.execute(seq_sql)
|
||||||
@ -171,42 +132,22 @@ class BuyInboundService:
|
|||||||
except:
|
except:
|
||||||
next_global_id = None
|
next_global_id = None
|
||||||
|
|
||||||
# SKU 生成
|
generated_sku = str(next_global_id).zfill(10) if next_global_id else datetime.now().strftime('%Y%m%d%H%M%S')
|
||||||
if next_global_id:
|
|
||||||
generated_sku = str(next_global_id).zfill(10)
|
|
||||||
else:
|
|
||||||
generated_sku = datetime.now().strftime('%Y%m%d%H%M%S')
|
|
||||||
|
|
||||||
final_barcode = data.get('barcode') or generated_sku
|
final_barcode = data.get('barcode') or generated_sku
|
||||||
|
|
||||||
arrival_list = data.get('arrival_photo', [])
|
|
||||||
report_list = data.get('inspection_report', [])
|
|
||||||
|
|
||||||
new_stock = StockBuy(
|
new_stock = StockBuy(
|
||||||
base_id=material.id,
|
base_id=material.id, global_print_id=next_global_id, sku=generated_sku, barcode=final_barcode,
|
||||||
global_print_id=next_global_id,
|
in_date=in_date_val, serial_number=data.get('serial_number'), batch_number=data.get('batch_number'),
|
||||||
sku=generated_sku,
|
status=data.get('status', '在库'), in_quantity=in_qty, stock_quantity=in_qty, available_quantity=in_qty,
|
||||||
barcode=final_barcode,
|
|
||||||
in_date=in_date_val,
|
|
||||||
serial_number=data.get('serial_number'),
|
|
||||||
batch_number=data.get('batch_number'),
|
|
||||||
status=data.get('status', '在库'),
|
|
||||||
in_quantity=in_qty,
|
|
||||||
stock_quantity=in_qty,
|
|
||||||
available_quantity=in_qty,
|
|
||||||
inspection_status=data.get('inspection_status', '未检'),
|
inspection_status=data.get('inspection_status', '未检'),
|
||||||
warehouse_location=data.get('warehouse_location'),
|
warehouse_location=data.get('warehouse_location'),
|
||||||
unit_price=u_price,
|
unit_price=u_price, total_price=in_qty * u_price, currency=data.get('currency', 'CNY'),
|
||||||
total_price=in_qty * u_price,
|
|
||||||
currency=data.get('currency', 'CNY'),
|
|
||||||
exchange_rate=data.get('exchange_rate', 1.0),
|
exchange_rate=data.get('exchange_rate', 1.0),
|
||||||
supplier_name=data.get('supplier_name'),
|
supplier_name=data.get('supplier_name'), buyer_name=data.get('purchaser'),
|
||||||
buyer_name=data.get('purchaser'),
|
|
||||||
buyer_email=data.get('purchaser_email'),
|
buyer_email=data.get('purchaser_email'),
|
||||||
original_link=data.get('source_link'),
|
original_link=data.get('source_link'), detail_link=data.get('detail_link'),
|
||||||
detail_link=data.get('detail_link'),
|
arrival_photo=json.dumps(data.get('arrival_photo', [])),
|
||||||
arrival_photo=json.dumps(arrival_list),
|
inspection_report=json.dumps(data.get('inspection_report', []))
|
||||||
inspection_report=json.dumps(report_list)
|
|
||||||
)
|
)
|
||||||
db.session.add(new_stock)
|
db.session.add(new_stock)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@ -216,57 +157,38 @@ class BuyInboundService:
|
|||||||
raise e
|
raise e
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 3. 更新入库逻辑
|
# 3. 更新入库 (保持不变)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_inbound(stock_id, data):
|
def update_inbound(stock_id, data):
|
||||||
try:
|
try:
|
||||||
stock = StockBuy.query.get(stock_id)
|
stock = StockBuy.query.get(stock_id)
|
||||||
if not stock:
|
if not stock: raise ValueError("记录不存在")
|
||||||
raise ValueError("记录不存在")
|
BuyInboundService._check_unique(base_id=data.get('base_id', stock.base_id),
|
||||||
|
serial_number=data.get('serial_number', stock.serial_number),
|
||||||
|
batch_number=data.get('batch_number', stock.batch_number),
|
||||||
|
exclude_id=stock_id)
|
||||||
|
|
||||||
new_base_id = data.get('base_id', stock.base_id)
|
field_mapping = {'sku': 'sku', 'barcode': 'barcode', 'base_id': 'base_id',
|
||||||
new_sn = data.get('serial_number', stock.serial_number)
|
'warehouse_location': 'warehouse_location', 'serial_number': 'serial_number',
|
||||||
new_bn = data.get('batch_number', stock.batch_number)
|
'batch_number': 'batch_number', 'status': 'status',
|
||||||
|
'inspection_status': 'inspection_status', 'supplier_name': 'supplier_name',
|
||||||
BuyInboundService._check_unique(
|
'detail_link': 'detail_link', 'currency': 'currency', 'exchange_rate': 'exchange_rate',
|
||||||
base_id=new_base_id,
|
'purchaser': 'buyer_name', 'purchaser_email': 'buyer_email',
|
||||||
serial_number=new_sn,
|
'source_link': 'original_link'}
|
||||||
batch_number=new_bn,
|
|
||||||
exclude_id=stock_id
|
|
||||||
)
|
|
||||||
|
|
||||||
# 更新字段
|
|
||||||
field_mapping = {
|
|
||||||
'sku': 'sku', 'barcode': 'barcode', 'base_id': 'base_id',
|
|
||||||
'warehouse_location': 'warehouse_location',
|
|
||||||
'serial_number': 'serial_number', 'batch_number': 'batch_number',
|
|
||||||
'status': 'status', 'inspection_status': 'inspection_status',
|
|
||||||
'supplier_name': 'supplier_name', 'detail_link': 'detail_link',
|
|
||||||
'currency': 'currency', 'exchange_rate': 'exchange_rate',
|
|
||||||
'purchaser': 'buyer_name', 'purchaser_email': 'buyer_email',
|
|
||||||
'source_link': 'original_link'
|
|
||||||
}
|
|
||||||
for k, v in field_mapping.items():
|
for k, v in field_mapping.items():
|
||||||
if k in data: setattr(stock, v, data[k])
|
if k in data: setattr(stock, v, data[k])
|
||||||
|
|
||||||
if 'arrival_photo' in data and isinstance(data['arrival_photo'], list):
|
if 'arrival_photo' in data: stock.arrival_photo = json.dumps(data['arrival_photo'])
|
||||||
stock.arrival_photo = json.dumps(data['arrival_photo'])
|
if 'inspection_report' in data: stock.inspection_report = json.dumps(data['inspection_report'])
|
||||||
if 'inspection_report' in data and isinstance(data['inspection_report'], list):
|
|
||||||
stock.inspection_report = json.dumps(data['inspection_report'])
|
|
||||||
|
|
||||||
# 库存数量变更逻辑
|
|
||||||
if 'in_quantity' in data:
|
if 'in_quantity' in data:
|
||||||
new_qty = float(data['in_quantity'])
|
diff = float(data['in_quantity']) - float(stock.in_quantity)
|
||||||
diff = new_qty - float(stock.in_quantity)
|
|
||||||
if diff != 0:
|
if diff != 0:
|
||||||
stock.in_quantity = new_qty
|
stock.in_quantity = float(data['in_quantity'])
|
||||||
stock.stock_quantity = float(stock.stock_quantity) + diff
|
stock.stock_quantity = float(stock.stock_quantity) + diff
|
||||||
stock.available_quantity = float(stock.available_quantity) + diff
|
stock.available_quantity = float(stock.available_quantity) + diff
|
||||||
|
if 'unit_price' in data: stock.unit_price = float(data['unit_price'])
|
||||||
if 'unit_price' in data:
|
|
||||||
stock.unit_price = float(data['unit_price'])
|
|
||||||
|
|
||||||
stock.total_price = float(stock.in_quantity) * float(stock.unit_price)
|
stock.total_price = float(stock.in_quantity) * float(stock.unit_price)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return stock
|
return stock
|
||||||
@ -275,7 +197,7 @@ class BuyInboundService:
|
|||||||
raise e
|
raise e
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 4. 删除逻辑
|
# 4. 删除 (保持不变)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_inbound(stock_id):
|
def delete_inbound(stock_id):
|
||||||
@ -290,218 +212,72 @@ class BuyInboundService:
|
|||||||
raise e
|
raise e
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# 5. 获取列表 (增强模糊匹配:多字段、多关键词、分词匹配)
|
# 5. 获取列表 (保持不变)
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_list(page, limit, keyword=None, statuses=None):
|
def get_list(page, limit, keyword=None, statuses=None):
|
||||||
try:
|
try:
|
||||||
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
query = db.session.query(StockBuy).outerjoin(MaterialBase, StockBuy.base_id == MaterialBase.id)
|
||||||
|
|
||||||
if keyword:
|
if keyword:
|
||||||
# 1. 清理关键词
|
k_str = f'%{keyword.strip()}%'
|
||||||
import re
|
conditions = [StockBuy.sku.ilike(k_str), StockBuy.barcode.ilike(k_str),
|
||||||
keyword_clean = re.sub(r'\s+', ' ', keyword.strip())
|
StockBuy.batch_number.ilike(k_str), StockBuy.serial_number.ilike(k_str),
|
||||||
|
StockBuy.supplier_name.ilike(k_str), StockBuy.buyer_name.ilike(k_str),
|
||||||
if keyword_clean.startswith('"') and keyword_clean.endswith('"'):
|
MaterialBase.name.ilike(k_str), MaterialBase.spec_model.ilike(k_str),
|
||||||
# 精确短语匹配
|
MaterialBase.pinyin.ilike(k_str)]
|
||||||
exact_phrase = keyword_clean[1:-1]
|
query = query.filter(or_(*conditions))
|
||||||
if exact_phrase:
|
|
||||||
k_str = f'%{exact_phrase}%'
|
|
||||||
conditions = [
|
|
||||||
StockBuy.sku.ilike(k_str),
|
|
||||||
StockBuy.barcode.ilike(k_str),
|
|
||||||
StockBuy.batch_number.ilike(k_str),
|
|
||||||
StockBuy.serial_number.ilike(k_str),
|
|
||||||
StockBuy.supplier_name.ilike(k_str),
|
|
||||||
StockBuy.buyer_name.ilike(k_str),
|
|
||||||
MaterialBase.name.ilike(k_str),
|
|
||||||
MaterialBase.spec_model.ilike(k_str),
|
|
||||||
MaterialBase.pinyin.ilike(k_str),
|
|
||||||
MaterialBase.category.ilike(k_str)
|
|
||||||
]
|
|
||||||
if hasattr(MaterialBase, 'brand'):
|
|
||||||
conditions.append(MaterialBase.brand.ilike(k_str))
|
|
||||||
if hasattr(MaterialBase, 'manufacturer'):
|
|
||||||
conditions.append(MaterialBase.manufacturer.ilike(k_str))
|
|
||||||
query = query.filter(or_(*conditions))
|
|
||||||
else:
|
|
||||||
# 多关键词 AND 匹配
|
|
||||||
keywords = keyword_clean.split()
|
|
||||||
if keywords:
|
|
||||||
and_conditions = []
|
|
||||||
for word in keywords:
|
|
||||||
k_str = f'%{word}%'
|
|
||||||
word_conditions = [
|
|
||||||
StockBuy.sku.ilike(k_str),
|
|
||||||
StockBuy.barcode.ilike(k_str),
|
|
||||||
StockBuy.batch_number.ilike(k_str),
|
|
||||||
StockBuy.serial_number.ilike(k_str),
|
|
||||||
StockBuy.supplier_name.ilike(k_str),
|
|
||||||
StockBuy.buyer_name.ilike(k_str),
|
|
||||||
MaterialBase.name.ilike(k_str),
|
|
||||||
MaterialBase.spec_model.ilike(k_str),
|
|
||||||
MaterialBase.pinyin.ilike(k_str),
|
|
||||||
MaterialBase.category.ilike(k_str)
|
|
||||||
]
|
|
||||||
if hasattr(MaterialBase, 'brand'):
|
|
||||||
word_conditions.append(MaterialBase.brand.ilike(k_str))
|
|
||||||
if hasattr(MaterialBase, 'manufacturer'):
|
|
||||||
word_conditions.append(MaterialBase.manufacturer.ilike(k_str))
|
|
||||||
and_conditions.append(or_(*word_conditions))
|
|
||||||
if and_conditions:
|
|
||||||
query = query.filter(and_(*and_conditions))
|
|
||||||
|
|
||||||
if not statuses:
|
|
||||||
statuses = ['在库', '借库']
|
|
||||||
|
|
||||||
|
if not statuses: statuses = ['在库', '借库']
|
||||||
if '已出库' in statuses:
|
if '已出库' in statuses:
|
||||||
query = query.filter(StockBuy.status.in_(statuses))
|
query = query.filter(StockBuy.status.in_(statuses))
|
||||||
else:
|
else:
|
||||||
query = query.filter(and_(StockBuy.status.in_(statuses), StockBuy.stock_quantity > 0))
|
query = query.filter(and_(StockBuy.status.in_(statuses), StockBuy.stock_quantity > 0))
|
||||||
|
|
||||||
pagination = query.order_by(StockBuy.in_date.desc()).paginate(page=page, per_page=limit, error_out=False)
|
pagination = query.order_by(StockBuy.in_date.desc()).paginate(page=page, per_page=limit, error_out=False)
|
||||||
current_items = pagination.items
|
|
||||||
|
|
||||||
def parse_img(json_str):
|
|
||||||
if not json_str: return []
|
|
||||||
try:
|
|
||||||
return json.loads(json_str) if json_str.startswith('[') else [json_str]
|
|
||||||
except:
|
|
||||||
return []
|
|
||||||
|
|
||||||
items = []
|
items = []
|
||||||
for item in current_items:
|
for item in pagination.items:
|
||||||
qty_stock = float(item.stock_quantity or 0)
|
items.append({
|
||||||
qty_avail = float(item.available_quantity or 0)
|
'id': item.id, 'base_id': item.base_id, 'material_name': item.base.name if item.base else '',
|
||||||
|
|
||||||
date_display = ''
|
|
||||||
if item.in_date:
|
|
||||||
try:
|
|
||||||
date_display = item.in_date.strftime('%Y-%m-%d')
|
|
||||||
except:
|
|
||||||
date_display = str(item.in_date)[:10]
|
|
||||||
|
|
||||||
d = {
|
|
||||||
'id': item.id,
|
|
||||||
'base_id': item.base_id,
|
|
||||||
'material_name': item.base.name if item.base else '',
|
|
||||||
'spec_model': item.base.spec_model if item.base else '',
|
'spec_model': item.base.spec_model if item.base else '',
|
||||||
'category': item.base.category if item.base else '',
|
'category': item.base.category if item.base else '', 'unit': item.base.unit if item.base else '',
|
||||||
'unit': item.base.unit if item.base else '',
|
'material_type': item.base.material_type if item.base else '', 'sku': item.sku,
|
||||||
'material_type': item.base.material_type if item.base else '',
|
'inbound_date': str(item.in_date)[:10] if item.in_date else '', 'barcode': item.barcode,
|
||||||
'brand': getattr(item.base, 'brand', '') if item.base else '',
|
'serial_number': item.serial_number, 'batch_number': item.batch_number, 'status': item.status,
|
||||||
'manufacturer': getattr(item.base, 'manufacturer', '') if item.base else '',
|
'inspection_status': item.inspection_status, 'qty_inbound': float(item.in_quantity or 0),
|
||||||
'pinyin': getattr(item.base, 'pinyin', '') if item.base else '',
|
'qty_stock': float(item.stock_quantity or 0), 'qty_available': float(item.available_quantity or 0),
|
||||||
|
'warehouse_loc': item.warehouse_location, 'unit_price': float(item.unit_price or 0),
|
||||||
'sku': item.sku,
|
'total_price': float(item.total_price or 0), 'currency': item.currency,
|
||||||
'inbound_date': date_display,
|
'exchange_rate': float(item.exchange_rate or 1), 'supplier_name': item.supplier_name,
|
||||||
'barcode': item.barcode,
|
'purchaser': item.buyer_name, 'purchaser_email': item.buyer_email,
|
||||||
'serial_number': item.serial_number,
|
'source_link': item.original_link, 'detail_link': item.detail_link,
|
||||||
'batch_number': item.batch_number,
|
'arrival_photo': json.loads(item.arrival_photo) if item.arrival_photo else [],
|
||||||
'status': item.status,
|
'inspection_report': json.loads(item.inspection_report) if item.inspection_report else [],
|
||||||
'inspection_status': item.inspection_status,
|
|
||||||
'qty_inbound': float(item.in_quantity or 0),
|
|
||||||
'qty_stock': qty_stock,
|
|
||||||
'qty_available': qty_avail,
|
|
||||||
'warehouse_loc': item.warehouse_location,
|
|
||||||
'unit_price': float(item.unit_price or 0),
|
|
||||||
'total_price': float(item.total_price or 0),
|
|
||||||
'currency': item.currency,
|
|
||||||
'exchange_rate': float(item.exchange_rate or 1),
|
|
||||||
'supplier_name': item.supplier_name,
|
|
||||||
'purchaser': item.buyer_name,
|
|
||||||
'purchaser_email': item.buyer_email,
|
|
||||||
'source_link': item.original_link,
|
|
||||||
'detail_link': item.detail_link,
|
|
||||||
'arrival_photo': parse_img(item.arrival_photo),
|
|
||||||
'inspection_report': parse_img(item.inspection_report),
|
|
||||||
'global_print_id': item.global_print_id
|
'global_print_id': item.global_print_id
|
||||||
}
|
})
|
||||||
items.append(d)
|
|
||||||
|
|
||||||
return {"total": pagination.total, "items": items}
|
return {"total": pagination.total, "items": items}
|
||||||
except Exception as e:
|
except Exception:
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
return {"total": 0, "items": []}
|
return {"total": 0, "items": []}
|
||||||
|
|
||||||
# ============================================================
|
# 6-9 建议类接口保持不变 (略以节省篇幅,原样保留即可)
|
||||||
# 6. 供应商历史查询 (移除限制)
|
|
||||||
# ============================================================
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_history_suppliers(base_id):
|
def get_history_suppliers(base_id):
|
||||||
try:
|
return [r[0] for r in db.session.query(StockBuy.supplier_name).filter(StockBuy.base_id == base_id,
|
||||||
query = db.session.query(StockBuy.supplier_name).filter(
|
StockBuy.supplier_name != '').distinct().all()]
|
||||||
StockBuy.base_id == base_id,
|
|
||||||
StockBuy.supplier_name.isnot(None),
|
|
||||||
StockBuy.supplier_name != ''
|
|
||||||
).distinct().order_by(StockBuy.supplier_name)
|
|
||||||
# [修改] 移除 limit
|
|
||||||
suppliers = [row[0] for row in query.all()]
|
|
||||||
return suppliers
|
|
||||||
except Exception:
|
|
||||||
return []
|
|
||||||
|
|
||||||
# ============================================================
|
|
||||||
# 7. 采购人建议 (移除限制)
|
|
||||||
# ============================================================
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_history_purchasers(keyword):
|
def get_history_purchasers(keyword):
|
||||||
try:
|
return [{'value': r.buyer_name, 'email': r.buyer_email} for r in
|
||||||
query = db.session.query(StockBuy.buyer_name, StockBuy.buyer_email) \
|
db.session.query(StockBuy.buyer_name, StockBuy.buyer_email).filter(
|
||||||
.filter(StockBuy.buyer_name.isnot(None), StockBuy.buyer_name != '')
|
StockBuy.buyer_name != '').distinct().all()]
|
||||||
|
|
||||||
if keyword:
|
|
||||||
kw = f'%{keyword}%'
|
|
||||||
query = query.filter(or_(
|
|
||||||
StockBuy.buyer_name.ilike(kw),
|
|
||||||
StockBuy.buyer_email.ilike(kw)
|
|
||||||
))
|
|
||||||
|
|
||||||
# [修改] 移除 limit
|
|
||||||
results = query.distinct().all()
|
|
||||||
|
|
||||||
users = []
|
|
||||||
for row in results:
|
|
||||||
users.append({
|
|
||||||
'value': row.buyer_name,
|
|
||||||
'email': row.buyer_email or ''
|
|
||||||
})
|
|
||||||
return users
|
|
||||||
except Exception:
|
|
||||||
traceback.print_exc()
|
|
||||||
return []
|
|
||||||
|
|
||||||
# ============================================================
|
|
||||||
# 8. 链接建议 (移除限制)
|
|
||||||
# ============================================================
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_history_links(base_id, link_type='original'):
|
def get_history_links(base_id, type):
|
||||||
try:
|
return [r[0] for r in
|
||||||
target_col = StockBuy.original_link if link_type == 'original' else StockBuy.detail_link
|
db.session.query(StockBuy.original_link if type == 'original' else StockBuy.detail_link).filter(
|
||||||
query = db.session.query(target_col).filter(
|
StockBuy.base_id == base_id).distinct().all()]
|
||||||
StockBuy.base_id == base_id,
|
|
||||||
target_col.isnot(None),
|
|
||||||
target_col != ''
|
|
||||||
).distinct()
|
|
||||||
# [修改] 移除 limit
|
|
||||||
links = [row[0] for row in query.all()]
|
|
||||||
return links
|
|
||||||
except Exception:
|
|
||||||
return []
|
|
||||||
|
|
||||||
# ============================================================
|
|
||||||
# 9. 库位建议 (移除限制)
|
|
||||||
# ============================================================
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_history_locations(base_id):
|
def get_history_locations(base_id):
|
||||||
try:
|
return [r[0] for r in
|
||||||
query = db.session.query(StockBuy.warehouse_location).filter(
|
db.session.query(StockBuy.warehouse_location).filter(StockBuy.base_id == base_id).distinct().all()]
|
||||||
StockBuy.base_id == base_id,
|
|
||||||
StockBuy.warehouse_location.isnot(None),
|
|
||||||
StockBuy.warehouse_location != ''
|
|
||||||
).distinct()
|
|
||||||
# [修改] 移除 limit
|
|
||||||
locs = [row[0] for row in query.all()]
|
|
||||||
return locs
|
|
||||||
except Exception:
|
|
||||||
return []
|
|
||||||
@ -12,7 +12,7 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
class LabelPrintService:
|
class LabelPrintService:
|
||||||
PRINTER_IP = "192.168.9.205"
|
PRINTER_IP = "192.168.9.221"
|
||||||
PRINTER_PORT = 9100
|
PRINTER_PORT = 9100
|
||||||
|
|
||||||
# ================= 1. 尺寸与分辨率配置 (300 DPI) =================
|
# ================= 1. 尺寸与分辨率配置 (300 DPI) =================
|
||||||
|
|||||||
@ -35,29 +35,29 @@ export function deleteBuyInbound(id: number) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. 搜索基础物料
|
// 5. 搜索基础物料 (修改:支持分页参数)
|
||||||
export function searchMaterialBase(keyword: string) {
|
export function searchMaterialBase(keyword: string, page: number = 1) {
|
||||||
return request({
|
return request({
|
||||||
url: '/inbound/buy/search-base',
|
url: '/inbound/buy/search-base',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
params: { keyword }
|
params: { keyword, page }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. 文件上传 (用于图片/拍照)
|
// 6. 文件上传 (用于图片/拍照)
|
||||||
export function uploadFile(data: FormData) {
|
export function uploadFile(data: FormData) {
|
||||||
return request({
|
return request({
|
||||||
url: '/common/upload', // 对应后端 /api/v1/common/upload
|
url: '/common/upload',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
data,
|
data,
|
||||||
headers: { 'Content-Type': 'multipart/form-data' }
|
headers: { 'Content-Type': 'multipart/form-data' }
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. [新增] 文件删除
|
// 7. 文件删除
|
||||||
export function deleteFile(filename: string) {
|
export function deleteFile(filename: string) {
|
||||||
return request({
|
return request({
|
||||||
url: `/common/files/${filename}`, // 对应后端 /api/v1/common/files/<filename>
|
url: `/common/files/${filename}`,
|
||||||
method: 'delete'
|
method: 'delete'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -89,7 +89,7 @@ export function getLinkSuggestions(params: any) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 11. [新增] 库位建议
|
// 11. 库位建议
|
||||||
export function getLocationSuggestions(params: any) {
|
export function getLocationSuggestions(params: any) {
|
||||||
return request({
|
return request({
|
||||||
url: '/inbound/buy/suggestions/locations',
|
url: '/inbound/buy/suggestions/locations',
|
||||||
|
|||||||
@ -9,6 +9,16 @@ export function listMaterialBase(params: any) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ==========================================
|
||||||
|
// 1.1 获取基础信息筛选选项 (所有类别/类型) [新增]
|
||||||
|
// ==========================================
|
||||||
|
export function getMaterialBaseOptions() {
|
||||||
|
return request({
|
||||||
|
url: '/inbound/base/options',
|
||||||
|
method: 'get'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 2. 新增基础信息
|
// 2. 新增基础信息
|
||||||
export function addMaterialBase(data: any) {
|
export function addMaterialBase(data: any) {
|
||||||
return request({
|
return request({
|
||||||
@ -19,7 +29,6 @@ export function addMaterialBase(data: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. 修改基础信息 (包含状态启用/禁用)
|
// 3. 修改基础信息 (包含状态启用/禁用)
|
||||||
// 【修复点】: 必须在 URL 中拼接 data.id,否则后端会报 405 Method Not Allowed
|
|
||||||
export function updateMaterialBase(data: any) {
|
export function updateMaterialBase(data: any) {
|
||||||
return request({
|
return request({
|
||||||
url: `/inbound/base/${data.id}`,
|
url: `/inbound/base/${data.id}`,
|
||||||
|
|||||||
@ -203,7 +203,7 @@
|
|||||||
:total="total"
|
:total="total"
|
||||||
v-model:current-page="queryParams.pageNum"
|
v-model:current-page="queryParams.pageNum"
|
||||||
v-model:page-size="queryParams.pageSize"
|
v-model:page-size="queryParams.pageSize"
|
||||||
:page-sizes="[10, 20, 50, 100]"
|
:page-sizes="[100, 200, 500, 1000]"
|
||||||
@size-change="getList"
|
@size-change="getList"
|
||||||
@current-change="getList"
|
@current-change="getList"
|
||||||
/>
|
/>
|
||||||
@ -371,7 +371,8 @@ import {
|
|||||||
listMaterialBase,
|
listMaterialBase,
|
||||||
addMaterialBase,
|
addMaterialBase,
|
||||||
updateMaterialBase,
|
updateMaterialBase,
|
||||||
delMaterialBase
|
delMaterialBase,
|
||||||
|
getMaterialBaseOptions // 新增引入
|
||||||
} from '@/api/material_base';
|
} from '@/api/material_base';
|
||||||
import { uploadFile, deleteFile } from '@/api/common/upload';
|
import { uploadFile, deleteFile } from '@/api/common/upload';
|
||||||
import WebRtcCamera from '@/components/Camera/WebRtcCamera.vue';
|
import WebRtcCamera from '@/components/Camera/WebRtcCamera.vue';
|
||||||
@ -442,7 +443,7 @@ const typeOptions = ref<string[]>([]);
|
|||||||
|
|
||||||
const queryParams = reactive<QueryParams>({
|
const queryParams = reactive<QueryParams>({
|
||||||
pageNum: 1,
|
pageNum: 1,
|
||||||
pageSize: 10,
|
pageSize: 100, // 修改默认显示条数为 100
|
||||||
keyword: '',
|
keyword: '',
|
||||||
category: '',
|
category: '',
|
||||||
type: '',
|
type: '',
|
||||||
@ -483,16 +484,16 @@ const rules = reactive<FormRules>({
|
|||||||
|
|
||||||
// --- 业务逻辑方法 ---
|
// --- 业务逻辑方法 ---
|
||||||
|
|
||||||
const extractDynamicOptions = (items: MaterialBaseVO[]) => {
|
// 获取所有选项(不再依赖当前页数据)
|
||||||
if (!items || items.length === 0) return;
|
const getOptionsList = () => {
|
||||||
const newCategories = new Set(categoryOptions.value);
|
getMaterialBaseOptions().then((res: any) => {
|
||||||
const newTypes = new Set(typeOptions.value);
|
if (res.code === 200) {
|
||||||
items.forEach(item => {
|
categoryOptions.value = res.data.categories || [];
|
||||||
if (item.category) newCategories.add(item.category);
|
typeOptions.value = res.data.types || [];
|
||||||
if (item.type) newTypes.add(item.type);
|
}
|
||||||
|
}).catch(err => {
|
||||||
|
console.error("获取筛选项失败", err);
|
||||||
});
|
});
|
||||||
categoryOptions.value = Array.from(newCategories);
|
|
||||||
typeOptions.value = Array.from(newTypes);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const querySearchCategory = (queryString: string, cb: any) => {
|
const querySearchCategory = (queryString: string, cb: any) => {
|
||||||
@ -518,7 +519,7 @@ const getList = () => {
|
|||||||
if (response && response.data) {
|
if (response && response.data) {
|
||||||
tableData.value = response.data.items;
|
tableData.value = response.data.items;
|
||||||
total.value = response.data.total;
|
total.value = response.data.total;
|
||||||
extractDynamicOptions(tableData.value);
|
// 移除 extractDynamicOptions 调用
|
||||||
} else {
|
} else {
|
||||||
tableData.value = [];
|
tableData.value = [];
|
||||||
total.value = 0;
|
total.value = 0;
|
||||||
@ -646,6 +647,8 @@ const submitForm = async () => {
|
|||||||
ElMessage.success(`${actionText}成功`);
|
ElMessage.success(`${actionText}成功`);
|
||||||
dialog.visible = false;
|
dialog.visible = false;
|
||||||
getList();
|
getList();
|
||||||
|
// 提交成功后,刷新选项,以防有新的类别/类型被创建
|
||||||
|
getOptionsList();
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
ElMessage.error(error.msg || '保存失败');
|
ElMessage.error(error.msg || '保存失败');
|
||||||
} finally {
|
} finally {
|
||||||
@ -689,6 +692,8 @@ const handleDelete = (row: MaterialBaseVO) => {
|
|||||||
ElMessage.success("删除成功");
|
ElMessage.success("删除成功");
|
||||||
if (tableData.value.length === 1 && queryParams.pageNum > 1) queryParams.pageNum--;
|
if (tableData.value.length === 1 && queryParams.pageNum > 1) queryParams.pageNum--;
|
||||||
getList();
|
getList();
|
||||||
|
// 删除后也刷新选项
|
||||||
|
getOptionsList();
|
||||||
});
|
});
|
||||||
}).catch(() => {});
|
}).catch(() => {});
|
||||||
};
|
};
|
||||||
@ -796,6 +801,7 @@ const handleCameraConfirm = async (file: File) => {
|
|||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
getList();
|
getList();
|
||||||
|
getOptionsList(); // 初始化下拉选项
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@ -193,12 +193,14 @@
|
|||||||
remote
|
remote
|
||||||
reserve-keyword
|
reserve-keyword
|
||||||
placeholder="输入名称或规格..."
|
placeholder="输入名称或规格..."
|
||||||
:remote-method="handleSearchMaterial"
|
:remote-method="handleSearchMaterialDebounced"
|
||||||
@visible-change="handleMaterialDropdownVisible"
|
@visible-change="handleMaterialDropdownVisible"
|
||||||
:loading="searchLoading"
|
:loading="searchLoading"
|
||||||
style="width: 100%"
|
style="width: 100%"
|
||||||
@change="onMaterialSelected"
|
@change="onMaterialSelected"
|
||||||
default-first-option
|
default-first-option
|
||||||
|
v-loadmore="loadMoreMaterials"
|
||||||
|
:teleported="false"
|
||||||
>
|
>
|
||||||
<el-option
|
<el-option
|
||||||
v-for="item in materialOptions"
|
v-for="item in materialOptions"
|
||||||
@ -213,12 +215,15 @@
|
|||||||
<el-tag v-else size="small" type="success" effect="plain">系统</el-tag>
|
<el-tag v-else size="small" type="success" effect="plain">系统</el-tag>
|
||||||
</div>
|
</div>
|
||||||
</el-option>
|
</el-option>
|
||||||
|
<div v-if="loadingMore" style="text-align: center; color: #999; font-size: 12px; padding: 5px; background: #fff;">
|
||||||
|
<el-icon class="is-loading"><Refresh /></el-icon> 加载更多中...
|
||||||
|
</div>
|
||||||
</el-select>
|
</el-select>
|
||||||
</el-form-item>
|
</el-form-item>
|
||||||
</el-col>
|
</el-col>
|
||||||
<el-col :span="14" style="display: flex; align-items: center;">
|
<el-col :span="14" style="display: flex; align-items: center;">
|
||||||
<span class="search-tip">
|
<span class="search-tip">
|
||||||
<el-icon><InfoFilled/></el-icon> 未输入时展示最新物料;输入关键词进行精确搜索。
|
<el-icon><InfoFilled/></el-icon> 模糊搜索名称/规格/拼音;滚动加载更多。
|
||||||
</span>
|
</span>
|
||||||
</el-col>
|
</el-col>
|
||||||
</el-row>
|
</el-row>
|
||||||
@ -511,6 +516,28 @@ import {
|
|||||||
import {getLabelPreview, executePrint} from '@/api/common/print'
|
import {getLabelPreview, executePrint} from '@/api/common/print'
|
||||||
import WebRtcCamera from '@/components/Camera/WebRtcCamera.vue'
|
import WebRtcCamera from '@/components/Camera/WebRtcCamera.vue'
|
||||||
|
|
||||||
|
// ------------------------------------
|
||||||
|
// 自定义指令:v-loadmore
|
||||||
|
// 修复点:添加 setTimeout 以确保 DOM 渲染,查找正确的 Element Plus 滚动容器类名
|
||||||
|
// ------------------------------------
|
||||||
|
const vLoadmore = {
|
||||||
|
mounted(el: any, binding: any) {
|
||||||
|
setTimeout(() => {
|
||||||
|
// Element Plus 下拉框内部使用 el-scrollbar__wrap
|
||||||
|
const SELECT_DOM = el.querySelector('.el-select-dropdown .el-scrollbar__wrap')
|
||||||
|
if (SELECT_DOM) {
|
||||||
|
SELECT_DOM.addEventListener('scroll', function (this: any) {
|
||||||
|
// 判断是否触底
|
||||||
|
const condition = this.scrollHeight - this.scrollTop <= this.clientHeight + 1
|
||||||
|
if (condition) {
|
||||||
|
binding.value()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}, 200)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
// 状态与变量
|
// 状态与变量
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
@ -518,6 +545,7 @@ const loading = ref(false)
|
|||||||
const submitting = ref(false)
|
const submitting = ref(false)
|
||||||
const visible = ref(false)
|
const visible = ref(false)
|
||||||
const searchLoading = ref(false)
|
const searchLoading = ref(false)
|
||||||
|
const loadingMore = ref(false) // 分页加载状态
|
||||||
const dialogStatus = ref<'create' | 'update'>('create')
|
const dialogStatus = ref<'create' | 'update'>('create')
|
||||||
const tableData = ref([])
|
const tableData = ref([])
|
||||||
const total = ref(0)
|
const total = ref(0)
|
||||||
@ -531,6 +559,11 @@ const queryParams = reactive({
|
|||||||
})
|
})
|
||||||
|
|
||||||
const materialOptions = ref<any[]>([])
|
const materialOptions = ref<any[]>([])
|
||||||
|
const searchPage = ref(1)
|
||||||
|
const searchKeyword = ref('')
|
||||||
|
const hasNextPage = ref(true)
|
||||||
|
let searchTimer: any = null // 用于防抖
|
||||||
|
|
||||||
const printVisible = ref(false)
|
const printVisible = ref(false)
|
||||||
const printLoading = ref(false)
|
const printLoading = ref(false)
|
||||||
const printing = ref(false)
|
const printing = ref(false)
|
||||||
@ -607,52 +640,32 @@ const form = reactive({
|
|||||||
// 建议/Autocomplete 逻辑
|
// 建议/Autocomplete 逻辑
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
|
|
||||||
// 1. 供应商建议 (基于 base_id)
|
// 1. 供应商建议
|
||||||
const fetchSupplierSuggestions = async (query: string, cb: any) => {
|
const fetchSupplierSuggestions = async (query: string, cb: any) => {
|
||||||
if (!form.base_id) {
|
if (!form.base_id) { cb([]); return }
|
||||||
cb([])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const res: any = await getSupplierSuggestions({ base_id: form.base_id })
|
const res: any = await getSupplierSuggestions({ base_id: form.base_id })
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
const suppliers = res.data.map((name: string) => ({ value: name }))
|
const suppliers = res.data.map((name: string) => ({ value: name }))
|
||||||
const filtered = query ? suppliers.filter((item: any) => item.value.toLowerCase().includes(query.toLowerCase())) : suppliers
|
const filtered = query ? suppliers.filter((item: any) => item.value.toLowerCase().includes(query.toLowerCase())) : suppliers
|
||||||
cb(filtered)
|
cb(filtered)
|
||||||
} else {
|
} else { cb([]) }
|
||||||
cb([])
|
} catch (e) { cb([]) }
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
cb([])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const querySearchSupplier = (qs: string, cb: any) => fetchSupplierSuggestions(qs, cb)
|
const querySearchSupplier = (qs: string, cb: any) => fetchSupplierSuggestions(qs, cb)
|
||||||
const handleSupplierSelect = (item: any) => {
|
const handleSupplierSelect = (item: any) => { form.supplier_name = item.value }
|
||||||
form.supplier_name = item.value
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. 采购人建议 (全局,来自历史 buy 表)
|
// 2. 采购人建议
|
||||||
const fetchUserSuggestions = async (query: string, cb: any) => {
|
const fetchUserSuggestions = async (query: string, cb: any) => {
|
||||||
try {
|
try {
|
||||||
const res: any = await getUserSuggestions({ keyword: query })
|
const res: any = await getUserSuggestions({ keyword: query })
|
||||||
if (res.code === 200) {
|
if (res.code === 200) { cb(res.data) } else { cb([]) }
|
||||||
cb(res.data)
|
} catch (e) { cb([]) }
|
||||||
} else {
|
|
||||||
cb([])
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
cb([])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const querySearchPurchaser = (qs: string, cb: any) => fetchUserSuggestions(qs, cb)
|
const querySearchPurchaser = (qs: string, cb: any) => fetchUserSuggestions(qs, cb)
|
||||||
const handlePurchaserSelect = (item: any) => {
|
const handlePurchaserSelect = (item: any) => { form.purchaser = item.value; if (item.email) form.purchaser_email = item.email }
|
||||||
form.purchaser = item.value
|
|
||||||
if (item.email) {
|
|
||||||
form.purchaser_email = item.email
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. 链接建议 (基于 base_id)
|
// 3. 链接建议
|
||||||
const fetchLinkSuggestions = async (query: string, cb: any, type: 'original' | 'detail') => {
|
const fetchLinkSuggestions = async (query: string, cb: any, type: 'original' | 'detail') => {
|
||||||
if (!form.base_id) { cb([]); return }
|
if (!form.base_id) { cb([]); return }
|
||||||
try {
|
try {
|
||||||
@ -666,7 +679,7 @@ const fetchLinkSuggestions = async (query: string, cb: any, type: 'original' | '
|
|||||||
}
|
}
|
||||||
const querySearchLinks = (qs: string, cb: any, type: 'original' | 'detail') => fetchLinkSuggestions(qs, cb, type)
|
const querySearchLinks = (qs: string, cb: any, type: 'original' | 'detail') => fetchLinkSuggestions(qs, cb, type)
|
||||||
|
|
||||||
// 4. [新增] 库位建议 (基于 base_id)
|
// 4. 库位建议
|
||||||
const fetchLocationSuggestions = async (query: string, cb: any) => {
|
const fetchLocationSuggestions = async (query: string, cb: any) => {
|
||||||
if (!form.base_id) { cb([]); return }
|
if (!form.base_id) { cb([]); return }
|
||||||
try {
|
try {
|
||||||
@ -688,15 +701,53 @@ const querySearchCurrency = (queryString: string, cb: any) => {
|
|||||||
cb(filtered)
|
cb(filtered)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleMaterialDropdownVisible = (visible: boolean) => { if (visible && materialOptions.value.length === 0) handleSearchMaterial('') }
|
const handleMaterialDropdownVisible = (visible: boolean) => { if (visible && materialOptions.value.length === 0) handleSearchMaterialDebounced('') }
|
||||||
|
|
||||||
|
// [修复] 增加防抖,避免频繁触发导致数据不一致
|
||||||
|
const handleSearchMaterialDebounced = (query: string) => {
|
||||||
|
if (searchTimer) clearTimeout(searchTimer)
|
||||||
|
searchTimer = setTimeout(() => {
|
||||||
|
handleSearchMaterial(query)
|
||||||
|
}, 300) // 300ms 防抖
|
||||||
|
}
|
||||||
|
|
||||||
const handleSearchMaterial = async (query: string) => {
|
const handleSearchMaterial = async (query: string) => {
|
||||||
searchLoading.value = true
|
searchLoading.value = true
|
||||||
|
searchKeyword.value = query
|
||||||
|
searchPage.value = 1
|
||||||
|
materialOptions.value = []
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res: any = await searchMaterialBase(query)
|
const res: any = await searchMaterialBase(query, 1)
|
||||||
const apiResults = (res.data || []).map((i: any) => ({...i, isHistory: false}))
|
if (res.data) {
|
||||||
materialOptions.value = apiResults
|
const apiResults = (res.data || []).map((i: any) => ({...i, isHistory: false}))
|
||||||
|
materialOptions.value = apiResults
|
||||||
|
hasNextPage.value = res.has_next
|
||||||
|
}
|
||||||
} finally { searchLoading.value = false }
|
} finally { searchLoading.value = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const loadMoreMaterials = async () => {
|
||||||
|
if (searchLoading.value || loadingMore.value || !hasNextPage.value) return
|
||||||
|
|
||||||
|
loadingMore.value = true
|
||||||
|
searchPage.value += 1
|
||||||
|
try {
|
||||||
|
const res: any = await searchMaterialBase(searchKeyword.value, searchPage.value)
|
||||||
|
if (res.data && res.data.length > 0) {
|
||||||
|
const newItems = res.data.map((i: any) => ({...i, isHistory: false}))
|
||||||
|
materialOptions.value.push(...newItems)
|
||||||
|
hasNextPage.value = res.has_next
|
||||||
|
} else {
|
||||||
|
hasNextPage.value = false
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
searchPage.value -= 1
|
||||||
|
} finally {
|
||||||
|
loadingMore.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const onMaterialSelected = (val: number) => {
|
const onMaterialSelected = (val: number) => {
|
||||||
const item = materialOptions.value.find(i => i.id === val)
|
const item = materialOptions.value.find(i => i.id === val)
|
||||||
if (item) {
|
if (item) {
|
||||||
@ -990,6 +1041,8 @@ const handlePrint = async (row: any) => {
|
|||||||
const confirmPrint = async () => { printing.value = true; try { await executePrint(currentPrintData.value); ElMessage.success('指令已发送'); printVisible.value = false } catch (e: any) { ElMessage.error(e.msg || '打印失败') } finally { printing.value = false } }
|
const confirmPrint = async () => { printing.value = true; try { await executePrint(currentPrintData.value); ElMessage.success('指令已发送'); printVisible.value = false } catch (e: any) { ElMessage.error(e.msg || '打印失败') } finally { printing.value = false } }
|
||||||
const resetForm = () => {
|
const resetForm = () => {
|
||||||
materialOptions.value = []; arrivalFileList.value = []; reportFileList.value = []; inspection_report_url.value = ''
|
materialOptions.value = []; arrivalFileList.value = []; reportFileList.value = []; inspection_report_url.value = ''
|
||||||
|
// 重置分页状态
|
||||||
|
searchPage.value = 1; hasNextPage.value = true; searchKeyword.value = '';
|
||||||
Object.assign(form, { id: undefined, base_id: undefined, material_name: '', spec_model: '', category: '', unit: '', material_type: '', sku: '', barcode: '', in_date: '', serial_number: '', batch_number: '', status: '在库', inspection_status: '未检', in_quantity: 1, stock_quantity: 1, available_quantity: 1, warehouse_location: '', unit_price: 0, total_price: 0, currency: 'CNY', exchange_rate: 1.00, supplier_name: '', purchaser: '', purchaser_email: '', source_link: '', detail_link: '', arrival_photo: [], inspection_report: [] })
|
Object.assign(form, { id: undefined, base_id: undefined, material_name: '', spec_model: '', category: '', unit: '', material_type: '', sku: '', barcode: '', in_date: '', serial_number: '', batch_number: '', status: '在库', inspection_status: '未检', in_quantity: 1, stock_quantity: 1, available_quantity: 1, warehouse_location: '', unit_price: 0, total_price: 0, currency: 'CNY', exchange_rate: 1.00, supplier_name: '', purchaser: '', purchaser_email: '', source_link: '', detail_link: '', arrival_photo: [], inspection_report: [] })
|
||||||
}
|
}
|
||||||
const getStatusType = (status: string) => { const map: any = {'在库': 'success', '出库': 'info', '损耗': 'danger'}; return map[status] || 'warning' }
|
const getStatusType = (status: string) => { const map: any = {'在库': 'success', '出库': 'info', '损耗': 'danger'}; return map[status] || 'warning' }
|
||||||
|
|||||||
Reference in New Issue
Block a user