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
@@ -41,6 +41,21 @@ spring:
min-idle: 0
max-wait: -1ms
# 订单统计读模型
order-stat:
stat-source: daily
auto-refresh-on-miss: true
refresh-worker:
enabled: true
pool-size: 4
poll-interval-ms: 2000
cache:
enabled: true
ttl-history-seconds: 86400
ttl-today-seconds: 900
nightly-rebuild-enabled: false
nightly-rebuild-cron: "0 30 2 * * ?"
# JWT配置
# ⚠️ 生产环境必须通过环境变量 JWT_SECRET 覆盖,默认值仅用于本地开发
jwt:
@@ -0,0 +1,94 @@
<?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.FeeStatDailyMapper">
<delete id="deletePromotionDaily">
DELETE FROM ec_promotion_fee_stat_daily
WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date = #{statDate}
</delete>
<insert id="batchInsertPromotionDaily">
INSERT INTO ec_promotion_fee_stat_daily (
company_id, shop_id, product_id, category_id, stat_date, fee_amount
) VALUES
<foreach collection="list" item="r" separator=",">
(#{r.companyId}, #{r.shopId}, #{r.productId}, #{r.categoryId}, #{r.statDate}, #{r.feeAmount})
</foreach>
</insert>
<insert id="rollupPromotionMonthly">
INSERT INTO ec_promotion_fee_stat_monthly (
company_id, shop_id, product_id, category_id, stat_month, fee_amount
)
SELECT company_id, shop_id, product_id, category_id, #{statMonth}, SUM(fee_amount)
FROM ec_promotion_fee_stat_daily
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 fee_amount = VALUES(fee_amount), update_time = CURRENT_TIMESTAMP
</insert>
<insert id="rollupPromotionYearly">
INSERT INTO ec_promotion_fee_stat_yearly (
company_id, shop_id, product_id, category_id, stat_year, fee_amount
)
SELECT company_id, shop_id, product_id, category_id, #{statYear}, SUM(fee_amount)
FROM ec_promotion_fee_stat_monthly
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 fee_amount = VALUES(fee_amount), update_time = CURRENT_TIMESTAMP
</insert>
<delete id="deleteBrushDaily">
DELETE FROM ec_brush_fee_stat_daily
WHERE company_id = #{companyId} AND shop_id = #{shopId} AND stat_date = #{statDate}
</delete>
<insert id="upsertBrushDaily">
INSERT INTO ec_brush_fee_stat_daily (company_id, shop_id, stat_date, brush_amount)
VALUES (#{companyId}, #{shopId}, #{statDate}, #{brushAmount})
ON DUPLICATE KEY UPDATE brush_amount = VALUES(brush_amount), update_time = CURRENT_TIMESTAMP
</insert>
<insert id="rollupBrushMonthly">
INSERT INTO ec_brush_fee_stat_monthly (company_id, shop_id, stat_month, brush_amount)
SELECT company_id, shop_id, #{statMonth}, SUM(brush_amount)
FROM ec_brush_fee_stat_daily
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 brush_amount = VALUES(brush_amount), update_time = CURRENT_TIMESTAMP
</insert>
<insert id="rollupBrushYearly">
INSERT INTO ec_brush_fee_stat_yearly (company_id, shop_id, stat_year, brush_amount)
SELECT company_id, shop_id, #{statYear}, SUM(brush_amount)
FROM ec_brush_fee_stat_monthly
WHERE company_id = #{companyId} AND shop_id = #{shopId} AND YEAR(stat_month) = #{statYear}
GROUP BY company_id, shop_id
ON DUPLICATE KEY UPDATE brush_amount = VALUES(brush_amount), update_time = CURRENT_TIMESTAMP
</insert>
<select id="aggregatePromotionByShopDay"
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.mapper.FeeStatDailyMapper$PromotionFeeAggRow">
SELECT
IFNULL(f.product_id, 0) AS productId,
IFNULL(f.category_id, 0) AS categoryId,
SUM(IFNULL(f.fee_amount, 0)) AS feeAmount
FROM ec_promotion_fee f
WHERE f.company_id = #{companyId}
AND f.shop_id = #{shopId}
AND f.fee_date = #{statDate}
GROUP BY IFNULL(f.product_id, 0), IFNULL(f.category_id, 0)
</select>
<select id="aggregateBrushByShopDay" resultType="java.math.BigDecimal">
SELECT IFNULL(SUM(IFNULL(b.brush_amount, 0)), 0)
FROM ec_brush_order b
WHERE b.company_id = #{companyId}
AND b.shop_id = #{shopId}
AND DATE(b.brush_time) = #{statDate}
</select>
</mapper>
@@ -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>
@@ -0,0 +1,284 @@
<?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.OrderStatQueryMapper">
<sql id="shopFilter">
company_id = #{companyId}
AND shop_id IN
<foreach collection="shopIds" item="sid" open="(" separator="," close=")">#{sid}</foreach>
</sql>
<select id="sumDailyShop" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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,
COALESCE(SUM(gmv_amount), 0) AS gmvAmount,
COALESCE(SUM(gmv_order_count), 0) AS gmvOrderCount
FROM ec_order_stat_daily_shop
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
</select>
<select id="sumDailyShopByDay" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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
FROM ec_order_stat_daily_shop
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
GROUP BY stat_date
ORDER BY stat_date
</select>
<select id="sumDailyShopByShop" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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
FROM ec_order_stat_daily_shop
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
GROUP BY shop_id
</select>
<select id="sumDailyProductByCategory" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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_daily_product
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
GROUP BY category_id
</select>
<select id="sumDailyProductByProduct" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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_daily_product
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
GROUP BY product_id
</select>
<select id="sumDailyProductByShopProduct" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
SELECT
shop_id AS shopId,
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_daily_product
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
GROUP BY shop_id, product_id
</select>
<select id="sumDailyProductForOneProduct" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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}
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
<if test="shopIds != null and shopIds.size() &gt; 0">
AND shop_id IN
<foreach collection="shopIds" item="sid" open="(" separator="," close=")">#{sid}</foreach>
</if>
</select>
<select id="sumDailyProductByDayForOneProduct" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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
FROM ec_order_stat_daily_product
WHERE company_id = #{companyId}
AND product_id = #{productId}
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
<if test="shopIds != null and shopIds.size() &gt; 0">
AND shop_id IN
<foreach collection="shopIds" item="sid" open="(" separator="," close=")">#{sid}</foreach>
</if>
GROUP BY stat_date
ORDER BY stat_date
</select>
<select id="sumMonthlyProductByCategory" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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 <include refid="shopFilter"/>
AND stat_month &gt;= #{startMonth}
AND stat_month &lt;= #{endMonth}
GROUP BY category_id
</select>
<select id="sumMonthlyProductByProduct" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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 <include refid="shopFilter"/>
AND stat_month &gt;= #{startMonth}
AND stat_month &lt;= #{endMonth}
GROUP BY product_id
</select>
<select id="sumYearlyProductByCategory" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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_yearly_product
WHERE <include refid="shopFilter"/>
AND stat_year &gt;= #{startYear}
AND stat_year &lt;= #{endYear}
GROUP BY category_id
</select>
<select id="sumYearlyProductByProduct" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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_yearly_product
WHERE <include refid="shopFilter"/>
AND stat_year &gt;= #{startYear}
AND stat_year &lt;= #{endYear}
GROUP BY product_id
</select>
<select id="sumDailySpecBySpec" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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 <include refid="shopFilter"/>
AND product_id = #{productId}
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
GROUP BY product_spec_id
</select>
<select id="sumMonthlySpecBySpec" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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_monthly_spec
WHERE <include refid="shopFilter"/>
AND product_id = #{productId}
AND stat_month &gt;= #{startMonth}
AND stat_month &lt;= #{endMonth}
GROUP BY product_spec_id
</select>
<select id="sumYearlySpecBySpec" resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.model.dto.OrderStatMetricsDTO">
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 <include refid="shopFilter"/>
AND product_id = #{productId}
AND stat_year &gt;= #{startYear}
AND stat_year &lt;= #{endYear}
GROUP BY product_spec_id
</select>
<select id="countDailyShopRows" resultType="int">
SELECT COUNT(*)
FROM ec_order_stat_daily_shop
WHERE <include refid="shopFilter"/>
AND stat_date &gt;= #{startDate}
AND stat_date &lt;= #{endDate}
</select>
</mapper>
@@ -0,0 +1,78 @@
<?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.OrderStatRefreshMapper">
<sql id="qty">IFNULL(NULLIF(o.quantity, 0), 1)</sql>
<insert id="insertPendingIgnore">
INSERT IGNORE INTO ec_order_stat_refresh_queue (
company_id, shop_id, stat_date, source, status, retry_count
) VALUES (
#{companyId}, #{shopId}, #{statDate}, #{source}, 'pending', 0
)
</insert>
<update id="markDone">
UPDATE ec_order_stat_refresh_queue SET status = 'done', update_time = CURRENT_TIMESTAMP
WHERE id = #{id}
</update>
<update id="markFailed">
UPDATE ec_order_stat_refresh_queue
SET status = 'failed', retry_count = #{retryCount}, update_time = CURRENT_TIMESTAMP
WHERE id = #{id}
</update>
<!-- fetchAndLockPending 由 Service 层 MyBatis-Plus + FOR UPDATE SKIP LOCKED 实现 -->
<select id="countGmvOrdersByShopDay" resultType="java.lang.Integer">
SELECT COUNT(*)
FROM ec_order o
WHERE o.company_id = #{companyId}
AND o.shop_id = #{shopId}
AND o.order_time &gt;= #{start}
AND o.order_time &lt;= #{end}
</select>
<select id="aggregateSpecByShopDay"
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.stat.mapper.OrderStatRefreshMapper$SpecDayRow">
SELECT
IFNULL(ps.product_id, 0) AS productId,
IFNULL(o.product_spec_id, 0) AS productSpecId,
SUM(CASE WHEN o.status_id IN
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN o.received_amount ELSE 0 END) AS salesAmount,
SUM(CASE WHEN o.status_id IN
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN 1 ELSE 0 END) AS orderCount,
SUM(CASE WHEN o.status_id IN
<foreach collection="refundStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN o.received_amount ELSE 0 END) AS refundAmount,
SUM(CASE WHEN o.status_id IN
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN <include refid="qty"/> ELSE 0 END) AS quantitySum,
SUM(CASE WHEN o.status_id IN
<foreach collection="productCostStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN o.product_cost * <include refid="qty"/> ELSE 0 END) AS productCost,
SUM(CASE WHEN o.status_id IN
<foreach collection="freightCostStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN o.shipping_cost * <include refid="qty"/> ELSE 0 END) AS freightCost,
SUM(CASE WHEN o.status_id IN
<foreach collection="otherCostStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
THEN o.other_cost * <include refid="qty"/> ELSE 0 END) AS otherCost
FROM ec_order o
LEFT JOIN ec_product_spec ps ON ps.id = o.product_spec_id
WHERE o.company_id = #{companyId}
AND o.shop_id = #{shopId}
AND o.order_time &gt;= #{start}
AND o.order_time &lt;= #{end}
<choose>
<when test="unionStatusIds != null and unionStatusIds.size() > 0">
AND o.status_id IN
<foreach collection="unionStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
</when>
<otherwise>AND 1 = 0</otherwise>
</choose>
GROUP BY IFNULL(o.product_spec_id, 0), IFNULL(ps.product_id, 0)
</select>
</mapper>