<?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="shop.alien.mapper.ManagementInfoMapper">

    <select id="getAllManagementInfo" resultType="shop.alien.entity.store.ManagementInfo">
        SELECT
            store.id AS store_id,
            store.store_name,
            IFNULL(meal.coupon_count, 0)  AS meal_coupon_count,
            IFNULL(meal.order_count, 0) AS meal_order_count,
            ROUND(IFNULL(meal.order_amount, 0) / 100,2) AS meal_order_amount,
            IFNULL(meal.income_count, 0) AS meal_income_count,
            ROUND(IFNULL(meal.income_money, 0) / 100,2) AS meal_income_money,
            IFNULL(coupon.coupon_count, 0) AS coupon_coupon_count,
            IFNULL(coupon.order_count, 0) AS coupon_order_count,
            ROUND(IFNULL(coupon.order_amount, 0) / 100,2) AS coupon_order_amount,
            IFNULL(coupon.income_count, 0) AS coupon_income_count,
            ROUND(IFNULL(coupon.income_money, 0) / 100,2) AS coupon_income_money,
            ROUND(IFNULL(allInfo.cash_out_money, 0) / 100,2) AS cashout_money,
            ROUND(IFNULL(allInfo.account_frozen, 0) / 100,2) AS account_frozen
        FROM
            store_info store
                LEFT JOIN merchant_reconciliation_coupon coupon ON store.id = coupon.store_id
                LEFT JOIN merchant_reconciliation_meal meal ON store.id = meal.store_id
                LEFT JOIN merchant_reconciliation allInfo ON store.id = allInfo.store_id
        where 1 = 1
        <if test="storeName != null and storeName != ''">
            AND store.store_name LIKE CONCAT('%', #{storeName}, '%')
        </if>
    </select>
    <select id="getBillingRanking" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            store.id,
            store.store_name,
            ROUND(IFNULL( sum( money ), 0 ) / 100,2) AS income_money
        FROM
            store_info store
                LEFT JOIN ( SELECT * FROM store_income_details_record WHERE created_time > #{startDate} AND created_time &lt; #{endDate} ) income ON store.id = income.store_id
        WHERE store.delete_flag = 0
        GROUP BY
            store.id
        ORDER BY
            income_money DESC
    </select>
    <select id="getAmountPaid" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            ROUND(IFNULL(SUM( money ),0) / 100,2) AS amountPaid
        FROM
            store_cash_out_record cash
        WHERE
            created_time > #{startDate} AND created_time &lt; #{endDate}

    </select>
    <select id="getTotalTransactionAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        ROUND(IFNULL(SUM( money ),0) / 100,2) AS totalTransactionAmount
        FROM
        store_income_details_record income
        where
        1 = 1
        <if test="storeId != null and storeId != ''">
            AND store_id = #{storeId}
        </if>
    </select>
    <select id="getTransactionAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        ROUND(IFNULL(SUM( money ),0) / 100,2) AS totalTransactionAmount
        FROM
        store_income_details_record income
        WHERE
        created_time > #{startDate} AND created_time &lt; #{endDate}
    </select>
    <select id="getTransactionNumber" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        COUNT(1) AS transactionNumber
        FROM
        store_cash_out_record cash
        WHERE
            created_time > #{startDate} AND created_time &lt; #{endDate}
    </select>
    <select id="getAllOrderAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            ROUND(IFNULL(SUM( final_price ),0) / 100,2) AS allOrderAmount
        FROM
            life_user_order
        where
            1 = 1
        <if test="storeId != null and storeId != ''">
            AND store_id = #{storeId}
        </if>
    </select>
    <select id="getOrderAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        ROUND(IFNULL(SUM( final_price ),0),2) AS allOrderAmount
        FROM
        life_user_order
        where
        created_time > #{startDate} AND created_time &lt; #{endDate}
    </select>
    <select id="getAllWrittenOffAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            ROUND(IFNULL(SUM( money ),0) / 100,2) AS allWrittenOffAmount
        FROM
            store_income_details_record
        where
        1 = 1
        <if test="storeId != null and storeId != ''">
            AND store_id = #{storeId}
        </if>
    </select>
    <select id="getWrittenOffAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        ROUND(IFNULL(SUM( money ),0) / 100,2) AS allWrittenOffAmount
        FROM
        store_income_details_record
        where
        created_time > #{startDate} AND created_time &lt; #{endDate}
    </select>
    <select id="getTotalWithdrawalAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            ROUND(IFNULL(SUM( money ),0) / 100,2) AS totalWithdrawalAmount
        FROM
            store_cash_out_record cash
        where
        1 = 1
        <if test="storeId != null and storeId != ''">
            AND store_id = #{storeId}
        </if>
    </select>
    <select id="getWithdrawalAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        ROUND(IFNULL(SUM( money ),0) / 100,2) AS totalWithdrawalAmount
        FROM
        store_cash_out_record cash
        where payment_status = 1 and
        created_time > #{startDate} AND created_time &lt; #{endDate}
    </select>
    <select id="getStoreOrderDetail" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            userOrder.id,
            userOrder.user_id,
            userInfo.user_name,
            coupon.type,
            CASE
                type
                WHEN 1 THEN
                    "代金券"
                WHEN 2 THEN
                    "套餐"
                END AS type_name,
            coupon.`name`,
            "1" AS buy_count,
            ROUND(userOrder.price ,2) AS buy_amount,
            userOrder.buy_time AS buy_time,
            userOrder.used_time AS used_time,
            userOrder.refund_time AS refund_time,
            userOrder.`status` AS `status`,
            CASE
                coupon.STATUS
                WHEN 0 THEN
                    "待使用"
                WHEN 1 THEN
                    "已核销"
                WHEN 2 THEN
                    "已过期"
                WHEN 3 THEN
                    "待退款"
                WHEN 4 THEN
                    "已退款"
                END AS status_name,
        userOrder.order_no,
        userOrder.coupon_type
        FROM
            life_user_order userOrder
                LEFT JOIN life_coupon coupon ON coupon.id = userOrder.id
                LEFT JOIN life_user userInfo ON userOrder.user_id = userInfo.id
        WHERE
            userOrder.store_id = #{storeId}
        <if test = "orderNo != null and orderNo != ''" >
            AND userOrder.order_no = #{orderNo}
        </if >
        <if test = "userId != null and userId != ''" >
            AND userOrder.user_id = #{userId}
        </if >
        <if test = "userName != null and userName != ''" >
            AND userInfo.user_name like concat('%',#{userName},'%')
        </if >
    </select>
    <select id="getUserCount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        count( 1 ) AS user_count
        FROM
        ( SELECT DISTINCT user_id FROM life_user_order
        WHERE 1 = 1
        <if test = "storeId != null and storeId != ''" >
            AND store_id = #{storeId}
        </if >
        ) a
    </select>
    <select id="getOrderTransactionNumber" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            count( 1 ) AS order_transaction_number
        FROM
            life_user_order
        WHERE
            1 = 1
        <if test = "storeId != null and storeId != ''" >
            AND store_id = #{storeId}
        </if >
    </select>
    <select id="getVerificationAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        ROUND(IFNULL(SUM(money),0) / 100,2) AS verification_amount
        FROM
        store_income_details_record
        WHERE 1 = 1
        <if test = "storeId != null and storeId != ''" >
          AND store_id = #{storeId}
        </if >

    </select>
    <select id="getApplicationForWithdrawal" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            cash.id AS cash_id,
            store.store_name AS store_name ,
            storeUser.phone AS store_phone ,
            store.id AS store_id,
            cash.cash_out_type,
            CASE
                cash.cash_out_type
                WHEN 0 THEN
                    "手动提现"
                WHEN 1 THEN
                    "到期自动提现"
                END AS cash_out_type_name,
            ROUND(IFNULL(cash.money, 0) / 100,2) AS money,
            cash.created_time,
            cash.payment_date,
            cash.approve_time,
            cash.pay_date,
            cash.fail_reason,
            cash.payment_status
        FROM
            store_cash_out_record cash
        LEFT JOIN store_info store ON cash.store_id = store.id
        LEFT JOIN store_user storeUser ON storeUser.store_id = store.id
        WHERE 1 = 1
        <if test = "storeId != null and storeId != ''" >
            AND cash.store_id = #{storeId}
        </if >
        <if test="storeName != null and storeName != ''">
            AND store.store_name LIKE CONCAT('%', #{storeName}, '%')
        </if>
        ORDER BY cash.created_time DESC
    </select>

    <select id="getCashOutDetail" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
        cash.id AS cash_id,
        store.store_name AS store_name ,
        storeUser.phone AS store_phone ,
        store.id AS store_id,
        cash.cash_out_type,
        CASE
        cash.cash_out_type
        WHEN 0 THEN
        "手动提现"
        WHEN 1 THEN
        "到期自动提现"
        END AS cash_out_type_name,
        ROUND(IFNULL(cash.money, 0) / 100,2) AS money,
        cash.created_time,
        cash.payment_date,
        cash.approve_time,
        cash.pay_date,
        cash.fail_reason,
        cash.payment_status,
        cash.approve_fail_reason
        FROM
        store_cash_out_record cash
        LEFT JOIN store_info store ON cash.store_id = store.id
        LEFT JOIN store_user storeUser ON storeUser.store_id = store.id
        WHERE cash.id = #{id}
    </select>

    <select id="getPlatformNetProfit" resultType="shop.alien.entity.store.vo.ManagementInfoVo">

        select IFNULL(ROUND(sum(sidr.commission)/100,2), 0) AS profit
        from store_income_details_record sidr
        where 1=1
        <if test="dataTime != null and dataTime != ''">
            AND
            sidr.created_time > #{dataTime}
        </if>
    </select>
    <select id="getOrderQuantity" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        select count(*) count
        from life_user_order luo
        where luo.status in (1, 7)
        <if test="dataTime != null and dataTime != ''">
            and luo.pay_time > #{dataTime}
        </if>
    </select>
    <select id="getTradingVolume" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
       select IFNULL(ROUND(sum(all_data.profit_a), 2 ),0) profit from (
        SELECT
            IFNULL(ROUND(luo.final_price , 2 ),0) AS profit_a
        FROM
            life_user_order luo
        WHERE
            luo.status in (1, 7)
            <if test="dataTime != null and dataTime != ''">
                and luo.pay_time > #{dataTime}
            </if>
        ) all_data
    </select>
    <select id="getQueryNetProfitDay" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        -- 使用递归 CTE 生成 1 到 23 的小时序列
        WITH RECURSIVE hours AS (
        SELECT 0 AS hour_num
        UNION ALL
        SELECT hour_num + 1
        FROM hours
        WHERE hour_num &lt; 23
        ),
        order_data AS (
        SELECT
        sidr.commission,
        sidr.created_time
        FROM store_income_details_record sidr where 1=1
        <if test="dataTime != null and dataTime != ''">
            AND sidr.created_time >= #{dataTime}
        </if>
        )

        SELECT
        concat(h.hour_num,":00") AS hour_num,
        COALESCE(IFNULL(ROUND(sum(o.commission)/100,2), 0)) AS profit
        FROM
        hours h
        LEFT JOIN order_data o ON h.hour_num = HOUR(o.created_time)
        GROUP BY
        h.hour_num
        ORDER BY
        h.hour_num;
    </select>
    <select id="getQueryNetProfitWeek" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE date_sequence AS (
        SELECT DATE_SUB(#{dataTime}, INTERVAL WEEKDAY(#{dataTime}) DAY) AS hour_num
        UNION ALL
        SELECT hour_num + INTERVAL 1 DAY
        FROM date_sequence
        WHERE hour_num &lt; DATE_SUB(#{dataTime}, INTERVAL WEEKDAY(#{dataTime}) DAY) + INTERVAL 6 DAY
        ),
        order_data AS (
        SELECT
        sidr.commission,
        sidr.created_time
        FROM store_income_details_record sidr
        )
        SELECT
        DATE_FORMAT(ds.hour_num, '%c-%d') AS hour_num,
        COALESCE(IFNULL(ROUND(sum(o.commission)/100,2), 0)) AS profit
        FROM
        date_sequence ds
        LEFT JOIN order_data o ON
        o.created_time >= ds.hour_num
        AND o.created_time &lt; ds.hour_num + INTERVAL 1 DAY
        GROUP BY
        ds.hour_num
        ORDER BY
        ds.hour_num;
    </select>
    <select id="getQueryNetProfitMonth" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE date_sequence AS (
        SELECT DATE_FORMAT(#{dataTime}, '%Y-%m-01') AS hour_num
        UNION ALL
        SELECT DATE_ADD(hour_num, INTERVAL 1 DAY)
        FROM date_sequence
        WHERE DATE_ADD(hour_num, INTERVAL 1 DAY)  &lt;= LAST_DAY(#{dataTime})
        ),
        order_data AS (
        SELECT
        sidr.commission,
        sidr.created_time
        FROM store_income_details_record sidr
        )
        SELECT
        DATE_FORMAT(ds.hour_num, '%c-%d') AS hour_num,
        COALESCE(IFNULL(ROUND(sum(o.commission)/100,2), 0)) AS profit
        FROM
        date_sequence ds
        LEFT JOIN order_data o ON
        o.created_time >= ds.hour_num AND o.created_time &lt; DATE_ADD(ds.hour_num, INTERVAL 1 DAY)
        GROUP BY
        ds.hour_num
        ORDER BY
        ds.hour_num;
    </select>
    <select id="getQueryNetProfitYear" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE month_sequence AS (
        SELECT 1 AS hour_num
        UNION ALL
        SELECT hour_num + 1
        FROM month_sequence
        WHERE hour_num &lt; 12
        ),
        order_data AS (
        SELECT
        sidr.commission,
        sidr.created_time
        FROM store_income_details_record sidr
        )
        SELECT
        CONCAT(ms.hour_num,'月') AS hour_num,
        COALESCE(IFNULL(ROUND(sum(o.commission)/100,2), 0)) AS profit
        FROM
        month_sequence ms
        LEFT JOIN order_data o ON
        MONTH(o.created_time) = ms.hour_num
        AND YEAR(o.created_time) = YEAR(#{dataTime})
        GROUP BY
        ms.hour_num
        ORDER BY
        ms.hour_num;
    </select>
    <select id="getTradingVolumeDay" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE hours AS (
        SELECT 0 AS hour_num
        UNION ALL
        SELECT hour_num + 1
        FROM hours
        WHERE hour_num &lt; 23
        ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
        concat(h.hour_num,":00") AS hour_num,
        COALESCE(ROUND(SUM(o.final_price), 2), 0) AS profit
        FROM
        hours h
        LEFT JOIN order_data o
        ON ( h.hour_num = HOUR( o.used_time ) AND ( o.`status` = 2 AND o.used_time >= #{dataTime} ) )
        OR ( h.hour_num = HOUR( o.pay_time ) AND ( o.`status` = 1 AND o.pay_time >= #{dataTime} ) )
        GROUP BY
        h.hour_num
        ORDER BY
        h.hour_num;
    </select>
    <select id="getTradingVolumeWeek" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE date_sequence AS (
            SELECT DATE_SUB(#{dataTime}, INTERVAL WEEKDAY(#{dataTime}) DAY) AS hour_num
            UNION ALL
            SELECT hour_num + INTERVAL 1 DAY
        FROM date_sequence
        WHERE hour_num &lt; DATE_SUB(#{dataTime}, INTERVAL WEEKDAY(#{dataTime}) DAY) + INTERVAL 6 DAY
            ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
            DATE_FORMAT(ds.hour_num, '%c-%d') AS hour_num,
            COALESCE(ROUND(SUM(o.final_price), 2), 0) AS profit
        FROM
            date_sequence ds
                LEFT JOIN
        order_data o
            ON
                (o.used_time >= ds.hour_num
                AND o.used_time &lt; ds.hour_num + INTERVAL 1 DAY
                AND o.`status` = 2 )
            OR
                (o.pay_time >= ds.hour_num
                AND o.pay_time &lt; ds.hour_num + INTERVAL 1 DAY
                AND o.`status` = 1 )
        GROUP BY
            ds.hour_num
        ORDER BY
            ds.hour_num;
    </select>
    <select id="getTradingVolumeMonth" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE date_sequence AS (
            SELECT DATE_FORMAT(#{dataTime}, '%Y-%m-01') AS hour_num
            UNION ALL
            SELECT DATE_ADD(hour_num, INTERVAL 1 DAY)
            FROM date_sequence
            WHERE DATE_ADD(hour_num, INTERVAL 1 DAY)  &lt;= LAST_DAY(#{dataTime})
        ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
            DATE_FORMAT(ds.hour_num, '%c-%d') AS hour_num,
            COALESCE(ROUND(SUM(o.final_price), 2), 0) AS profit
        FROM
            date_sequence ds
            LEFT JOIN
            order_data o
            ON
                (o.used_time >= ds.hour_num
                AND o.used_time &lt; DATE_ADD( ds.hour_num, INTERVAL 1 DAY )
                AND o.`status` = 2 )
            OR
                (o.pay_time >= ds.hour_num
                AND o.pay_time &lt; DATE_ADD( ds.hour_num, INTERVAL 1 DAY )
                AND o.`status` = 1 )
        GROUP BY
            ds.hour_num
        ORDER BY
            ds.hour_num;
    </select>
    <select id="getTradingVolumeYear" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE month_sequence AS (
            SELECT 1 AS hour_num
            UNION ALL
            SELECT hour_num + 1
            FROM month_sequence
            WHERE hour_num &lt; 12
        ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
            CONCAT(ms.hour_num,'月') AS hour_num,
            COALESCE(ROUND(SUM(o.final_price), 2), 0) AS profit
        FROM
            month_sequence ms
        LEFT JOIN
        order_data o
            ON
                (MONTH( o.used_time ) = ms.hour_num
                    AND YEAR( o.used_time ) = YEAR( #{dataTime} )
                    AND o.`status` = 1 )
            OR
                (MONTH( o.pay_time ) = ms.hour_num
                    AND YEAR( o.pay_time ) = YEAR( #{dataTime} )
                    AND o.`status` = 0 )
        GROUP BY
            ms.hour_num
        ORDER BY
            ms.hour_num;
    </select>
    <select id="getOrderQuantityDay" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        -- 使用 CTE 生成 0 到 23 的小时序列
        WITH hours AS (
            SELECT 0 AS hour_of_day
            UNION ALL SELECT 1
            UNION ALL SELECT 2
            UNION ALL SELECT 3
            UNION ALL SELECT 4
            UNION ALL SELECT 5
            UNION ALL SELECT 6
            UNION ALL SELECT 7
            UNION ALL SELECT 8
            UNION ALL SELECT 9
            UNION ALL SELECT 10
            UNION ALL SELECT 11
            UNION ALL SELECT 12
            UNION ALL SELECT 13
            UNION ALL SELECT 14
            UNION ALL SELECT 15
            UNION ALL SELECT 16
            UNION ALL SELECT 17
            UNION ALL SELECT 18
            UNION ALL SELECT 19
            UNION ALL SELECT 20
            UNION ALL SELECT 21
            UNION ALL SELECT 22
            UNION ALL SELECT 23
        ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
-- 主查询，左连接小时序列和原查询结果
        SELECT
            concat(h.hour_of_day,":00") AS hour_num,
            COALESCE(orders.order_count, 0) AS profit
        FROM
            hours h
                LEFT JOIN (
                SELECT
                    HOUR(CASE WHEN  oa.`status` = 1 THEN  oa.used_time ELSE  oa.pay_time END) AS hour_of_day,
                    COUNT(*) AS order_count
                FROM
                  order_data oa
                WHERE
                    (
                  oa.`status` = 2
                  AND oa.used_time > #{dataTime}
                    )
                   OR (
                    oa.`status` = 1
                  AND oa.pay_time > #{dataTime}
                    )
                GROUP BY
                    HOUR(CASE WHEN  oa.`status` = 2 THEN  oa.used_time ELSE  oa.pay_time END)
            ) orders ON h.hour_of_day = orders.hour_of_day
        ORDER BY
            h.hour_of_day;
    </select>
    <select id="getOrderQuantityWeek" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE date_sequence AS (
            SELECT DATE_SUB(#{dataTime}, INTERVAL WEEKDAY(#{dataTime}) DAY) AS hour_num
            UNION ALL
            SELECT hour_num + INTERVAL 1 DAY
        FROM date_sequence
        WHERE hour_num &lt; DATE_SUB(#{dataTime}, INTERVAL WEEKDAY(#{dataTime}) DAY) + INTERVAL 6 DAY
            ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
            DATE_FORMAT(ds.hour_num, '%c-%d') AS hour_num,
            COALESCE(count(o.id), 0) AS profit
        FROM
            date_sequence ds
                LEFT JOIN
        order_data o
            ON
               (o.used_time >= ds.hour_num AND o.used_time &lt; ds.hour_num + INTERVAL 1 DAY
                   AND o.`status` = 2)  or
               (o.pay_time >= ds.hour_num AND o.pay_time &lt; ds.hour_num + INTERVAL 1 DAY
                   AND o.`status` = 1)
        GROUP BY
            ds.hour_num
        ORDER BY
            ds.hour_num;
    </select>
    <select id="getOrderQuantityMonth" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH RECURSIVE date_sequence AS (
            SELECT DATE_FORMAT(#{dataTime}, '%Y-%m-01') AS hour_num
            UNION ALL
            SELECT DATE_ADD(hour_num, INTERVAL 1 DAY)
            FROM date_sequence
            WHERE DATE_ADD(hour_num, INTERVAL 1 DAY)  &lt;= LAST_DAY(#{dataTime})
        ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
            DATE_FORMAT(ds.hour_num, '%c-%d') AS hour_num,
            COALESCE(count(o.id), 0) AS profit
        FROM
            date_sequence ds
                LEFT JOIN
        order_data o
                ON
                (o.used_time >= ds.hour_num AND o.used_time &lt; ds.hour_num + INTERVAL 1 DAY
                   AND o.`status` = 2)  or
               (o.pay_time >= ds.hour_num AND o.pay_time &lt; ds.hour_num + INTERVAL 1 DAY
                   AND o.`status` = 1)
        GROUP BY
            ds.hour_num
        ORDER BY
            ds.hour_num;
    </select>
    <select id="getOrderQuantityYear" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        WITH months AS (
            SELECT
                CONCAT(YEAR(#{dataTime}), '-', LPAD(seq, 2, '0')) AS month
        FROM (
            SELECT 1 AS seq UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL
            SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL
            SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 UNION ALL
            SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12
            ) seq
            ),
        order_data AS (
        SELECT
        o.id,
        si.commission_rate,
        o.final_price,
        ocm.used_time,
        o.pay_time,
        o.status
        FROM life_user_order o
        LEFT JOIN order_coupon_middle ocm ON ocm.order_id = o.id
        LEFT JOIN store_info si ON si.id = o.store_id
        WHERE o.delete_flag = 0
        )
        SELECT
            m.MONTH AS hour_num,
            COALESCE(orders.order_count, 0) AS profit
        FROM
            months m
                LEFT JOIN (
                SELECT
                    DATE_FORMAT(CASE WHEN oa.`status` = 1 THEN oa.used_time ELSE oa.pay_time END, '%Y-%m') AS month,
        COUNT(*) AS order_count
                FROM
              order_data oa
                WHERE
                    (
                    oa.`status` = 2
                  AND oa.used_time > #{dataTime}
                    )
                   OR (
                    oa.`status` = 1
                  AND pay_time > #{dataTime}
                    )
                GROUP BY
                    DATE_FORMAT(CASE WHEN `status` = 2 THEN used_time ELSE pay_time END, '%Y-%m')
            ) orders ON m.month = orders.month
        ORDER BY
            m.month;
    </select>
    <select id="getSalesVolume" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        <!-- 初始化用户变量 -->
        SELECT
        profit,
        store.`store_name`
        FROM
        (
        SELECT
        count(id) AS profit,
        store_id
        FROM
        life_user_order
        WHERE
        ( `status` = 7 AND finish_time >= #{startTime} AND finish_time &lt; #{endTime})
        OR ( `status` = 1  AND pay_time >= #{startTime} AND pay_time &lt; #{endTime})
        GROUP BY
        store_id
        LIMIT 10
        ) statistic
        LEFT JOIN store_info store ON store.id = statistic.store_id
        ORDER BY
        profit DESC limit 10
    </select>
    <select id="getStorePlatformProfitDetails" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            store_id,
            store.store_name,
            ROUND(SUM(final_price), 2 ) AS operation_revenue,
            ROUND(SUM(final_price) - ROUND(SUM(final_price) * (IFNULL(store.commission_rate, 3)/100),2), 2) AS store_profit,
            ROUND(SUM(final_price) * (IFNULL(store.commission_rate, 3)/100),2) AS platform_profit
        FROM
            life_user_order `order`
                LEFT JOIN store_info store ON store.id = `order`.store_id
        WHERE
            `status` = 2
        <if test="startTime != null and startTime != ''" >
            AND `order`.updated_time > #{startTime}
        </if>
        <if test="endTime != null and endTime != ''" >
            AND `order`.updated_time &lt; #{endTime}
        </if>
        <if test="storeId != null and storeId != ''" >
            AND store_id like concat('%',#{storeId},'%')
        </if>
        <if test="storeName != null and storeName != ''" >
            AND store_name like concat('%',#{storeName},'%')
        </if>
        GROUP BY
            store.id
        ORDER BY
            `order`.created_time DESC
    </select>
    <select id="getRefundOrderQuantity" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            COUNT( 1 ) AS  `count`
        FROM
            life_user_order
        WHERE
            `status` = 5
            <if test="dataTime != null and dataTime != ''">
                AND refund_time > #{dataTime}
            </if>
    </select>
    <select id="getTotalOrderVolume" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            COUNT( 1 ) AS `count`
        FROM
            life_user_order `order`
        WHERE
            1=1
                <if test="dataTime != null and dataTime != ''">
                         AND created_time > #{dataTime}
                </if>
    </select>
    <select id="getStorePlatformOrderVolume" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        with total_coupon as(
        select id coupon_id,2 coupon_type,lgbm.group_name coupon_name, SUBSTRING_INDEX(image_id, ',', 1) AS image_id
        from life_group_buy_main lgbm where lgbm.delete_flag = 0
        union all
        select id coupon_id,1 coupon_type,lc.name coupon_name, SUBSTRING_INDEX(image_path, ',', 1) AS image_id
        from life_coupon lc where lc.delete_flag = 0
        )

        SELECT
         order_no,
        `order`.id as order_id,
        `order`.created_time AS order_time,
        `order`.finish_time AS finish_time,
        `order`.cancel_time AS cancel_time,
        `order`.pay_time AS pay_time,
        `order`.buy_time AS buy_time_str,
        ROUND(`order`.final_price, 2) AS final_price,
        CASE
        `order`.`status`
        WHEN 0 THEN '待支付'
        WHEN 1 THEN '已支付/待使用'
        WHEN 2 THEN '已核销'
        WHEN 3 THEN '已过期'
        WHEN 4 THEN '已取消'
        WHEN 5 THEN '已退款'
        WHEN 6 THEN '退款失败'
        WHEN 7 THEN '已完成'
        END AS status_name,
        simg.img_url,
        tc.*,
        store.store_name
        FROM
        life_user_order `order`
        left join store_info store on `order`.store_id = store.id
        left join order_coupon_middle ocm on ocm.order_id = `order`.id and ocm.delete_flag = 0
        inner join total_coupon tc on tc.coupon_id = ocm.coupon_id and tc.coupon_type = `order`.coupon_type
        left join  store_img simg on simg.id = tc.image_id and simg.delete_flag = 0
        <where>
            1 = 1
                <if test="orderType != null and orderType != ''">
                    AND `order`.`status` = #{orderType}
                </if>
                <if test="startTime != null and startTime != ''">
                    AND buy_time > #{startTime}
                </if>
                <if test="orderId != null and orderId != ''">
                    AND `order`.`order_no` like concat('%',#{orderId},'%')
                </if>
            <if test="couponType != null and couponType != ''">
                AND tc.coupon_type = #{couponType}
            </if>
            <if test="couponName != null and couponName != ''">
                AND tc.coupon_name like concat('%',#{couponName},'%')
            </if>
            <if test="storeName != null and storeName != ''">
                AND store.store_name like concat('%',#{storeName},'%')
            </if>
        </where>
        GROUP by `order`.`id`
        ORDER BY `order`.created_time DESC
    </select>
    <select id="getRefundAmount" resultType="shop.alien.entity.store.vo.ManagementInfoVo">
        SELECT
            IFNULL(ROUND( SUM( final_price ), 2 ),0) AS profit
        FROM
            life_user_order `order`
        WHERE
            `status` = 5
        <if test="dataTime != null and dataTime != ''" >
            AND `order`.refund_time > #{dataTime}
        </if>
    </select>
</mapper>