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

    <resultMap type="ApprovalWorkflowDefinition" id="ApprovalWorkflowDefinitionResult">
        <id property="id" column="id"/>
        <result property="workflowCode" column="workflow_code"/>
        <result property="workflowName" column="workflow_name"/>
        <result property="workflowType" column="workflow_type"/>
        <result property="description" column="description"/>
        <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"/>
        <collection property="nodes" javaType="java.util.List" resultMap="NodeResult"/>
    </resultMap>

    <resultMap id="NodeResult" type="ApprovalNodeDefinition">
        <id property="id" column="node_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="node_status"/>
    </resultMap>

    <sql id="selectApprovalWorkflowDefinitionVo">
        select id,
               workflow_code,
               workflow_name,
               workflow_type,
               description,
               status,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from approval_workflow_definition
    </sql>

    <select id="selectApprovalWorkflowDefinitionList" parameterType="ApprovalWorkflowDefinition"
            resultMap="ApprovalWorkflowDefinitionResult">
        <include refid="selectApprovalWorkflowDefinitionVo"/>
        <where>
            <if test="workflowCode != null  and workflowCode != ''">and workflow_code like concat('%', #{workflowCode},
                '%')
            </if>
            <if test="workflowName != null  and workflowName != ''">and workflow_name like concat('%', #{workflowName},
                '%')
            </if>
            <if test="workflowType != null  and workflowType != ''">and workflow_type = #{workflowType}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectApprovalWorkflowDefinitionById" parameterType="Long" resultMap="ApprovalWorkflowDefinitionResult">
        <include refid="selectApprovalWorkflowDefinitionVo"/>
        where id = #{id}
    </select>

    <select id="selectApprovalWorkflowDefinitionByCode" parameterType="String"
            resultMap="ApprovalWorkflowDefinitionResult">
        <include refid="selectApprovalWorkflowDefinitionVo"/>
        where workflow_code = #{workflowCode} and status = '0'
    </select>

    <select id="selectApprovalWorkflowDefinitionByType" parameterType="String" resultType="ApprovalWorkflowDefinition">
        <include refid="selectApprovalWorkflowDefinitionVo"/>
        where workflow_type = #{workflowType} and status = '0'
        order by create_time desc
    </select>

    <insert id="insertApprovalWorkflowDefinition" parameterType="ApprovalWorkflowDefinition" useGeneratedKeys="true"
            keyProperty="id">
        insert into approval_workflow_definition
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workflowCode != null and workflowCode != ''">workflow_code,</if>
            <if test="workflowName != null and workflowName != ''">workflow_name,</if>
            <if test="workflowType != null and workflowType != ''">workflow_type,</if>
            <if test="description != null">description,</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="workflowCode != null and workflowCode != ''">#{workflowCode},</if>
            <if test="workflowName != null and workflowName != ''">#{workflowName},</if>
            <if test="workflowType != null and workflowType != ''">#{workflowType},</if>
            <if test="description != null">#{description},</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="updateApprovalWorkflowDefinition" parameterType="ApprovalWorkflowDefinition">
        update approval_workflow_definition
        <trim prefix="SET" suffixOverrides=",">
            <if test="workflowCode != null and workflowCode != ''">workflow_code = #{workflowCode},</if>
            <if test="workflowName != null and workflowName != ''">workflow_name = #{workflowName},</if>
            <if test="workflowType != null and workflowType != ''">workflow_type = #{workflowType},</if>
            <if test="description != null">description = #{description},</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="deleteApprovalWorkflowDefinitionById" parameterType="Long">
        delete
        from approval_workflow_definition
        where id = #{id}
    </delete>

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