<?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="jnpf.cultivate.mapper.FtbCultivateStatisticsMapper">
    <!--统计培训学员数量-->
    <select id="numOfTrainees" resultType="java.lang.Integer">
        SELECT COUNT(DISTINCT F_UserId) FROM ftb_cultivate_position_cource_learning WHERE F_EnabledMark=0 AND F_state in (1,2)
        <if test="params.userIds!=null and params.userIds.size()>0">
            and F_UserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            and F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
    </select>
    <!--获取热门课程信息-->
    <select id="getPopularCourses"
            resultType="jnpf.model.cultivate.vo.statistics.FtbCultivateStatisticsVO$PopularCourses">
        SELECT
            COUNT(DISTINCT lea.F_CreatorUserId ) AS number,
            cou.F_Name as courseName
        FROM
        ftb_cultivate_course_learning_log AS lea
                LEFT JOIN ftb_cultivate_course cou ON lea.F_CourseId = cou.F_Id
        WHERE
        lea.F_EnabledMark=0
        and cou.F_EnabledMark = 0
        <if test="dto.userIds!=null and dto.userIds.size()>0">
            and lea.F_CreatorUserId  in
            <foreach collection="dto.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        AND lea.F_CreatorTime BETWEEN  #{dto.startTime} AND #{dto.endTime}

        GROUP BY
            lea.F_CourseId
        ORDER BY number desc, cou.F_Name asc
        LIMIT 10
    </select>
    <!--统计学习中的课程数量-->
    <select id="numberOfStudyCourses" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(a.t),0) from (SELECT COUNT(distinct a.F_CourceId) as t
        FROM ftb_cultivate_position_cource_learning a join ftb_cultivate_course b on a.F_CourceId = b.F_Id
        where
        b.F_EnabledMark = 0 and F_state in (1,2) and a.F_EnabledMark=0
        and F_UserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        <if test="params.startTime != null and params.endTime != null">
            and a.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
        GROUP BY F_CourceId) a
    </select>
    <!--计算学员累计学习时长-->
    <select id="accumulatedTime" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(F_LearnTime),0)
        FROM ftb_cultivate_position_cource_learning a join ftb_cultivate_course b on a.F_CourceId = b.F_Id
        where
        b.F_EnabledMark=0
        and a.F_EnabledMark=0
        and F_UserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        <if test="params.startTime != null and params.endTime != null">
            and a.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
    </select>
    <!--计算课程完成率-->
    <select id="courseCompletionRate" resultType="java.lang.Integer">
        SELECT COUNT(*) as t
        FROM ftb_cultivate_position_cource_learning a join ftb_cultivate_course b on a.F_CourceId = b.F_Id
        where
        b.F_EnabledMark = 0 and b.F_IsGrounding=1 and a.F_EnabledMark = 0 and
        F_state in
        <foreach collection="states" item="state" open="(" close=")" separator=",">
            #{state}
        </foreach>
        <if test="params.userIds != null and params.userIds.size() != 0">
            and F_UserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            <if test="states.size() == 1">
                and a.F_LastModifyTime BETWEEN  #{params.startTime} AND #{params.endTime}
            </if>
            <if test="states.size() > 1">
                and (b.F_CreatorTime &lt;= #{params.endTime}
                or a.F_LastModifyTime BETWEEN #{params.startTime} AND #{params.endTime}
                or a.F_CreatorTime BETWEEN #{params.startTime} AND #{params.endTime})
            </if>
        </if>
    </select>
    <!--获取学习进度排名靠前的成员列表-->
    <select id="getTopMembers" resultType="jnpf.model.cultivate.vo.statistics.FtbCultivateStatisticsVO$TopMembers">
        SELECT
        a.F_CreatorUserId AS ordinate,
        SUM(a.F_LearnTime) AS abscissa
        FROM
        ftb_cultivate_course_learning_log a
        join
        ftb_cultivate_course b
        on a.F_CourseId = b.F_Id
        <where>
            a.F_EnabledMark=0
            and b.F_EnabledMark = 0
           <if test="params.userIds!=null and params.userIds.size()>0">
               and a.F_CreatorUserId in
               <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                   #{userId}
               </foreach>
           </if>
           <if test="params.startTime != null and params.endTime != null">
               and a.F_CreatorTime BETWEEN #{params.startTime} AND #{params.endTime}
           </if>
       </where>
        GROUP BY a.F_CreatorUserId ORDER BY abscissa DESC LIMIT 10
    </select>
    <!--统计新学习的课程数量-->
    <select id="numberOfStudyCoursesNew" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(a.t),0) from (SELECT COUNT(*) as t
        FROM ftb_cultivate_position_cource_learning a join ftb_cultivate_course b on a.F_CourceId = b.F_Id
        where
        b.F_EnabledMark = 0 and F_state in (1,2) and a.F_EnabledMark=0
        <if test="params.userIds != null and params.userIds.size() != 0">
        and F_UserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            and a.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
        GROUP BY F_CourceId) a
    </select>

    <select id="queryStudyUserNumAndTime" resultType="jnpf.model.cultivate.v2.statistics.V2CultivateLineNumDto">
        SELECT COUNT(DISTINCT F_UserId) as numberOfStudents,IFNULL(SUM(F_LearnTime),0) as studyDuration
        FROM ftb_cultivate_position_cource_learning WHERE F_EnabledMark=0 AND F_state in (1,2)
        <if test="params.userIds!=null and params.userIds.size()>0">
            and F_UserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            and F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>

    </select>

    <select id="queryStudyUserNumAndTimeV2" resultType="jnpf.model.cultivate.v2.statistics.V2CultivateLineNumDto">
        SELECT COUNT(DISTINCT F_CreatorUserId) as numberOfStudents,IFNULL(SUM(F_LearnTime),0) as studyDuration
        FROM ftb_cultivate_course_learning_log WHERE F_EnabledMark=0
        <if test="params.userIds!=null and params.userIds.size()>0">
            and F_CreatorUserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            and F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
    </select>

    <select id="numOfTraineesV2" resultType="java.lang.Integer">
        SELECT count(DISTINCT log.F_CreatorUserId)
        FROM
        ftb_cultivate_course_learning_log log
        LEFT JOIN ftb_cultivate_course course ON course.F_Id = log.F_CourseId
        WHERE
        log.F_EnabledMark = 0
        AND course.F_EnabledMark = 0
        <if test="params.userIds!=null and params.userIds.size()>0">
            and log.F_CreatorUserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            and log.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
    </select>

    <select id="numberOfStudyCoursesV2" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(a.t),0) from (SELECT COUNT(distinct a.F_CourseId) as t
        FROM ftb_cultivate_course_learning_log a join ftb_cultivate_course b on a.F_CourseId = b.F_Id
        where
        b.F_EnabledMark = 0  and a.F_EnabledMark=0
        and a.F_CreatorUserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        <if test="params.startTime != null and params.endTime != null">
            and a.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
        GROUP BY a.F_CourseId) a
    </select>

    <select id="accumulatedTimeV2" resultType="java.lang.Integer">
        SELECT IFNULL(SUM(F_LearnTime),0)
        FROM ftb_cultivate_course_learning_log a join ftb_cultivate_course b on a.F_CourseId = b.F_Id
        where
        b.F_EnabledMark=0
        and a.F_EnabledMark=0
        and a.F_CreatorUserId in
        <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
        <if test="params.startTime != null and params.endTime != null">
            and a.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}
        </if>
    </select>

    <select id="courseCompletionRateV2" resultType="java.lang.Integer">

        SELECT COUNT(*) as t
        FROM ftb_cultivate_course_learning_log a join ftb_cultivate_course b on a.F_CourseId = b.F_Id
        where
        b.F_EnabledMark = 0 and b.F_IsGrounding=1 and a.F_EnabledMark = 0 and
        a.F_Status in
        <foreach collection="states" item="state" open="(" close=")" separator=",">
            #{state}
        </foreach>
        <if test="params.userIds != null and params.userIds.size() != 0">
            and a.F_CreatorUserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">

            and a.F_CreatorTime BETWEEN  #{params.startTime} AND #{params.endTime}

        </if>
    </select>

    <select id="getTopMembersV2" resultType="jnpf.model.cultivate.vo.statistics.FtbCultivateStatisticsVO$TopMembers">
        SELECT SUM(F_LearnTime) as abscissa,F_CreatorUserId as ordinate
        FROM ftb_cultivate_course_learning_log
        WHERE F_EnabledMark=0 and F_Status =1
        <if test="params.userIds!=null and params.userIds.size()>0">
            and F_CreatorUserId in
            <foreach collection="params.userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="params.startTime != null and params.endTime != null">
            and F_CreatorTime BETWEEN #{params.startTime} AND #{params.endTime}
        </if>
        GROUP BY F_CreatorUserId ORDER BY abscissa DESC LIMIT 10
    </select>
</mapper>