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

    <resultMap type="com.zhongzheng.modules.course.domain.Course" id="CourseResult">
        <result property="courseId" column="course_id"/>
        <result property="categoryId" column="category_id"/>
        <result property="price" column="price"/>
        <result property="duration" column="duration"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="status" column="status"/>
        <result property="teacherIds" column="teacher_ids"/>
        <result property="courseName" column="course_name"/>
        <result property="chapterNum" column="chapter_num"/>
        <result property="sectionNum" column="section_num"/>
        <result property="categoryName" column="category_name"/>
        <result property="sort" column="sort"/>
    </resultMap>


    <select id="selectCourseList" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo"  resultMap="CourseResult">
        SELECT rs.course_name,rs.sort,
               rs.course_id,rs.status,sum(section_num) as section_num,count(chapter_id)
                                                            as chapter_num,mc.category_name,mc.category_id,sum(total_time) as duration from
            (SELECT c.*,count(cs.section_id) as section_num,cs.chapter_id,IFNULL(sum(cs.video_time),0) as total_time
             FROM course c LEFT JOIN course_chapter cc on c.course_id = cc.course_id
                 LEFT JOIN course_section cs
                 on cc.chapter_id = cs.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="courseName != null and courseName != ''">
                    AND c.course_name like concat('%', #{courseName}, '%')
                </if>
                <if test="categoryId != null and categoryId != ''">
                    AND c.category_id = #{categoryId}
                </if>

             GROUP BY cs.chapter_id,c.course_id)rs LEFT JOIN major_category mc on rs.category_id = mc.category_id
        GROUP BY course_id ORDER BY rs.sort DESC
    </select>

    <select id="queryRecommendList" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo"  resultMap="CourseResult">
        SELECT
        course_id,course_name,cover_url,price
        FROM
        course
        WHERE
        STATUS = 1
        <if test="courseId != null and courseId != ''">
            AND course_id != #{courseId}
        </if>
        ORDER BY
        RAND()
        LIMIT 4
    </select>

</mapper>
