<?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.system.mapper.TestStudentsMapper">
    
    <resultMap type="TestStudents" id="TestStudentsResult">
        <result property="studentId"    column="student_id"    />
        <result property="name"    column="name"    />
        <result property="age"    column="age"    />
        <result property="gender"    column="gender"    />
        <result property="major"    column="major"    />
        <result property="enrollmentDate"    column="enrollment_date"    />
    </resultMap>

    <resultMap id="TestStudentsTestStudentsScoresResult" type="TestStudents" extends="TestStudentsResult">
        <collection property="testStudentsScoresList" ofType="TestStudentsScores" column="student_id" select="selectTestStudentsScoresList" />
    </resultMap>

    <resultMap type="TestStudentsScores" id="TestStudentsScoresResult">
        <result property="scoreId"    column="score_id"    />
        <result property="studentId"    column="student_id"    />
        <result property="courseName"    column="course_name"    />
        <result property="score"    column="score"    />
    </resultMap>

    <sql id="selectTestStudentsVo">
        select student_id, name, age, gender, major, enrollment_date from test_students
    </sql>

    <select id="selectTestStudentsList" parameterType="TestStudents" resultMap="TestStudentsResult">
        <include refid="selectTestStudentsVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="gender != null  and gender != ''"> and gender = #{gender}</if>
            <if test="major != null  and major != ''"> and major = #{major}</if>
            <if test="enrollmentDate != null "> and enrollment_date = #{enrollmentDate}</if>
        </where>
    </select>
    
    <select id="selectTestStudentsByStudentId" parameterType="Long" resultMap="TestStudentsTestStudentsScoresResult">
        select student_id, name, age, gender, major, enrollment_date
        from test_students
        where student_id = #{studentId}
    </select>

    <select id="selectTestStudentsScoresList" resultMap="TestStudentsScoresResult">
        select score_id, student_id, course_name, score
        from test_students_scores
        where student_id = #{student_id}
    </select>

    <insert id="insertTestStudents" parameterType="TestStudents" useGeneratedKeys="true" keyProperty="studentId">
        insert into test_students
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="age != null">age,</if>
            <if test="gender != null">gender,</if>
            <if test="major != null">major,</if>
            <if test="enrollmentDate != null">enrollment_date,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="age != null">#{age},</if>
            <if test="gender != null">#{gender},</if>
            <if test="major != null">#{major},</if>
            <if test="enrollmentDate != null">#{enrollmentDate},</if>
         </trim>
    </insert>

    <update id="updateTestStudents" parameterType="TestStudents">
        update test_students
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="age != null">age = #{age},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="major != null">major = #{major},</if>
            <if test="enrollmentDate != null">enrollment_date = #{enrollmentDate},</if>
        </trim>
        where student_id = #{studentId}
    </update>

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

    <delete id="deleteTestStudentsByStudentIds" parameterType="String">
        delete from test_students where student_id in 
        <foreach item="studentId" collection="array" open="(" separator="," close=")">
            #{studentId}
        </foreach>
    </delete>
    
    <delete id="deleteTestStudentsScoresByStudentIds" parameterType="String">
        delete from test_students_scores where student_id in 
        <foreach item="studentId" collection="array" open="(" separator="," close=")">
            #{studentId}
        </foreach>
    </delete>

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

    <insert id="batchTestStudentsScores">
        insert into test_students_scores( score_id, student_id, course_name, score) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.scoreId}, #{item.studentId}, #{item.courseName}, #{item.score})
        </foreach>
    </insert>
</mapper>