<?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.sundot.airport.system.mapper.approval.ApprovalNodeDefinitionMapper">

    <resultMap type="ApprovalNodeDefinition" id="ApprovalNodeDefinitionResult">
        <id property="id" column="id"/>
        <result property="workflowId" column="workflow_id"/>
        <result property="nodeCode" column="node_code"/>
        <result property="nodeName" column="node_name"/>
        <result property="nodeType" column="node_type"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="approverType" column="approver_type"/>
        <result property="approverValue" column="approver_value"/>
        <result property="canReject" column="can_reject"/>
        <result property="isRequired" column="is_required"/>
        <result property="timeoutHours" column="timeout_hours"/>
        <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="selectApprovalNodeDefinitionVo">
        select id,
               workflow_id,
               node_code,
               node_name,
               node_type,
               sort_order,
               approver_type,
               approver_value,
               can_reject,
               is_required,
               timeout_hours,
               status,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from approval_node_definition
    </sql>

    <select id="selectApprovalNodeDefinitionList" parameterType="ApprovalNodeDefinition"
            resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        <where>
            <if test="workflowId != null ">and workflow_id = #{workflowId}</if>
            <if test="nodeCode != null  and nodeCode != ''">and node_code like concat('%', #{nodeCode}, '%')</if>
            <if test="nodeName != null  and nodeName != ''">and node_name like concat('%', #{nodeName}, '%')</if>
            <if test="nodeType != null  and nodeType != ''">and node_type = #{nodeType}</if>
            <if test="approverType != null  and approverType != ''">and approver_type = #{approverType}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
        order by workflow_id, sort_order
    </select>

    <select id="selectApprovalNodeDefinitionById" parameterType="Long" resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        where id = #{id}
    </select>

    <select id="selectApprovalNodeDefinitionByWorkflowId" parameterType="Long" resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        where workflow_id = #{workflowId} and status = '0'
        order by sort_order
    </select>

    <select id="selectApprovalNodeDefinitionByWorkflowAndCode" resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        where workflow_id = #{workflowId} and node_code = #{nodeCode} and status = '0'
    </select>

    <select id="selectFirstNodeByWorkflowId" parameterType="Long" resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        where workflow_id = #{workflowId} and status = '0'
        order by sort_order limit 1
    </select>

    <select id="selectNextNodeByOrder" resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        where workflow_id = #{workflowId} and sort_order > #{currentOrder} and status = '0'
        order by sort_order limit 1
    </select>

    <select id="selectPreviousNodeByOrder" resultMap="ApprovalNodeDefinitionResult">
        <include refid="selectApprovalNodeDefinitionVo"/>
        where workflow_id = #{workflowId} and sort_order &lt; #{currentOrder} and status = '0'
        order by sort_order desc limit 1
    </select>

    <insert id="insertApprovalNodeDefinition" parameterType="ApprovalNodeDefinition" useGeneratedKeys="true"
            keyProperty="id">
        insert into approval_node_definition
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workflowId != null">workflow_id,</if>
            <if test="nodeCode != null and nodeCode != ''">node_code,</if>
            <if test="nodeName != null and nodeName != ''">node_name,</if>
            <if test="nodeType != null and nodeType != ''">node_type,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="approverType != null">approver_type,</if>
            <if test="approverValue != null">approver_value,</if>
            <if test="canReject != null">can_reject,</if>
            <if test="isRequired != null">is_required,</if>
            <if test="timeoutHours != null">timeout_hours,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workflowId != null">#{workflowId},</if>
            <if test="nodeCode != null and nodeCode != ''">#{nodeCode},</if>
            <if test="nodeName != null and nodeName != ''">#{nodeName},</if>
            <if test="nodeType != null and nodeType != ''">#{nodeType},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="approverType != null">#{approverType},</if>
            <if test="approverValue != null">#{approverValue},</if>
            <if test="canReject != null">#{canReject},</if>
            <if test="isRequired != null">#{isRequired},</if>
            <if test="timeoutHours != null">#{timeoutHours},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateApprovalNodeDefinition" parameterType="ApprovalNodeDefinition">
        update approval_node_definition
        <trim prefix="SET" suffixOverrides=",">
            <if test="workflowId != null">workflow_id = #{workflowId},</if>
            <if test="nodeCode != null and nodeCode != ''">node_code = #{nodeCode},</if>
            <if test="nodeName != null and nodeName != ''">node_name = #{nodeName},</if>
            <if test="nodeType != null and nodeType != ''">node_type = #{nodeType},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="approverType != null">approver_type = #{approverType},</if>
            <if test="approverValue != null">approver_value = #{approverValue},</if>
            <if test="canReject != null">can_reject = #{canReject},</if>
            <if test="isRequired != null">is_required = #{isRequired},</if>
            <if test="timeoutHours != null">timeout_hours = #{timeoutHours},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteApprovalNodeDefinitionById" parameterType="Long">
        delete
        from approval_node_definition
        where id = #{id}
    </delete>

    <delete id="deleteApprovalNodeDefinitionByIds" parameterType="String">
        delete from approval_node_definition where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteApprovalNodeDefinitionByWorkflowId" parameterType="Long">
        delete
        from approval_node_definition
        where workflow_id = #{workflowId}
    </delete>
</mapper>