<?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.ApprovalLineMapper">

    <resultMap id="VOMap"   type="com.ga.core.vo.approval.ApprovalLineVO"/>
    <resultMap id="RespMap" type="com.ga.core.vo.approval.ApprovalLineResp">
        <id  property="lineId"     column="line_id"/>
        <result property="lineCode"   column="line_code"/>
        <result property="lineName"   column="line_name"/>
        <result property="maxStep"    column="max_step"/>
        <result property="targetType" column="target_type"/>
        <result property="isActive"   column="is_active"/>
        <collection property="steps"
                    ofType="com.ga.core.vo.approval.ApprovalLineStepVO"
                    select="com.ga.core.mapper.approval.ApprovalLineStepMapper.selectByLine"
                    column="line_id"/>
    </resultMap>

    <select id="selectById" resultMap="RespMap">
        SELECT line_id, line_code, line_name, max_step, target_type, is_active
          FROM approval_line
         WHERE line_id = #{lineId}
    </select>

    <select id="selectByCode" resultMap="VOMap">
        SELECT line_id, line_code, line_name, max_step, target_type, is_active,
               created_at, created_by, updated_at
          FROM approval_line
         WHERE line_code = #{lineCode}
    </select>

    <select id="selectList" resultMap="RespMap">
        SELECT line_id, line_code, line_name, max_step, target_type, is_active
          FROM approval_line
        <where>
            <if test="targetType != null and targetType != ''">AND target_type = #{targetType}</if>
            <if test="isActive != null">AND is_active = #{isActive}</if>
        </where>
        ORDER BY target_type, line_code
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="lineId">
        INSERT INTO approval_line (line_code, line_name, max_step, target_type, is_active, created_by)
        VALUES (#{lineCode}, #{lineName}, #{maxStep}, #{targetType}, TRUE, #{createdBy})
    </insert>

    <update id="update">
        UPDATE approval_line
           SET line_name   = #{lineName},
               max_step    = #{maxStep},
               updated_at  = NOW()
         WHERE line_id = #{lineId}
    </update>

    <update id="updateActive">
        UPDATE approval_line SET is_active = #{isActive}, updated_at = NOW() WHERE line_id = #{lineId}
    </update>

</mapper>
