<?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.srs.comprehensive.mapper.SrsGradeMapper">
    
    <resultMap type="SrsGrade" id="SrsGradeResult">
        <result property="gradeId"    column="grade_id"    />
        <result property="gradeName"    column="grade_name"    />
        <result property="gradeStatus"    column="grade_status"    />
        <result property="gradeCode"    column="grade_code"    />
        <result property="status"    column="status"    />
        <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="selectSrsGradeVo">
        select grade_id,grade_status, grade_name, grade_code, status, del_flag, create_by, create_time, update_by, update_time from srs_grade
    </sql>

    <select id="selectSrsGradeList" parameterType="SrsGrade" resultMap="SrsGradeResult">
        <include refid="selectSrsGradeVo"/>
        <where>  
            <if test="gradeName != null  and gradeName != ''"> and grade_name like concat('%', #{gradeName}, '%')</if>
            <if test="gradeCode != null  and gradeCode != ''"> and grade_code = #{gradeCode}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectSrsGradeByGradeId" parameterType="Long" resultMap="SrsGradeResult">
        <include refid="selectSrsGradeVo"/>
        where grade_id = #{gradeId}
    </select>
    <select id="selectSrsGradeByGradeCode" parameterType="String" resultMap="SrsGradeResult">
        <include refid="selectSrsGradeVo"/>
        where grade_code = #{gradeCode}
    </select>
    <select id="selectSrsGradeByGradeCodes" resultType="com.srs.comprehensive.domain.SrsGrade">
        select *
        from srs_grade
        where srs_grade.grade_code in
        <foreach collection="gradeCodes" item="code" open="(" separator="," close=")">
            #{code}
        </foreach>
    </select>
    <insert id="insertSrsGrade" parameterType="SrsGrade" useGeneratedKeys="true" keyProperty="gradeId">
        insert into srs_grade
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gradeName != null">grade_name,</if>
            <if test="gradeCode != null">grade_code,</if>
            <if test="gradeStatus != null">grade_status,</if>
            <if test="status != null">status,</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="gradeName != null">#{gradeName},</if>
            <if test="gradeCode != null">#{gradeCode},</if>
            <if test="gradeStatus != null">#{gradeStatus},</if>
            <if test="status != null">#{status},</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="updateSrsGrade" parameterType="SrsGrade">
        update srs_grade
        <trim prefix="SET" suffixOverrides=",">
            <if test="gradeName != null">grade_name = #{gradeName},</if>
            <if test="gradeCode != null">grade_code = #{gradeCode},</if>
            <if test="gradeStatus != null">grade_status = #{gradeStatus},</if>
            <if test="status != null">status = #{status},</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 grade_id = #{gradeId}
    </update>
<!--    批量更新-->
    <update id="updateSrsClassList" parameterType="java.util.List">
        update srs_grade
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="grade_name=case" suffix="end,">
                <foreach collection="studentList" item="i" index="index">
                    <if test="i.gradeName != null ">
                        when grade_code=#{i.gradeCode} then #{i.gradeName}
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time=case" suffix="end,">
                <foreach collection="studentList" item="i" index="index">
                    <if test="i.updateTime != null ">
                        when grade_code=#{i.gradeCode} then #{i.updateTime}
                    </if>
                </foreach>
            </trim>
        </trim>
        where grade_code in
        <foreach collection="studentList" separator="," item="i" index="index" open="(" close=")">
            #{i.gradeCode}
        </foreach>
    </update>

    <delete id="deleteSrsGradeByGradeId" parameterType="Long">
        delete from srs_grade where grade_id = #{gradeId}
    </delete>

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