<?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 COUNT(*) FROM qw_user) AS qw_member_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>
            <if test="userType != null">
                and send_type=${userType}
            </if>
            <if test="startTime != null and endTime != null">
                and create_time between #{startTime} and #{endTime}
            </if>
            <if test="companyId != null">
                and company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="queryCompletedUserCount" resultType="java.lang.Long">
        select count(DISTINCT user_id) from fs_course_watch_log
        <where>
            finish_time is not null
            <if test="userType != null">
                and send_type=${userType}
            </if>
            <if test="startTime != null and endTime != null">
                and create_time between #{startTime} and #{endTime}
            </if>
            <if test="companyId != null">
                and company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="queryWatchUserCount" resultType="java.lang.Long">
        select count(distinct user_id) from fs_course_watch_log
        <where>
            <if test="userType != null">
                and send_type=${userType}
            </if>
            <if test="startTime != null and endTime != null">
                and create_time between #{startTime} and #{endTime}
            </if>
            <if test="companyId != null">
                and company_id = #{companyId}
            </if>

        </where>
    </select>
    <select id="queryCompletedCount" resultType="java.lang.Long">
        SELECT COUNT(log_id) FROM fs_course_watch_log
        <where>
            finish_time IS NOT NULL
            <if test="startTime != null and endTime != null">
                AND create_time BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="userType != null">
                and send_type=${userType}
            </if>
            <if test="companyId != null">
                and company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="queryAnswerMemberCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT user_id) FROM fs_course_answer_logs
        <where>
            <if test="startTime != null and endTime != null">
                create_time BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="companyId != null">
                and company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="queryCorrectUserCount" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT user_id) FROM fs_course_answer_logs
        <where>
            is_right = 1
            <if test="startTime != null and endTime != null">
                and create_time BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="companyId != null">
                and company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="queryRewardCount" resultType="java.lang.Long">
        select count(*) from company_red_package_logs rpl
        left join fs_course_watch_log log
        on rpl.watch_log_id=log.log_id
        <where>
            rpl.operate_type=1
            <if test="startTime != null and endTime != null">
                and rpl.create_time BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="userType != null">
                and log.send_type = ${userType}
            </if>
            <if test="companyId != null">
                and log.company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="queryRewardMoney" resultType="java.math.BigDecimal">
        select sum(up_money) from company_red_package_logs rpl
        left join fs_course_watch_log log
        on rpl.watch_log_id=log.log_id
        <where>
            rpl.operate_type=1
            <if test="startTime != null and endTime != null">
                and rpl.create_time BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="userType != null">
                and log.send_type = ${userType}
            </if>
            <if test="companyId != null">
                and log.company_id = #{companyId}
            </if>
        </where>
    </select>
    <select id="smsBalance" resultType="java.lang.Long">
        select sum(remain_sms_count) from company_sms
    </select>
    <select id="smsBalanceCompany" resultType="java.lang.Long">
        select sum(remain_sms_count) from company_sms
        <where>
            <if test="companyId != null">
                company_id = #{companyId}
            </if>
        </where>
    </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>
            <if test="userType != null">
                AND send_type = ${userType}
            </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>
            <if test="userType != null">
                AND send_type = ${userType}
            </if>
            <if test="companyId != null">
                AND company_id = ${companyId}
            </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>
            <if test="userType != null">
                AND send_type = ${userType}
            </if>
            <if test="companyId != null">
                AND w.company_id = ${companyId}
            </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
        -- 按公司
        <if test="dataType == 0">
            rpl.company_id as company_id,
        </if>
        -- 按课程
        <if test="dataType == 1">
            rpl.course_id as course_id,
        </if>
        SUM(rpl.money) as rewardMoney
        FROM
        company_red_package_logs rpl
        left join fs_course_watch_log log
        on rpl.watch_log_id=log.log_id
        <where>
            rpl.operate_type = 1
            AND rpl.status = 1
            <if test="startTime != null">
                AND rpl.create_time <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND rpl.create_time <![CDATA[<]]> #{endTime}
            </if>
            <if test="userType != null">
                and log.send_type = ${userType}
            </if>
            <if test="companyId != null">
                and log.company_id = ${companyId}
            </if>
        </where>
        GROUP BY
        <if test="dataType == 0">
            rpl.company_id
        </if>
        <if test="dataType == 1">
            rpl.course_id
        </if>
        ORDER BY
        rewardMoney DESC
        LIMIT 10
    </select>
    <select id="rewardMoneyTrendDTO" resultType="com.fs.statis.dto.RewardMoneyTrendDTO">
        select
        --             今日/昨日 小时
        <if test="type == 0 or type == 1">
            DATE_FORMAT(rpl.create_time, '%H') AS start_date,
        </if>
        --                 本周/本月/上月 天
        <if test="type == 2 or type == 3 or type == 4">
            DATE_FORMAT(rpl.create_time, '%Y-%m-%d') AS start_date,
        </if>
        SUM(rpl.money) as rewardMoney
        from company_red_package_logs rpl
        left join fs_course_watch_log log
        on rpl.watch_log_id=log.log_id
        <where>
            <if test="startTime != null">
                rpl.create_time <![CDATA[>=]]> #{startTime}
            </if>
            <if test="endTime != null">
                AND rpl.create_time <![CDATA[<]]> #{endTime}
            </if>
            <if test="userType != null">
                and log.send_type = ${userType}
            </if>
            <if test="companyId != null">
                and log.company_id = ${companyId}
            </if>
            AND rpl.operate_type = 1
            AND rpl.status = 1
        </where>
        group by start_date
    </select>
    <select id="getCurrentBalance" resultType="java.math.BigDecimal">
        select sum(money) from company
    </select>
    <select id="dealerAggregatedCompanyId" resultType="com.fs.statis.dto.DealerAggregatedDTO">
        SELECT
        1 AS dealder_count,
        (SELECT COUNT(*) FROM COMPANY_USER
        <where>
            <if test="companyId != null">
                company_id = #{companyId}
            </if>
        </where>
        ) AS group_mgr_count,
        (SELECT COUNT(*) FROM FS_USER
        <where>
            <if test="companyId != null">
                company_id = #{companyId}
            </if>
        </where>
        ) AS member_count,
        (SELECT COUNT(*) FROM FS_USER
        <where>
            STATUS=1
            <if test="companyId != null">
                AND company_id = #{companyId}
            </if>
        </where>
        ) AS normal_num,
        (SELECT COUNT(*) FROM FS_USER
        <where>
            STATUS=0
            <if test="companyId != null">
                AND company_id = #{companyId}
            </if>
        </where>
        ) AS black_num,
        (select COUNT(*) FROM qw_user) AS qw_member_num
    </select>
    <select id="getCurrentBalanceCompanyId" resultType="java.math.BigDecimal">
        select sum(money) from company
        <where>
            <if test="companyId != null">
                company_id = #{companyId}
            </if>
        </where>
    </select>

</mapper>
