<?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="com.zsElectric.boot.system.mapper.DataBoardMapper">

    <!-- 获取累计实时数据 -->
    <select id="selectRealTimeData" resultType="com.zsElectric.boot.system.model.vo.DataBoardRealTimeVO">
        SELECT
            COALESCE(SUM(total_charge), 0) AS totalChargePower,
            COALESCE(SUM(third_party_total_cost), 0) AS totalChargeAmount,
            COALESCE(SUM(discount_money), 0) AS totalDiscountAmount,
            COALESCE(SUM(real_service_cost), 0) AS totalServiceFeeAmount,
            COALESCE(SUM(real_cost), 0) AS totalActualPayAmount,
            COALESCE(SUM(coupon_price), 0) AS totalCouponAmount,
            COALESCE(SUM(firm_price), 0) AS totalFirmDiscountAmount,
            0 AS totalCommissionAmount
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
    </select>

    <!-- 获取今日实时数据 -->
    <select id="selectTodayData" resultType="com.zsElectric.boot.system.model.vo.DataBoardTodayVO">
        SELECT
            COALESCE(SUM(total_charge), 0) AS todayChargePower,
            COALESCE(SUM(third_party_total_cost), 0) AS todayChargeAmount,
            COALESCE(SUM(discount_money), 0) AS todayDiscountAmount,
            COALESCE(SUM(real_service_cost), 0) AS todayServiceFeeAmount,
            COALESCE(SUM(real_cost), 0) AS todayActualPayAmount,
            COALESCE(SUM(coupon_price), 0) AS todayCouponAmount,
            COALESCE(SUM(firm_price), 0) AS todayFirmDiscountAmount,
            0 AS todayCommissionAmount
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND create_time BETWEEN #{todayStart} AND #{todayEnd}
    </select>

    <!-- 获取今日退款金额 -->
    <select id="selectTodayRefundAmount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(amount), 0)
        FROM c_user_refunds_order_info
        WHERE status = 'SUCCESS' AND is_deleted = 0
        AND success_time BETWEEN #{todayStart} AND #{todayEnd}
    </select>

    <!-- 获取累计退款金额 -->
    <select id="selectTotalRefundAmount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(amount), 0)
        FROM c_user_refunds_order_info
        WHERE status = 'SUCCESS' AND is_deleted = 0
    </select>

    <!-- 按小时统计充电度数 -->
    <select id="selectHourlyChargePower" resultType="java.util.Map">
        SELECT
            HOUR(create_time) AS hour,
            COALESCE(SUM(total_charge), 0) AS chargePower
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND create_time BETWEEN #{dateStart} AND #{dateEnd}
        GROUP BY HOUR(create_time)
        ORDER BY hour
    </select>

    <!-- 按日统计充电度数 -->
    <select id="selectDailyChargePower" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(create_time, '%m.%d') AS dateLabel,
            COALESCE(SUM(total_charge), 0) AS value
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND DATE(create_time) BETWEEN #{startDate} AND #{endDate}
        GROUP BY DATE(create_time)
        ORDER BY DATE(create_time)
    </select>

    <!-- 按日统计充电金额 -->
    <select id="selectDailyChargeAmount" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(create_time, '%m.%d') AS dateLabel,
            COALESCE(SUM(real_cost), 0) AS value
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND DATE(create_time) BETWEEN #{startDate} AND #{endDate}
        GROUP BY DATE(create_time)
        ORDER BY DATE(create_time)
    </select>

    <!-- 按日统计有效订单数 -->
    <select id="selectDailyValidOrders" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(create_time, '%m.%d') AS dateLabel,
            COUNT(*) AS value
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND DATE(create_time) BETWEEN #{startDate} AND #{endDate}
        GROUP BY DATE(create_time)
        ORDER BY DATE(create_time)
    </select>

    <!-- 按日统计注册用户数 -->
    <select id="selectDailyRegisterUsers" resultType="java.util.Map">
        SELECT
            DATE_FORMAT(create_time, '%m.%d') AS dateLabel,
            COUNT(*) AS value
        FROM c_user_info
        WHERE is_deleted = 0
        AND DATE(create_time) BETWEEN #{startDate} AND #{endDate}
        GROUP BY DATE(create_time)
        ORDER BY DATE(create_time)
    </select>

    <!-- 获取热门充电站排名 -->
    <select id="selectHotStations" resultType="com.zsElectric.boot.system.model.vo.StationRankVO">
        SELECT
            s.id AS stationId,
            s.station_name AS stationName,
            COALESCE(SUM(o.total_charge), 0) AS chargePower
        FROM c_charge_order_info o
                 LEFT JOIN third_party_station_info s ON o.third_party_station_id = s.id
        WHERE o.status = 3 AND o.is_deleted = 0
        AND DATE(o.create_time) BETWEEN #{startDate} AND #{endDate}
        GROUP BY o.third_party_station_id, s.id, s.station_name
        ORDER BY chargePower DESC
        LIMIT #{limit}
    </select>

    <!-- 获取站点在时间段内的充电度数 -->
    <select id="selectStationChargePower" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(total_charge), 0)
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND third_party_station_id = #{stationId}
        AND DATE(create_time) BETWEEN #{startDate} AND #{endDate}
    </select>

    <!-- 获取活跃用户数 -->
    <select id="selectActiveUserCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT user_id)
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND DATE(create_time) BETWEEN #{startDate} AND #{endDate}
    </select>

    <!-- 获取流失用户数 -->
    <select id="selectChurnUserCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT early.user_id)
        FROM (
            SELECT DISTINCT user_id
            FROM c_charge_order_info
            WHERE status = 3 AND is_deleted = 0
            AND DATE(create_time) BETWEEN #{earlyStart} AND #{earlyEnd}
        ) early
        LEFT JOIN (
            SELECT DISTINCT user_id
            FROM c_charge_order_info
            WHERE status = 3 AND is_deleted = 0
            AND DATE(create_time) BETWEEN #{recentStart} AND #{recentEnd}
        ) recent ON early.user_id = recent.user_id
        WHERE recent.user_id IS NULL
    </select>

    <!-- 获取今日首单金额 -->
    <select id="selectTodayFirstOrderAmount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(first_order.real_cost), 0)
        FROM (
            SELECT user_id, MIN(create_time) AS first_time
            FROM c_charge_order_info
            WHERE status = 3 AND is_deleted = 0
            GROUP BY user_id
        ) first_order_time
        INNER JOIN c_charge_order_info first_order
            ON first_order.user_id = first_order_time.user_id
            AND first_order.create_time = first_order_time.first_time
        WHERE first_order.status = 3 AND first_order.is_deleted = 0
        AND DATE(first_order.create_time) BETWEEN #{todayStart} AND #{todayEnd}
    </select>

    <!-- 获取累计首单金额 -->
    <select id="selectTotalFirstOrderAmount" resultType="java.math.BigDecimal">
        SELECT COALESCE(SUM(first_order.real_cost), 0)
        FROM (
            SELECT user_id, MIN(create_time) AS first_time
            FROM c_charge_order_info
            WHERE status = 3 AND is_deleted = 0
            GROUP BY user_id
        ) first_order_time
        INNER JOIN c_charge_order_info first_order
            ON first_order.user_id = first_order_time.user_id
            AND first_order.create_time = first_order_time.first_time
        WHERE first_order.status = 3 AND first_order.is_deleted = 0
    </select>

    <!-- 获取流失用户列表（包含用户信息和最后一次充电信息） -->
    <select id="selectChurnUserList" resultType="com.zsElectric.boot.system.model.dto.ChurnUserExportDTO">
        SELECT
        u.nick_name AS nickname,
        u.phone AS phone,
        CONCAT(DATE_FORMAT(last_order.start_time, '%Y-%m-%d %H:%i:%s'), ' 至 ', DATE_FORMAT(last_order.end_time, '%Y-%m-%d %H:%i:%s')) AS
        lastChargeTimePeriod
        FROM (
        <!-- 早期有充电记录的用户 -->
        SELECT DISTINCT user_id
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND DATE(create_time) BETWEEN #{earlyStart} AND #{earlyEnd}
        ) early_users
        <!-- 排除近期有充电记录的用户 -->
        LEFT JOIN (
        SELECT DISTINCT user_id
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        AND DATE(create_time) BETWEEN #{recentStart} AND #{recentEnd}
        ) recent_users ON early_users.user_id = recent_users.user_id
        <!-- 关联用户信息 -->
        LEFT JOIN c_user_info u ON early_users.user_id = u.id
        <!-- 关联最后一次充电订单 -->
        LEFT JOIN (
        SELECT o1.*
        FROM c_charge_order_info o1
        INNER JOIN (
        SELECT user_id, MAX(create_time) AS max_time
        FROM c_charge_order_info
        WHERE status = 3 AND is_deleted = 0
        GROUP BY user_id
        ) o2 ON o1.user_id = o2.user_id AND o1.create_time = o2.max_time
        WHERE o1.status = 3 AND o1.is_deleted = 0
        ) last_order ON early_users.user_id = last_order.user_id
        WHERE recent_users.user_id IS NULL
        AND u.is_deleted = 0
        ORDER BY last_order.create_time DESC
    </select>

</mapper>
