From b2a25a236f579f535c940342488a51602b8b0463 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E8=9F=91=E8=9E=82?= Date: Wed, 3 Jun 2026 20:51:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(order-stat,workbench):=20=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E7=BB=9F=E8=AE=A1=E6=97=A5=E8=A1=A8=E6=89=A9=E5=B1=95?= =?UTF-8?q?=20GMV/=E9=94=80=E5=94=AE=E5=8D=95=E9=87=8F=E4=BB=B6=E6=95=B0?= =?UTF-8?q?=E5=B9=B6=E8=B4=AF=E9=80=9A=E5=88=B7=E6=96=B0=E8=AF=BB=E5=86=99?= =?UTF-8?q?=EF=BC=8C=E5=B7=A5=E4=BD=9C=E5=8F=B0=E7=BB=8F=E8=90=A5=20KPI=20?= =?UTF-8?q?=E6=89=A9=E8=87=B3=2022=20=E9=A1=B9=E5=8F=8A=E5=89=8D=E7=AB=AF?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ecom-dmb-backend/sql/ec_order_stat_design.sql | 122 +++++--- ...07__ec_order_stat_kpi_quantity_columns.sql | 57 ++++ .../ecom/order/mapper/OrderStatAggMapper.java | 3 + .../stat/mapper/OrderStatRefreshMapper.java | 5 +- .../stat/model/dto/OrderStatMetricsDTO.java | 5 +- .../model/po/OrderStatDailyProductPO.java | 12 + .../stat/model/po/OrderStatDailyShopPO.java | 6 + .../stat/model/po/OrderStatDailySpecPO.java | 13 +- .../stat/service/OrderStatReadFacade.java | 29 +- .../stat/service/OrderStatRefreshService.java | 25 +- .../impl/ProductOverviewServiceImpl.java | 2 +- .../service/impl/WorkbenchServiceImpl.java | 242 +++++++++------- .../mapper/order/OrderStatAggMapper.xml | 13 +- .../order/stat/OrderStatDailyMapper.xml | 152 ++++++---- .../order/stat/OrderStatQueryMapper.xml | 147 ++++------ .../order/stat/OrderStatRefreshMapper.xml | 12 +- .../finance/components/FinanceKpiGrid.vue | 270 +++++++++--------- .../components/FinanceKpiSettingsModal.vue | 200 +++++++++++++ .../tabs/finance/finance-kpi-hints.ts | 45 +++ .../workbench/tabs/finance/index.vue | 53 +++- .../workbench/tabs/finance/kpi-catalog.ts | 134 +++++++++ .../tabs/finance/useFinanceKpiPreferences.ts | 96 +++++++ .../views/dashboard/workbench/utils/money.ts | 4 +- 23 files changed, 1192 insertions(+), 455 deletions(-) create mode 100644 ecom-dmb-backend/sql/migration/V20260607__ec_order_stat_kpi_quantity_columns.sql create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiSettingsModal.vue create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/finance-kpi-hints.ts create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/kpi-catalog.ts create mode 100644 ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/useFinanceKpiPreferences.ts diff --git a/ecom-dmb-backend/sql/ec_order_stat_design.sql b/ecom-dmb-backend/sql/ec_order_stat_design.sql index 2f171216..e291e2bc 100644 --- a/ecom-dmb-backend/sql/ec_order_stat_design.sql +++ b/ecom-dmb-backend/sql/ec_order_stat_design.sql @@ -2,7 +2,7 @@ -- 订单统计读模型:日 / 月 / 年 聚合表设计 -- ============================================================================= -- 目标:支撑 1 天~5 年(1825 天)区间查询与导出,避免在线扫 ec_order 明细。 --- 口径:与 OrderStatusStatSets + OrderStatAggMapper 一致(五维度 + GMV + 件数)。 +-- 口径:与 OrderStatusStatSets + OrderStatAggMapper 一致(成本/退款五维度 + KPI 双三角)。 -- 写入:ec_order 变更 / 导入完成后,按 (company, shop, stat_date) 重算日表,再 rollup 月/年。 -- 详细方案见:docs/订单统计读模型方案.md -- @@ -22,18 +22,31 @@ -- ----------------------------------------------------------------------------- --- 一、公共指标列说明(日 / 月 / 年 三张同维度表字段一致,仅时间键不同) +-- 一、公共指标列说明(日 / 月 / 年 同维度表字段一致,仅时间键 stat_date/stat_month/stat_year 不同) -- ----------------------------------------------------------------------------- --- sales_amount 计入销售额的 received_amount 合计(salesStatusIds) --- refund_amount 计入退款的 received_amount 合计(refundStatusIds) --- sales_order_count 计入销售额的订单笔数 --- product_cost 商品成本 × qty(productCostStatusIds) --- freight_cost 运费成本 × qty(freightCostStatusIds) --- other_cost 其他成本 × qty(otherCostStatusIds) --- gmv_amount 全状态 received_amount 合计(仅 shop / spec 层需要 GMV 时保留) --- gmv_order_count 全状态订单笔数(仅 shop 层) --- quantity_sum 销量件数合计(spec 层;qty = IFNULL(NULLIF(quantity,0),1)) --- stat_version 重算批次号,状态配置 rebuild 时递增,便于排查 +-- 【KPI · 实际销售口径】salesStatusIds(订单状态 includeSalesAmount=1) +-- sales_amount 实际销售额:SUM(received_amount) +-- sales_order_count 实际销售单量:符合条件的订单笔数 +-- sales_quantity_sum 实际销量件数:SUM(qty),qty=IFNULL(NULLIF(quantity,0),1) +-- +-- 【KPI · GMV 口径】全订单状态(不按 status_id 过滤) +-- gmv_amount 总 GMV:SUM(received_amount) +-- gmv_order_count GMV 单量:COUNT(*) 订单笔数 +-- gmv_quantity_sum GMV 销量件数:SUM(qty) 全状态 +-- +-- 【利润相关】仍按 OrderStatusStatSets 分维度 +-- refund_amount 计入退款的 received_amount(refundStatusIds) +-- product_cost 商品成本 × qty(productCostStatusIds) +-- freight_cost 运费成本 × qty(freightCostStatusIds) +-- other_cost 其他成本 × qty(otherCostStatusIds) +-- +-- 【各层落表范围】 +-- 店铺 shop :KPI 双三角(6 列)+ 利润相关 → 工作台 KPI / 趋势主读 +-- 商品 product:KPI 双三角(6 列)+ 利润相关 → 多维排行 / 品类商品表 +-- 规格 spec :KPI 双三角(6 列)+ 利润相关 → 商品概览 spec 块 +-- +-- stat_version 重算批次号,状态配置 rebuild 时递增 +-- 存量库升级见:migration/V20260607__ec_order_stat_kpi_quantity_columns.sql -- ----------------------------------------------------------------------------- @@ -49,14 +62,16 @@ CREATE TABLE `ec_order_stat_daily_shop` ( `platform_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '平台ID(写入时快照 ec_shop.platform_id)', `stat_date` date NOT NULL COMMENT '自然日(Asia/Shanghai,与 order_time 的 DATE 一致)', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV(全状态商家实收)', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', - `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT 'GMV(全状态商家实收)', - `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV 订单笔数', `stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', @@ -80,14 +95,16 @@ CREATE TABLE `ec_order_stat_monthly_shop` ( `platform_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '平台ID(月 rollup 时取当月最后写入快照)', `stat_month` date NOT NULL COMMENT '自然月首日,如 2024-03-01', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', - `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT 'GMV', - `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV 订单笔数', `stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', @@ -110,14 +127,16 @@ CREATE TABLE `ec_order_stat_yearly_shop` ( `platform_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '平台ID', `stat_year` smallint NOT NULL COMMENT '自然年,如 2024', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', - `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT 'GMV', - `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV 订单笔数', `stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', @@ -146,9 +165,13 @@ CREATE TABLE `ec_order_stat_daily_product` ( `category_id` bigint NOT NULL DEFAULT 0 COMMENT '品类ID,0=未分类', `stat_date` date NOT NULL COMMENT '自然日', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', @@ -177,9 +200,13 @@ CREATE TABLE `ec_order_stat_monthly_product` ( `category_id` bigint NOT NULL DEFAULT 0 COMMENT '品类ID', `stat_month` date NOT NULL COMMENT '自然月首日', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', @@ -207,9 +234,13 @@ CREATE TABLE `ec_order_stat_yearly_product` ( `category_id` bigint NOT NULL DEFAULT 0 COMMENT '品类ID', `stat_year` smallint NOT NULL COMMENT '自然年', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', @@ -241,10 +272,13 @@ CREATE TABLE `ec_order_stat_daily_spec` ( `product_spec_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '规格ID,0=未绑定', `stat_date` date NOT NULL COMMENT '自然日', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', - `quantity_sum` int NOT NULL DEFAULT 0 COMMENT '销量件数合计', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', @@ -271,10 +305,13 @@ CREATE TABLE `ec_order_stat_monthly_spec` ( `product_spec_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '规格ID', `stat_month` date NOT NULL COMMENT '自然月首日', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', - `quantity_sum` int NOT NULL DEFAULT 0 COMMENT '销量件数合计', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', @@ -300,10 +337,13 @@ CREATE TABLE `ec_order_stat_yearly_spec` ( `product_spec_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '规格ID', `stat_year` smallint NOT NULL COMMENT '自然年', - `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '销售额', + `sales_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '实际销售额', + `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '实际销售单量', + `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV', + `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量', + `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数', `refund_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '退款金额', - `sales_order_count` int NOT NULL DEFAULT 0 COMMENT '销售单量', - `quantity_sum` int NOT NULL DEFAULT 0 COMMENT '销量件数合计', `product_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '商品成本', `freight_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '运费成本', `other_cost` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '其他成本', @@ -503,7 +543,9 @@ CREATE TABLE `ec_order_stat_refresh_queue` ( -- INSERT INTO ec_order_stat_monthly_shop (...) -- SELECT company_id, shop_id, MAX(platform_id), -- DATE_FORMAT(stat_date, '%Y-%m-01'), --- SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), ... +-- SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), +-- SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), +-- SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost) -- FROM ec_order_stat_daily_shop -- WHERE company_id=? AND shop_id=? AND stat_date BETWEEN ? AND ? -- GROUP BY company_id, shop_id, DATE_FORMAT(stat_date, '%Y-%m-01') diff --git a/ecom-dmb-backend/sql/migration/V20260607__ec_order_stat_kpi_quantity_columns.sql b/ecom-dmb-backend/sql/migration/V20260607__ec_order_stat_kpi_quantity_columns.sql new file mode 100644 index 00000000..7f439354 --- /dev/null +++ b/ecom-dmb-backend/sql/migration/V20260607__ec_order_stat_kpi_quantity_columns.sql @@ -0,0 +1,57 @@ +-- ================================================ +-- 订单统计读模型:KPI 双三角指标列(实际销售 + GMV × 金额/单量/件数) +-- 设计说明见 sql/ec_order_stat_design.sql +-- 适用:已执行 V20260606 的存量库;新库若 V20260606 已含下列字段可跳过本脚本 +-- 升级后需对受影响区间触发 rebuild / refresh 回填数据 +-- ================================================ + +-- ---------- 店铺:补实际销量、GMV 销量 ---------- +ALTER TABLE `ec_order_stat_daily_shop` + ADD COLUMN `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数' AFTER `sales_order_count`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +ALTER TABLE `ec_order_stat_monthly_shop` + ADD COLUMN `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数' AFTER `sales_order_count`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +ALTER TABLE `ec_order_stat_yearly_shop` + ADD COLUMN `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数' AFTER `sales_order_count`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +-- ---------- 商品:补 KPI 双三角 ---------- +ALTER TABLE `ec_order_stat_daily_product` + ADD COLUMN `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数' AFTER `sales_order_count`, + ADD COLUMN `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV' AFTER `sales_quantity_sum`, + ADD COLUMN `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量' AFTER `gmv_amount`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +ALTER TABLE `ec_order_stat_monthly_product` + ADD COLUMN `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数' AFTER `sales_order_count`, + ADD COLUMN `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV' AFTER `sales_quantity_sum`, + ADD COLUMN `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量' AFTER `gmv_amount`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +ALTER TABLE `ec_order_stat_yearly_product` + ADD COLUMN `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数' AFTER `sales_order_count`, + ADD COLUMN `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV' AFTER `sales_quantity_sum`, + ADD COLUMN `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量' AFTER `gmv_amount`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +-- ---------- 规格:quantity_sum 更名为 sales_quantity_sum,并补 GMV 三角 ---------- +ALTER TABLE `ec_order_stat_daily_spec` + CHANGE COLUMN `quantity_sum` `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + ADD COLUMN `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV' AFTER `sales_quantity_sum`, + ADD COLUMN `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量' AFTER `gmv_amount`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +ALTER TABLE `ec_order_stat_monthly_spec` + CHANGE COLUMN `quantity_sum` `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + ADD COLUMN `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV' AFTER `sales_quantity_sum`, + ADD COLUMN `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量' AFTER `gmv_amount`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; + +ALTER TABLE `ec_order_stat_yearly_spec` + CHANGE COLUMN `quantity_sum` `sales_quantity_sum` int NOT NULL DEFAULT 0 COMMENT '实际销量件数', + ADD COLUMN `gmv_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '总GMV' AFTER `sales_quantity_sum`, + ADD COLUMN `gmv_order_count` int NOT NULL DEFAULT 0 COMMENT 'GMV单量' AFTER `gmv_amount`, + ADD COLUMN `gmv_quantity_sum` int NOT NULL DEFAULT 0 COMMENT 'GMV销量件数' AFTER `gmv_order_count`; diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/mapper/OrderStatAggMapper.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/mapper/OrderStatAggMapper.java index 420f5794..b45db9d5 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/mapper/OrderStatAggMapper.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/mapper/OrderStatAggMapper.java @@ -110,11 +110,14 @@ public interface OrderStatAggMapper { private Long categoryId; private BigDecimal salesAmount; private Long orderCount; + private Long salesQuantitySum; private BigDecimal refundAmount; private BigDecimal productCost; private BigDecimal freightCost; private BigDecimal otherCost; private BigDecimal gmvAllStatus; + private Long gmvOrderCount; + private Long gmvQuantitySum; } @Data diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/mapper/OrderStatRefreshMapper.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/mapper/OrderStatRefreshMapper.java index a661793a..de803940 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/mapper/OrderStatRefreshMapper.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/mapper/OrderStatRefreshMapper.java @@ -52,7 +52,10 @@ public interface OrderStatRefreshMapper extends BaseMapper shopIds, LocalDate startDate, LocalDate endDate) { if (!usesDailyStatSource()) { - OrderStatusStatSets stat = OrderStatusStatSets.from(List.of()); - List rows = loadShopProductRows(companyId, shopIds, startDate, endDate, stat); + OrderStatusStatSets stat = orderStatusStatResolver.resolve(companyId); + List rows = orderStatAggMapper.aggregateByShopProduct( + companyId, shopIds, startDate.atStartOfDay(), endDate.atTime(23, 59, 59), + OrderStatParamHelper.unionList(stat), OrderStatParamHelper.salesList(stat), + OrderStatParamHelper.refundList(stat), OrderStatParamHelper.productCostList(stat), + OrderStatParamHelper.freightCostList(stat), OrderStatParamHelper.otherCostList(stat)); OrderStatMetricsDTO dto = new OrderStatMetricsDTO(); - BigDecimal sales = BigDecimal.ZERO, refund = BigDecimal.ZERO; + BigDecimal sales = BigDecimal.ZERO, refund = BigDecimal.ZERO, gmv = BigDecimal.ZERO; BigDecimal pc = BigDecimal.ZERO, fc = BigDecimal.ZERO, oc = BigDecimal.ZERO; - int cnt = 0; + int cnt = 0, salesQty = 0, gmvCnt = 0, gmvQty = 0; for (OrderStatAggMapper.ShopProductRow r : rows) { sales = sales.add(nz(r.getSalesAmount())); refund = refund.add(nz(r.getRefundAmount())); + gmv = gmv.add(nz(r.getGmvAllStatus())); pc = pc.add(nz(r.getProductCost())); fc = fc.add(nz(r.getFreightCost())); oc = oc.add(nz(r.getOtherCost())); cnt += r.getOrderCount() != null ? r.getOrderCount().intValue() : 0; + salesQty += r.getSalesQuantitySum() != null ? r.getSalesQuantitySum().intValue() : 0; + gmvCnt += r.getGmvOrderCount() != null ? r.getGmvOrderCount().intValue() : 0; + gmvQty += r.getGmvQuantitySum() != null ? r.getGmvQuantitySum().intValue() : 0; } dto.setSalesAmount(sales); dto.setRefundAmount(refund); dto.setSalesOrderCount(cnt); + dto.setSalesQuantitySum(salesQty); + dto.setGmvAmount(gmv); + dto.setGmvOrderCount(gmvCnt); + dto.setGmvQuantitySum(gmvQty); dto.setProductCost(pc); dto.setFreightCost(fc); dto.setOtherCost(oc); diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/service/OrderStatRefreshService.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/service/OrderStatRefreshService.java index f506f472..04d19310 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/service/OrderStatRefreshService.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/order/stat/service/OrderStatRefreshService.java @@ -118,11 +118,13 @@ public class OrderStatRefreshService { row.setSalesAmount(BigDecimal.ZERO); row.setRefundAmount(BigDecimal.ZERO); row.setSalesOrderCount(0); + row.setSalesQuantitySum(0); row.setProductCost(BigDecimal.ZERO); row.setFreightCost(BigDecimal.ZERO); row.setOtherCost(BigDecimal.ZERO); row.setGmvAmount(BigDecimal.ZERO); row.setGmvOrderCount(0); + row.setGmvQuantitySum(0); row.setStatVersion(0); orderStatDailyMapper.upsertDailyShop(row); } @@ -138,6 +140,8 @@ public class OrderStatRefreshService { BigDecimal oc = BigDecimal.ZERO; BigDecimal gmv = BigDecimal.ZERO; int orderCount = 0; + int salesQty = 0; + int gmvQty = 0; for (OrderStatAggMapper.ShopProductRow r : rows) { sales = sales.add(nz(r.getSalesAmount())); refund = refund.add(nz(r.getRefundAmount())); @@ -146,6 +150,8 @@ public class OrderStatRefreshService { oc = oc.add(nz(r.getOtherCost())); gmv = gmv.add(nz(r.getGmvAllStatus())); orderCount += r.getOrderCount() != null ? r.getOrderCount().intValue() : 0; + salesQty += r.getSalesQuantitySum() != null ? r.getSalesQuantitySum().intValue() : 0; + gmvQty += r.getGmvQuantitySum() != null ? r.getGmvQuantitySum().intValue() : 0; } OrderStatDailyShopPO row = new OrderStatDailyShopPO(); row.setCompanyId(companyId); @@ -155,11 +161,13 @@ public class OrderStatRefreshService { row.setSalesAmount(sales); row.setRefundAmount(refund); row.setSalesOrderCount(orderCount); + row.setSalesQuantitySum(salesQty); row.setProductCost(pc); row.setFreightCost(fc); row.setOtherCost(oc); row.setGmvAmount(gmv); row.setGmvOrderCount(gmvOrderCount); + row.setGmvQuantitySum(gmvQty); row.setStatVersion(0); return row; } @@ -180,6 +188,10 @@ public class OrderStatRefreshService { p.setSalesAmount(nz(r.getSalesAmount())); p.setRefundAmount(nz(r.getRefundAmount())); p.setSalesOrderCount(r.getOrderCount() != null ? r.getOrderCount().intValue() : 0); + p.setSalesQuantitySum(r.getSalesQuantitySum() != null ? r.getSalesQuantitySum().intValue() : 0); + p.setGmvAmount(nz(r.getGmvAllStatus())); + p.setGmvOrderCount(r.getGmvOrderCount() != null ? r.getGmvOrderCount().intValue() : 0); + p.setGmvQuantitySum(r.getGmvQuantitySum() != null ? r.getGmvQuantitySum().intValue() : 0); p.setProductCost(nz(r.getProductCost())); p.setFreightCost(nz(r.getFreightCost())); p.setOtherCost(nz(r.getOtherCost())); @@ -205,7 +217,10 @@ public class OrderStatRefreshService { s.setSalesAmount(nz(r.getSalesAmount())); s.setRefundAmount(nz(r.getRefundAmount())); s.setSalesOrderCount(r.getOrderCount() != null ? r.getOrderCount().intValue() : 0); - s.setQuantitySum(r.getQuantitySum() != null ? r.getQuantitySum() : 0); + s.setSalesQuantitySum(r.getSalesQuantitySum() != null ? r.getSalesQuantitySum() : 0); + s.setGmvAmount(nz(r.getGmvAmount())); + s.setGmvOrderCount(r.getGmvOrderCount() != null ? r.getGmvOrderCount().intValue() : 0); + s.setGmvQuantitySum(r.getGmvQuantitySum() != null ? r.getGmvQuantitySum() : 0); s.setProductCost(nz(r.getProductCost())); s.setFreightCost(nz(r.getFreightCost())); s.setOtherCost(nz(r.getOtherCost())); @@ -218,13 +233,17 @@ public class OrderStatRefreshService { private static boolean isAllZeroProductRow(OrderStatAggMapper.ShopProductRow r) { return nz(r.getSalesAmount()).signum() == 0 && nz(r.getRefundAmount()).signum() == 0 - && (r.getOrderCount() == null || r.getOrderCount() == 0); + && nz(r.getGmvAllStatus()).signum() == 0 + && (r.getOrderCount() == null || r.getOrderCount() == 0) + && (r.getGmvOrderCount() == null || r.getGmvOrderCount() == 0); } private static boolean isAllZeroSpecRow(OrderStatRefreshMapper.SpecDayRow r) { return nz(r.getSalesAmount()).signum() == 0 && nz(r.getRefundAmount()).signum() == 0 - && (r.getOrderCount() == null || r.getOrderCount() == 0); + && nz(r.getGmvAmount()).signum() == 0 + && (r.getOrderCount() == null || r.getOrderCount() == 0) + && (r.getGmvOrderCount() == null || r.getGmvOrderCount() == 0); } private static BigDecimal nz(BigDecimal v) { diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java index 2e47e21e..799bc2c0 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/product/overview/service/impl/ProductOverviewServiceImpl.java @@ -981,7 +981,7 @@ public class ProductOverviewServiceImpl implements ProductOverviewService { item.setSpecCode(spec != null ? spec.getSpecCode() : null); item.setSalesAmount(sales); item.setOrderCount(m.getSalesOrderCount() != null ? m.getSalesOrderCount() : 0); - item.setQuantity(m.getQuantitySum() != null ? m.getQuantitySum() : 0); + item.setQuantity(m.getSalesQuantitySum() != null ? m.getSalesQuantitySum() : 0); item.setProductCost(nz(m.getProductCost())); item.setShippingCost(nz(m.getFreightCost())); item.setOtherCost(nz(m.getOtherCost())); diff --git a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/workbench/service/impl/WorkbenchServiceImpl.java b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/workbench/service/impl/WorkbenchServiceImpl.java index 9f39d3e4..f0b38b0b 100644 --- a/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/workbench/service/impl/WorkbenchServiceImpl.java +++ b/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/modules/ecom/workbench/service/impl/WorkbenchServiceImpl.java @@ -6,6 +6,7 @@ import top.xyquan.ecomdmbbackend.common.exception.BusinessException; import top.xyquan.ecomdmbbackend.modules.ecom.category.model.po.CategoryPO; import top.xyquan.ecomdmbbackend.modules.ecom.category.service.CategoryService; import top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper; +import top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO; import top.xyquan.ecomdmbbackend.modules.ecom.order.stat.service.OrderStatReadFacade; import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatResolver; import top.xyquan.ecomdmbbackend.modules.ecom.order.status.support.OrderStatusStatSets; @@ -107,7 +108,8 @@ public class WorkbenchServiceImpl implements WorkbenchService { SqlAggBundle agg = loadSqlAgg(ctx); ComparePeriodBundle compare = loadCompareFromSql(ctx); - Totals cur = totalsFromOrderRows(agg.orderRows(), agg.promoTotal(), agg.brushTotal(), ctx.taxRate()); + // 店铺日汇总表口径:含 GMV/销售双三角 + 成本;推广/刷单仍用区间聚合 + Totals cur = buildShopPeriodTotals(ctx, ctx.range(), agg.promoTotal(), agg.brushTotal()); WorkbenchFinancePanelVO finance = buildFinancePanelFromSql(ctx, cur, compare, agg); @@ -220,18 +222,11 @@ public class WorkbenchServiceImpl implements WorkbenchService { return new SqlAggBundle(orderRows, dailyRows, promotionRows, brushRows); } - /** SQL-3 + 推广/刷单三段时间 KPI */ + /** 环比/同比:店铺汇总表 + 各段推广/刷单 */ private ComparePeriodBundle loadCompareFromSql(ShopContext ctx) { - OrderStatusStatSets stat = ctx.stat(); DateRange lastRange = lastMonthSameSpan(ctx.range()); DateRange yoyRange = new DateRange(ctx.range().startDate().minusYears(1), ctx.range().endDate().minusYears(1)); - OrderStatAggMapper.PeriodKpiRow orderKpi = orderStatReadFacade.loadPeriodKpi( - ctx.companyId(), ctx.shopIds(), - ctx.range().startDate(), ctx.range().endDate(), - lastRange.startDate(), lastRange.endDate(), - yoyRange.startDate(), yoyRange.endDate(), stat); - LocalDate feeScanStart = minDate(lastRange.startDate(), yoyRange.startDate(), ctx.range().startDate()); LocalDate feeScanEnd = maxDate(lastRange.endDate(), yoyRange.endDate(), ctx.range().endDate()); OrderStatAggMapper.FeeKpiRow promoKpi = orderStatReadFacade.loadPromotionPeriodKpi( @@ -247,10 +242,24 @@ public class WorkbenchServiceImpl implements WorkbenchService { lastRange.startDateTime(), lastRange.endDateTime(), yoyRange.startDateTime(), yoyRange.endDateTime()); - return new ComparePeriodBundle(lastRange, yoyRange, - totalsFromPeriodKpi(orderKpi, promoKpi, brushKpi, PeriodSlice.PREV, ctx.taxRate()), - totalsFromPeriodKpi(orderKpi, promoKpi, brushKpi, PeriodSlice.YOY, ctx.taxRate()), - nz(orderKpi.getCurGmvAllStatus()), nz(orderKpi.getPrevGmvAllStatus()), nz(orderKpi.getYoyGmvAllStatus())); + Totals prev = buildShopPeriodTotals(ctx, lastRange, nz(promoKpi.getPrevAmount()), nz(brushKpi.getPrevAmount())); + Totals yoy = buildShopPeriodTotals(ctx, yoyRange, nz(promoKpi.getYoyAmount()), nz(brushKpi.getYoyAmount())); + return new ComparePeriodBundle(lastRange, yoyRange, prev, yoy); + } + + /** 单时间段店铺 KPI:读 ec_order_stat_daily_shop 合计 */ + private Totals buildShopPeriodTotals(ShopContext ctx, DateRange range, BigDecimal promotion, BigDecimal brush) { + OrderStatMetricsDTO m = orderStatReadFacade.sumShopMetrics( + ctx.companyId(), ctx.shopIds(), range.startDate(), range.endDate()); + return buildTotals( + nz(m.getSalesAmount()), nz(m.getRefundAmount()), + nz(m.getProductCost()), nz(m.getFreightCost()), nz(m.getOtherCost()), + promotion, brush, ctx.taxRate(), + m.getSalesOrderCount() != null ? m.getSalesOrderCount() : 0, + m.getSalesQuantitySum() != null ? m.getSalesQuantitySum() : 0, + nz(m.getGmvAmount()), + m.getGmvOrderCount() != null ? m.getGmvOrderCount() : 0, + m.getGmvQuantitySum() != null ? m.getGmvQuantitySum() : 0); } private WorkbenchFinancePanelVO buildFinancePanelFromSql(ShopContext ctx, Totals cur, @@ -261,8 +270,7 @@ public class WorkbenchServiceImpl implements WorkbenchService { platformAgg.computeIfAbsent(String.valueOf(pk), k -> new Agg()).addFromAggRow(row); } return WorkbenchFinancePanelVO.builder() - .kpis(buildKpis(cur, compare.prev(), compare.yoy(), ctx.range(), ctx.taxRate(), - compare.totalGmvCur(), compare.totalGmvPrev(), compare.totalGmvYoy())) + .kpis(buildKpis(cur, compare.prev(), compare.yoy(), ctx.range(), ctx.taxRate())) .gmvTrend(buildTrendFromDaily(agg.dailyRows(), ctx.range(), cur)) .platformSalesShare(buildPlatformSalesSlices(platformAgg, loadPlatformNames(ctx.shops()), cur.totalSales())) .momCompareDateStart(compare.lastRange().startDate().toString()) @@ -316,35 +324,29 @@ public class WorkbenchServiceImpl implements WorkbenchService { oc = oc.add(nz(row.getOtherCost())); orderCount += row.getOrderCount() != null ? row.getOrderCount().intValue() : 0; } - return buildTotals(sales, refund, pc, fc, oc, promotion, brush, taxRate, orderCount); - } - - private Totals totalsFromPeriodKpi(OrderStatAggMapper.PeriodKpiRow o, OrderStatAggMapper.FeeKpiRow promo, - OrderStatAggMapper.FeeKpiRow brush, PeriodSlice slice, BigDecimal taxRate) { - return switch (slice) { - case CUR -> buildTotals(nz(o.getCurSalesAmount()), nz(o.getCurRefundAmount()), - nz(o.getCurProductCost()), nz(o.getCurFreightCost()), nz(o.getCurOtherCost()), - nz(promo.getCurAmount()), nz(brush.getCurAmount()), taxRate, - o.getCurOrderCount() != null ? o.getCurOrderCount().intValue() : 0); - case PREV -> buildTotals(nz(o.getPrevSalesAmount()), nz(o.getPrevRefundAmount()), - nz(o.getPrevProductCost()), nz(o.getPrevFreightCost()), nz(o.getPrevOtherCost()), - nz(promo.getPrevAmount()), nz(brush.getPrevAmount()), taxRate, - o.getPrevOrderCount() != null ? o.getPrevOrderCount().intValue() : 0); - case YOY -> buildTotals(nz(o.getYoySalesAmount()), nz(o.getYoyRefundAmount()), - nz(o.getYoyProductCost()), nz(o.getYoyFreightCost()), nz(o.getYoyOtherCost()), - nz(promo.getYoyAmount()), nz(brush.getYoyAmount()), taxRate, - o.getYoyOrderCount() != null ? o.getYoyOrderCount().intValue() : 0); - }; + return buildTotals(sales, refund, pc, fc, oc, promotion, brush, taxRate, orderCount, 0, + BigDecimal.ZERO, 0, 0); } private Totals buildTotals(BigDecimal sales, BigDecimal refund, BigDecimal pc, BigDecimal fc, BigDecimal oc, - BigDecimal promotion, BigDecimal brush, BigDecimal taxRate, int orderCount) { + BigDecimal promotion, BigDecimal brush, BigDecimal taxRate, + int orderCount, int salesQuantitySum, + BigDecimal totalGmv, int gmvOrderCount, int gmvQuantitySum) { BigDecimal cost = pc.add(fc).add(oc); BigDecimal gross = sales.subtract(cost).subtract(refund); BigDecimal pbt = gross.subtract(promotion).subtract(brush); BigDecimal tax = salesTaxOnNetSales(sales, refund, taxRate); - BigDecimal aov = orderCount > 0 ? sales.divide(BigDecimal.valueOf(orderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; - return new Totals(sales, refund, cost, pc, fc, oc, promotion, brush, tax, gross, pbt.subtract(tax), orderCount, aov); + BigDecimal aov = orderCount > 0 + ? sales.divide(BigDecimal.valueOf(orderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal gmvAov = gmvOrderCount > 0 + ? totalGmv.divide(BigDecimal.valueOf(gmvOrderCount), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal salesPiecePrice = salesQuantitySum > 0 + ? sales.divide(BigDecimal.valueOf(salesQuantitySum), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal gmvPiecePrice = gmvQuantitySum > 0 + ? totalGmv.divide(BigDecimal.valueOf(gmvQuantitySum), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; + return new Totals(sales, refund, cost, pc, fc, oc, promotion, brush, tax, gross, pbt.subtract(tax), + orderCount, salesQuantitySum, totalGmv, gmvOrderCount, gmvQuantitySum, + aov, gmvAov, salesPiecePrice, gmvPiecePrice); } private List buildTrendFromDaily(List dailyRows, @@ -444,8 +446,6 @@ public class WorkbenchServiceImpl implements WorkbenchService { return v != null ? v : BigDecimal.ZERO; } - private enum PeriodSlice { CUR, PREV, YOY } - private record SqlAggBundle(List orderRows, List dailyRows, List promotionRows, @@ -661,67 +661,112 @@ public class WorkbenchServiceImpl implements WorkbenchService { } /** - * 组装财务区 KPI 卡片:总 GMV(全状态)、实际销售额、毛利、净利、订单量、客单价、退款率、广告 ROI; - * 环比用 {@code prev},同比用 {@code yoy};副文案中带毛利率、净利率、税率等说明。 + * 经营概览 KPI 卡片:GMV 三角、实际销售三角、净收/退款、成本分项、利润费用、运营效率等。 */ - private List buildKpis(Totals cur, Totals prev, Totals yoy, DateRange range, BigDecimal taxRate, - BigDecimal totalGmvCur, BigDecimal totalGmvPrev, BigDecimal totalGmvYoy) { + private List buildKpis(Totals cur, Totals prev, Totals yoy, DateRange range, BigDecimal taxRate) { int days = (int) (range.endDate().toEpochDay() - range.startDate().toEpochDay() + 1); DecimalFormat df = new DecimalFormat("#,###", DecimalFormatSymbols.getInstance(Locale.CHINA)); - BigDecimal netRecv = cur.totalSales().subtract(cur.totalRefund()); + BigDecimal netRecv = cur.netSales(); BigDecimal grossMarginPct = safePct(cur.grossOrder(), cur.totalSales()); BigDecimal netMarginPct = safePct(cur.netProfit(), cur.totalSales()); BigDecimal refundRate = safePct(cur.totalRefund(), cur.totalSales()); + BigDecimal costRate = safePct(cur.totalCost(), cur.totalSales()); BigDecimal roi = cur.promotion().compareTo(BigDecimal.ZERO) > 0 - ? netRecv.divide(cur.promotion(), 2, RoundingMode.HALF_UP) - : BigDecimal.ZERO; - - BigDecimal yoyNetRecv = yoy.totalSales().subtract(yoy.totalRefund()); + ? netRecv.divide(cur.promotion(), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; BigDecimal yoyRoi = yoy.promotion().compareTo(BigDecimal.ZERO) > 0 - ? yoyNetRecv.divide(yoy.promotion(), 2, RoundingMode.HALF_UP) - : BigDecimal.ZERO; + ? yoy.netSales().divide(yoy.promotion(), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; + BigDecimal prevRoi = prev.promotion().compareTo(BigDecimal.ZERO) > 0 + ? prev.netSales().divide(prev.promotion(), 2, RoundingMode.HALF_UP) : BigDecimal.ZERO; - BigDecimal avgDaily = days > 0 + BigDecimal avgDailySalesOrders = days > 0 ? BigDecimal.valueOf(cur.orderCount()).divide(BigDecimal.valueOf(days), 1, RoundingMode.HALF_UP) : BigDecimal.ZERO; - - BigDecimal yoyRefundRate = safePct(yoy.totalRefund(), yoy.totalSales()); + BigDecimal avgDailyGmvOrders = days > 0 + ? BigDecimal.valueOf(cur.gmvOrderCount()).divide(BigDecimal.valueOf(days), 1, RoundingMode.HALF_UP) + : BigDecimal.ZERO; + BigDecimal gmvSalesGap = cur.totalGmv().subtract(cur.totalSales()); List list = new ArrayList<>(); - list.add(kpi("总GMV", formatYuanComma(totalGmvCur), "元", totalGmvCur, totalGmvPrev, totalGmvYoy, - "全部订单状态商家实收合计", - totalGmvCur, true)); + + // —— GMV 口径(全状态)—— + list.add(kpi("总GMV", formatYuanComma(cur.totalGmv()), "元", cur.totalGmv(), prev.totalGmv(), yoy.totalGmv(), + "全状态商家实收", cur.totalGmv(), true)); + list.add(kpiCount("GMV单量", cur.gmvOrderCount(), prev.gmvOrderCount(), yoy.gmvOrderCount(), "单", + "日均约 " + avgDailyGmvOrders.stripTrailingZeros().toPlainString() + " 单")); + list.add(kpiCount("GMV销量", cur.gmvQuantitySum(), prev.gmvQuantitySum(), yoy.gmvQuantitySum(), "件", "全状态件数合计")); + list.add(kpi("GMV客单价", formatYuanComma(cur.gmvAverageOrderValue()), "元", + cur.gmvAverageOrderValue(), prev.gmvAverageOrderValue(), yoy.gmvAverageOrderValue(), + "GMV÷GMV单量", cur.gmvAverageOrderValue(), false)); + list.add(kpi("GMV件单价", formatYuanComma(cur.gmvPiecePrice()), "元", + cur.gmvPiecePrice(), prev.gmvPiecePrice(), yoy.gmvPiecePrice(), + "GMV÷GMV销量", cur.gmvPiecePrice(), false)); + + // —— 实际销售口径(计入销售额状态)—— list.add(kpi("实际销售额", formatYuanComma(cur.totalSales()), "元", cur.totalSales(), prev.totalSales(), yoy.totalSales(), - "退款 " + formatYuanComma(cur.totalRefund()) + " 元 · 计入销售额的订单状态口径", - cur.totalSales(), true)); - list.add(kpi("毛利润", formatYuanComma(cur.grossOrder()), "元", cur.grossOrder(), prev.grossOrder(), yoy.grossOrder(), - "毛利率 " + grossMarginPct.stripTrailingZeros().toPlainString() + "%", - cur.grossOrder(), true)); - list.add(kpi("净利润", formatYuanComma(cur.netProfit()), "元", cur.netProfit(), prev.netProfit(), yoy.netProfit(), - "净利率 " + netMarginPct.stripTrailingZeros().toPlainString() + "% · 税额口径税率 " - + taxRate.multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString() + "%", - cur.netProfit(), true)); - list.add(kpi("订单量", df.format(cur.orderCount()), "单", - BigDecimal.valueOf(cur.orderCount()), BigDecimal.valueOf(prev.orderCount()), BigDecimal.valueOf(yoy.orderCount()), - "日均约 " + avgDaily.stripTrailingZeros().toPlainString() + " 单", - null, null)); + "计入销售额的订单状态", cur.totalSales(), true)); + list.add(kpiCount("销售单量", cur.orderCount(), prev.orderCount(), yoy.orderCount(), "单", + "日均约 " + avgDailySalesOrders.stripTrailingZeros().toPlainString() + " 单")); + list.add(kpiCount("实际销量", cur.salesQuantitySum(), prev.salesQuantitySum(), yoy.salesQuantitySum(), "件", + "计入销售额状态件数")); list.add(kpi("客单价", formatYuanComma(cur.averageOrderValue()), "元", - cur.averageOrderValue(), prev.averageOrderValue(), yoy.averageOrderValue(), "按计入销售额单量", - cur.averageOrderValue(), false)); + cur.averageOrderValue(), prev.averageOrderValue(), yoy.averageOrderValue(), + "销售额÷销售单量", cur.averageOrderValue(), false)); + list.add(kpi("销售件单价", formatYuanComma(cur.salesPiecePrice()), "元", + cur.salesPiecePrice(), prev.salesPiecePrice(), yoy.salesPiecePrice(), + "销售额÷实际销量", cur.salesPiecePrice(), false)); + + // —— 净收与退款 —— + list.add(kpi("净销售额", formatYuanComma(netRecv), "元", netRecv, prev.netSales(), yoy.netSales(), + "实际销售额−退款", netRecv, true)); + list.add(kpi("退款额", formatYuanComma(cur.totalRefund()), "元", cur.totalRefund(), prev.totalRefund(), yoy.totalRefund(), + "计入退款状态口径", cur.totalRefund(), true)); list.add(kpi("退款率", refundRate.setScale(2, RoundingMode.HALF_UP).toPlainString(), "%", - refundRate, safePct(prev.totalRefund(), prev.totalSales()), yoyRefundRate, "销售额口径", - null, null)); - list.add(kpi("广告ROI", roi.stripTrailingZeros().toPlainString(), ":1", roi, - prev.promotion().compareTo(BigDecimal.ZERO) > 0 - ? prev.totalSales().subtract(prev.totalRefund()).divide(prev.promotion(), 2, RoundingMode.HALF_UP) - : BigDecimal.ZERO, - yoyRoi, - "推广花费 " + formatYuanComma(cur.promotion()) + " 元", - null, null)); + refundRate, safePct(prev.totalRefund(), prev.totalSales()), safePct(yoy.totalRefund(), yoy.totalSales()), + "退款÷实际销售额", null, null)); + list.add(kpi("GMV与销售差额", formatYuanComma(gmvSalesGap), "元", gmvSalesGap, + prev.totalGmv().subtract(prev.totalSales()), yoy.totalGmv().subtract(yoy.totalSales()), + "总GMV−实际销售额", gmvSalesGap, true)); + + // —— 成本 —— + list.add(kpi("订单成本", formatYuanComma(cur.totalCost()), "元", cur.totalCost(), prev.totalCost(), yoy.totalCost(), + "成本率 " + costRate.stripTrailingZeros().toPlainString() + "%", cur.totalCost(), true)); + list.add(kpi("商品成本", formatYuanComma(cur.productCost()), "元", cur.productCost(), prev.productCost(), yoy.productCost(), + "按状态配置计入", cur.productCost(), true)); + list.add(kpi("运费成本", formatYuanComma(cur.freightCost()), "元", cur.freightCost(), prev.freightCost(), yoy.freightCost(), + null, cur.freightCost(), true)); + list.add(kpi("其他成本", formatYuanComma(cur.otherCost()), "元", cur.otherCost(), prev.otherCost(), yoy.otherCost(), + null, cur.otherCost(), true)); + + // —— 利润与费用 —— + list.add(kpi("毛利润", formatYuanComma(cur.grossOrder()), "元", cur.grossOrder(), prev.grossOrder(), yoy.grossOrder(), + "毛利率 " + grossMarginPct.stripTrailingZeros().toPlainString() + "%", cur.grossOrder(), true)); + list.add(kpi("推广费", formatYuanComma(cur.promotion()), "元", cur.promotion(), prev.promotion(), yoy.promotion(), + "区间推广费合计", cur.promotion(), true)); + list.add(kpi("刷单费", formatYuanComma(cur.brush()), "元", cur.brush(), prev.brush(), yoy.brush(), + "区间刷单费合计", cur.brush(), true)); + list.add(kpi("税额", formatYuanComma(cur.tax()), "元", cur.tax(), prev.tax(), yoy.tax(), + "税率 " + taxRate.multiply(new BigDecimal("100")).stripTrailingZeros().toPlainString() + "%(净销售额口径)", + cur.tax(), true)); + list.add(kpi("净利润", formatYuanComma(cur.netProfit()), "元", cur.netProfit(), prev.netProfit(), yoy.netProfit(), + "净利率 " + netMarginPct.stripTrailingZeros().toPlainString() + "%", + cur.netProfit(), true)); + + // —— 运营效率 —— + list.add(kpi("广告ROI", roi.stripTrailingZeros().toPlainString(), ":1", roi, prevRoi, yoyRoi, + "净销售额÷推广费", null, null)); + return list; } + /** 非金额类 KPI(单量、件数等) */ + private WorkbenchKpiItemVO kpiCount(String label, int cur, int prev, int yoy, String unit, String sub) { + DecimalFormat df = new DecimalFormat("#,###", DecimalFormatSymbols.getInstance(Locale.CHINA)); + return kpi(label, df.format(cur), unit, + BigDecimal.valueOf(cur), BigDecimal.valueOf(prev), BigDecimal.valueOf(yoy), + sub, null, null); + } + /** * 单张 KPI:展示格式化主值 + 环比/同比变化率;{@code amountYuan} 供前端换算「万」; * {@code preferWanDisplay} 为 true 时建议主数字用万展示。 @@ -850,13 +895,7 @@ public class WorkbenchServiceImpl implements WorkbenchService { } /** 环比/同比汇总 */ - private record ComparePeriodBundle(DateRange lastRange, - DateRange yoyRange, - Totals prev, - Totals yoy, - BigDecimal totalGmvCur, - BigDecimal totalGmvPrev, - BigDecimal totalGmvYoy) { + private record ComparePeriodBundle(DateRange lastRange, DateRange yoyRange, Totals prev, Totals yoy) { } /** 四维聚合桶 + 展示名称 */ @@ -871,21 +910,7 @@ public class WorkbenchServiceImpl implements WorkbenchService { } /** - * 某一时间段、在「计入销售额」口径下聚合后的财务总量。 - * - * @param totalSales 计入销售额的商家实收合计 - * @param totalRefund 退款侧实收合计(与 stat 一致) - * @param totalCost 订单成本合计(商品+物流+其他) - * @param productCost 商品成本分项 - * @param freightCost 物流成本分项 - * @param otherCost 其他成本分项 - * @param promotion 区间内推广费合计 - * @param brush 区间内刷单金额合计 - * @param tax 按净销售额与税率计算的税额 - * @param grossOrder 毛利润 = 销售 − 成本 − 退款 - * @param netProfit 净利润 = 毛利润 − 推广 − 刷单 − 税 - * @param orderCount 计入销售额状态的订单笔数 - * @param averageOrderValue 客单价 = 销售额 / 笔数 + * 单时间段店铺财务 KPI 总量(与汇总表 + 推广/刷单区间一致)。 */ private record Totals(BigDecimal totalSales, BigDecimal totalRefund, @@ -899,7 +924,18 @@ public class WorkbenchServiceImpl implements WorkbenchService { BigDecimal grossOrder, BigDecimal netProfit, int orderCount, - BigDecimal averageOrderValue) { + int salesQuantitySum, + BigDecimal totalGmv, + int gmvOrderCount, + int gmvQuantitySum, + BigDecimal averageOrderValue, + BigDecimal gmvAverageOrderValue, + BigDecimal salesPiecePrice, + BigDecimal gmvPiecePrice) { + + BigDecimal netSales() { + return totalSales.subtract(totalRefund); + } } /** 多维或按日聚合用的累加桶:累计销售额、退款、成本 */ diff --git a/ecom-dmb-backend/src/main/resources/mapper/order/OrderStatAggMapper.xml b/ecom-dmb-backend/src/main/resources/mapper/order/OrderStatAggMapper.xml index 716b0726..e3b4d0fb 100644 --- a/ecom-dmb-backend/src/main/resources/mapper/order/OrderStatAggMapper.xml +++ b/ecom-dmb-backend/src/main/resources/mapper/order/OrderStatAggMapper.xml @@ -40,6 +40,14 @@ #{id} THEN 1 ELSE 0 END) + + SUM(CASE WHEN o.status_id IN + #{id} + THEN ELSE 0 END) + + + SUM() + SUM(CASE WHEN o.status_id IN #{id} @@ -67,11 +75,14 @@ IFNULL(ps.product_id, -1) AS productId, IFNULL(p.category_id, 0) AS categoryId, AS salesAmount, AS orderCount, + AS salesQuantitySum, AS refundAmount, AS productCost, AS freightCost, AS otherCost, - SUM(o.received_amount) AS gmvAllStatus + SUM(o.received_amount) AS gmvAllStatus, + COUNT(*) AS gmvOrderCount, + AS gmvQuantitySum FROM ec_order o INNER JOIN ec_shop s ON s.id = o.shop_id LEFT JOIN ec_product_spec ps ON ps.id = o.product_spec_id diff --git a/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatDailyMapper.xml b/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatDailyMapper.xml index 69a08b8b..30fa8411 100644 --- a/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatDailyMapper.xml +++ b/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatDailyMapper.xml @@ -5,25 +5,29 @@ INSERT INTO ec_order_stat_daily_shop ( company_id, shop_id, platform_id, stat_date, - sales_amount, refund_amount, sales_order_count, - product_cost, freight_cost, other_cost, - gmv_amount, gmv_order_count, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, + stat_version ) VALUES ( #{companyId}, #{shopId}, #{platformId}, #{statDate}, - #{salesAmount}, #{refundAmount}, #{salesOrderCount}, - #{productCost}, #{freightCost}, #{otherCost}, - #{gmvAmount}, #{gmvOrderCount}, #{statVersion} + #{salesAmount}, #{salesOrderCount}, #{salesQuantitySum}, + #{gmvAmount}, #{gmvOrderCount}, #{gmvQuantitySum}, + #{refundAmount}, #{productCost}, #{freightCost}, #{otherCost}, + #{statVersion} ) ON DUPLICATE KEY UPDATE platform_id = VALUES(platform_id), sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), - gmv_amount = VALUES(gmv_amount), - gmv_order_count = VALUES(gmv_order_count), stat_version = VALUES(stat_version), update_time = CURRENT_TIMESTAMP @@ -36,13 +40,15 @@ INSERT INTO ec_order_stat_daily_product ( company_id, shop_id, product_id, category_id, stat_date, - sales_amount, refund_amount, sales_order_count, - product_cost, freight_cost, other_cost, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) VALUES (#{r.companyId}, #{r.shopId}, #{r.productId}, #{r.categoryId}, #{r.statDate}, - #{r.salesAmount}, #{r.refundAmount}, #{r.salesOrderCount}, - #{r.productCost}, #{r.freightCost}, #{r.otherCost}, #{r.statVersion}) + #{r.salesAmount}, #{r.salesOrderCount}, #{r.salesQuantitySum}, + #{r.gmvAmount}, #{r.gmvOrderCount}, #{r.gmvQuantitySum}, + #{r.refundAmount}, #{r.productCost}, #{r.freightCost}, #{r.otherCost}, #{r.statVersion}) @@ -54,13 +60,15 @@ INSERT INTO ec_order_stat_daily_spec ( company_id, shop_id, product_id, product_spec_id, stat_date, - sales_amount, refund_amount, sales_order_count, quantity_sum, - product_cost, freight_cost, other_cost, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) VALUES (#{r.companyId}, #{r.shopId}, #{r.productId}, #{r.productSpecId}, #{r.statDate}, - #{r.salesAmount}, #{r.refundAmount}, #{r.salesOrderCount}, #{r.quantitySum}, - #{r.productCost}, #{r.freightCost}, #{r.otherCost}, #{r.statVersion}) + #{r.salesAmount}, #{r.salesOrderCount}, #{r.salesQuantitySum}, + #{r.gmvAmount}, #{r.gmvOrderCount}, #{r.gmvQuantitySum}, + #{r.refundAmount}, #{r.productCost}, #{r.freightCost}, #{r.otherCost}, #{r.statVersion}) @@ -68,14 +76,16 @@ INSERT INTO ec_order_stat_monthly_shop ( company_id, shop_id, platform_id, stat_month, - sales_amount, refund_amount, sales_order_count, - product_cost, freight_cost, other_cost, gmv_amount, gmv_order_count, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) SELECT company_id, shop_id, MAX(platform_id), #{statMonth}, - SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), - SUM(product_cost), SUM(freight_cost), SUM(other_cost), - SUM(gmv_amount), SUM(gmv_order_count), MAX(stat_version) + SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), + SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), + SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost), + MAX(stat_version) FROM ec_order_stat_daily_shop WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date >= #{statMonth} @@ -84,13 +94,15 @@ ON DUPLICATE KEY UPDATE platform_id = VALUES(platform_id), sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), - gmv_amount = VALUES(gmv_amount), - gmv_order_count = VALUES(gmv_order_count), stat_version = VALUES(stat_version), update_time = CURRENT_TIMESTAMP @@ -98,14 +110,16 @@ INSERT INTO ec_order_stat_yearly_shop ( company_id, shop_id, platform_id, stat_year, - sales_amount, refund_amount, sales_order_count, - product_cost, freight_cost, other_cost, gmv_amount, gmv_order_count, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) SELECT company_id, shop_id, MAX(platform_id), #{statYear}, - SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), - SUM(product_cost), SUM(freight_cost), SUM(other_cost), - SUM(gmv_amount), SUM(gmv_order_count), MAX(stat_version) + SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), + SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), + SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost), + MAX(stat_version) FROM ec_order_stat_monthly_shop WHERE company_id = #{companyId} AND shop_id = #{shopId} AND YEAR(stat_month) = #{statYear} @@ -113,13 +127,15 @@ ON DUPLICATE KEY UPDATE platform_id = VALUES(platform_id), sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), - gmv_amount = VALUES(gmv_amount), - gmv_order_count = VALUES(gmv_order_count), stat_version = VALUES(stat_version), update_time = CURRENT_TIMESTAMP @@ -127,13 +143,16 @@ INSERT INTO ec_order_stat_monthly_product ( company_id, shop_id, product_id, category_id, stat_month, - sales_amount, refund_amount, sales_order_count, - product_cost, freight_cost, other_cost, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) SELECT company_id, shop_id, product_id, category_id, #{statMonth}, - SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), - SUM(product_cost), SUM(freight_cost), SUM(other_cost), MAX(stat_version) + SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), + SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), + SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost), + MAX(stat_version) FROM ec_order_stat_daily_product WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date >= #{statMonth} @@ -141,8 +160,12 @@ GROUP BY company_id, shop_id, product_id, category_id ON DUPLICATE KEY UPDATE sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), @@ -153,21 +176,28 @@ INSERT INTO ec_order_stat_yearly_product ( company_id, shop_id, product_id, category_id, stat_year, - sales_amount, refund_amount, sales_order_count, - product_cost, freight_cost, other_cost, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) SELECT company_id, shop_id, product_id, category_id, #{statYear}, - SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), - SUM(product_cost), SUM(freight_cost), SUM(other_cost), MAX(stat_version) + SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), + SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), + SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost), + MAX(stat_version) FROM ec_order_stat_monthly_product WHERE company_id = #{companyId} AND shop_id = #{shopId} AND YEAR(stat_month) = #{statYear} GROUP BY company_id, shop_id, product_id, category_id ON DUPLICATE KEY UPDATE sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), @@ -178,13 +208,16 @@ INSERT INTO ec_order_stat_monthly_spec ( company_id, shop_id, product_id, product_spec_id, stat_month, - sales_amount, refund_amount, sales_order_count, quantity_sum, - product_cost, freight_cost, other_cost, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) SELECT company_id, shop_id, product_id, product_spec_id, #{statMonth}, - SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), SUM(quantity_sum), - SUM(product_cost), SUM(freight_cost), SUM(other_cost), MAX(stat_version) + SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), + SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), + SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost), + MAX(stat_version) FROM ec_order_stat_daily_spec WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date >= #{statMonth} @@ -192,9 +225,12 @@ GROUP BY company_id, shop_id, product_id, product_spec_id ON DUPLICATE KEY UPDATE sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), - quantity_sum = VALUES(quantity_sum), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), @@ -205,22 +241,28 @@ INSERT INTO ec_order_stat_yearly_spec ( company_id, shop_id, product_id, product_spec_id, stat_year, - sales_amount, refund_amount, sales_order_count, quantity_sum, - product_cost, freight_cost, other_cost, stat_version + sales_amount, sales_order_count, sales_quantity_sum, + gmv_amount, gmv_order_count, gmv_quantity_sum, + refund_amount, product_cost, freight_cost, other_cost, stat_version ) SELECT company_id, shop_id, product_id, product_spec_id, #{statYear}, - SUM(sales_amount), SUM(refund_amount), SUM(sales_order_count), SUM(quantity_sum), - SUM(product_cost), SUM(freight_cost), SUM(other_cost), MAX(stat_version) + SUM(sales_amount), SUM(sales_order_count), SUM(sales_quantity_sum), + SUM(gmv_amount), SUM(gmv_order_count), SUM(gmv_quantity_sum), + SUM(refund_amount), SUM(product_cost), SUM(freight_cost), SUM(other_cost), + MAX(stat_version) FROM ec_order_stat_monthly_spec WHERE company_id = #{companyId} AND shop_id = #{shopId} AND YEAR(stat_month) = #{statYear} GROUP BY company_id, shop_id, product_id, product_spec_id ON DUPLICATE KEY UPDATE sales_amount = VALUES(sales_amount), - refund_amount = VALUES(refund_amount), sales_order_count = VALUES(sales_order_count), - quantity_sum = VALUES(quantity_sum), + sales_quantity_sum = VALUES(sales_quantity_sum), + gmv_amount = VALUES(gmv_amount), + gmv_order_count = VALUES(gmv_order_count), + gmv_quantity_sum = VALUES(gmv_quantity_sum), + refund_amount = VALUES(refund_amount), product_cost = VALUES(product_cost), freight_cost = VALUES(freight_cost), other_cost = VALUES(other_cost), diff --git a/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatQueryMapper.xml b/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatQueryMapper.xml index a89aaebe..915a09d0 100644 --- a/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatQueryMapper.xml +++ b/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatQueryMapper.xml @@ -8,16 +8,48 @@ #{sid} + + COALESCE(SUM(sales_amount), 0) AS salesAmount, + COALESCE(SUM(refund_amount), 0) AS refundAmount, + COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, + COALESCE(SUM(sales_quantity_sum), 0) AS salesQuantitySum, + COALESCE(SUM(gmv_amount), 0) AS gmvAmount, + COALESCE(SUM(gmv_order_count), 0) AS gmvOrderCount, + COALESCE(SUM(gmv_quantity_sum), 0) AS gmvQuantitySum + + + + COALESCE(SUM(sales_amount), 0) AS salesAmount, + COALESCE(SUM(refund_amount), 0) AS refundAmount, + COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, + COALESCE(SUM(sales_quantity_sum), 0) AS salesQuantitySum, + COALESCE(SUM(gmv_amount), 0) AS gmvAmount, + COALESCE(SUM(gmv_order_count), 0) AS gmvOrderCount, + COALESCE(SUM(gmv_quantity_sum), 0) AS gmvQuantitySum, + COALESCE(SUM(product_cost), 0) AS productCost, + COALESCE(SUM(freight_cost), 0) AS freightCost, + COALESCE(SUM(other_cost), 0) AS otherCost + + + + COALESCE(SUM(sales_amount), 0) AS salesAmount, + COALESCE(SUM(refund_amount), 0) AS refundAmount, + COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, + COALESCE(SUM(sales_quantity_sum), 0) AS salesQuantitySum, + COALESCE(SUM(gmv_amount), 0) AS gmvAmount, + COALESCE(SUM(gmv_order_count), 0) AS gmvOrderCount, + COALESCE(SUM(gmv_quantity_sum), 0) AS gmvQuantitySum, + COALESCE(SUM(product_cost), 0) AS productCost, + COALESCE(SUM(freight_cost), 0) AS freightCost, + COALESCE(SUM(other_cost), 0) AS otherCost + + SELECT stat_date AS statDate, - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, + , COALESCE(SUM(product_cost), 0) AS productCost, COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost, - COALESCE(SUM(gmv_amount), 0) AS gmvAmount, - COALESCE(SUM(gmv_order_count), 0) AS gmvOrderCount + COALESCE(SUM(other_cost), 0) AS otherCost FROM ec_order_stat_daily_shop WHERE AND stat_date >= #{startDate} @@ -47,14 +75,10 @@ SELECT shop_id AS shopId, MAX(platform_id) AS platformId, - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, + , COALESCE(SUM(product_cost), 0) AS productCost, COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost, - COALESCE(SUM(gmv_amount), 0) AS gmvAmount, - COALESCE(SUM(gmv_order_count), 0) AS gmvOrderCount + COALESCE(SUM(other_cost), 0) AS otherCost FROM ec_order_stat_daily_shop WHERE AND stat_date >= #{startDate} @@ -65,12 +89,7 @@ SELECT - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, - COALESCE(SUM(product_cost), 0) AS productCost, - COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost + FROM ec_order_stat_daily_product WHERE company_id = #{companyId} AND product_id = #{productId} @@ -135,12 +139,7 @@ SELECT category_id AS categoryId, - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, - COALESCE(SUM(product_cost), 0) AS productCost, - COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost + FROM ec_order_stat_monthly_product WHERE AND stat_month >= #{startMonth} @@ -174,12 +168,7 @@ SELECT product_id AS productId, MAX(category_id) AS categoryId, - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, - COALESCE(SUM(product_cost), 0) AS productCost, - COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost + FROM ec_order_stat_monthly_product WHERE AND stat_month >= #{startMonth} @@ -190,12 +179,7 @@ SELECT product_spec_id AS productSpecId, - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, - COALESCE(SUM(quantity_sum), 0) AS quantitySum, - COALESCE(SUM(product_cost), 0) AS productCost, - COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost + FROM ec_order_stat_daily_spec WHERE AND product_id = #{productId} @@ -241,13 +214,7 @@ SELECT product_spec_id AS productSpecId, - COALESCE(SUM(sales_amount), 0) AS salesAmount, - COALESCE(SUM(refund_amount), 0) AS refundAmount, - COALESCE(SUM(sales_order_count), 0) AS salesOrderCount, - COALESCE(SUM(quantity_sum), 0) AS quantitySum, - COALESCE(SUM(product_cost), 0) AS productCost, - COALESCE(SUM(freight_cost), 0) AS freightCost, - COALESCE(SUM(other_cost), 0) AS otherCost + FROM ec_order_stat_yearly_spec WHERE AND product_id = #{productId} diff --git a/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatRefreshMapper.xml b/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatRefreshMapper.xml index 1aea2ef5..d0149576 100644 --- a/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatRefreshMapper.xml +++ b/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatRefreshMapper.xml @@ -50,7 +50,10 @@ THEN o.received_amount ELSE 0 END) AS refundAmount, SUM(CASE WHEN o.status_id IN #{id} - THEN ELSE 0 END) AS quantitySum, + THEN ELSE 0 END) AS salesQuantitySum, + SUM(o.received_amount) AS gmvAmount, + COUNT(*) AS gmvOrderCount, + SUM() AS gmvQuantitySum, SUM(CASE WHEN o.status_id IN #{id} THEN o.product_cost * ELSE 0 END) AS productCost, @@ -66,13 +69,6 @@ AND o.shop_id = #{shopId} AND o.order_time >= #{start} AND o.order_time <= #{end} - - - AND o.status_id IN - #{id} - - AND 1 = 0 - GROUP BY IFNULL(o.product_spec_id, 0), IFNULL(ps.product_id, 0) diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiGrid.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiGrid.vue index 0ae7213d..893c03be 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiGrid.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiGrid.vue @@ -1,115 +1,109 @@ + \ No newline at end of file diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiSettingsModal.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiSettingsModal.vue new file mode 100644 index 00000000..ed6ff374 --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/components/FinanceKpiSettingsModal.vue @@ -0,0 +1,200 @@ + + + diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/finance-kpi-hints.ts b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/finance-kpi-hints.ts new file mode 100644 index 00000000..d12438cd --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/finance-kpi-hints.ts @@ -0,0 +1,45 @@ +import { overviewTableHints, salesTaxColumnHint } from '#/utils/overview-table-hints'; + +/** 工作台经营 KPI 计算说明(与汇总表、后端 WorkbenchServiceImpl 口径一致) */ +const FINANCE_KPI_HINTS: Record = { + 总GMV: overviewTableHints.gmv, + GMV单量: + '所选时间范围内,全部订单状态的订单笔数合计(不按是否计入销售额过滤)。环比/同比为笔数变化率。', + GMV销量: + '所选时间范围内,全部订单状态的销售件数合计(订单行件数汇总)。', + GMV客单价: 'GMV ÷ GMV单量;GMV单量为 0 时为 0。', + GMV件单价: 'GMV ÷ GMV销量;GMV销量为 0 时为 0。', + 实际销售额: overviewTableHints.salesAmount, + 销售单量: + '统计口径下,计入销售额的有效订单笔数(与下方汇总表「单量」列一致)。环比/同比为笔数变化率。', + 实际销量: '计入销售额的订单状态件数合计。', + 客单价: '实际销售额 ÷ 销售单量;销售单量为 0 时为 0。', + 销售件单价: '实际销售额 ÷ 实际销量;实际销量为 0 时为 0。', + 净销售额: '实际销售额 − 退款额(与广告 ROI 分子、税额计税基数同口径)。', + 退款额: overviewTableHints.refundAmount, + 退款率: '退款额 ÷ 实际销售额 × 100%;实际销售额为 0 时为 0。', + 'GMV与销售差额': '总GMV − 实际销售额,反映未计入销售口径的订单实收差额。', + 订单成本: overviewTableHints.orderCost, + 商品成本: overviewTableHints.productCost, + 运费成本: overviewTableHints.shippingCost, + 其他成本: overviewTableHints.otherCost, + 毛利润: overviewTableHints.grossProfit, + 推广费: overviewTableHints.totalPromotionFee, + 刷单费: overviewTableHints.brushFee, + 净利润: + '净利润 = 毛利润 − 推广费 − 刷单费 − 税额(与汇总表损益链路一致)。卡片副文案中的净利率 = 净利润 ÷ 实际销售额。', + 广告ROI: overviewTableHints.roi, +}; + +/** + * 获取 KPI 标题旁问号 Tooltip 文案;税额随页面税率变化。 + */ +export function getFinanceKpiHint(label: string, taxRate = 0.04): string { + if (label === '税额') { + return `${salesTaxColumnHint(taxRate)} 工作台按净销售额计税基数汇总。`; + } + return ( + FINANCE_KPI_HINTS[label] ?? + '该指标说明暂未配置,请以汇总表同名列或系统配置口径为准。' + ); +} diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/index.vue index 4354f5fa..c3256b08 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/workbench/tabs/finance/index.vue @@ -4,11 +4,18 @@ import type { CompanyOverviewVO } from '#/api/ecommerce/store-overview'; import { computed, nextTick, ref } from 'vue'; +import { createIconifyIcon } from '@vben/icons'; +import { Button, message } from 'ant-design-vue'; + import FinanceCostPieChart from './components/FinanceCostPieChart.vue'; import FinanceKpiGrid from './components/FinanceKpiGrid.vue'; +import FinanceKpiSettingsModal from './components/FinanceKpiSettingsModal.vue'; import FinanceTrendChart from './components/FinanceTrendChart.vue'; import OverviewTables from './components/OverviewTables.vue'; +import { sortLabelsByCatalog } from './kpi-catalog'; +import { useFinanceKpiPreferences } from './useFinanceKpiPreferences'; + import { buildCategoryOverviewFromDimRows, buildPlatformOverviewFromDimRows, @@ -52,6 +59,23 @@ const productOverview = computed( buildCategoryOverviewFromDimRows(props.multiAnalysis.product), ); +const IconSliders = createIconifyIcon('lucide:sliders-horizontal'); + +const { visibleLabels, colsPerRow, applySettings } = useFinanceKpiPreferences(); +const kpiSettingsOpen = ref(false); + +const visibleKpiCount = computed(() => visibleLabels.value.length); + +function onKpiSettingsSave(payload: { labels: string[]; colsPerRow: number }) { + const normalized = sortLabelsByCatalog(payload.labels); + if (normalized.length === 0) { + message.warning('请至少选择一项 KPI'); + return; + } + applySettings(normalized, payload.colsPerRow); + message.success('KPI 显示已更新'); +} + const trendChartRef = ref | null>(null); @@ -69,9 +93,36 @@ defineExpose({ redraw });