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,230 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.mapper.OrderStatDailyMapper">
<insert id="upsertDailyShop">
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
) VALUES (
#{companyId}, #{shopId}, #{platformId}, #{statDate},
#{salesAmount}, #{refundAmount}, #{salesOrderCount},
#{productCost}, #{freightCost}, #{otherCost},
#{gmvAmount}, #{gmvOrderCount}, #{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),
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
</insert>
<delete id="deleteDailyProduct">
DELETE FROM ec_order_stat_daily_product
WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date = #{statDate}
</delete>
<insert id="batchInsertDailyProduct">
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
) VALUES
<foreach collection="list" item="r" separator=",">
(#{r.companyId}, #{r.shopId}, #{r.productId}, #{r.categoryId}, #{r.statDate},
#{r.salesAmount}, #{r.refundAmount}, #{r.salesOrderCount},
#{r.productCost}, #{r.freightCost}, #{r.otherCost}, #{r.statVersion})
</foreach>
</insert>
<delete id="deleteDailySpec">
DELETE FROM ec_order_stat_daily_spec
WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date = #{statDate}
</delete>
<insert id="batchInsertDailySpec">
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
) VALUES
<foreach collection="list" item="r" separator=",">
(#{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})
</foreach>
</insert>
<!-- 月/年 rollup:从日表 SUM 写入对应粒度表 -->
<insert id="rollupMonthlyShop">
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
)
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)
FROM ec_order_stat_daily_shop
WHERE company_id = #{companyId} AND shop_id = #{shopId}
AND stat_date &gt;= #{statMonth}
AND stat_date &lt; DATE_ADD(#{statMonth}, INTERVAL 1 MONTH)
GROUP BY company_id, shop_id
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),
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
</insert>
<insert id="rollupYearlyShop">
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
)
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)
FROM ec_order_stat_monthly_shop
WHERE company_id = #{companyId} AND shop_id = #{shopId}
AND YEAR(stat_month) = #{statYear}
GROUP BY company_id, shop_id
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),
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
</insert>
<insert id="rollupMonthlyProduct">
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
)
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)
FROM ec_order_stat_daily_product
WHERE company_id = #{companyId} AND shop_id = #{shopId}
AND stat_date &gt;= #{statMonth}
AND stat_date &lt; DATE_ADD(#{statMonth}, INTERVAL 1 MONTH)
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),
product_cost = VALUES(product_cost),
freight_cost = VALUES(freight_cost),
other_cost = VALUES(other_cost),
stat_version = VALUES(stat_version),
update_time = CURRENT_TIMESTAMP
</insert>
<insert id="rollupYearlyProduct">
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
)
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)
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),
product_cost = VALUES(product_cost),
freight_cost = VALUES(freight_cost),
other_cost = VALUES(other_cost),
stat_version = VALUES(stat_version),
update_time = CURRENT_TIMESTAMP
</insert>
<insert id="rollupMonthlySpec">
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
)
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)
FROM ec_order_stat_daily_spec
WHERE company_id = #{companyId} AND shop_id = #{shopId}
AND stat_date &gt;= #{statMonth}
AND stat_date &lt; DATE_ADD(#{statMonth}, INTERVAL 1 MONTH)
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),
product_cost = VALUES(product_cost),
freight_cost = VALUES(freight_cost),
other_cost = VALUES(other_cost),
stat_version = VALUES(stat_version),
update_time = CURRENT_TIMESTAMP
</insert>
<insert id="rollupYearlySpec">
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
)
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)
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),
product_cost = VALUES(product_cost),
freight_cost = VALUES(freight_cost),
other_cost = VALUES(other_cost),
stat_version = VALUES(stat_version),
update_time = CURRENT_TIMESTAMP
</insert>
</mapper>