feat(订单统计): 落地读模型汇总表与 refresh 写入链路,工作台/概览/导出切读 daily 汇总并补充 Admin rebuild 与对账

This commit is contained in:
小蟑螂
2026-06-01 15:45:31 +08:00
parent e1d90ccba3
commit b19f6d60f9
58 changed files with 5332 additions and 131 deletions
@@ -0,0 +1,522 @@
-- =============================================================================
-- 订单统计读模型:日 / 月 / 年 聚合表设计
-- =============================================================================
-- 目标:支撑 1 天~5 年(1825 天)区间查询与导出,避免在线扫 ec_order 明细。
-- 口径:与 OrderStatusStatSets + OrderStatAggMapper 一致(五维度 + GMV + 件数)。
-- 写入: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,毫秒级
-- =============================================================================
-- -----------------------------------------------------------------------------
-- 一、公共指标列说明(日 / 月 / 年 三张同维度表字段一致,仅时间键不同)
-- -----------------------------------------------------------------------------
-- 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 / 趋势 / 店铺表 / 平台 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 '销售额',
`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 '创建时间',
`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 '销售额',
`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 '创建时间',
`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 '销售额',
`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 '创建时间',
`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 '销售额',
`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 '其他成本',
`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 '销售额',
`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 '其他成本',
`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 '销售额',
`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 '其他成本',
`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 '销售额',
`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 '其他成本',
`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 '销售额',
`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 '其他成本',
`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 '销售额',
`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 '其他成本',
`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(refund_amount), SUM(sales_order_count), ...
-- 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 对应月/年行
-- =============================================================================