Commit 13164fab by zhouhaibao

refactor(data-report): 优化财务成本分项SQL查询逻辑

- 实现两层GROUP BY结构避免JOIN笛卡尔乘积导致的数据重复计算
- 内层按merchant_name + order_code分组处理子订单级和订单级字段聚合
- 外层按merchant_name分组进行跨订单数据汇总
- 统一产品成本口径为upgrade_shop_price - base_shop_price计算方式
- 优化字段别名定义提升查询可读性
- 更新注释说明关键实现逻辑和业务规则变化
1 parent f5007c9d
...@@ -5,13 +5,21 @@ ...@@ -5,13 +5,21 @@
-- 业务: business_type = 1 OR 2 (轻改不区分) -- 业务: business_type = 1 OR 2 (轻改不区分)
-- 日期: 支付成功时间 po.success_date BETWEEN :start AND :end (含 fee 子查询同步) -- 日期: 支付成功时间 po.success_date BETWEEN :start AND :end (含 fee 子查询同步)
-- --
-- 关键: 两层 GROUP BY 避免 JOIN 笛卡尔乘积 (同店中店)
-- gopa 是子订单级 (一个 order_code 多条 gopa 记录), fee/gopa2 是订单级
-- 直接 LEFT JOIN 会导致 fee.total / gopa2 字段被重复 SUM 多次
-- 内层按 merchant_name + order_code GROUP BY
-- - gopa/gso 级字段 → SUM(同订单多 sub_order_code 要累加)
-- - order 级字段(fee.total) → MAX(避免 JOIN 膨胀)
-- 外层按 merchant_name GROUP BY → 全部 SUM 跨订单聚合
--
-- 与"轻改店中店"的区别: -- 与"轻改店中店"的区别:
-- 园区店只消费 4 个字段 (不需要窗膜/车衣/精品/原厂/副厂/施工收入等): -- 园区店只消费 4 个字段 (不需要窗膜/车衣/精品/原厂/副厂/施工收入等):
-- 产品成本(财务列表) = dj_order_type != 6 -- 产品成本(财务列表) = dj_order_type != 6 → upgrade_shop_price - base_shop_price
-- 内部结算成本(财务列表) = dj_order_type = 6 -- 内部结算成本(财务列表) = dj_order_type = 6 → upgrade_shop_price - base_shop_price
-- 流量费(财务列表) = dj_order_type != 6 的 shop_traffic_money -- 流量费(财务列表) = dj_order_type != 6 的 shop_traffic_money
-- 市场费(加权测算) = 四项费用累计 -- 市场费(加权测算) = 四项费用累计 (fee 子查询, 内层用 MAX)
-- 字段口径/表关联/商品成本公式(upgrade_shop_price - base_shop_price)与店中店完全相同 -- 字段口径/表关联与店中店完全相同
-- ============================================================================ -- ============================================================================
-- ========= 查询参数 ========= -- ========= 查询参数 =========
...@@ -20,61 +28,77 @@ SET @end_time = '2026-08-31 23:59:59'; ...@@ -20,61 +28,77 @@ SET @end_time = '2026-08-31 23:59:59';
SET @scene_type = 1; -- 固定: 1=轻改 SET @scene_type = 1; -- 固定: 1=轻改
SELECT SELECT
go.merchant_name AS `门店名称`, t.merchant_name AS `门店名称`,
-- ===== 产品成本口径: upgrade_shop_price - base_shop_price ===== -- ===== 产品成本口径: upgrade_shop_price - base_shop_price =====
-- 外层 SUM 跨订单聚合, 内层已按 merchant_name + order_code 聚合好
-- 产品成本(财务列表) = 非内部结算 dj_order_type != 6 SUM(t.product_cost_finance) AS `产品成本(财务列表)`,
SUM(CASE WHEN gso.dj_order_type != 6 SUM(t.internal_settle_cost) AS `内部结算成本(财务列表)`,
THEN gso.upgrade_shop_price - gso.base_shop_price SUM(t.store_share) AS `流量费(财务列表)`,
ELSE 0 END) AS `产品成本(财务列表)`,
-- 内部结算成本(财务列表) = 内部结算 dj_order_type = 6 -- order 级字段: 内层 MAX 避免 JOIN 膨胀, 外层 SUM 跨订单聚合
SUM(CASE WHEN gso.dj_order_type = 6 SUM(t.market_fee) AS `市场费`
THEN gso.upgrade_shop_price - gso.base_shop_price
ELSE 0 END) AS `内部结算成本(财务列表)`,
-- 流量费(财务列表) = 非内部结算的 shop_traffic_money FROM (
SUM(CASE WHEN gso.dj_order_type != 6 SELECT
THEN IFNULL(gopa.shop_traffic_money, 0) go.merchant_name,
ELSE 0 END) AS `流量费(财务列表)`, gopa.order_code,
-- 市场费(加权测算) = 四项费用累计 (fee 子查询) -- ===== 内层: 按 merchant_name + order_code GROUP BY =====
SUM(IFNULL(fee.total, 0)) AS `市场费` -- gopa/gso 子订单级: 同订单多 sub_order_code → SUM 累加
FROM dj_car_life_goods_order_profit_allocation gopa SUM(CASE WHEN gso.dj_order_type != 6
INNER JOIN dj_car_life_goods_order go THEN gso.upgrade_shop_price - gso.base_shop_price
ON go.order_code = gopa.order_code AND go.deleted_flag = 'N' ELSE 0 END) AS product_cost_finance,
INNER JOIN dj_car_life_pay_order po
ON go.order_code = po.order_code AND po.deleted_flag = 'N' SUM(CASE WHEN gso.dj_order_type = 6
LEFT JOIN dj_car_life_goods_sub_order gso THEN gso.upgrade_shop_price - gso.base_shop_price
ON gopa.sub_order_code = gso.sub_order_code AND gso.deleted_flag = 'N' ELSE 0 END) AS internal_settle_cost,
LEFT JOIN dj_car_life_goods_order_profit_allocation gopa2
ON gopa2.order_code = gopa.order_code SUM(CASE WHEN gso.dj_order_type != 6
AND gopa2.order_flag = 'Y' THEN IFNULL(gopa.shop_traffic_money, 0)
AND gopa2.grain_size_type = 1 ELSE 0 END) AS store_share,
AND gopa2.deleted_flag = 'N'
LEFT JOIN ( -- order 级: LEFT JOIN 被 gopa 多行放大 → MAX 取单次值
-- fee 子查询: 日期 + scene_type 与外层同步 MAX(IFNULL(fee.total, 0)) AS market_fee
SELECT
ecc.order_code, FROM dj_car_life_goods_order_profit_allocation gopa
SUM(ecc.expense_category_result) AS total INNER JOIN dj_car_life_goods_order go
FROM dj_car_life_order_expense_category_calculated ecc ON go.order_code = gopa.order_code AND go.deleted_flag = 'N'
INNER JOIN dj_car_life_goods_order go ON ecc.order_code = go.order_code INNER JOIN dj_car_life_pay_order po
INNER JOIN dj_car_life_pay_order po ON go.order_code = po.order_code ON go.order_code = po.order_code AND po.deleted_flag = 'N'
WHERE ecc.deleted_flag = 'N' LEFT JOIN dj_car_life_goods_sub_order gso
AND go.deleted_flag = 'N' ON gopa.sub_order_code = gso.sub_order_code AND gso.deleted_flag = 'N'
AND po.deleted_flag = 'N' LEFT JOIN dj_car_life_goods_order_profit_allocation gopa2
AND ecc.expense_category_id IN (706, 808, 809, 901) ON gopa2.order_code = gopa.order_code
AND gopa2.order_flag = 'Y'
AND gopa2.grain_size_type = 1
AND gopa2.deleted_flag = 'N'
LEFT JOIN (
-- fee 子查询: 日期 + scene_type 与外层同步 (轻改无 business_type 过滤)
SELECT
ecc.order_code,
SUM(ecc.expense_category_result) AS total
FROM dj_car_life_order_expense_category_calculated ecc
INNER JOIN dj_car_life_goods_order go ON ecc.order_code = go.order_code
INNER JOIN dj_car_life_pay_order po ON go.order_code = po.order_code
WHERE ecc.deleted_flag = 'N'
AND go.deleted_flag = 'N'
AND po.deleted_flag = 'N'
AND ecc.expense_category_id IN (706, 808, 809, 901)
AND po.success_date >= @start_time
AND po.success_date <= @end_time
AND go.scene_type = @scene_type
GROUP BY ecc.order_code
) fee ON fee.order_code = gopa.order_code
WHERE gopa.deleted_flag = 'N'
AND gopa.order_flag = 'N'
AND go.scene_type = @scene_type
AND po.success_date >= @start_time AND po.success_date >= @start_time
AND po.success_date <= @end_time AND po.success_date <= @end_time
AND go.scene_type = @scene_type
GROUP BY ecc.order_code
) fee ON fee.order_code = gopa.order_code
WHERE gopa.deleted_flag = 'N' GROUP BY go.merchant_name, gopa.order_code
AND gopa.order_flag = 'N' ) t
AND go.scene_type = @scene_type GROUP BY t.merchant_name;
AND po.success_date >= @start_time
AND po.success_date <= @end_time
GROUP BY go.merchant_name;
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!