<?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.xjrsoft.module.courseTable.mapper.SecondCourseMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.courseTable.dto.SecondCoursePageDto" resultType="com.xjrsoft.module.courseTable.vo.SecondCoursePageVo">
        SELECT t1.id,t2.name AS semester_name,t1.name,t1.place,t1.content,t1.teacher_name,t1.status,t1.cover_id,
        (SELECT COUNT(*) FROM second_course_enroll WHERE delete_mark = 0 AND second_course_id = t1.id) AS enroll_count,
        ifnull((SELECT COUNT(*) FROM second_course_enroll WHERE delete_mark = 0 AND second_course_id = t1.id and user_id = #{dto.userId}), 0) AS enroll_status FROM second_course t1
        LEFT JOIN base_semester t2 ON t1.base_semester_id = t2.id
        WHERE t1.delete_mark = 0
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.semesterId != null">
            and t1.base_semester_id = #{dto.semesterId}
        </if>
        <if test="dto.status != null">
            and t1.status = #{dto.status}
        </if>
    </select>
    <select id="getEnrollPage" parameterType="com.xjrsoft.module.courseTable.dto.SecondCourseEnrollPageDto" resultType="com.xjrsoft.module.courseTable.vo.SecondCourseEnrollVo">
        SELECT t1.id,t1.status,t2.name,t1.create_date,t2.user_name,t2.mobile,t1.user_id,
        (SELECT id FROM base_teacher WHERE user_id = t1.user_id) AS teacher_id,
        (SELECT id FROM base_student WHERE user_id = t1.user_id) AS student_id FROM second_course_enroll t1
        INNER JOIN xjr_user t2 ON t1.user_id = t2.id
        WHERE t1.delete_mark = 0 AND t1.second_course_id = #{dto.secondCourseId}
        <if test="dto.roleId != null">
            <if test="dto.roleId == 2">
                and (select id from base_teacher where user_id = t1.user_id) is not null
            </if>
            <if test="dto.roleId == 3">
                and (SELECT id FROM base_student WHERE user_id = t1.user_id) is not null
            </if>
        </if>
    </select>
    <select id="getEnrollList" parameterType="com.xjrsoft.module.courseTable.dto.SecondCourseEnrollPageDto" resultType="com.xjrsoft.module.courseTable.vo.SecondCourseEnrollVo">
        SELECT t1.id,t1.status,t2.name,t1.create_date,t2.user_name,t2.mobile,t1.user_id,
        (SELECT id FROM base_teacher WHERE user_id = t1.user_id) AS teacher_id,
        (SELECT id FROM base_student WHERE user_id = t1.user_id) AS student_id FROM second_course_enroll t1
        INNER JOIN xjr_user t2 ON t1.user_id = t2.id
        WHERE t1.delete_mark = 0 AND t1.second_course_id = #{dto.secondCourseId}
        <if test="dto.roleId != null">
            <if test="dto.roleId == 2">
                and (select id from base_teacher where user_id = t1.user_id) is not null
            </if>
            <if test="dto.roleId == 3">
                and (SELECT id FROM base_student WHERE user_id = t1.user_id) is not null
            </if>
        </if>
    </select>
    <select id="getMyPage" parameterType="com.xjrsoft.module.courseTable.dto.SecondCoursePageDto" resultType="com.xjrsoft.module.courseTable.vo.SecondCoursePageVo">
        SELECT t1.id,t2.name AS semester_name,t1.name,t1.place,t1.content,t1.teacher_name,t1.status,
        (SELECT COUNT(*) FROM second_course_enroll WHERE delete_mark = 0 AND second_course_id = t1.id) AS enroll_count FROM second_course t1
        LEFT JOIN base_semester t2 ON t1.base_semester_id = t2.id
        LEFT JOIN second_course_enroll t3 ON t1.id = t3.second_course_id
        WHERE t1.delete_mark = 0 and t3.user_id = #{dto.userId}
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.semesterId != null">
            and t1.base_semester_id = #{dto.semesterId}
        </if>
        <if test="dto.status != null">
            and t1.status = #{dto.status}
        </if>
    </select>
</mapper>