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

    <resultMap type="com.zhongzheng.modules.course.domain.CourseModule" id="CourseModuleResult">
        <result property="moduleId" column="module_id"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="moduleName" column="module_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="code" column="code"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseModuleVo" id="CourseModuleResultVo">
        <result property="moduleId" column="module_id"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="moduleName" column="module_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="status" column="status"/>
        <result property="code" column="code"/>
        <result property="chapterNum" column="chapter_num"/>
        <result property="sectionNum" column="section_num"/>
        <result property="durationTime" column="duration_time"/>
        <collection property="businessList" javaType="java.util.List" resultMap="CourseModuleBusiness"/>
        <collection property="courseList" javaType="java.util.List" resultMap="CourseSectionListResult"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.domain.CourseModuleBusiness" id="CourseModuleBusiness">
        <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.CourseSectionListVo" id="CourseSectionListResult">
        <result property="courseId" column="course_id"/>
        <result property="courseName" column="course_name"/>
    </resultMap>

    <select id="selectList" parameterType="com.zhongzheng.modules.course.bo.CourseModuleQueryBo" resultMap="CourseModuleResultVo">
        SELECT
        c.*,
        cmb.education_type_id,
        cmb.id,
        cmb.business_id,
        cmb.subject_id,
        cmb.project_id,
        cet.education_name,
        cpt.project_name,
        cb.business_name,
        cs.subject_name,
        m.course_id,
        co.course_name
        FROM
        (
        SELECT
        cm.*,
        count( ccs.section_id ) AS section_num,
        count( DISTINCT cc.chapter_id ) AS chapter_num,
        COALESCE ( SUM( cs.duration_time ), 0 ) AS duration_time
        FROM
        course_module cm
        LEFT JOIN course_module_chapter cmc ON cm.module_id = cmc.module_id
        LEFT JOIN course_chapter cc ON cmc.chapter_id = cc.chapter_id
        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
        WHERE
        cm.STATUS !=- 1
        AND cs.`status` !=- 1
        AND cc.`status` !=- 1
        GROUP BY
        cm.module_id
        ) c
        LEFT JOIN course_module_business cmb ON cmb.module_id = c.module_id
        LEFT JOIN course_education_type cet ON cmb.education_type_id = cet.id
        LEFT JOIN course_project_type cpt ON cmb.project_id = cpt.id
        LEFT JOIN course_business cb ON cmb.business_id = cb.id
        LEFT JOIN course_subject cs ON cmb.subject_id = cs.id
        LEFT JOIN ( SELECT course_id, menu_id FROM course_menu WHERE type = 1 AND STATUS = 1 ) m ON c.module_id = m.menu_id
        LEFT JOIN course co ON m.course_id = co.course_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 publishStatus != ''">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="educationTypeId != null and educationTypeId != ''">
            AND cmb.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND cmb.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND cmb.business_id = #{businessId}
        </if>
        <if test="moduleName != null and moduleName != ''">
            AND c.module_name like concat('%', #{moduleName}, '%')
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
    </select>


    <select id="selectList_COUNT" resultType="Long">
        SELECT
        count( DISTINCT c.module_id )
        FROM
        course_module c
        LEFT JOIN course_module_business cmb ON cmb.module_id = c.module_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 publishStatus != ''">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="educationTypeId != null and educationTypeId != ''">
            AND cmb.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND cmb.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND cmb.business_id = #{businessId}
        </if>
        <if test="moduleName != null and moduleName != ''">
            AND c.module_name like concat('%', #{moduleName}, '%')
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
    </select>
</mapper>
