Files
ecom-dmb/ecom-dmb-backend/sql/design/ec_order_stat_design.sql
T

565 lines
38 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =============================================================================
-- 订单统计读模型:日 / 月 / 年 聚合表设计
-- =============================================================================
-- 目标:支撑 1 天~5 年(1825 天)区间查询与导出,避免在线扫 ec_order 明细。
-- 口径:与 OrderStatusStatSets + OrderStatAggMapper 一致(成本/退款五维度 + KPI 双三角)。
-- 写入:ec_order 变更 / 导入完成后,按 (company, shop, stat_date) 重算日表,再 rollup 月/年。
-- 详细方案见:docs/订单统计读模型方案.md
--
-- 【读路径路由建议】
-- ≤ 90 天 → 日表(趋势、KPI、四维汇总)
-- 91~1095 天 → 店铺/平台 KPI、趋势按月:月表;商品/品类明细:月表
-- > 3 年 或整年对比 → 年表(KPI/平台);商品 TopN 仍走月表
-- 推广费/刷单 → ec_promotion_fee_stat_* / ec_brush_fee_stat_*(小表,与日表同策略)
--
-- 【容量估算 · 单公司 · 50 店 · 5000 SKU · 日 10 万单】
-- daily_shop 50 × 1825 ≈ 9.1 万行/5年
-- daily_product 活跃维度 × 1825(全量 SKU 约数百万,按店×品×日)
-- monthly_shop 50 × 60 ≈ 3 千行/5年
-- yearly_shop 50 × 5 ≈ 250 行/5年
-- 5 年全店 KPI 读 yearly_shop:1 条 SUM,毫秒级
-- =============================================================================
-- -----------------------------------------------------------------------------
-- 一、公共指标列说明(日 / 月 / 年 同维度表字段一致,仅时间键 stat_date/stat_month/stat_year 不同)
-- -----------------------------------------------------------------------------
-- 【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
-- -----------------------------------------------------------------------------
-- =============================================================================
-- 二、店铺维度(KPI / 趋势 / 店铺表 / 平台 rollup 主读表)
-- =============================================================================
DROP TABLE IF EXISTS `ec_order_stat_daily_shop`;
CREATE TABLE `ec_order_stat_daily_shop` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_date` (`company_id`, `shop_id`, `stat_date`) USING BTREE,
INDEX `idx_company_date` (`company_id`, `stat_date`) USING BTREE,
INDEX `idx_company_platform_date` (`company_id`, `platform_id`, `stat_date`) USING BTREE,
INDEX `idx_shop_date` (`shop_id`, `stat_date`) USING BTREE,
CONSTRAINT `fk_oss_daily_shop_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_daily_shop_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-店铺-日汇总';
DROP TABLE IF EXISTS `ec_order_stat_monthly_shop`;
CREATE TABLE `ec_order_stat_monthly_shop` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_month` (`company_id`, `shop_id`, `stat_month`) USING BTREE,
INDEX `idx_company_month` (`company_id`, `stat_month`) USING BTREE,
INDEX `idx_company_platform_month` (`company_id`, `platform_id`, `stat_month`) USING BTREE,
CONSTRAINT `fk_oss_monthly_shop_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_monthly_shop_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-店铺-月汇总(由日表 SUM 或订单直算)';
DROP TABLE IF EXISTS `ec_order_stat_yearly_shop`;
CREATE TABLE `ec_order_stat_yearly_shop` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_year` (`company_id`, `shop_id`, `stat_year`) USING BTREE,
INDEX `idx_company_year` (`company_id`, `stat_year`) USING BTREE,
INDEX `idx_company_platform_year` (`company_id`, `platform_id`, `stat_year`) USING BTREE,
CONSTRAINT `fk_oss_yearly_shop_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_yearly_shop_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-店铺-年汇总(3~5 年 KPI / 平台占比主读表)';
-- =============================================================================
-- 三、商品维度(品类 / 商品 / 排行 / 工作台底表)
-- product_id = 0 表示未匹配商品;category_id 为订单写入时快照,商品改类目不回算历史
-- =============================================================================
DROP TABLE IF EXISTS `ec_order_stat_daily_product`;
CREATE TABLE `ec_order_stat_daily_product` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint NOT NULL DEFAULT 0 COMMENT '商品ID,0=未匹配',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_prod_cat_date` (`company_id`, `shop_id`, `product_id`, `category_id`, `stat_date`) USING BTREE,
INDEX `idx_company_date` (`company_id`, `stat_date`) USING BTREE,
INDEX `idx_company_cat_date` (`company_id`, `category_id`, `stat_date`) USING BTREE,
INDEX `idx_company_prod_date` (`company_id`, `product_id`, `stat_date`) USING BTREE,
INDEX `idx_shop_prod_date` (`shop_id`, `product_id`, `stat_date`) USING BTREE,
CONSTRAINT `fk_oss_daily_product_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_daily_product_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-商品-日汇总';
DROP TABLE IF EXISTS `ec_order_stat_monthly_product`;
CREATE TABLE `ec_order_stat_monthly_product` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint NOT NULL DEFAULT 0 COMMENT '商品ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_prod_cat_month` (`company_id`, `shop_id`, `product_id`, `category_id`, `stat_month`) USING BTREE,
INDEX `idx_company_month` (`company_id`, `stat_month`) USING BTREE,
INDEX `idx_company_cat_month` (`company_id`, `category_id`, `stat_month`) USING BTREE,
INDEX `idx_company_prod_month` (`company_id`, `product_id`, `stat_month`) USING BTREE,
CONSTRAINT `fk_oss_monthly_product_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_monthly_product_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-商品-月汇总(跨月~3年品类/商品明细主读表)';
DROP TABLE IF EXISTS `ec_order_stat_yearly_product`;
CREATE TABLE `ec_order_stat_yearly_product` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint NOT NULL DEFAULT 0 COMMENT '商品ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_prod_cat_year` (`company_id`, `shop_id`, `product_id`, `category_id`, `stat_year`) USING BTREE,
INDEX `idx_company_year` (`company_id`, `stat_year`) USING BTREE,
INDEX `idx_company_cat_year` (`company_id`, `category_id`, `stat_year`) USING BTREE,
INDEX `idx_company_prod_year` (`company_id`, `product_id`, `stat_year`) USING BTREE,
CONSTRAINT `fk_oss_yearly_product_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_yearly_product_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-商品-年汇总(3~5 年商品/品类排行)';
-- =============================================================================
-- 四、规格维度(商品概览 spec 块专用)
-- =============================================================================
DROP TABLE IF EXISTS `ec_order_stat_daily_spec`;
CREATE TABLE `ec_order_stat_daily_spec` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_spec_date` (`company_id`, `shop_id`, `product_spec_id`, `stat_date`) USING BTREE,
INDEX `idx_company_prod_date` (`company_id`, `product_id`, `stat_date`) USING BTREE,
INDEX `idx_company_date` (`company_id`, `stat_date`) USING BTREE,
CONSTRAINT `fk_oss_daily_spec_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_daily_spec_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-规格-日汇总';
DROP TABLE IF EXISTS `ec_order_stat_monthly_spec`;
CREATE TABLE `ec_order_stat_monthly_spec` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_spec_month` (`company_id`, `shop_id`, `product_spec_id`, `stat_month`) USING BTREE,
INDEX `idx_company_prod_month` (`company_id`, `product_id`, `stat_month`) USING BTREE,
CONSTRAINT `fk_oss_monthly_spec_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_monthly_spec_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-规格-月汇总';
DROP TABLE IF EXISTS `ec_order_stat_yearly_spec`;
CREATE TABLE `ec_order_stat_yearly_spec` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品ID',
`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_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 '退款金额',
`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 '其他成本',
`stat_version` int NOT NULL DEFAULT 0 COMMENT '重算版本号',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_spec_year` (`company_id`, `shop_id`, `product_spec_id`, `stat_year`) USING BTREE,
INDEX `idx_company_prod_year` (`company_id`, `product_id`, `stat_year`) USING BTREE,
CONSTRAINT `fk_oss_yearly_spec_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_oss_yearly_spec_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计-规格-年汇总';
-- =============================================================================
-- 五、推广费 / 刷单费 聚合(跨年区间 SUM 推广/刷单小表仍可能扫大量行,预聚合更稳)
-- 粒度与 ec_promotion_fee 一致:shop + 可选 product_id / category_id
-- =============================================================================
DROP TABLE IF EXISTS `ec_promotion_fee_stat_daily`;
CREATE TABLE `ec_promotion_fee_stat_daily` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品ID,0=非商品级',
`category_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '品类ID,0=非品类级',
`stat_date` date NOT NULL COMMENT '自然日(fee_date)',
`fee_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '推广费合计',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_dims_date` (`company_id`, `shop_id`, `product_id`, `category_id`, `stat_date`) USING BTREE,
INDEX `idx_company_date` (`company_id`, `stat_date`) USING BTREE,
CONSTRAINT `fk_pfs_daily_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_pfs_daily_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '推广费-日汇总';
DROP TABLE IF EXISTS `ec_promotion_fee_stat_monthly`;
CREATE TABLE `ec_promotion_fee_stat_monthly` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品ID,0=非商品级',
`category_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '品类ID,0=非品类级',
`stat_month` date NOT NULL COMMENT '自然月首日',
`fee_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '推广费合计',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_dims_month` (`company_id`, `shop_id`, `product_id`, `category_id`, `stat_month`) USING BTREE,
INDEX `idx_company_month` (`company_id`, `stat_month`) USING BTREE,
CONSTRAINT `fk_pfs_monthly_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_pfs_monthly_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '推广费-月汇总';
DROP TABLE IF EXISTS `ec_promotion_fee_stat_yearly`;
CREATE TABLE `ec_promotion_fee_stat_yearly` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`product_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品ID,0=非商品级',
`category_id` bigint UNSIGNED NOT NULL DEFAULT 0 COMMENT '品类ID,0=非品类级',
`stat_year` smallint NOT NULL COMMENT '自然年',
`fee_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '推广费合计',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_dims_year` (`company_id`, `shop_id`, `product_id`, `category_id`, `stat_year`) USING BTREE,
INDEX `idx_company_year` (`company_id`, `stat_year`) USING BTREE,
CONSTRAINT `fk_pfs_yearly_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_pfs_yearly_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '推广费-年汇总';
DROP TABLE IF EXISTS `ec_brush_fee_stat_daily`;
CREATE TABLE `ec_brush_fee_stat_daily` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`stat_date` date NOT NULL COMMENT '自然日',
`brush_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '刷单费合计',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_date` (`company_id`, `shop_id`, `stat_date`) USING BTREE,
INDEX `idx_company_date` (`company_id`, `stat_date`) USING BTREE,
CONSTRAINT `fk_bfs_daily_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_bfs_daily_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '刷单费-日汇总';
DROP TABLE IF EXISTS `ec_brush_fee_stat_monthly`;
CREATE TABLE `ec_brush_fee_stat_monthly` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`stat_month` date NOT NULL COMMENT '自然月首日',
`brush_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '刷单费合计',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_month` (`company_id`, `shop_id`, `stat_month`) USING BTREE,
INDEX `idx_company_month` (`company_id`, `stat_month`) USING BTREE,
CONSTRAINT `fk_bfs_monthly_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_bfs_monthly_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '刷单费-月汇总';
DROP TABLE IF EXISTS `ec_brush_fee_stat_yearly`;
CREATE TABLE `ec_brush_fee_stat_yearly` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`stat_year` smallint NOT NULL COMMENT '自然年',
`brush_amount` decimal(16, 2) NOT NULL DEFAULT 0.00 COMMENT '刷单费合计',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_year` (`company_id`, `shop_id`, `stat_year`) USING BTREE,
INDEX `idx_company_year` (`company_id`, `stat_year`) USING BTREE,
CONSTRAINT `fk_bfs_yearly_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_bfs_yearly_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '刷单费-年汇总';
-- =============================================================================
-- 六、刷新 / 重建任务(Backfill、导入后异步、状态配置变更全量 rebuild)
-- =============================================================================
DROP TABLE IF EXISTS `ec_order_stat_rebuild_task`;
CREATE TABLE `ec_order_stat_rebuild_task` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '店铺ID,NULL=全店',
`start_date` date NOT NULL COMMENT '重建起始日',
`end_date` date NOT NULL COMMENT '重建结束日',
`granularity` varchar(16) NOT NULL DEFAULT 'all' COMMENT 'daily/monthly/yearly/all',
`status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT 'pending/running/done/failed',
`progress_date` date NULL DEFAULT NULL COMMENT '已处理到的日期(断点续跑)',
`error_message` text NULL COMMENT '失败原因',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
INDEX `idx_company_status` (`company_id`, `status`) USING BTREE,
CONSTRAINT `fk_osrt_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计重建任务';
DROP TABLE IF EXISTS `ec_order_stat_refresh_queue`;
CREATE TABLE `ec_order_stat_refresh_queue` (
`id` bigint UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '主键',
`company_id` bigint UNSIGNED NOT NULL COMMENT '公司ID',
`shop_id` bigint UNSIGNED NOT NULL COMMENT '店铺ID',
`stat_date` date NOT NULL COMMENT '待刷新自然日',
`source` varchar(32) NOT NULL COMMENT 'import/crud/status_change/manual',
`status` varchar(20) NOT NULL DEFAULT 'pending' COMMENT 'pending/running/done/failed',
`retry_count` int NOT NULL DEFAULT 0 COMMENT '重试次数',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uk_company_shop_date_pending` (`company_id`, `shop_id`, `stat_date`, `status`) USING BTREE,
INDEX `idx_status_ctime` (`status`, `create_time`) USING BTREE,
CONSTRAINT `fk_osrq_company` FOREIGN KEY (`company_id`) REFERENCES `org_company` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_osrq_shop` FOREIGN KEY (`shop_id`) REFERENCES `ec_shop` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '订单统计日汇总待刷新队列';
-- =============================================================================
-- 七、Rollup 示例(写入侧 Job 参考,不在应用启动时执行)
-- =============================================================================
--
-- 月表 ← 日表(店铺):
-- 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(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')
-- ON DUPLICATE KEY UPDATE sales_amount=VALUES(sales_amount), ...;
--
-- 年表 ← 月表(店铺):
-- INSERT INTO ec_order_stat_yearly_shop (...)
-- SELECT company_id, shop_id, MAX(platform_id), YEAR(stat_month),
-- SUM(sales_amount), ...
-- FROM ec_order_stat_monthly_shop
-- WHERE company_id=? AND stat_year BETWEEN ? AND ?
-- GROUP BY company_id, shop_id, YEAR(stat_month)
-- ON DUPLICATE KEY UPDATE ...;
--
-- 单日 refresh 顺序:ec_order → daily_* → 触达 stat_month/stat_year 边界时 rollup 对应月/年行
-- =============================================================================