<?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.FtbCultivateCourseStatisticesMapper">
    <!--组织列表统计方法-->
    <select id="organizationListStatistics"
            resultType="jnpf.model.cultivate.vo.course.web.FtbCultivateCourseOrgStatisticesVO">
        SELECT
        a.*,
        IFNULL( b.numberHaveCourse, 0 ) as numberHaveCourse,
        IFNULL( ROUND( a.totalDurationOfStudy / a.numberInLearning, 2 ), 0 ) as  averageLearningPerson
        FROM
        (
        SELECT
        ler.F_CourceId as courseId,
        co.F_Name courseTitle,
        co.F_Format courseFormat,
        type.F_Name as courseType,
        co.F_Id,
        co.F_CreatorTime as courseCreationTime,
        COUNT( ler.F_UserId ) AS numberInLearning ,
        group_concat( ler.F_UserId ) AS temp ,
        ROUND(SUM( F_LearnTime ) / 60 / 60 , 2)	 AS totalDurationOfStudy
        FROM
        ftb_cultivate_position_cource_learning AS ler
        JOIN ftb_cultivate_course AS co ON ler.F_CourceId = co.F_Id
        JOIN ftb_cultivate_label as type ON co.F_TypeId = type.F_Id
        <where>
            ler.F_state in (1,2) and  ler.F_EnabledMark = 0 and
            co.F_EnabledMark = 0
            <if test="params.userIds != null and params.userIds.size() > 0">
                and ler.F_UserId IN
                <foreach collection="params.userIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="params.courseFormat != null and params.courseFormat != ''">
                and co.F_Format = #{params.courseFormat}
            </if>
            <if test="params.startTime != null and params.endTime != null">
                and co.F_CreatorTime BETWEEN #{params.startTime} AND #{params.endTime}
            </if>
            <if test="params.nameType != null and params.nameType != ''">
                and type.F_Id = #{params.nameType}
            </if>
            <if test="params.chooseATypes != null and params.chooseATypes.size() > 0">
                and co.F_Id IN
                <foreach collection="params.chooseATypes" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY
        co.F_Id
        ) a
        LEFT JOIN (
        SELECT
        co.F_Id,
        COUNT( ler.F_UserId ) AS numberHaveCourse
        FROM
        ftb_cultivate_position_cource_learning AS ler
        JOIN ftb_cultivate_course AS co ON ler.F_CourceId = co.F_Id
        <where>
            ler.F_state = 1 and co.F_EnabledMark = 0 and ler.F_EnabledMark = 0
            <if test="params.userIds != null and params.userIds.size() > 0">
                and ler.F_UserId IN
                <foreach collection="params.userIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="params.courseFormat != null and params.courseFormat != ''">
                and co.F_Format = #{params.courseFormat}
            </if>
            <if test="params.startTime != null and params.endTime != null">
                and co.F_CreatorTime BETWEEN #{params.startTime} AND #{params.endTime}
            </if>
            <if test="params.chooseATypes != null and params.chooseATypes.size() > 0">
                and co.F_Id IN
                <foreach collection="params.chooseATypes" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY co.F_Id
        ) b ON a.F_Id = b.F_Id
    </select>
    <!--人员列表统计方法-->
    <select id="personListStatistics" resultType="jnpf.model.cultivate.vo.course.web.FtbCultivateCoursePersonStatisticesVO">
        SELECT
        ler.F_UserId userId,
        ler.F_CourceId courseId,
        ler.F_LastModifyTime recentStudyTime,
        ler.F_LearnTime participationTimeInStudy
        FROM
        ftb_cultivate_position_cource_learning AS ler
        JOIN ftb_cultivate_course AS co ON ler.F_CourceId = co.F_Id
        <where>
            ler.F_EnabledMark = 0 and co.F_EnabledMark = 0 and ler.F_state in(1,2)
            <if test="params.selectPeoples != null and params.selectPeoples.size() > 0">
              and  (ler.F_UserId IN
                <foreach collection="params.selectPeoples" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                )
            </if>
            <if test="params.keyWords != null and params.keyWords != ''">
                and co.F_Name  like concat('%',#{params.keyWords},'%')
            </if>
            <if test="params.startTime != null and params.endTime != null">
                and ler.F_LastModifyTime BETWEEN #{params.startTime} AND #{params.endTime}
            </if>
            <if test="params.courseId != null and params.courseId != ''">
                and ler.F_CourceId = #{params.courseId}
            </if>
        </where>
        GROUP BY ler.F_UserId,ler.F_CourceId,ler.F_LastModifyTime
        order by ler.F_LastModifyTime desc
    </select>
    <!--课程信息查询方法-->
    <select id="courseInfo" resultType="jnpf.model.cultivate.vo.course.web.FtbCultivateCoursePersonStatisticesVO">
        SELECT
        a.F_Name as courseTitle,
        b.F_Name as courseType,
        a.F_Format as courseFormat,
        a.F_CreatorTime as courseCreationTime,
        a.F_Id as courseId,
        a.F_ChapterNumber as chapterNumber
        from ftb_cultivate_course a JOIN ftb_cultivate_label b ON b.F_Id=a.F_TypeId
        WHERE a.F_Id in
        <foreach collection="courseIds" item="item" separator="," close=")" open="(">
            #{item}
        </foreach>
    </select>
    <!--学习章节数量统计方法-->
    <select id="numberOfStudyChapters" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM ftb_cultivate_position_cource_chapter_learning WHERE F_UserId = #{userId}
        AND F_CourceId = #{courseId} AND F_EnabledMark = 0
        <if test="type !=null and type == 1">
            and F_state = 1
        </if>
    </select>

    <select id="organizationListStatisticsV2"
            resultType="jnpf.model.cultivate.v2.course.web.vo.V2InnerCultivateCourseOrgStatisticsDto">
        SELECT
        ler.F_CourceId as courseId,
        ler.F_UserId as userId,
        ler.F_state AS state,
        ler.F_LearnTime as learnTime,
        co.F_Name courseName,
        ty.F_Name as courseTypeName,
        ty.F_Id as courseTypeId,
        co.F_CreatorTime as courseCreationTime
        FROM
        ftb_cultivate_position_cource_learning AS ler
        JOIN ftb_cultivate_course AS co ON ler.F_CourceId = co.F_Id
        JOIN ftb_cultivate_label AS ty ON co.F_TypeId = ty.F_Id
        <where>
            ler.F_state in (1,2)
            and ler.F_EnabledMark = 0
            and co.F_EnabledMark = 0

            <if test="params.startTime != null and params.endTime != null">
                and co.F_CreatorTime BETWEEN #{params.startTime} AND #{params.endTime}
            </if>
            <if test="params.nameType != null and params.nameType != ''">
                and ty.F_Id = #{params.nameType}
            </if>
            <if test="params.courseIds != null and params.courseIds.size() > 0">
                and co.F_Id IN
                <foreach collection="params.courseIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="personListStatisticsV2"
            resultType="jnpf.model.cultivate.v2.course.web.vo.V2InnerCultivateChapterStatisticsDto">
        SELECT
        ler.F_CourceId as courseId,
        ler.F_UserId as userId,
        ler.F_state AS state,
        ler.F_ChapterTime as chapterTime,
        ler.F_chapterId as chapterId
        FROM
        ftb_cultivate_position_cource_chapter_learning AS ler
        JOIN ftb_cultivate_course_chapter AS co ON ler.F_chapterId = co.F_Id
        <where>
            ler.F_EnabledMark = 0
            and co.F_EnabledMark = 0
            <if test="state != null and state.size() > 0">
                and ler.F_state IN
                <foreach collection="state" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="userIds != null and userIds.size() > 0">
                and ler.F_UserId IN
                <foreach collection="userIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>