<?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.CourseChapterSectionMapper">

    <resultMap type="com.zhongzheng.modules.course.domain.CourseChapterSection" id="CourseChapterSectionResult">
        <result property="sectionId" column="section_id"/>
        <result property="chapterId" column="chapter_id"/>
        <result property="sort" column="sort"/>
        <result property="id" column="id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseChapterSectionVo" id="CourseChapterSectionResultVo">
        <result property="sectionId" column="section_id"/>
        <result property="chapterId" column="chapter_id"/>
        <result property="sort" column="c_sort"/>
        <result property="id" column="id"/>
        <result property="type" column="type"/>

        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="status" column="status"/>
        <result property="sectionType" column="section_type"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="durationTime" column="duration_time"/>
        <result property="liveStartTime" column="live_start_time"/>
        <result property="liveEndTime" column="live_end_time"/>
        <result property="recordingUrl" column="recording_url"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseUserChapterSectionVo" id="CourseUserChapterSectionVo">
        <result property="typeId" column="type_id"/>
        <result property="chapterId" column="chapter_id"/>
        <result property="sort" column="c_sort"/>
        <result property="id" column="id"/>
        <result property="type" column="type"/>

        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="status" column="status"/>
        <result property="sectionType" column="section_type"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="durationTime" column="duration_time"/>
        <result property="liveStartTime" column="live_start_time"/>
        <result property="liveEndTime" column="live_end_time"/>
        <result property="recordingUrl" column="recording_url"/>
        <result property="doType" column="do_type"/>
    </resultMap>


    <select id="getListById" parameterType="Long"  resultMap="CourseChapterSectionResultVo">
        SELECT
            cs.*,
            ccs.sort as c_sort,
            ccs.id,
            ccs.chapter_id
        FROM
            course_chapter_section ccs
                LEFT JOIN course_section cs ON ccs.section_id = cs.section_id
        WHERE
            cs.`status` !=- 1
	        AND ccs.chapter_id = #{id}
        ORDER BY
            c_sort asc
    </select>

    <select id="getInformById" parameterType="Long"  resultMap="CourseChapterSectionResultVo">
        SELECT
            cs.*,
            ccs.sort as c_sort,
            ccs.id,
            ccs.chapter_id,
               3 as type
        FROM
            course_chapter_section ccs
                LEFT JOIN course_section cs ON ccs.section_id = cs.section_id
        WHERE
            cs.`status` !=- 1
	        AND ccs.chapter_id = #{id}
        ORDER BY
            c_sort DESC
    </select>

    <select id="sectionList" parameterType="com.zhongzheng.modules.course.bo.CourseMenuQueryBo"  resultMap="CourseUserChapterSectionVo">
        SELECT
            cs.section_id as type_id,
            #{gradeId} as grade_id,
        <if test="moduleId != null ">
            #{moduleId} as module_id,
        </if>
        <if test="moduleId == null ">
            0 as module_id,
        </if>
            cs.*,
            ccs.sort as c_sort,
            ccs.id,
            ccs.chapter_id,
            1 as type

        FROM
            course_chapter_section ccs
                LEFT JOIN course_section cs ON ccs.section_id = cs.section_id
        WHERE
            cs.`status` = 1
          AND ccs.chapter_id = #{chapterId}
        ORDER BY
            c_sort ASC
    </select>

    <select id="examList" parameterType="com.zhongzheng.modules.course.bo.CourseMenuQueryBo"  resultMap="CourseUserChapterSectionVo">
        SELECT
            e.*,
            cme.course_id,
            cme.module_id,
            cme.chapter_id,
            cme.section_id,
            e.exam_name as name,
            e.exam_id as type_id,
            #{gradeId} as grade_id,
            2 as type
        FROM
            exam e
                LEFT JOIN course_menu_exam cme ON cme.exam_id = e.exam_id
        where 1=1
          and cme.chapter_id = #{chapterId}
        and cme.course_id = #{courseId}
    </select>

    <select id="getListTotalTime" parameterType="Long"  resultType="java.lang.Long">
        SELECT
            IFNULL(sum( cs.duration_time ),0) AS duration_time
        FROM
            course_chapter_section ccs
                LEFT JOIN course_section cs ON ccs.section_id = cs.section_id
        WHERE
            cs.`status` !=- 1
	    AND ccs.chapter_id  =#{id}
    </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= #{moduleId}
          and up.chapter_id=#{chapterId}
          and up.section_id=#{typeId}
          and up.exam_id=#{examId}
          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= #{moduleId}
          and up.chapter_id=#{chapterId}
          and up.section_id=#{typeId}
          and up.exam_id=#{examId}
          AND ups.period_status =1
    </select>
</mapper>
