🤖 每日自动备份 - 2026-04-29 08:00:01
This commit is contained in:
parent
40bae07dd6
commit
82a5f07fa0
@ -12,4 +12,4 @@ vala-component-practice-stat 8e768e2641019d27bd41f4647d2d90f24182a0554dad5ad9f41
|
||||
cron-schedule e103cbb1806b28c891b9c856963325086ecaff32edec208f0a841865f26e8f3e
|
||||
refund-user-learning-analysis 648fd4ae2b29167fd66eab4245bdaaef00242db3131f4919cc02f07ca2a9b59c
|
||||
phone-chapter-query ac429b4da5a89db16efdf1066edf4ecb1c050b93aff20dd4c652af5f5568e44f
|
||||
vala-order-amortization-stat 9363fb9e3b3f001493b1a191df283ee287526ed4ed8cd1748f300fb332425c83
|
||||
vala-order-amortization-stat de5f77b9e4ab865df5efe595e59e877257ac489a195fb969b9d20d1a71158b97
|
||||
|
||||
5
memory/2026-04-28-0306.md
Normal file
5
memory/2026-04-28-0306.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Session: 2026-04-28 03:06:34 UTC
|
||||
|
||||
- **Session Key**: agent:main:feishu:direct:ou_e63ce6b760ad39382852472f28fbe2a2
|
||||
- **Session ID**: 8b82aba4-e718-4230-ae03-cdca489297a3
|
||||
- **Source**: feishu
|
||||
@ -56,7 +56,7 @@ vala-order-amortization-stat/
|
||||
- 默认输出维度:账期整体汇总,无需按天/周/月拆分,无需分渠道统计
|
||||
### 步骤2:数据过滤规则
|
||||
1. 订单范围:
|
||||
- 2025-09-01 至 账期结束日 内创建的所有订单
|
||||
- 2025-06-01 至 账期结束日 内创建的所有订单
|
||||
- bi_vala_order.status IN (3,4)(已完成、已退款订单)
|
||||
- 订单实际支付金额≥10元:bi_vala_order.pay_amount_int ≥ 1000(单位:分)
|
||||
- 关联bi_vala_app_account表(不限制status,不剔除测试账号)
|
||||
|
||||
@ -4,7 +4,6 @@ all_refund_records AS (
|
||||
SELECT
|
||||
out_trade_no AS order_no,
|
||||
SUM(refund_amount_int) / 100 AS total_refund_amount,
|
||||
MAX(CASE WHEN refund_type = 2 THEN 1 ELSE 0 END) AS is_full_refund,
|
||||
MAX(refund_type) AS refund_type,
|
||||
MAX(DATE(updated_at)) AS latest_refund_date
|
||||
FROM bi_refund_order
|
||||
@ -29,14 +28,15 @@ order_base AS (
|
||||
o.sale_channel,
|
||||
CASE WHEN ar.order_no IS NOT NULL THEN 1 ELSE 0 END AS has_refund,
|
||||
COALESCE(ar.total_refund_amount, 0) AS total_refund_amount,
|
||||
COALESCE(ar.is_full_refund, 0) AS is_full_refund,
|
||||
CASE WHEN ar.order_no IS NOT NULL AND COALESCE(ar.total_refund_amount, 0) >= o.pay_amount_int / 100 THEN 1 ELSE 0 END AS is_full_refund,
|
||||
ar.refund_type,
|
||||
ar.latest_refund_date
|
||||
FROM bi_vala_order o
|
||||
JOIN bi_vala_app_account a ON o.account_id = a.id
|
||||
LEFT JOIN all_refund_records ar ON o.out_trade_no = ar.order_no
|
||||
WHERE
|
||||
o.created_at >= '2025-09-01'
|
||||
o.created_at >= '2025-06-01'
|
||||
AND o.created_at <= '{period_end}'::date + INTERVAL '1 day' - INTERVAL '1 second'
|
||||
AND o.order_status IN (3,4)
|
||||
AND o.pay_amount_int >= 1000
|
||||
),
|
||||
@ -102,6 +102,12 @@ order_classified AS (
|
||||
-- 均摊有效结束日
|
||||
LEAST(owc.amortization_end_date, '{period_end}'::date) AS eff_end,
|
||||
|
||||
-- 判断是否为试用期内退费(退费日 < 转正日,从未产生均摊,不应触发冲销)
|
||||
CASE WHEN owc.has_refund = 1
|
||||
AND owc.latest_refund_date < DATE(owc.amortization_start_date)
|
||||
THEN 1 ELSE 0
|
||||
END AS is_trial_period_refund,
|
||||
|
||||
-- 判断是否为账期前全额退费(场景F:排除)
|
||||
CASE WHEN owc.has_refund = 1 AND owc.is_full_refund = 1
|
||||
AND owc.latest_refund_date < '{period_start}'::date
|
||||
@ -181,6 +187,7 @@ SELECT
|
||||
CASE
|
||||
WHEN is_pre_period_full_refund = 1 THEN '账期前全额退费'
|
||||
WHEN is_pre_period_partial_refund = 1 THEN '账期前部分退费'
|
||||
WHEN is_trial_period_refund = 1 AND is_full_refund = 0 THEN '试用期内部分退费'
|
||||
WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 1 THEN '账期内下单全额退费'
|
||||
WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 0 THEN '历史下单账期内全额退费'
|
||||
WHEN is_current_period_refund = 1 AND is_full_refund = 0 AND is_ordered_in_period = 1 THEN '账期内下单部分退费'
|
||||
@ -198,25 +205,28 @@ SELECT
|
||||
-- 历史均摊金额(基于原始日均摊)
|
||||
ROUND((historical_amorted_days * original_daily_amort_amount)::numeric, 2) AS "历史均摊金额",
|
||||
|
||||
-- 冲销历史均摊(场景B/E:历史下单+账期内退费)
|
||||
-- 冲销历史均摊(场景B/E:历史下单+账期内退费,排除试用期内退费)
|
||||
ROUND(
|
||||
CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0 AND historical_amorted_days > 0
|
||||
AND is_trial_period_refund = 0
|
||||
THEN -(historical_amorted_days * original_daily_amort_amount)
|
||||
ELSE 0
|
||||
END::numeric, 2
|
||||
) AS "冲销历史均摊",
|
||||
|
||||
-- 冲销原税款(场景B/E:历史下单+账期内退费)
|
||||
-- 冲销原税款(场景B/E:历史下单+账期内退费,排除试用期内退费)
|
||||
ROUND(
|
||||
CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
|
||||
AND is_trial_period_refund = 0
|
||||
THEN -tax_amount
|
||||
ELSE 0
|
||||
END::numeric, 2
|
||||
) AS "冲销原税款",
|
||||
|
||||
-- 冲销未确认收入(场景B/E:历史下单+账期内退费)
|
||||
-- 冲销未确认收入(场景B/E:历史下单+账期内退费,排除试用期内退费)
|
||||
ROUND(
|
||||
CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
|
||||
AND is_trial_period_refund = 0
|
||||
THEN -(after_tax_amount - historical_amorted_days * original_daily_amort_amount)
|
||||
ELSE 0
|
||||
END::numeric, 2
|
||||
@ -227,6 +237,12 @@ SELECT
|
||||
CASE
|
||||
-- 场景F:账期前全额退费,不统计
|
||||
WHEN is_pre_period_full_refund = 1 THEN 0
|
||||
-- 试用期内全额退费,无均摊
|
||||
WHEN is_trial_period_refund = 1 AND is_full_refund = 1 THEN 0
|
||||
-- 试用期内部分退费,按退后金额从转正日正常均摊
|
||||
WHEN is_trial_period_refund = 1 AND is_full_refund = 0 THEN
|
||||
CASE WHEN total_cycle_days > 0 AND period_amort_days > 0
|
||||
THEN daily_amort_amount * period_amort_days ELSE 0 END
|
||||
-- 场景D:账期内下单+全额退费,无均摊
|
||||
WHEN is_current_period_refund = 1 AND is_full_refund = 1 AND is_ordered_in_period = 1 THEN 0
|
||||
-- 场景E:历史下单+账期内全额退费,无退后均摊
|
||||
@ -298,4 +314,5 @@ SELECT
|
||||
END AS "剩余周期"
|
||||
|
||||
FROM order_with_days
|
||||
WHERE is_pre_period_full_refund = 0;
|
||||
WHERE is_pre_period_full_refund = 0
|
||||
AND NOT (is_trial_period_refund = 1 AND is_full_refund = 1);
|
||||
|
||||
@ -4,7 +4,6 @@ all_refund_records AS (
|
||||
SELECT
|
||||
out_trade_no AS order_no,
|
||||
SUM(refund_amount_int) / 100 AS total_refund_amount,
|
||||
MAX(CASE WHEN refund_type = 2 THEN 1 ELSE 0 END) AS is_full_refund,
|
||||
MAX(refund_type) AS refund_type,
|
||||
MAX(DATE(updated_at)) AS latest_refund_date
|
||||
FROM bi_refund_order
|
||||
@ -29,14 +28,15 @@ order_base AS (
|
||||
o.sale_channel,
|
||||
CASE WHEN ar.order_no IS NOT NULL THEN 1 ELSE 0 END AS has_refund,
|
||||
COALESCE(ar.total_refund_amount, 0) AS total_refund_amount,
|
||||
COALESCE(ar.is_full_refund, 0) AS is_full_refund,
|
||||
CASE WHEN ar.order_no IS NOT NULL AND COALESCE(ar.total_refund_amount, 0) >= o.pay_amount_int / 100 THEN 1 ELSE 0 END AS is_full_refund,
|
||||
ar.refund_type,
|
||||
ar.latest_refund_date
|
||||
FROM bi_vala_order o
|
||||
JOIN bi_vala_app_account a ON o.account_id = a.id
|
||||
LEFT JOIN all_refund_records ar ON o.out_trade_no = ar.order_no
|
||||
WHERE
|
||||
o.created_at >= '2025-09-01'
|
||||
o.created_at >= '2025-06-01'
|
||||
AND o.created_at <= '{period_end}'::date + INTERVAL '1 day' - INTERVAL '1 second'
|
||||
AND o.order_status IN (3,4)
|
||||
AND o.pay_amount_int >= 1000
|
||||
),
|
||||
@ -83,6 +83,10 @@ order_classified AS (
|
||||
owc.*,
|
||||
GREATEST(owc.amortization_start_date, '{period_start}'::date) AS eff_start,
|
||||
LEAST(owc.amortization_end_date, '{period_end}'::date) AS eff_end,
|
||||
-- 试用期内退费(退费日 < 转正日,从未产生均摊,不应触发冲销)
|
||||
CASE WHEN owc.has_refund = 1
|
||||
AND owc.latest_refund_date < DATE(owc.amortization_start_date)
|
||||
THEN 1 ELSE 0 END AS is_trial_period_refund,
|
||||
CASE WHEN owc.has_refund = 1 AND owc.is_full_refund = 1 AND owc.latest_refund_date < '{period_start}'::date
|
||||
THEN 1 ELSE 0 END AS is_pre_period_full_refund,
|
||||
CASE WHEN owc.has_refund = 1 AND owc.is_full_refund = 0 AND owc.latest_refund_date < '{period_start}'::date
|
||||
@ -129,25 +133,28 @@ summary_calc AS (
|
||||
COUNT(CASE WHEN order_date >= '{period_start}'::date AND order_date <= '{period_end}'::date
|
||||
AND amortization_start_date > '{period_end}'::date THEN 1 END) AS trial_orders,
|
||||
|
||||
-- 冲销历史均摊(场景B/E:历史下单+账期内退费)
|
||||
-- 冲销历史均摊(场景B/E:历史下单+账期内退费,排除试用期内退费)
|
||||
ROUND(SUM(
|
||||
CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0 AND historical_amorted_days > 0
|
||||
AND is_trial_period_refund = 0
|
||||
THEN historical_amorted_days * original_daily_amort_amount
|
||||
ELSE 0
|
||||
END
|
||||
)::numeric, 2) AS writeoff_amort,
|
||||
|
||||
-- 冲销原税款(场景B/E:历史下单+账期内退费)
|
||||
-- 冲销原税款(场景B/E:历史下单+账期内退费,排除试用期内退费)
|
||||
ROUND(SUM(
|
||||
CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
|
||||
AND is_trial_period_refund = 0
|
||||
THEN tax_amount
|
||||
ELSE 0
|
||||
END
|
||||
)::numeric, 2) AS writeoff_tax,
|
||||
|
||||
-- 冲销未确认收入(场景B/E:历史下单+账期内退费)
|
||||
-- 冲销未确认收入(场景B/E:历史下单+账期内退费,排除试用期内退费)
|
||||
ROUND(SUM(
|
||||
CASE WHEN is_current_period_refund = 1 AND is_ordered_in_period = 0
|
||||
AND is_trial_period_refund = 0
|
||||
THEN after_tax_amount - historical_amorted_days * original_daily_amort_amount
|
||||
ELSE 0
|
||||
END
|
||||
@ -157,6 +164,10 @@ summary_calc AS (
|
||||
ROUND(SUM(
|
||||
CASE
|
||||
WHEN is_pre_period_full_refund = 1 THEN 0
|
||||
WHEN is_trial_period_refund = 1 AND is_full_refund = 1 THEN 0 -- 试用期内全额退费,无均摊
|
||||
WHEN is_trial_period_refund = 1 AND is_full_refund = 0 THEN -- 试用期内部分退费,按退后金额从转正日正常均摊
|
||||
CASE WHEN total_cycle_days > 0 AND period_amort_days > 0
|
||||
THEN daily_amort_amount * period_amort_days ELSE 0 END
|
||||
WHEN is_current_period_refund = 1 AND is_full_refund = 1 THEN 0
|
||||
-- 场景A:账期内下单+部分退费
|
||||
WHEN is_current_period_refund = 1 AND is_full_refund = 0 AND is_ordered_in_period = 1 THEN
|
||||
|
||||
Loading…
Reference in New Issue
Block a user