<?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.ruoyi.learn.mapper.LearnStudentMapper">
    
    <resultMap type="LearnStudent" id="LearnStudentResult">
        <result property="studentId"    column="student_id"    />
        <result property="classId"    column="class_id"    />
        <result property="campusId"    column="campus_id"    />
        <result property="universityId"    column="university_id"    />
        <result property="directKey"    column="direct_key"    />
        <result property="majorGroup"    column="major_group"    />
        <result property="majorPlanId"    column="major_plan_id"    />
    </resultMap>

    <sql id="selectLearnStudentVo">
        select student_id, class_id, campus_id, university_id,direct_key, major_group,major_plan_id from learn_student
    </sql>

    <select id="selectLearnStudentList" parameterType="LearnStudent" resultMap="LearnStudentResult">
        <include refid="selectLearnStudentVo"/>
        <where>  
            <if test="directKey != null  and directKey != ''"> and direct_key = #{directKey}</if>
        </where>
    </select>

    <select id="selectLearnStudentsByMap" parameterType="map" resultMap="LearnStudentResult">
        SELECT distinct ls.`university_id`, ls.`major_group`, ls.`major_plan_id`
        FROM `learn_student` ls
        LEFT JOIN `learn_test_student` ts ON ts.`student_id` = ls.`student_id` AND ts.`batch_id` = #{batchId}
        JOIN `sys_user` u ON ls.`student_id` = u.`user_id`
        <where> ts.`id` IS NULL and u.`location` = #{location}
            <if test="examTypes != null  and examTypes.size() > 0"> AND u.`exam_type` in <foreach item="id" collection="examTypes" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="classIds != null  and classIds.size() > 0"> AND ls.`class_id` IN <foreach item="id" collection="classIds" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="universityIds != null  and universityIds.size() > 0"> AND ls.`university_id` IN <foreach item="id" collection="universityIds" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="groups != null  and groups.size() > 0"> AND ls.`major_group` IN <foreach item="id" collection="groups" open="(" separator="," close=")">#{id}</foreach></if>
            <if test="planIds != null  and planIds.size() > 0"> AND ls.`major_plan_id` IN <foreach item="id" collection="planIds" open="(" separator="," close=")">#{id}</foreach></if>
        </where>
    </select>

    <select id="selectClassStudents" resultMap="LearnStudentResult">
        SELECT ls.*, u.nick_name studentName , tp.`paper_id` paperId, ts.`status`
        FROM `learn_student` ls
        JOIN `sys_user` u ON ls.`student_id` = u.`user_id`
        LEFT JOIN `learn_test_paper` tp ON tp.`batch_id` = #{batchId} AND tp.`direct_key` = ls.`direct_key`
        LEFT JOIN `learn_test_student` ts ON ts.`batch_id` = #{batchId} AND ls.`student_id` = ts.`student_id`
        where ts.`id` IS NULL AND ls.`class_id` IN <foreach item="id" collection="classIds" open="(" separator="," close=")">#{id}</foreach>
    </select>




    <select id="selectLearnStudentByStudentId" parameterType="Long" resultMap="LearnStudentResult">
        <include refid="selectLearnStudentVo"/>
        where student_id = #{studentId}
    </select>

    <insert id="insertLearnStudent" parameterType="LearnStudent">
        insert into learn_student
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="studentId != null">student_id,</if>
            <if test="classId != null">class_id,</if>
            <if test="campusId != null">campus_id,</if>
            <if test="universityId != null">university_id,</if>
            <if test="directKey != null">direct_key,</if>
            <if test="majorGroup != null">major_group,</if>
            <if test="majorPlanId != null">major_plan_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="studentId != null">#{studentId},</if>
            <if test="classId != null">#{classId},</if>
            <if test="campusId != null">#{campusId},</if>
            <if test="universityId != null">#{universityId},</if>
            <if test="directKey != null">#{directKey},</if>
            <if test="majorGroup != null">#{majorGroup},</if>
            <if test="majorPlanId != null">#{majorPlanId},</if>
         </trim>
    </insert>

    <update id="updateLearnStudent" parameterType="LearnStudent">
        update learn_student
        <trim prefix="SET" suffixOverrides=",">
            <if test="classId != null">class_id = #{classId},</if>
            <if test="campusId != null">campus_id = #{campusId},</if>
            <if test="universityId != null">university_id = #{universityId},</if>
            <if test="directKey != null">direct_key = #{directKey},</if>
            <if test="majorGroup != null">major_group = #{majorGroup},</if>
            <if test="majorPlanId != null">major_plan_id = #{majorPlanId},</if>
        </trim>
        where student_id = #{studentId}
    </update>

    <delete id="deleteLearnStudentByStudentId" parameterType="Long">
        delete from learn_student where student_id = #{studentId}
    </delete>

    <delete id="deleteLearnStudentByStudentIds" parameterType="String">
        delete from learn_student where student_id in 
        <foreach item="studentId" collection="array" open="(" separator="," close=")">
            #{studentId}
        </foreach>
    </delete>
</mapper>