<?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.training.TrainingRequirementMapper">

    <resultMap id="VOMap"   type="com.ga.core.vo.training.TrainingRequirementVO"/>
    <resultMap id="RespMap" type="com.ga.core.vo.training.TrainingRequirementResp"/>

    <select id="selectByYear" resultMap="RespMap">
        SELECT req_id, target_year, required_hours, description
          FROM training_requirement
         WHERE target_year = #{targetYear}
    </select>

    <select id="selectList" resultMap="RespMap">
        SELECT req_id, target_year, required_hours, description
          FROM training_requirement
        <where>
            <if test="targetYear != null">AND target_year = #{targetYear}</if>
        </where>
        ORDER BY target_year DESC
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="reqId">
        INSERT INTO training_requirement (target_year, required_hours, description, created_by)
        VALUES (#{targetYear}, #{requiredHours}, #{description}, #{createdBy})
        ON CONFLICT (target_year) DO NOTHING
    </insert>

    <update id="update">
        UPDATE training_requirement
           SET required_hours = #{requiredHours},
               description   = #{description},
               updated_at    = NOW()
         WHERE target_year = #{targetYear}
    </update>

</mapper>
