<?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.schoolinout.system.mapper.StudentMapper">

    <resultMap type="Student" id="StudentResult">
        <result property="id" column="id"/>
        <result property="schoolId" column="school_id"/>
        <result property="classId" column="class_id"/>
        <result property="studentName" column="student_name"/>
        <result property="studentPic" column="student_pic"/>
        <result property="studentGender" column="student_gender"/>
        <result property="studentNum" column="student_num"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectStudentVo">
        select id,
               school_id,
               class_id,
               student_name,
               student_pic,
               student_gender,
               student_num,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from tb_school_student
    </sql>

    <select id="selectStudentList" parameterType="Student" resultMap="StudentResult">
        <include refid="selectStudentVo"/>
        <where>
            <if test="schoolId != null ">and school_id = #{schoolId}</if>
            <if test="classId != null ">and class_id = #{classId}</if>
            <if test="studentName != null  and studentName != ''">and student_name like concat('%', #{studentName},
                '%')
            </if>
            <if test="studentGender != null  and studentGender != ''">and student_gender = #{studentGender}</if>
            <if test="studentNum != null  and studentNum != ''">and student_num like concat('%', #{studentNum}, '%')
            </if>
        </where>
    </select>

    <select id="selectStudentById" parameterType="Long" resultMap="StudentResult">
        <include refid="selectStudentVo"/>
        where id = #{id}
    </select>
    <select id="selectStudentListAll" resultType="com.schoolinout.system.domain.vo.StudentListVo">
        SELECT
        student.id,
        student.student_name,
        student.student_pic,
        student.student_gender,
        student.student_num,
        school.school_name,
        clazz.class_name
        FROM tb_school_student student
        LEFT JOIN tb_school school ON student.school_id = school.id
        LEFT JOIN tb_school_class clazz ON student.class_id = clazz.id
        <where>
            <if test="schoolId != null">
                AND student.school_id = #{schoolId}
            </if>
            <if test="classId != null">
                AND student.class_id = #{classId}
            </if>
        </where>
    </select>
    <select id="selectOnlyOne" resultType="com.schoolinout.system.domain.Student" resultMap="StudentResult">
        <include refid="selectStudentVo"/>
        <where>
            <if test="id != null">
                AND id = #{id}
            </if>
            <if test="schoolId != null">
                AND school_id = #{schoolId}
            </if>
            <if test="id != null">
                AND class_id = #{classId}
            </if>
            <if test="studentNum != null and studentNum != ''">
                AND student_num = #{studentNum}
            </if>
        </where>
    </select>

    <insert id="insertStudent" parameterType="Student" useGeneratedKeys="true" keyProperty="id">
        insert into tb_school_student
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="classId != null">class_id,</if>
            <if test="studentName != null and studentName != ''">student_name,</if>
            <if test="studentPic != null and studentPic != ''">student_pic,</if>
            <if test="studentGender != null">student_gender,</if>
            <if test="studentNum != null">student_num,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">#{schoolId},</if>
            <if test="classId != null">#{classId},</if>
            <if test="studentName != null and studentName != ''">#{studentName},</if>
            <if test="studentPic != null and studentPic != ''">#{studentPic},</if>
            <if test="studentGender != null">#{studentGender},</if>
            <if test="studentNum != null">#{studentNum},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <update id="updateStudent" parameterType="Student">
        update tb_school_student
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="classId != null">class_id = #{classId},</if>
            <if test="studentName != null and studentName != ''">student_name = #{studentName},</if>
            <if test="studentPic != null and studentPic != ''">student_pic = #{studentPic},</if>
            <if test="studentGender != null">student_gender = #{studentGender},</if>
            <if test="studentNum != null">student_num = #{studentNum},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStudentById" parameterType="Long">
        delete
        from tb_school_student
        where id = #{id}
    </delete>

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