<?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.fs.statis.mapper.ConsumptionBalanceMapper">

    <select id="rechargeConsumption" resultType="com.fs.statis.dto.ConsumptionBalanceDataDTO">
        select
            14371277 AS balance,
            103100 AS today_comsumption,
            195280 AS yesterday_comsumption
    </select>
    <select id="dealerAggregated" resultType="com.fs.statis.dto.DealerAggregatedDTO">
        SELECT
                (SELECT COUNT(*) FROM COMPANY) AS dealder_count,
                (SELECT COUNT(*) FROM COMPANY_USER) AS group_mgr_count,
                (SELECT COUNT(*) FROM FS_USER) AS member_count,
                (SELECT COUNT(*) FROM FS_USER WHERE STATUS=1) AS normal_num,
                (SELECT COUNT(*) FROM FS_USER WHERE STATUS=0) AS black_num
    </select>
    <select id="analysisPreview" resultType="com.fs.statis.dto.AnalysisPreviewDTO">
        -- 观看人数
        select (select count(*) from fs_course_watch_log where create_time between #{startTime} and #{endTime} group by user_id) as watch_count,
-- 完播人数
               (select count(*) from fs_course_watch_log where finish_time is not null and create_time between #{startTime} and #{endTime} group by user_id)
                                                                                                                                                     as completed_user_count,
-- 观看次数
               (select count(*) from fs_course_watch_log where create_time between #{startTime} and #{endTime}) as watch_count,
-- 完播次数
               (select count(*) from fs_course_watch_log where create_time between #{startTime} and #{endTime}) as completed_count,
-- 答题人数
               (select count(*) from fs_course_answer_logs where create_time between #{startTime} and #{endTime}) as answer_member_count,
-- 正确人数
               (select count(*) from fs_course_answer_logs where is_right=1 AND create_time between #{startTime} and #{endTime}) as correct_user_count,
-- 答题红包个数
               (select 0) as reward_type,
               (select 0) as reward_money
    </select>
    <select id="queryWatchCount" resultType="java.lang.Long">
        select count(log_id) from fs_course_watch_log where create_time between #{startTime} and #{endTime}
    </select>
    <select id="queryCompletedUserCount" resultType="java.lang.Long">
        select count(DISTINCT user_id) from fs_course_watch_log where finish_time is not null and create_time between #{startTime} and #{endTime}
    </select>
    <select id="queryWatchUserCount" resultType="java.lang.Long">
        select count(distinct user_id) from fs_course_watch_log where create_time between #{startTime} and #{endTime}
    </select>
    <select id="queryCompletedCount" resultType="java.lang.Long">
        SELECT COUNT(log_id) FROM fs_course_watch_log
        WHERE finish_time IS NOT NULL AND create_time BETWEEN #{startTime} AND #{endTime}
    </select>
    <select id="queryAnswerMemberCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT user_id) FROM fs_course_answer_logs
        WHERE create_time BETWEEN #{startTime} AND #{endTime}
    </select>
    <select id="queryCorrectUserCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT user_id) FROM fs_course_answer_logs
        WHERE is_right = 1 AND create_time BETWEEN #{startTime} AND #{endTime}
    </select>
    <select id="queryRewardCount" resultType="java.lang.Long">
        select count(*) from company_red_package_logs where operate_type=1
    </select>
    <select id="queryRewardMoney" resultType="java.math.BigDecimal">
        select sum(up_money) from company_red_package_logs where operate_type=1
    </select>
    <select id="smsBalance" resultType="java.lang.Long">
        select sum(remain_sms_count) from company_sms
    </select>
    <select id="authorizationInfo" resultType="com.fs.statis.dto.AuthorizationInfoDTO">
        select version_limit,today_watch_user_count from fs_statistics_index limit 1
    </select>
    <select id="watchEndPlayTrend" resultType="com.fs.statis.dto.WatchEndPlayTrendDTO">
        SELECT
--             今日/昨日 小时
            <if test="type == 0 or type == 1">
                DATE_FORMAT(create_time, '%H') AS start_date,
            </if>
--                 本周/本月/上月 天
            <if test="type == 2 or type == 3 or type == 4">
                DATE_FORMAT(create_time, '%Y-%m-%d') AS start_date,
            </if>
        COUNT(DISTINCT user_id) AS watch_user_count,
        COUNT(DISTINCT CASE WHEN log_type = 2 THEN user_id END) AS completed_user_count
        FROM
        fs_course_watch_log
        <where>
            <if test="startTime != null">
                create_time <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND create_time <![CDATA[<]]> #{endTime}
            </if>
        </where>
        GROUP BY
        start_date
        ORDER BY
        start_date
    </select>
    <select id="deaMemberTopTen" resultType="com.fs.statis.dto.DeaMemberTopTenDTO">
        SELECT
        company_id,
        <if test="statisticalType == 0">
            count(DISTINCT user_id) AS watch_user_count
        </if>
        <if test="statisticalType == 1">
            COUNT(DISTINCT CASE WHEN log_type = 2 THEN user_id END) AS watch_user_count
        </if>
        FROM
        fs_course_watch_log
        <where>
            <if test="startTime != null">
                create_time <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND create_time <![CDATA[<]]> #{endTime}
            </if>
        </where>
        GROUP BY company_id
        limit 10
    </select>
    <select id="watchCourseTopTen" resultType="com.fs.statis.dto.CourseStatsDTO">
        SELECT
        w.course_id AS course_id,
        COUNT(DISTINCT w.user_id) AS watch_user_count,
        COUNT(DISTINCT CASE WHEN w.log_type = 2 THEN w.user_id END) AS completed_user_count,
        COUNT(DISTINCT a.user_id) AS answer_user_count,
        COUNT(DISTINCT CASE WHEN a.is_right = 1 THEN a.user_id END) AS correct_user_count
        FROM
        fs_course_watch_log w
        LEFT JOIN
        fs_course_answer_logs a ON w.video_id = a.video_id AND w.user_id = a.user_id
        <where>
            <if test="startTime != null">
                w.create_time <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND w.create_time <![CDATA[<]]> #{endTime}
            </if>
        </where>
        GROUP BY
        w.course_id
        ORDER BY
            -- 观看人数
            <if test="statisticalType == 0">
                COUNT(DISTINCT w.user_id)
            </if>
            <if test="statisticalType == 1">
                COUNT(DISTINCT CASE WHEN w.log_type = 2 THEN w.user_id END)
            </if>
            <if test="statisticalType == 2">
                COUNT(DISTINCT a.user_id)
            </if>
            <if test="statisticalType == 3">
                COUNT(DISTINCT CASE WHEN a.is_right = 1 THEN a.user_id END)
            </if>
        ${sort}
        LIMIT 10
    </select>
    <select id="rewardMoneyTopTen" resultType="com.fs.statis.dto.RewardMoneyTopTenDTO">

    </select>
    <select id="rewardMoneyTrendDTO" resultType="com.fs.statis.dto.RewardMoneyTrendDTO">

    </select>

</mapper>
