fix: refactor advanced filters to use WHERE for subquery columns and rewrite warning sort in pure order_by expressions
This commit is contained in:
@ -230,10 +230,10 @@ class MaterialBaseService:
|
||||
.outerjoin(MaterialWarningSetting, MaterialBase.id == MaterialWarningSetting.base_id)
|
||||
|
||||
# ============================================================
|
||||
# 【修复3】高级筛选:聚合字段使用 HAVING,非聚合字段使用 WHERE
|
||||
# 【修复3】高级筛选:统一使用 WHERE (filter)
|
||||
# 聚合字段同样是子查询列,直接用 filter 过滤即可
|
||||
# ============================================================
|
||||
advanced_filters = filters.get('advancedFilters', []) if filters else []
|
||||
having_conditions = []
|
||||
filter_conditions = []
|
||||
|
||||
if advanced_filters:
|
||||
@ -248,7 +248,7 @@ class MaterialBaseService:
|
||||
'inventoryCount': 'total_inv',
|
||||
'availableCount': 'total_avail'
|
||||
}
|
||||
# 聚合字段列表(这些需要用 HAVING)
|
||||
# 聚合字段列表
|
||||
aggregate_fields = {'inventoryCount', 'availableCount'}
|
||||
|
||||
field_permission_map = {
|
||||
@ -286,7 +286,7 @@ class MaterialBaseService:
|
||||
is_aggregate = field in aggregate_fields
|
||||
|
||||
if is_aggregate:
|
||||
# 聚合字段:使用 HAVING - 通过子查询列进行比较
|
||||
# 聚合字段:直接使用子查询列,通过 WHERE (filter) 过滤
|
||||
col = inner_sub.c.total_inv if field == 'inventoryCount' else inner_sub.c.total_avail
|
||||
try:
|
||||
num_val = float(value)
|
||||
@ -294,13 +294,13 @@ class MaterialBaseService:
|
||||
continue
|
||||
|
||||
if operator == 'eq':
|
||||
having_conditions.append(col == num_val)
|
||||
filter_conditions.append(col == num_val)
|
||||
elif operator == 'ne':
|
||||
having_conditions.append(col != num_val)
|
||||
filter_conditions.append(col != num_val)
|
||||
elif operator == 'ge':
|
||||
having_conditions.append(col >= num_val)
|
||||
filter_conditions.append(col >= num_val)
|
||||
elif operator == 'le':
|
||||
having_conditions.append(col <= num_val)
|
||||
filter_conditions.append(col <= num_val)
|
||||
else:
|
||||
# 非聚合字段:使用 WHERE
|
||||
column = getattr(MaterialBase, db_field, None)
|
||||
@ -326,14 +326,10 @@ class MaterialBaseService:
|
||||
except ValueError:
|
||||
continue
|
||||
|
||||
# 应用 WHERE 条件
|
||||
# 应用 WHERE 条件(统一使用 filter)
|
||||
if filter_conditions:
|
||||
query = query.filter(and_(*filter_conditions))
|
||||
|
||||
# 应用 HAVING 条件(聚合字段筛选)
|
||||
if having_conditions:
|
||||
query = query.having(and_(*having_conditions))
|
||||
|
||||
# 排序处理(支持全字段)
|
||||
order_by_column = filters.get('orderByColumn', '')
|
||||
is_asc = filters.get('isAsc', None)
|
||||
@ -342,9 +338,8 @@ class MaterialBaseService:
|
||||
enable_warning_sort = filters.get('enableWarningSort', False)
|
||||
|
||||
if enable_warning_sort:
|
||||
print("====== [DEBUG] 成功进入预警强排逻辑 (SQLA 2.0) ======")
|
||||
# 【核心修复】使用子查询列进行预警排序计算
|
||||
# total_inv 现在是 inner_sub.c.total_inv,可以安全参与 case 计算
|
||||
print("====== [DEBUG] 成功进入预警强排逻辑 ======")
|
||||
# 直接在 order_by 中进行计算排序,不污染 select 列
|
||||
inv_val = inner_sub.c.total_inv
|
||||
red_val = cast(MaterialWarningSetting.red_threshold, Numeric)
|
||||
yellow_val = cast(MaterialWarningSetting.yellow_threshold, Numeric)
|
||||
@ -354,26 +349,24 @@ class MaterialBaseService:
|
||||
(and_(MaterialWarningSetting.is_enabled.is_(True), inv_val <= red_val), 2),
|
||||
(and_(MaterialWarningSetting.is_enabled.is_(True), inv_val <= yellow_val), 1),
|
||||
else_=0
|
||||
).label('sort_level')
|
||||
|
||||
# 红色预警时的缺口(库存与红色阈值的差距)
|
||||
)
|
||||
# 红色预警时的缺口
|
||||
red_shortage = case(
|
||||
(and_(MaterialWarningSetting.is_enabled.is_(True), inv_val <= red_val), red_val - inv_val),
|
||||
else_=0
|
||||
).label('sort_red')
|
||||
|
||||
)
|
||||
# 黄色预警时的缺口
|
||||
yellow_distance = case(
|
||||
(and_(MaterialWarningSetting.is_enabled.is_(True), inv_val > red_val, inv_val <= yellow_val), inv_val - red_val),
|
||||
else_=999999
|
||||
).label('sort_yellow')
|
||||
)
|
||||
|
||||
query = query.add_columns(warning_level, red_shortage, yellow_distance)
|
||||
query = query.order_by(None).order_by(
|
||||
text("sort_level DESC"),
|
||||
text("sort_red DESC"),
|
||||
text("sort_yellow ASC"),
|
||||
MaterialBase.id.desc()
|
||||
# 直接在 order_by 中使用 case() 表达式
|
||||
query = query.order_by(
|
||||
desc(warning_level),
|
||||
desc(red_shortage),
|
||||
asc(yellow_distance),
|
||||
desc(MaterialBase.id)
|
||||
)
|
||||
elif order_by_column:
|
||||
# 字段映射 - 使用子查询列进行排序
|
||||
|
||||
Reference in New Issue
Block a user