<?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.pangu.base.mapper.GradeMapper">

    <resultMap id="GradeResult" type="com.pangu.base.domain.Grade">
        <id property="gradeId" column="grade_id"/>
        <result property="gradeCode" column="grade_code"/>
        <result property="gradeName" column="grade_name"/>
        <result property="orderNum" column="order_num"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectGradeVo">
        SELECT grade_id, grade_code, grade_name, order_num, status,
               create_by, create_time, update_by, update_time, remark
        FROM pg_grade
        WHERE del_flag = '0'
    </sql>

    <select id="selectGradeList" parameterType="com.pangu.base.domain.Grade" resultMap="GradeResult">
        <include refid="selectGradeVo"/>
        <if test="gradeName != null and gradeName != ''">
            AND grade_name LIKE CONCAT('%', #{gradeName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ORDER BY order_num
    </select>

    <select id="selectGradeById" parameterType="Long" resultMap="GradeResult">
        <include refid="selectGradeVo"/>
        AND grade_id = #{gradeId}
    </select>

    <select id="checkGradeNameUnique" parameterType="String" resultMap="GradeResult">
        <include refid="selectGradeVo"/>
        AND grade_name = #{gradeName}
        LIMIT 1
    </select>

    <insert id="insertGrade" parameterType="com.pangu.base.domain.Grade" useGeneratedKeys="true" keyProperty="gradeId">
        INSERT INTO pg_grade (
            grade_code, grade_name, order_num, status,
            create_by, create_time, del_flag, remark
        ) VALUES (
            #{gradeCode}, #{gradeName}, #{orderNum}, #{status},
            #{createBy}, #{createTime}, '0', #{remark}
        )
    </insert>

    <update id="updateGrade" parameterType="com.pangu.base.domain.Grade">
        UPDATE pg_grade
        <set>
            <if test="gradeName != null and gradeName != ''">grade_name = #{gradeName},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            update_by = #{updateBy},
            update_time = #{updateTime}
        </set>
        WHERE grade_id = #{gradeId}
    </update>

    <update id="deleteGradeById" parameterType="Long">
        UPDATE pg_grade SET del_flag = '1' WHERE grade_id = #{gradeId}
    </update>

</mapper>
