<?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.fs.course.mapper.FsUserCourseTrainingCampMapper">

    <!-- 查询训练营列表 -->
    <select id="selectFsUserCourseTrainingCampVOListByMap" resultType="com.fs.course.vo.FsUserCourseTrainingCampVO">
        select
            ctc.training_camp_id,
            ctc.training_camp_name,
            ctc.order_number,
            min(if(ctp.period_starting_time > now(), ctp.period_starting_time, null)) as recent_date,
            count(distinct ctp.period_id) as period_count,
            count(distinct cu.user_id)  as vip_count
        from fs_user_course_training_camp ctc
        left join fs_user_course_period ctp on ctc.training_camp_id = ctp.training_camp_id
        left join fs_course_watch_log cu on cu.period_id = ctp.period_id
        <where>
            <if test="params.trainingCampName != null and params.trainingCampName != ''">
                and ctc.training_camp_name like concat('%',#{params.trainingCampName},'%')
            </if>
        </where>
        group by ctc.training_camp_id, ctc.training_camp_name, ctc.order_number
        order by
        <choose>
            <when test="params.scs != null and params.scs.size() > 0">
                <foreach collection="params.scs" item="sc" separator=",">
                    ${sc.column} ${sc.order}
                </foreach>
            </when>
            <otherwise>
                ctc.order_number desc, ctc.training_camp_id desc
            </otherwise>
        </choose>
    </select>
</mapper>
