<?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,
            tmp2.learner_number,
            <include refid="stateCondition"/>
        FROM
        (
            SELECT
                *
            FROM course
            WHERE
                open_type = 1
            <if test="user!=null and user.orgCode!=null and user.orgCode!=''">
                UNION
                SELECT
                    *
                FROM course
                WHERE
                    open_type = 2
                    AND learner like concat('%', #{user.orgCode}, '%')
            </if>
            <if test="user!=null and user.username!=null and user.username!=''">
                UNION
                SELECT
                    *
                FROM course
                WHERE
                    open_type = 3
                    AND learner like concat('%', #{user.username}, '%')
            </if>
        ) tmp1
        LEFT JOIN
        (
            SELECT
                cc.course_id,
                count(
                DISTINCT (ucc.user_id)) AS learner_number
            FROM
                user_course_catalog ucc
            LEFT JOIN
                course_catalog cc ON ucc.course_catalog_id = cc.id
            GROUP BY
                cc.course_id
        ) tmp2
        ON tmp1.id = tmp2.course_id
        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.categoryId!=null and course.categoryId!=''">
                AND tmp1.category_id = #{course.categoryId}
            </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>
    <sql id="stateCondition">
        CASE
        WHEN NOW() &lt; START_TIME THEN 2
        WHEN NOW() &gt; end_TIME THEN 3
        WHEN NOW() &gt; START_TIME AND NOW() &lt; end_TIME THEN 0
        ELSE NULL
        END
        AS state
    </sql>

    <select id="countCourseLearner" resultType="int">
        SELECT
            count(
                    DISTINCT ( ucc.user_id )) AS learner_number
        FROM
            user_course_catalog ucc
                LEFT JOIN course_catalog cc ON ucc.course_catalog_id = cc.id
        WHERE
            course_id = #{courseId}
    </select>
</mapper>