Files
ecom-dmb/ecom-dmb-backend/src/main/resources/mapper/order/stat/OrderStatDailyMapper.xml
T

273 lines
13 KiB
XML

<?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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost,
stat_version
) VALUES (
#{companyId}, #{shopId}, #{platformId}, #{statDate},
#{salesAmount}, #{salesOrderCount}, #{salesQuantitySum},
#{gmvAmount}, #{gmvOrderCount}, #{gmvQuantitySum},
#{refundAmount}, #{productCost}, #{freightCost}, #{otherCost},
#{statVersion}
)
ON DUPLICATE KEY UPDATE
platform_id = VALUES(platform_id),
sales_amount = VALUES(sales_amount),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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>
<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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, 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.salesOrderCount}, #{r.salesQuantitySum},
#{r.gmvAmount}, #{r.gmvOrderCount}, #{r.gmvQuantitySum},
#{r.refundAmount}, #{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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, 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.salesOrderCount}, #{r.salesQuantitySum},
#{r.gmvAmount}, #{r.gmvOrderCount}, #{r.gmvQuantitySum},
#{r.refundAmount}, #{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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost, stat_version
)
SELECT company_id, shop_id, MAX(platform_id),
#{statMonth},
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),
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),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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="rollupYearlyShop">
INSERT INTO ec_order_stat_yearly_shop (
company_id, shop_id, platform_id, stat_year,
sales_amount, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost, stat_version
)
SELECT company_id, shop_id, MAX(platform_id),
#{statYear},
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),
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),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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="rollupMonthlyProduct">
INSERT INTO ec_order_stat_monthly_product (
company_id, shop_id, product_id, category_id, stat_month,
sales_amount, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost, stat_version
)
SELECT company_id, shop_id, product_id, category_id,
#{statMonth},
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),
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),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost, stat_version
)
SELECT company_id, shop_id, product_id, category_id,
#{statYear},
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),
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),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost, stat_version
)
SELECT company_id, shop_id, product_id, product_spec_id,
#{statMonth},
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),
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),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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, sales_order_count, sales_quantity_sum,
gmv_amount, gmv_order_count, gmv_quantity_sum,
refund_amount, product_cost, freight_cost, other_cost, stat_version
)
SELECT company_id, shop_id, product_id, product_spec_id,
#{statYear},
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),
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),
sales_order_count = VALUES(sales_order_count),
sales_quantity_sum = VALUES(sales_quantity_sum),
gmv_amount = VALUES(gmv_amount),
gmv_order_count = VALUES(gmv_order_count),
gmv_quantity_sum = VALUES(gmv_quantity_sum),
refund_amount = VALUES(refund_amount),
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>