feat(order-stat,workbench): 订单统计日表扩展 GMV/销售单量件数并贯通刷新读写,工作台经营 KPI 扩至 22 项及前端展示配置

This commit is contained in:
小蟑螂
2026-06-03 20:51:26 +08:00
parent 28fba73928
commit b2a25a236f
23 changed files with 1192 additions and 455 deletions
+82 -40
View File
@@ -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')