<?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.ga.core.mapper.org.GradeMapper">

    <resultMap id="GradeMap" type="com.ga.core.vo.org.GradeVO"/>

    <sql id="cols">grade_id, grade_name, grade_level, grade_group, description, is_active, sort_order</sql>

    <select id="selectAll" resultMap="GradeMap">
        SELECT <include refid="cols"/> FROM grade ORDER BY grade_level, sort_order
    </select>

    <select id="selectActive" resultMap="GradeMap">
        SELECT <include refid="cols"/> FROM grade WHERE is_active = 'Y' ORDER BY grade_level, sort_order
    </select>

    <select id="selectById" resultMap="GradeMap">
        SELECT <include refid="cols"/> FROM grade WHERE grade_id = #{gradeId}
    </select>

    <insert id="insert" parameterType="com.ga.core.vo.org.GradeVO" useGeneratedKeys="true" keyProperty="gradeId">
        INSERT INTO grade (grade_name, grade_level, grade_group, description, is_active, sort_order)
        VALUES (#{gradeName}, #{gradeLevel}, #{gradeGroup}, #{description},
                COALESCE(#{isActive},'Y'), COALESCE(#{sortOrder},0))
    </insert>

    <update id="update" parameterType="com.ga.core.vo.org.GradeVO">
        UPDATE grade SET
            grade_name = #{gradeName}, grade_level = #{gradeLevel},
            grade_group = #{gradeGroup}, description = #{description},
            is_active = #{isActive}, sort_order = #{sortOrder}
        WHERE grade_id = #{gradeId}
    </update>

    <delete id="deleteById">DELETE FROM grade WHERE grade_id = #{gradeId}</delete>

</mapper>
