diff --git a/backend/app/routers/collections.py b/backend/app/routers/collections.py index b369641..4c33183 100644 --- a/backend/app/routers/collections.py +++ b/backend/app/routers/collections.py @@ -365,17 +365,22 @@ def get_stats( expected_profit = target_result - cost_result - # 已售商品统计 - sold_filter = base_filter & (Collection.f01_04_status == 'sold') & (Collection.f05_42_goal_price > 0) + # 已售商品统计 - 处理管理员和普通用户两种情况 + if current_user.role == "admin": + sold_filter = (Collection.f01_04_status == 'sold') & (Collection.f05_42_goal_price > 0) + else: + sold_filter = (Collection.f99_91_user_id == current_user.f99_90_id) & (Collection.f01_04_status == 'sold') & (Collection.f05_42_goal_price > 0) total_revenue = db.query(func.sum(Collection.f05_42_goal_price)).filter(sold_filter).scalar() or 0 + from sqlalchemy import case total_profit = db.query( func.sum( - (Collection.f05_42_goal_price or 0) - - (Collection.f05_40_cost_price or 0) - - (Collection.f05_43_repair_fee or 0) - - (Collection.f05_44_grading_fee or 0) + case( + (Collection.f05_42_goal_price.isnot(None), + Collection.f05_42_goal_price - func.coalesce(Collection.f05_40_cost_price, 0) - func.coalesce(Collection.f05_43_repair_fee, 0) - func.coalesce(Collection.f05_44_grading_fee, 0)), + else_=0 + ) ) ).filter(sold_filter).scalar() or 0