<?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="prefixName" column="prefix_name"/>
        <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="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="courseName" column="course_name"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="sort" column="sort"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="majorId" column="major_id"/>
        <result property="code" column="code"/>
        <result property="subjectId" column="subject_id"/>
        <result property="projectId" column="project_id"/>
    </resultMap>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseVo" id="CourseResultVo">
        <result property="courseId" column="course_id"/>
        <result property="prefixName" column="prefix_name"/>
        <result property="coverUrl" column="cover_url"/>
        <result property="introduction" column="introduction"/>
        <result property="status" column="status"/>
        <result property="educationTypeId" column="education_type_id"/>
        <result property="businessId" column="business_id"/>
        <result property="schoolId" column="school_id"/>
        <result property="courseName" column="course_name"/>
        <result property="publishStatus" column="publish_status"/>
        <result property="sort" column="sort"/>
        <result property="suitableObject" column="suitable_object"/>
        <result property="pcDetailHtml" column="pc_detail_html"/>
        <result property="mobileDetailHtml" column="mobile_detail_html"/>
        <result property="majorId" column="major_id"/>
        <result property="code" column="code"/>
        <result property="subjectId" column="subject_id"/>
        <result property="projectId" column="project_id"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="educationName" column="education_name"/>
        <result property="projectName" column="project_name"/>
        <result property="businessName" column="business_name"/>
        <result property="schoolName" column="school_name"/>
        <result property="categoryName" column="category_name"/>
        <result property="subjectName" column="subject_name"/>
        <collection property="goodsList" column="course_id" select="findGoodsList"/>
    </resultMap>

    <select id="findGoodsList" resultMap="CourseGoodsListResult">
        SELECT
            g.goods_name,
            g.goods_id
        FROM
            goods_course gc
             LEFT JOIN goods g ON gc.goods_id = g.goods_id
        WHERE
            gc.course_id =#{course_id}
    </select>

    <resultMap type="com.zhongzheng.modules.course.vo.CourseGoodsVo" id="CourseGoodsListResult">
        <result property="goodsName" column="goods_name"/>
        <result property="goodsId" column="goods_id"/>
    </resultMap>

    <select id="selectCourseList" parameterType="com.zhongzheng.modules.course.bo.CourseQueryBo" resultMap="CourseResultVo">
        SELECT
        c.*,
        cet.education_name,
        cpt.project_name,
        cb.business_name,
        s.school_name,
        m.category_name,
        cs.subject_name
        FROM
        course c
        LEFT JOIN course_education_type cet ON c.education_type_id = cet.id AND cet.status =1
        LEFT JOIN course_project_type cpt ON c.project_id = cpt.id AND cpt.status =1
        LEFT JOIN course_business cb ON c.business_id = cb.id AND cb.status =1
        LEFT JOIN school s ON s.id = c.school_id AND s.status =1
        LEFT JOIN major m ON c.major_id = m.id AND m.status =1
        LEFT JOIN course_subject cs ON cs.id = c.subject_id AND cs.status =1
        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="educationTypeId != null and educationTypeId != ''">
            AND c.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND c.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND c.business_id = #{businessId}
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
        <if test="courseName != null and courseName != ''">
            AND c.course_name like concat('%', #{courseName}, '%')
        </if>
        <if test="publishStatus != null ">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="schoolId != null and schoolId != ''">
            AND c.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND c.major_id = #{majorId}
        </if>
        GROUP BY
        c.course_id
        ORDER BY c.course_id DESC
    </select>

    <select id="selectCourseList_COUNT" resultType="Long">
        SELECT
            count(distinct c.course_id )
        FROM
            course c
        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="educationTypeId != null and educationTypeId != ''">
            AND c.education_type_id = #{educationTypeId}
        </if>
        <if test="subjectId != null and subjectId != ''">
            AND c.subject_id = #{subjectId}
        </if>
        <if test="businessId != null and businessId != ''">
            AND c.business_id = #{businessId}
        </if>
        <if test="prefixName != null and prefixName != ''">
            AND c.prefix_name like concat('%', #{prefixName}, '%')
        </if>
        <if test="courseName != null and courseName != ''">
            AND c.course_name like concat('%', #{courseName}, '%')
        </if>
        <if test="publishStatus != null ">
            AND c.publish_status = #{publishStatus}
        </if>
        <if test="schoolId != null and schoolId != ''">
            AND c.school_id = #{schoolId}
        </if>
        <if test="majorId != null and majorId != ''">
            AND c.major_id = #{majorId}
        </if>


    </select>

    <select id="selectDetailById" parameterType="Long"  resultMap="CourseResultVo">
        SELECT
            c.*,
            cet.education_name,
            cpt.project_name,
            cb.business_name,
            s.school_name,
            m.category_name,
            cs.subject_name
        FROM
            course c
                LEFT JOIN course_education_type cet ON c.education_type_id = cet.id
                LEFT JOIN course_project_type cpt ON c.project_id = cpt.id
                LEFT JOIN course_business cb ON c.business_id = cb.id
                LEFT JOIN school s ON s.id = c.school_id
                LEFT JOIN major m ON c.major_id = m.id
                LEFT JOIN course_subject cs ON cs.id = c.subject_id
        WHERE
            course_id = #{id}
    </select>
</mapper>
