修正新增入库时3个组件的名称筛选逻辑
This commit is contained in:
@ -2,30 +2,46 @@
|
||||
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 ProductInboundService:
|
||||
|
||||
# ============================================================
|
||||
# 1. 基础物料搜索 (已修正:完全对齐 Buy/Semi 的逻辑)
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def search_base_material(keyword):
|
||||
try:
|
||||
if not keyword:
|
||||
query = MaterialBase.query.filter(MaterialBase.is_enabled == True).order_by(
|
||||
MaterialBase.id.desc()).limit(20)
|
||||
else:
|
||||
query = MaterialBase.query.filter(
|
||||
MaterialBase.is_enabled == True,
|
||||
or_(MaterialBase.name.ilike(f'%{keyword}%'), MaterialBase.spec_model.ilike(f'%{keyword}%'))
|
||||
).limit(20)
|
||||
# 1. 基础查询:必须是已启用的物料
|
||||
query = MaterialBase.query.filter(MaterialBase.is_enabled == True)
|
||||
|
||||
# 2. 动态条件:如果传入了关键词,则增加模糊匹配条件
|
||||
if keyword:
|
||||
query = query.filter(
|
||||
or_(
|
||||
MaterialBase.name.ilike(f'%{keyword}%'),
|
||||
MaterialBase.spec_model.ilike(f'%{keyword}%')
|
||||
)
|
||||
)
|
||||
|
||||
# 3. 排序与限制:按ID倒序,取最新20条
|
||||
query = query.order_by(MaterialBase.id.desc()).limit(20)
|
||||
|
||||
# 4. 结果封装:确保字段名与前端 Vue 的 handleSelect 方法一致
|
||||
results = []
|
||||
for item in query.all():
|
||||
results.append({
|
||||
'id': item.id, 'name': item.name, 'spec': item.spec_model,
|
||||
'category': item.category, 'unit': item.unit, 'type': item.material_type
|
||||
'id': item.id,
|
||||
'name': item.name,
|
||||
'spec': item.spec_model, # 对应前端: item.spec
|
||||
'category': item.category, # 对应前端: item.category
|
||||
'unit': item.unit, # 对应前端: item.unit
|
||||
'type': item.material_type, # 对应前端: item.type
|
||||
'status': '启用'
|
||||
})
|
||||
return results
|
||||
except Exception:
|
||||
@ -129,6 +145,9 @@ class ProductInboundService:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
# ============================================================
|
||||
# 3. 更新逻辑
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def update_inbound(stock_id, data):
|
||||
from app.models.inbound.product import StockProduct
|
||||
@ -184,6 +203,9 @@ class ProductInboundService:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
# ============================================================
|
||||
# 4. 删除逻辑
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def delete_inbound(stock_id):
|
||||
from app.models.inbound.product import StockProduct
|
||||
@ -197,6 +219,9 @@ class ProductInboundService:
|
||||
db.session.rollback()
|
||||
raise e
|
||||
|
||||
# ============================================================
|
||||
# 5. 出库历史
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_outbound_history(stock_id):
|
||||
"""获取出库历史"""
|
||||
@ -209,7 +234,7 @@ class ProductInboundService:
|
||||
return []
|
||||
|
||||
# ============================================================
|
||||
# 获取列表 (修改:按时间倒序排序 + 展示只显示日期)
|
||||
# 6. 获取列表
|
||||
# ============================================================
|
||||
@staticmethod
|
||||
def get_list(page, limit, keyword=None, statuses=None):
|
||||
@ -240,7 +265,7 @@ class ProductInboundService:
|
||||
)
|
||||
)
|
||||
|
||||
# [核心修改] 按照 production_date (入库日期) 倒序排序
|
||||
# 按照 production_date (入库日期) 倒序排序
|
||||
pagination = query.order_by(StockProduct.production_date.desc()).paginate(page=page, per_page=limit,
|
||||
error_out=False)
|
||||
|
||||
@ -257,7 +282,7 @@ class ProductInboundService:
|
||||
for item in current_items:
|
||||
d = item.to_dict()
|
||||
|
||||
# [核心修改] 格式化日期
|
||||
# 格式化日期
|
||||
date_display = ''
|
||||
if item.production_date:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user