\"fix: rewrite warning sort logic with proper case syntax and is_(True)\"

This commit is contained in:
DXC
2026-03-11 17:28:50 +08:00
parent d0fa474ee4
commit 6bc2d2848e

View File

@ -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:
# 字段映射