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

    <resultMap type="com.zhongzheng.modules.course.domain.CourseModuleChapter" id="CourseModuleChapterResult">
        <result property="id" column="id"/>
        <result property="moduleId" column="module_id"/>
        <result property="chapterId" column="chapter_id"/>
        <result property="sort" column="c_sort"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseModuleChapterVo" id="CourseModuleChapterResultVo">
        <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="code" column="code"/>
        <result property="name" column="name"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="status" column="status"/>
        <result property="publishStatus" column="publish_status"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseUserModuleChapterVo" id="CourseUserModuleChapterVo">
        <result property="chapterId" column="chapter_id"/>
        <result property="sort" column="c_sort"/>
        <result property="id" column="id"/>
        <result property="gradeId" column="grade_id"/>

        <result property="code" column="code"/>
        <result property="name" column="name"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="status" column="status"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="courseId" column="course_id"/>
    </resultMap>

    <select id="getListById" parameterType="Long"  resultMap="CourseModuleChapterResultVo">
        SELECT
            cc.*,
            cmc.sort AS c_sort,
            cmc.id,
            cmc.module_id
        FROM
            course_module_chapter cmc
                LEFT JOIN course_chapter cc ON cmc.chapter_id = cc.chapter_id
        WHERE
            cc.`status` !=- 1
	AND cmc.module_id = #{id}
        ORDER BY
            c_sort asc
    </select>
    <select id="chapterList" parameterType="com.zhongzheng.modules.course.bo.CourseMenuQueryBo"  resultMap="CourseUserModuleChapterVo">
        SELECT
            cc.*,
            cmc.sort AS c_sort,
            cmc.id,
            cmc.module_id,
            #{gradeId} as grade_id,
            #{courseId} as course_id
        FROM
            course_module_chapter cmc
                LEFT JOIN course_chapter cc ON cmc.chapter_id = cc.chapter_id
        WHERE
            cc.`status` =1
	AND cmc.module_id = #{moduleId}
        ORDER BY
            c_sort DESC
    </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= #{moduleId}
          and up.chapter_id=#{chapterId}
          AND ups.period_status =1
          and (ups.`status` = 0 or ups.`status` = 3)
    </select>

</mapper>
