<?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.approval.ApprovalLineStepMapper">

    <resultMap id="VOMap"   type="com.ga.core.vo.approval.ApprovalLineStepVO"/>
    <resultMap id="RespMap" type="com.ga.core.vo.approval.ApprovalLineStepResp"/>

    <select id="selectByLineAndStep" resultMap="VOMap">
        SELECT s.step_id, s.line_id, s.step_no, s.role_code, s.created_at
          FROM approval_line_step s
         WHERE s.line_id = #{lineId}
           AND s.step_no = #{stepNo}
    </select>

    <select id="selectByLine" resultMap="VOMap">
        SELECT s.step_id, s.line_id, s.step_no, s.role_code, s.created_at
          FROM approval_line_step s
         WHERE s.line_id = #{lineId}
         ORDER BY s.step_no
    </select>

    <select id="selectList" resultMap="RespMap">
        SELECT s.step_id, s.line_id, l.line_code, s.step_no, s.role_code
          FROM approval_line_step s
          JOIN approval_line l ON l.line_id = s.line_id
        <where>
            <if test="lineId != null">AND s.line_id = #{lineId}</if>
        </where>
        ORDER BY s.line_id, s.step_no
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="stepId">
        INSERT INTO approval_line_step (line_id, step_no, role_code)
        VALUES (#{lineId}, #{stepNo}, #{roleCode})
        ON CONFLICT (line_id, step_no) DO UPDATE SET role_code = EXCLUDED.role_code
    </insert>

    <delete id="delete">
        DELETE FROM approval_line_step WHERE step_id = #{stepId}
    </delete>

</mapper>
