修正新增入库时3个组件的名称筛选逻辑

This commit is contained in:
dxc
2026-02-05 15:04:06 +08:00
parent 4f90e02dcf
commit cad5fd696c
6 changed files with 273 additions and 697 deletions

View File

@ -2,36 +2,44 @@
from app.extensions import db
from app.models.base import MaterialBase
from app.models.outbound import TransOutbound
from datetime import datetime, timedelta, timezone # [修改]
from datetime import datetime, timedelta, timezone
from sqlalchemy import or_, func, text, and_
import traceback
import json
class SemiInboundService:
# ============================================================
# 1. 基础物料搜索 (已修复:支持空关键词返回最新数据)
# ============================================================
@staticmethod
def search_base_material(keyword):
try:
if not keyword:
return []
# 基础查询:必须是已启用的物料
query = MaterialBase.query.filter(MaterialBase.is_enabled == True)
query = MaterialBase.query.filter(
MaterialBase.is_enabled == True,
or_(
MaterialBase.name.ilike(f'%{keyword}%'),
MaterialBase.spec_model.ilike(f'%{keyword}%')
# 如果有关键词,进行模糊匹配
if keyword:
query = query.filter(
or_(
MaterialBase.name.ilike(f'%{keyword}%'),
MaterialBase.spec_model.ilike(f'%{keyword}%')
)
)
).limit(20)
# 统一逻辑按ID倒序限制20条
query = query.order_by(MaterialBase.id.desc()).limit(20)
results = []
for item in query.all():
results.append({
'id': item.id,
'name': item.name,
'spec': item.spec_model,
'spec': item.spec_model, # 对应前端 item.spec
'category': item.category,
'unit': item.unit,
'type': item.material_type,
'type': item.material_type, # 对应前端 item.type
'status': '启用'
})
return results
@ -39,6 +47,9 @@ class SemiInboundService:
traceback.print_exc()
return []
# ============================================================
# 2. 新增入库逻辑
# ============================================================
@staticmethod
def handle_inbound(data):
from app.models.inbound.semi import StockSemi
@ -171,6 +182,9 @@ class SemiInboundService:
traceback.print_exc()
raise e
# ============================================================
# 3. 更新逻辑
# ============================================================
@staticmethod
def update_inbound(stock_id, data):
from app.models.inbound.semi import StockSemi
@ -268,6 +282,9 @@ class SemiInboundService:
db.session.rollback()
raise e
# ============================================================
# 4. 删除逻辑
# ============================================================
@staticmethod
def delete_inbound(stock_id):
from app.models.inbound.semi import StockSemi
@ -282,6 +299,9 @@ class SemiInboundService:
db.session.rollback()
raise e
# ============================================================
# 5. 出库历史
# ============================================================
@staticmethod
def get_outbound_history(stock_id):
"""获取出库历史"""
@ -294,7 +314,7 @@ class SemiInboundService:
return []
# ============================================================
# 6. 获取列表 (修改:按时间倒序排序 + 展示只显示日期)
# 6. 获取列表
# ============================================================
@staticmethod
def get_list(page, limit, keyword=None, statuses=None):
@ -329,7 +349,7 @@ class SemiInboundService:
)
)
# [核心修改] 按照 production_date (入库日期) 倒序排序
# 按照 production_date (入库日期) 倒序排序
pagination = query.order_by(StockSemi.production_date.desc()).paginate(page=page, per_page=limit,
error_out=False)
@ -346,7 +366,7 @@ class SemiInboundService:
for item in current_items:
d = item.to_dict()
# [核心修改] 格式化展示日期,覆盖 to_dict 的默认行为
# 格式化展示日期
date_display = ''
if item.production_date:
try: