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

    <resultMap type="com.zhongzheng.modules.course.domain.CourseChapter" id="CourseChapterResult">
        <result property="chapterId" column="chapter_id"/>
        <result property="name" column="name"/>
        <result property="sort" column="sort"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="code" column="code"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseChapterVo" id="CourseChapterResultVo">
        <result property="chapterId" column="chapter_id"/>
        <result property="name" column="name"/>
        <result property="sort" column="sort"/>
        <result property="status" column="status"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="code" column="code"/>
        <result property="sectionNum" column="section_num"/>
        <result property="durationTime" column="duration_time"/>
        <collection property="businessList" column="chapter_id" select="findBusinessList"/>
        <collection property="moduleList" column="chapter_id" select="findModuleList"/>
        <collection property="courseList" column="chapter_id" select="findCourseList"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.domain.CourseChapterBusiness" id="CourseChapterBusiness">
        <result property="id" column="id"/>
        <result property="subjectId" column="subject_id"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="projectId" column="project_id"/>

        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="subjectName" column="subject_name"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseModuleChapterListVo" id="CourseModuleChapterListResult">
        <result property="moduleId" column="module_id"/>
        <result property="moduleName" column="module_name"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseSectionListVo" id="CourseSectionListResult">
        <result property="courseId" column="course_id"/>
        <result property="courseName" column="course_name"/>
    </resultMap>

    <select id="findCourseList" resultMap="CourseSectionListResult">
        SELECT
            m.course_id,
            co.course_name
        FROM
                ( SELECT course_id, menu_id FROM course_menu WHERE type = 2 AND STATUS = 1 ) m
            LEFT JOIN course co ON m.course_id = co.course_id
        WHERE
            m.menu_id=#{chapter_id}
    </select>

    <select id="findModuleList" resultMap="CourseModuleChapterListResult">
        SELECT
            cmc.module_id,
            cm.module_name
        FROM
            course_module_chapter cmc
            LEFT JOIN course_module cm ON cmc.module_id = cm.module_id
        WHERE
            cmc.chapter_id=#{chapter_id}
            and cm.status =1
    </select>

    <select id="findBusinessList" resultMap="CourseChapterBusiness">
        SELECT
            ccb.education_type_id,
            ccb.id,
            ccb.business_id,
            ccb.subject_id,
            ccb.project_id,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            cs.subject_name
        FROM
            course_chapter_business ccb
            LEFT JOIN course_education_type cet ON ccb.education_type_id = cet.id AND cet.status =1
            LEFT JOIN course_project_type cpt ON ccb.project_id = cpt.id AND cpt.status =1
            LEFT JOIN course_business cb ON ccb.business_id = cb.id AND cb.status =1
            LEFT JOIN course_subject cs ON ccb.subject_id = cs.id AND cs.status =1
        WHERE
            ccb.chapter_id=#{chapter_id}
    </select>

    <select id="selectList" parameterType="com.zhongzheng.modules.course.bo.CourseChapterQueryBo" resultMap="CourseChapterResultVo">
        SELECT
        c.*
        FROM
        (
        SELECT
        cc.*,
        count( cs.section_id ) AS section_num,
        COALESCE ( SUM( cs.duration_time ), 0 ) AS duration_time
        FROM
        course_chapter cc
        LEFT JOIN course_chapter_section ccs ON cc.chapter_id = ccs.chapter_id
        LEFT JOIN course_section cs ON ccs.section_id = cs.section_id and cs.status!=-1
        where cc.status !=-1
        GROUP BY
        cc.chapter_id
        ) c
        LEFT JOIN course_chapter_business ccb ON ccb.chapter_id = c.chapter_id
        WHERE
        1 = 1 AND c.status !=-1
        <if test="status != null and status.size()!=0 ">
            AND c.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="publishStatus != null ">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="key != null and key != ''">
            AND (c.name like concat('%', #{key}, '%') or c.prefix_name like concat('%', #{key}, '%') or c.code = #{key})
        </if>
        <if test="educationTypeId != null and educationTypeId != ''">
            AND ccb.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND ccb.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND ccb.business_id = #{businessId}
        </if>
        <if test="name != null and name != ''">
            AND c.name like concat('%', #{name}, '%')
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
        GROUP BY
        c.chapter_id
        ORDER BY c.chapter_id DESC
   </select>


    <select id="selectList_COUNT" resultType="Long">
        SELECT
        count(distinct c.chapter_id )
        FROM
        course_chapter  c
        LEFT JOIN course_chapter_business ccb ON c.chapter_id = ccb.chapter_id
        WHERE
        1 = 1
        <if test="status != null and status.size()!=0 ">
            AND c.status in
            <foreach collection="status" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="publishStatus != null">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="educationTypeId != null and educationTypeId != ''">
            AND ccb.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND ccb.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND ccb.business_id = #{businessId}
        </if>
        <if test="name != null and name != ''">
            AND c.name like concat('%', #{name}, '%')
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>

    </select>
</mapper>
