-- 福利品用户筛选：第一步 - 查询符合条件的用户和订单
-- 修正版：去重 + 排除异常低价订单（pay_amount_int < 100000，即<1000元）

WITH 
target_orders AS (
  SELECT 
    o.account_id,
    o.trade_no,
    o.goods_id,
    o.goods_name,
    o.key_from,
    o.pay_success_date,
    o.pay_amount_int,
    o.order_status
  FROM bi_vala_order o
  JOIN bi_vala_app_account a ON o.account_id = a.id AND a.status = 1
  WHERE o.goods_id IN (31, 60, 61)
    AND o.order_status IN (3, 4)
    AND o.pay_success_date IS NOT NULL
    AND o.pay_amount_int >= 100000  -- 排除异常低价订单
),
refund_info AS (
  SELECT trade_no, refund_amount_int
  FROM bi_refund_order
  WHERE status = 3
),
order_with_refund AS (
  SELECT 
    t.*,
    r.refund_amount_int AS refund_amt,
    CASE 
      WHEN r.refund_amount_int IS NULL THEN 'none'
      WHEN r.refund_amount_int < t.pay_amount_int THEN 'partial'
      ELSE 'full'
    END AS refund_type
  FROM target_orders t
  LEFT JOIN refund_info r ON t.trade_no = r.trade_no
),

-- 情况1: goods_id=61, 无任何退费
case1_users AS (
  SELECT DISTINCT account_id FROM order_with_refund
  WHERE goods_id = 61 AND refund_type = 'none'
),

-- 情况2: goods_id IN (31,60), >=2笔有效订单, 最多一笔400元部分退费
case2_eligible AS (
  SELECT account_id
  FROM order_with_refund
  WHERE goods_id IN (31, 60) AND refund_type != 'full'
  GROUP BY account_id
  HAVING COUNT(*) >= 2
     AND SUM(CASE WHEN refund_type = 'partial' AND refund_amt = 40000 THEN 1 ELSE 0 END) <= 1
     AND SUM(CASE WHEN refund_type = 'partial' AND refund_amt != 40000 THEN 1 ELSE 0 END) = 0
),
case2_users AS (
  SELECT DISTINCT account_id FROM case2_eligible
),

-- 情况3: goods_id IN (31,60) 无退费 + goods_id=61 退费1999
case3_has_31_60 AS (
  SELECT DISTINCT account_id FROM order_with_refund
  WHERE goods_id IN (31, 60) AND refund_type = 'none'
),
case3_has_61_1999 AS (
  SELECT DISTINCT account_id FROM order_with_refund
  WHERE goods_id = 61 AND refund_type = 'partial' AND refund_amt = 199900
),
case3_users AS (
  SELECT a.account_id
  FROM case3_has_31_60 a
  JOIN case3_has_61_1999 b ON a.account_id = b.account_id
),

-- 合并所有符合条件的用户，记录每个用户属于哪些情况
all_eligible_users AS (
  SELECT account_id, '情况1' AS case_type FROM case1_users
  UNION
  SELECT account_id, '情况2' AS case_type FROM case2_users
  UNION
  SELECT account_id, '情况3' AS case_type FROM case3_users
),

-- 按用户汇总归属情况
user_cases AS (
  SELECT 
    account_id,
    STRING_AGG(DISTINCT case_type, '+' ORDER BY case_type) AS case_labels
  FROM all_eligible_users
  GROUP BY account_id
),

-- 所有符合条件的订单（去重）
eligible_orders AS (
  -- 情况1的订单
  SELECT * FROM order_with_refund WHERE goods_id = 61 AND refund_type = 'none' AND account_id IN (SELECT account_id FROM case1_users)
  UNION
  -- 情况2的订单
  SELECT * FROM order_with_refund WHERE goods_id IN (31, 60) AND refund_type != 'full' AND account_id IN (SELECT account_id FROM case2_users)
  UNION
  -- 情况3的订单
  SELECT * FROM order_with_refund 
  WHERE (goods_id IN (31, 60) AND refund_type = 'none' AND account_id IN (SELECT account_id FROM case3_users))
     OR (goods_id = 61 AND refund_type = 'partial' AND refund_amt = 199900 AND account_id IN (SELECT account_id FROM case3_users))
)

SELECT 
  eo.account_id AS "用户ID",
  eo.trade_no AS "交易号",
  eo.goods_id AS "商品ID",
  eo.goods_name AS "商品名称",
  eo.key_from AS "渠道",
  TO_CHAR(eo.pay_success_date, 'YYYY-MM-DD HH24:MI:SS') AS "购课日期",
  eo.pay_amount_int / 100.0 AS "支付金额(元)",
  eo.refund_amt / 100.0 AS "退款金额(元)",
  eo.refund_type AS "退费状态",
  uc.case_labels AS "来源情况"
FROM eligible_orders eo
JOIN user_cases uc ON eo.account_id = uc.account_id
ORDER BY eo.account_id, eo.goods_id, eo.trade_no;
