<?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.ynfy.buss.course.course.mapper.CourseMapper">
    <select id="selectCourseList"  resultType="com.ynfy.buss.course.course.entity.Course">
        SELECT
                tmp1.*,
                u.realname AS teacherName
        FROM
        (
            SELECT
                *
            FROM course
            WHERE
                open_type = 1
        <choose>
            <when test="user!=null and user.orgCode!=null and user.orgCode!=''">
            UNION
                SELECT
                    *
                FROM course
                WHERE
                    open_type = 2
                    AND learner like concat('%', #{user.orgCode}, '%')
            </when>

            <when test="user!=null and user.username!=null and user.username!=''">
            UNION
                SELECT
                    *
                FROM course
                WHERE
                    open_type = 3
                    AND learner like concat('%', #{user.username}, '%')
            </when>
        </choose>
        ) tmp1
        LEFT JOIN sys_user u on tmp1.teacher_id=u.id
        <where>
            <if test="course.name!=null and course.name!=''">
                AND tmp1.name like concat('%', #{course.name}, '%')
            </if>
            <if test="course.openType!=null">
                AND tmp1.open_type = #{course.openType}
            </if>
        </where>
    </select>
    <select id="queryById"  resultType="com.ynfy.buss.course.course.entity.Course">
        SELECT
            c.*,
            e.title AS examTitle,
            u.realname AS teacherName,
            u.avatar as teacherAvatar,
            u.teacher_introduce as teacherIntroduce,
            u.post as teacherPost
        FROM
            course c
                LEFT JOIN exam e ON c.exam_id = e.id
                LEFT JOIN sys_user u ON c.teacher_id = u.id
        where c.id = #{id}
    </select>

    <select id="listHotCourse" resultType="com.ynfy.buss.course.course.entity.Course">
        SELECT
            c.*,
            tmp.learn_number
        FROM
            course c
        LEFT JOIN
            (
                SELECT
                    cc.course_id,
                    count(
                            DISTINCT ( ucc.user_id )) AS learn_number
                FROM
                    user_course_catalog ucc
                        LEFT JOIN course_catalog cc ON ucc.course_catalog_id = cc.id
                GROUP BY
                    cc.course_id
            ) tmp
            ON c.id = tmp.course_id
        ORDER BY
                tmp.learn_number DESC
        <if test="limit !=null">
            LIMIT #{limit}
        </if>
    </select>
</mapper>