mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 15:49:19 +08:00
refactor(概览): 汇总字段 totalCost 重命名为 orderCost,列名统一为订单成本
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
<?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.mapper.OrderStatAggMapper">
|
||||
|
||||
<!-- 公司/店铺/时间(不含状态过滤,供 GMV 等全状态指标) -->
|
||||
<sql id="orderBaseFilter">
|
||||
o.company_id = #{companyId}
|
||||
<choose>
|
||||
<when test="shopIds != null and shopIds.size() > 0">
|
||||
AND o.shop_id IN
|
||||
<foreach collection="shopIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
</when>
|
||||
<otherwise>AND 1 = 0</otherwise>
|
||||
</choose>
|
||||
AND o.order_time >= #{start}
|
||||
AND o.order_time <= #{end}
|
||||
</sql>
|
||||
|
||||
<!-- 订单过滤 + 状态口径 CASE 表达式(与 OrderStatusStatSets 一致) -->
|
||||
<sql id="orderFilter">
|
||||
<include refid="orderBaseFilter"/>
|
||||
<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>
|
||||
</sql>
|
||||
|
||||
<sql id="qty">IFNULL(NULLIF(o.quantity, 0), 1)</sql>
|
||||
|
||||
<sql id="sumSales">
|
||||
SUM(CASE WHEN o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END)
|
||||
</sql>
|
||||
<sql id="sumOrderCount">
|
||||
SUM(CASE WHEN o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN 1 ELSE 0 END)
|
||||
</sql>
|
||||
<sql id="sumRefund">
|
||||
SUM(CASE WHEN o.status_id IN
|
||||
<foreach collection="refundStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END)
|
||||
</sql>
|
||||
<sql id="sumProductCost">
|
||||
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)
|
||||
</sql>
|
||||
<sql id="sumFreightCost">
|
||||
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)
|
||||
</sql>
|
||||
<sql id="sumOtherCost">
|
||||
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)
|
||||
</sql>
|
||||
|
||||
<select id="aggregateByShopProduct"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$ShopProductRow">
|
||||
SELECT o.shop_id AS shopId, s.platform_id AS platformId,
|
||||
IFNULL(ps.product_id, -1) AS productId, IFNULL(p.category_id, 0) AS categoryId,
|
||||
<include refid="sumSales"/> AS salesAmount,
|
||||
<include refid="sumOrderCount"/> AS orderCount,
|
||||
<include refid="sumRefund"/> AS refundAmount,
|
||||
<include refid="sumProductCost"/> AS productCost,
|
||||
<include refid="sumFreightCost"/> AS freightCost,
|
||||
<include refid="sumOtherCost"/> AS otherCost,
|
||||
SUM(o.received_amount) AS gmvAllStatus
|
||||
FROM ec_order o
|
||||
INNER JOIN ec_shop s ON s.id = o.shop_id
|
||||
LEFT JOIN ec_product_spec ps ON ps.id = o.product_spec_id
|
||||
LEFT JOIN ec_product p ON p.id = ps.product_id
|
||||
WHERE <include refid="orderBaseFilter"/>
|
||||
GROUP BY o.shop_id, s.platform_id, ps.product_id, p.category_id
|
||||
</select>
|
||||
|
||||
<select id="aggregateDaily"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$DailyRow">
|
||||
SELECT DATE(o.order_time) AS statDate,
|
||||
<include refid="sumSales"/> AS salesAmount,
|
||||
<include refid="sumRefund"/> AS refundAmount,
|
||||
<include refid="sumProductCost"/> AS productCost,
|
||||
<include refid="sumFreightCost"/> AS freightCost,
|
||||
<include refid="sumOtherCost"/> AS otherCost
|
||||
FROM ec_order o
|
||||
WHERE <include refid="orderFilter"/>
|
||||
GROUP BY DATE(o.order_time)
|
||||
ORDER BY statDate
|
||||
</select>
|
||||
|
||||
<select id="aggregatePeriodKpi"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$PeriodKpiRow">
|
||||
SELECT
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} AND o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END) AS curSalesAmount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} AND o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN 1 ELSE 0 END) AS curOrderCount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} AND o.status_id IN
|
||||
<foreach collection="refundStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END) AS curRefundAmount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} AND 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 curProductCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} AND 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 curFreightCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} AND 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 curOtherCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{curStart} AND #{curEnd} THEN o.received_amount ELSE 0 END) AS curGmvAllStatus,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} AND o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END) AS prevSalesAmount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} AND o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN 1 ELSE 0 END) AS prevOrderCount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} AND o.status_id IN
|
||||
<foreach collection="refundStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END) AS prevRefundAmount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} AND 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 prevProductCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} AND 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 prevFreightCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} AND 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 prevOtherCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{prevStart} AND #{prevEnd} THEN o.received_amount ELSE 0 END) AS prevGmvAllStatus,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} AND o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END) AS yoySalesAmount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} AND o.status_id IN
|
||||
<foreach collection="salesStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN 1 ELSE 0 END) AS yoyOrderCount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} AND o.status_id IN
|
||||
<foreach collection="refundStatusIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
THEN o.received_amount ELSE 0 END) AS yoyRefundAmount,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} AND 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 yoyProductCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} AND 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 yoyFreightCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} AND 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 yoyOtherCost,
|
||||
SUM(CASE WHEN o.order_time BETWEEN #{yoyStart} AND #{yoyEnd} THEN o.received_amount ELSE 0 END) AS yoyGmvAllStatus
|
||||
FROM ec_order o
|
||||
WHERE o.company_id = #{companyId}
|
||||
<choose>
|
||||
<when test="shopIds != null and shopIds.size() > 0">
|
||||
AND o.shop_id IN <foreach collection="shopIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
</when>
|
||||
<otherwise>AND 1 = 0</otherwise>
|
||||
</choose>
|
||||
AND o.order_time BETWEEN #{scanStart} AND #{scanEnd}
|
||||
<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>
|
||||
</select>
|
||||
|
||||
<select id="aggregatePromotionPeriodKpi"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$FeeKpiRow">
|
||||
SELECT
|
||||
SUM(CASE WHEN f.fee_date BETWEEN #{curStart} AND #{curEnd} THEN IFNULL(f.fee_amount,0) ELSE 0 END) AS curAmount,
|
||||
SUM(CASE WHEN f.fee_date BETWEEN #{prevStart} AND #{prevEnd} THEN IFNULL(f.fee_amount,0) ELSE 0 END) AS prevAmount,
|
||||
SUM(CASE WHEN f.fee_date BETWEEN #{yoyStart} AND #{yoyEnd} THEN IFNULL(f.fee_amount,0) ELSE 0 END) AS yoyAmount
|
||||
FROM ec_promotion_fee f
|
||||
WHERE f.company_id = #{companyId}
|
||||
<choose>
|
||||
<when test="shopIds != null and shopIds.size() > 0">
|
||||
AND f.shop_id IN <foreach collection="shopIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
</when>
|
||||
<otherwise>AND 1 = 0</otherwise>
|
||||
</choose>
|
||||
AND f.fee_date BETWEEN #{scanStart} AND #{scanEnd}
|
||||
</select>
|
||||
|
||||
<select id="aggregateBrushPeriodKpi"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$FeeKpiRow">
|
||||
SELECT
|
||||
SUM(CASE WHEN b.brush_time BETWEEN #{curStart} AND #{curEnd} THEN IFNULL(b.brush_amount,0) ELSE 0 END) AS curAmount,
|
||||
SUM(CASE WHEN b.brush_time BETWEEN #{prevStart} AND #{prevEnd} THEN IFNULL(b.brush_amount,0) ELSE 0 END) AS prevAmount,
|
||||
SUM(CASE WHEN b.brush_time BETWEEN #{yoyStart} AND #{yoyEnd} THEN IFNULL(b.brush_amount,0) ELSE 0 END) AS yoyAmount
|
||||
FROM ec_brush_order b
|
||||
WHERE b.company_id = #{companyId}
|
||||
<choose>
|
||||
<when test="shopIds != null and shopIds.size() > 0">
|
||||
AND b.shop_id IN <foreach collection="shopIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
</when>
|
||||
<otherwise>AND 1 = 0</otherwise>
|
||||
</choose>
|
||||
AND b.brush_time BETWEEN #{scanStart} AND #{scanEnd}
|
||||
</select>
|
||||
|
||||
<select id="aggregatePromotionFees"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$PromotionRow">
|
||||
SELECT f.shop_id AS shopId, f.product_id AS productId, f.category_id AS categoryId,
|
||||
SUM(IFNULL(f.fee_amount, 0)) AS feeAmount
|
||||
FROM ec_promotion_fee f
|
||||
WHERE f.company_id = #{companyId}
|
||||
<choose>
|
||||
<when test="shopIds != null and shopIds.size() > 0">
|
||||
AND f.shop_id IN <foreach collection="shopIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
</when>
|
||||
<otherwise>AND 1 = 0</otherwise>
|
||||
</choose>
|
||||
AND f.fee_date BETWEEN #{startDate} AND #{endDate}
|
||||
GROUP BY f.shop_id, f.product_id, f.category_id
|
||||
</select>
|
||||
|
||||
<select id="aggregateBrushFees"
|
||||
resultType="top.xyquan.ecomdmbbackend.modules.ecom.order.mapper.OrderStatAggMapper$BrushRow">
|
||||
SELECT b.shop_id AS shopId, SUM(IFNULL(b.brush_amount, 0)) AS brushAmount
|
||||
FROM ec_brush_order b
|
||||
WHERE b.company_id = #{companyId}
|
||||
<choose>
|
||||
<when test="shopIds != null and shopIds.size() > 0">
|
||||
AND b.shop_id IN <foreach collection="shopIds" item="id" open="(" separator="," close=")">#{id}</foreach>
|
||||
</when>
|
||||
<otherwise>AND 1 = 0</otherwise>
|
||||
</choose>
|
||||
AND b.brush_time BETWEEN #{start} AND #{end}
|
||||
GROUP BY b.shop_id
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user