<?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="org.dromara.mall.statistics.mapper.MallStatisticsMapper">

    <!-- 统计订单总数和总金额 -->
    <select id="countOrderStatistics" resultType="java.util.HashMap">
        SELECT
            COUNT(*) as totalOrderCount,
            IFNULL(SUM(total_amount), 0) as totalSalesAmount
        FROM mall_order
        <where>
            <if test="startDate != null">
                AND create_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND create_time &lt;= #{endDate}
            </if>
        </where>
    </select>

    <!-- 统计已支付订单数和金额 -->
    <select id="countPaidOrderStatistics" resultType="java.util.HashMap">
        SELECT
            COUNT(*) as paidOrderCount,
            IFNULL(SUM(pay_amount), 0) as paidAmount
        FROM mall_order
        <where>
            pay_status = 1
            <if test="startDate != null">
                AND create_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND create_time &lt;= #{endDate}
            </if>
        </where>
    </select>

    <!-- 统计退款金额 -->
    <select id="sumRefundAmount" resultType="java.lang.Long">
        SELECT IFNULL(SUM(refund_amount), 0)
        FROM mall_after_sale
        <where>
            status = 5
            <if test="startDate != null">
                AND refund_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND refund_time &lt;= #{endDate}
            </if>
        </where>
    </select>

    <!-- 统计每日订单数据 -->
    <select id="countDailyOrderStatistics" resultType="org.dromara.mall.statistics.domain.vo.TransactionStatisticsVo$DailyStatisticsVo">
        SELECT
            DATE_FORMAT(create_time, '%Y-%m-%d') as date,
            COUNT(*) as orderCount,
            IFNULL(SUM(total_amount), 0) as salesAmount
        FROM mall_order
        <where>
            <if test="startDate != null">
                AND create_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND create_time &lt;= #{endDate}
            </if>
        </where>
        GROUP BY DATE_FORMAT(create_time, '%Y-%m-%d')
        ORDER BY date ASC
    </select>

    <!-- 统计商品销售排行 -->
    <select id="queryProductSalesRank" resultType="org.dromara.mall.statistics.domain.vo.TransactionStatisticsVo$ProductSalesRankVo">
        SELECT
            oi.product_id as productId,
            oi.product_name as productName,
            oi.sku_id as skuId,
            oi.sku_name as skuName,
            SUM(oi.quantity) as salesQuantity,
            IFNULL(SUM(oi.total_amount), 0) as salesAmount
        FROM mall_order_item oi
        INNER JOIN mall_order o ON oi.order_id = o.id
        <where>
            o.pay_status = 1
            <if test="startDate != null">
                AND o.create_time &gt;= #{startDate}
            </if>
            <if test="endDate != null">
                AND o.create_time &lt;= #{endDate}
            </if>
        </where>
        GROUP BY oi.product_id, oi.product_name, oi.sku_id, oi.sku_name
        ORDER BY salesAmount DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>

</mapper>
