<?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.hchyun.test.dao.StuDao">
    
    <resultMap type="Stu" id="StuResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="tel"    column="tel"    />
        <result property="email"    column="email"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <resultMap id="StuResultsResult" type="Stu" extends="StuResult">
        <collection property="resultsList" notNullColumn="id" javaType="java.util.List" resultMap="ResultsResult" />
    </resultMap>

    <resultMap type="Results" id="ResultsResult">
        <result property="id"    column="id"    />
        <result property="sId"    column="s_id"    />
        <result property="java"    column="java"    />
        <result property="images"    column="images"    />
        <result property="file"    column="file"    />
    </resultMap>

    <sql id="selectStuVo">
        select id, name, tel, email, create_time, create_by, update_time, update_by from sys_stu
    </sql>

    <select id="selectStuList" parameterType="Stu" resultMap="StuResult">
        <include refid="selectStuVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="tel != null "> and tel like concat('%', #{tel}, '%')</if>
            <if test="email != null  and email != ''"> and email like concat('%', #{email}, '%')</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
        </where>
    </select>
    
    <select id="selectStuById" parameterType="Long" resultMap="StuResultsResult">
        select a.id, a.name, a.tel, a.email, a.create_time, a.create_by, a.update_time, a.update_by,
            b.id, b.s_id, b.java, b.images, b.file
        from sys_stu a
        left join sys_results b on b.s_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertStu" parameterType="Stu" useGeneratedKeys="true" keyProperty="id">
        insert into sys_stu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="tel != null">tel,</if>
            <if test="email != null and email != ''">email,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="tel != null">#{tel},</if>
            <if test="email != null and email != ''">#{email},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateStu" parameterType="Stu">
        update sys_stu
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="email != null and email != ''">email = #{email},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteStuById" parameterType="Long">
        delete from sys_stu where id = #{id}
    </delete>

    <delete id="deleteStuByIds" parameterType="String">
        delete from sys_stu where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteResultsBySIds" parameterType="String">
        delete from sys_results where s_id in 
        <foreach item="sId" collection="array" open="(" separator="," close=")">
            #{sId}
        </foreach>
    </delete>

    <delete id="deleteResultsBySId" parameterType="Long">
        delete from sys_results where s_id = #{sId}
    </delete>

    <insert id="batchResults">
        insert into sys_results(s_id,java,images,file) values
		<foreach item="item" index="index" collection="list" separator=",">
            (#{item.sId},#{item.java},#{item.images},#{item.file})
        </foreach>
    </insert>
</mapper>