<?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.zhongzheng.modules.course.mapper.CourseMenuMapper">

    <resultMap type="com.zhongzheng.modules.course.domain.CourseMenu" id="CourseMenuResult">
        <result property="id" column="id"/>
        <result property="courseId" column="course_id"/>
        <result property="menuId" column="menu_id"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="sort" column="sort"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseMenuVo" id="CourseMenuResultVo">
        <result property="id" column="id"/>
        <result property="courseId" column="course_id"/>
        <result property="menuId" column="menu_id"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="status" column="status"/>
        <result property="menuName" column="menu_name"/>
        <result property="durationTime" column="duration_time"/>
        <result property="sort" column="sort"/>
        <result property="sectionType" column="section_type"/>
        <result property="liveStartTime" column="live_start_time"/>
        <result property="liveEndTime" column="live_end_time"/>
        <result property="recordingUrl" column="recording_url"/>
        <result property="liveUrl" column="live_url"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseUserMenuVo" id="CourseUserMenuVo">
        <result property="id" column="id"/>
        <result property="courseId" column="course_id"/>
        <result property="gradeId" column="grade_id"/>
        <result property="menuId" column="menu_id"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="status" column="status"/>
        <result property="menuName" column="menu_name"/>
        <result property="durationTime" column="duration_time"/>
        <result property="sort" column="sort"/>
        <result property="sectionType" column="section_type"/>
        <result property="liveStartTime" column="live_start_time"/>
        <result property="liveEndTime" column="live_end_time"/>
        <result property="recordingUrl" column="recording_url"/>
        <result property="liveUrl" column="live_url"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.course.bo.CourseMenuQueryBo"  resultMap="CourseMenuResultVo">
        SELECT
            m.*,
            CASE

                WHEN m.type = 1 THEN
                    cm.module_name
                WHEN m.type = 2 THEN
                    cc.`name`
                WHEN m.type = 3 THEN
                    cs.`name`
                END menu_name,
            CASE

                WHEN m.type = 1 THEN
                    0
                WHEN m.type = 2 THEN
                    0
                WHEN m.type = 3 THEN
                    cs.duration_time
                END duration_time,
            cs.section_type,
            cs.live_start_time,
            cs.live_end_time,
            cs.live_url,
            CASE
                WHEN m.type = 1 THEN
                    ''
                WHEN m.type = 2 THEN
                    ''
                WHEN m.type = 3 THEN
                    cs.recording_url
                END recording_url

        FROM
            course_menu m
                LEFT JOIN course_module cm ON m.menu_id = cm.module_id
                AND m.type = 1
                LEFT JOIN course_chapter cc ON m.menu_id = cc.chapter_id
                AND m.type = 2
                LEFT JOIN course_section cs ON m.menu_id = cs.section_id
                AND m.type = 3
        WHERE
            m.course_id = #{courseId}
        ORDER BY m.sort asc
    </select>

    <select id="menuList" parameterType="com.zhongzheng.modules.course.bo.CourseMenuQueryBo"  resultMap="CourseUserMenuVo">
        SELECT
            m.*,
            #{gradeId} as grade_id,
            CASE

                WHEN m.type = 1 THEN
                    cm.module_name
                WHEN m.type = 2 THEN
                    cc.`name`
                WHEN m.type = 3 THEN
                    cs.`name`
                END menu_name,
            CASE

                WHEN m.type = 1 THEN
                    0
                WHEN m.type = 2 THEN
                    0
                WHEN m.type = 3 THEN
                    cs.duration_time
                END duration_time,
            cs.section_type,
            cs.live_start_time,
            cs.live_end_time,
            cs.live_url,
            CASE
                WHEN m.type = 1 THEN
                    ''
                WHEN m.type = 2 THEN
                    ''
                WHEN m.type = 3 THEN
                    cs.recording_url
                END recording_url

        FROM
            course_menu m
                LEFT JOIN course_module cm ON m.menu_id = cm.module_id
                AND m.type = 1 and cm.status=1
                LEFT JOIN course_chapter cc ON m.menu_id = cc.chapter_id
                AND m.type = 2 and cc.status=1
                LEFT JOIN course_section cs ON m.menu_id = cs.section_id
                AND m.type = 3 and cs.status=1
        WHERE
            m.course_id = #{courseId}
        ORDER BY m.sort
    </select>

    <select id="rebuildCheck" parameterType="map"  resultType="Integer">
        SELECT
            COUNT(up.id)
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
        WHERE
            up.grade_id =#{gradeId}
          AND up.user_id = #{userId}
          AND up.module_id= 0
          and up.chapter_id=0
          and up.section_id=#{sectionId}
          AND ups.period_status =1
          and (ups.`status` = 0 or ups.`status` = 3)
    </select>

    <select id="rebuildChapterCheck" parameterType="map"  resultType="Integer">
        SELECT
            COUNT(up.id)
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
        WHERE
            up.grade_id =#{gradeId}
          AND up.user_id = #{userId}
          AND up.module_id= 0
          and up.chapter_id=#{chapterId}
          AND ups.period_status =1
          and (ups.`status` = 0 or ups.`status` = 3)
    </select>

    <select id="rebuildModuleCheck" parameterType="map"  resultType="Integer">
        SELECT
            COUNT(up.id)
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
        WHERE
            up.grade_id =#{gradeId}
          AND up.user_id = #{userId}
          AND up.module_id= #{moduleId}
          AND ups.period_status =1
          and (ups.`status` = 0 or ups.`status` = 3)
    </select>
    <select id="learningCheck" parameterType="map"  resultType="Integer">
        SELECT
            COUNT(up.id)
        FROM
            user_period up
                LEFT JOIN user_period_status ups ON up.id = ups.period_id
        WHERE
            up.grade_id =#{gradeId}
          AND up.user_id = #{userId}
          AND up.module_id= 0
          and up.chapter_id=0
          and up.section_id=#{sectionId}
          AND ups.period_status =1
          and ups.record_end_time is not null
    </select>
</mapper>
