From 6bc2d2848e3c0a274209f0e63e3705fffe03d00e Mon Sep 17 00:00:00 2001 From: DXC Date: Wed, 11 Mar 2026 17:28:50 +0800 Subject: [PATCH] \"fix: rewrite warning sort logic with proper case syntax and is_(True)\" --- .../app/services/inbound/base_service.py | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/inventory-backend/app/services/inbound/base_service.py b/inventory-backend/app/services/inbound/base_service.py index 3eb33d7..65ea9f6 100644 --- a/inventory-backend/app/services/inbound/base_service.py +++ b/inventory-backend/app/services/inbound/base_service.py @@ -313,34 +313,32 @@ class MaterialBaseService: enable_warning_sort = has_warning_permission and filters.get('enableWarningSort', False) if enable_warning_sort: - # 预警智能排序:先按预警状态排序,再按缺口/余量排序 + print("====== [DEBUG] 成功进入预警智能排序逻辑 ======") + # 状态层级:红(2) > 黄(1) > 常规(0) - warning_level = case( - (and_(MaterialWarningSetting.is_enabled == True, total_inv <= MaterialWarningSetting.red_threshold), 2), - (and_(MaterialWarningSetting.is_enabled == True, total_inv <= MaterialWarningSetting.yellow_threshold), 1), - else_=0 - ) + warning_level = case([ + (and_(MaterialWarningSetting.is_enabled.is_(True), total_inv <= MaterialWarningSetting.red_threshold), 2), + (and_(MaterialWarningSetting.is_enabled.is_(True), total_inv <= MaterialWarningSetting.yellow_threshold), 1) + ], else_=0) # 红色组内部:缺口越大越靠前 (red_threshold - total_inv) DESC - red_shortage = case( - (and_(MaterialWarningSetting.is_enabled == True, total_inv <= MaterialWarningSetting.red_threshold), - MaterialWarningSetting.red_threshold - total_inv), - else_=0 - ) + red_shortage = case([ + (and_(MaterialWarningSetting.is_enabled.is_(True), total_inv <= MaterialWarningSetting.red_threshold), + MaterialWarningSetting.red_threshold - total_inv) + ], else_=0) # 黄色组内部:距离红线越近越靠前 (total_inv - red_threshold) ASC - yellow_distance = case( - (and_(MaterialWarningSetting.is_enabled == True, total_inv > MaterialWarningSetting.red_threshold, total_inv <= MaterialWarningSetting.yellow_threshold), - total_inv - MaterialWarningSetting.red_threshold), - else_=999999999 # 给一个极大值,让非黄色组沉底,不影响升序 - ) + yellow_distance = case([ + (and_(MaterialWarningSetting.is_enabled.is_(True), total_inv > MaterialWarningSetting.red_threshold, total_inv <= MaterialWarningSetting.yellow_threshold), + total_inv - MaterialWarningSetting.red_threshold) + ], else_=999999) - # 清除默认排序,应用全新的四级混合排序 + # 清除默认排序,应用严谨的四级排序 query = query.order_by( desc(warning_level), desc(red_shortage), asc(yellow_distance), - desc(MaterialBase.id) # 常规组兜底排序 + desc(MaterialBase.id) ) elif order_by_column: # 字段映射