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

    <!-- 课程分类列表 -->
    <select id="listCourseCats" resultType="com.bicloud.pojo.vo.CourseCatVo">
        SELECT
            id,
            cat_name AS catName
        FROM biology_course_cats
        ORDER BY position ASC, id ASC
    </select>

    <!-- 课程中心分页列表：只返回卡片展示字段（不返回播放地址） -->
    <select id="pageQuery" resultType="com.bicloud.pojo.vo.CourseCardVo">
        SELECT
        c.id,
        c.course_name  AS courseName,
        c.course_icon  AS courseIcon,
        c.addtime      AS addtime,
        c.course_collect AS hot,
        c.course_cat   AS catId,
        cat.cat_name   AS catName
        FROM biology_courses c
        LEFT JOIN biology_course_cats cat
        ON cat.id = c.course_cat
        WHERE c.removed = 0
        <if test="catId != null">
            AND c.course_cat = #{catId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND c.course_name LIKE CONCAT('%', #{keyword}, '%')
        </if>

        <!-- 排序白名单：latest / hot（默认 latest） -->
        <choose>
            <when test="sort != null and sort == 'hot'">
                ORDER BY c.course_collect DESC, c.addtime DESC
            </when>
            <otherwise>
                ORDER BY c.addtime DESC
            </otherwise>
        </choose>
    </select>


</mapper>
