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

    <resultMap type="ApprovalHistory" id="ApprovalHistoryResult">
        <result property="id" column="id"/>
        <result property="instanceId" column="instance_id"/>
        <result property="taskId" column="task_id"/>
        <result property="nodeId" column="node_id"/>
        <result property="nodeName" column="node_name"/>
        <result property="operatorId" column="operator_id"/>
        <result property="operatorName" column="operator_name"/>
        <result property="action" column="action"/>
        <result property="comment" column="comment"/>
        <result property="formData" column="form_data"/>
        <result property="operationTime" column="operation_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectApprovalHistoryVo">
        select id,
               instance_id,
               task_id,
               node_id,
               node_name,
               operator_id,
               operator_name, action, comment, form_data, operation_time, create_by, create_time, remark
        from approval_history
    </sql>

    <select id="selectApprovalHistoryList" parameterType="ApprovalHistory" resultMap="ApprovalHistoryResult">
        <include refid="selectApprovalHistoryVo"/>
        <where>
            <if test="instanceId != null ">and instance_id = #{instanceId}</if>
            <if test="taskId != null ">and task_id = #{taskId}</if>
            <if test="nodeId != null ">and node_id = #{nodeId}</if>
            <if test="nodeName != null  and nodeName != ''">and node_name like concat('%', #{nodeName}, '%')</if>
            <if test="operatorId != null ">and operator_id = #{operatorId}</if>
            <if test="operatorName != null  and operatorName != ''">and operator_name like concat('%', #{operatorName},
                '%')
            </if>
            <if test="action != null  and action != ''">and action = #{action}</if>
            <if test="comment != null  and comment != ''">and comment like concat('%', #{comment}, '%')</if>
            <if test="formData != null  and formData != ''">and form_data = #{formData}</if>
            <if test="operationTime != null ">and operation_time = #{operationTime}</if>
        </where>
        order by id asc
    </select>

    <select id="selectApprovalHistoryById" parameterType="Long" resultMap="ApprovalHistoryResult">
        <include refid="selectApprovalHistoryVo"/>
        where id = #{id}
    </select>

    <select id="selectApprovalHistoryByInstanceId" parameterType="Long" resultMap="ApprovalHistoryResult">
        <include refid="selectApprovalHistoryVo"/>
        where instance_id = #{instanceId}
        order by id asc
    </select>

    <select id="selectApprovalHistoryByTaskId" parameterType="Long" resultMap="ApprovalHistoryResult">
        <include refid="selectApprovalHistoryVo"/>
        where task_id = #{taskId}
        order by id asc
    </select>

    <select id="selectApprovalHistoryByOperator" resultMap="ApprovalHistoryResult">
        <include refid="selectApprovalHistoryVo"/>
        where operator_id = #{operatorId}
        <if test="action != null and action != ''">
            and action = #{action}
        </if>
        order by id asc
    </select>

    <insert id="insertApprovalHistory" parameterType="ApprovalHistory" useGeneratedKeys="true" keyProperty="id">
        insert into approval_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="instanceId != null">instance_id,</if>
            <if test="taskId != null">task_id,</if>
            <if test="nodeId != null">node_id,</if>
            <if test="nodeName != null and nodeName != ''">node_name,</if>
            <if test="operatorId != null">operator_id,</if>
            <if test="operatorName != null and operatorName != ''">operator_name,</if>
            <if test="action != null and action != ''">action,</if>
            <if test="comment != null">comment,</if>
            <if test="formData != null">form_data,</if>
            <if test="operationTime != null">operation_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="instanceId != null">#{instanceId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="nodeId != null">#{nodeId},</if>
            <if test="nodeName != null and nodeName != ''">#{nodeName},</if>
            <if test="operatorId != null">#{operatorId},</if>
            <if test="operatorName != null and operatorName != ''">#{operatorName},</if>
            <if test="action != null and action != ''">#{action},</if>
            <if test="comment != null">#{comment},</if>
            <if test="formData != null">#{formData},</if>
            <if test="operationTime != null">#{operationTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateApprovalHistory" parameterType="ApprovalHistory">
        update approval_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="instanceId != null">instance_id = #{instanceId},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="nodeId != null">node_id = #{nodeId},</if>
            <if test="nodeName != null and nodeName != ''">node_name = #{nodeName},</if>
            <if test="operatorId != null">operator_id = #{operatorId},</if>
            <if test="operatorName != null and operatorName != ''">operator_name = #{operatorName},</if>
            <if test="action != null and action != ''">action = #{action},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="formData != null">form_data = #{formData},</if>
            <if test="operationTime != null">operation_time = #{operationTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteApprovalHistoryById" parameterType="Long">
        delete
        from approval_history
        where id = #{id}
    </delete>

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

    <delete id="deleteApprovalHistoryByInstanceId" parameterType="Long">
        delete
        from approval_history
        where instance_id = #{instanceId}
    </delete>

</mapper>