<?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="cn.fateverse.workflow.mapper.HistoricalOperationMapper">
    <sql id="historicalOperationSql">
        select task_id,
               process_instance_id,
               operation_name,
               operation,
               approval_mode,
               message,
               finish_time,
               node_id,
               node_type,
               state,
               start_time,
               user_info,
               mark
        from wk_hi_operation
    </sql>

    <select id="selectListByProcessInstanceId"
            resultType="cn.fateverse.workflow.entity.HistoricalOperation">
        <include refid="historicalOperationSql"/>
        <where>
            <if test="processInstanceId != null and processInstanceId !=''">and process_instance_id =
                #{processInstanceId}
            </if>
            <if test="mark != null">and mark = #{mark}</if>
        </where>
        order by start_time asc
    </select>

    <select id="selectByTaskId"
            resultType="cn.fateverse.workflow.entity.HistoricalOperation">
        <include refid="historicalOperationSql"/>
        where task_id = #{taskId}
    </select>

    <select id="selectByProcessInstanceIdAndNodeId"
            resultType="cn.fateverse.workflow.entity.HistoricalOperation">
        <include refid="historicalOperationSql"/>
        where process_instance_id = #{processInstanceId} and node_id = #{nodeId} and mark = 0
    </select>

    <select id="selectCountByProcessInstanceIdAndNodeId" resultType="java.lang.Integer">
        select count(1)
        from wk_hi_operation
        where process_instance_id = #{processInstanceId}
          and node_id = #{nodeId}
          and mark = 0
    </select>

    <update id="updateStatusByProcessInstanceIdAndNodeId">
        update wk_hi_operation
        set state       = #{state},
            finish_time = sysdate()
        where process_instance_id = #{processInstanceId}
          and node_id = #{nodeId}
          and mark = 0
    </update>

    <select id="selectByProcessInstanceIdAndNotNodeId"
            resultType="cn.fateverse.workflow.entity.HistoricalOperation">
        <include refid="historicalOperationSql"/>
        where process_instance_id = #{processInstanceId} and state = #{state}
    </select>

    <select id="selectRunningByProcessInstanceId"
            resultType="cn.fateverse.workflow.entity.HistoricalOperation">
        <include refid="historicalOperationSql"/>
        where state = 'RUNNING' and mark = 0
        and process_instance_id in
        <foreach collection="list" item="processInstanceId" open="(" separator="," close=")">
            #{processInstanceId}
        </foreach>
    </select>

    <insert id="insert">
        insert into wk_hi_operation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null and taskId != ''">task_id,</if>
            <if test="nodeId != null and nodeId != ''">node_id,</if>
            <if test="processInstanceId != null and processInstanceId != ''">process_instance_id,</if>
            <if test="operationName != null and operationName != ''">operation_name,</if>
            <if test="operation != null">operation,</if>
            <if test="approvalMode != null and approvalMode != ''">approval_mode,</if>
            <if test="message != null and message != ''">message,</if>
            <if test="nodeType != null and nodeType != ''">node_type,</if>
            <if test="state != null">state,</if>
            <if test="startTime != null ">start_time,</if>
            <if test="finishTime != null ">finish_time,</if>
            <if test="userInfo != null and userInfo != ''">user_info,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null and taskId != ''">#{taskId},</if>
            <if test="nodeId != null and nodeId != ''">#{nodeId},</if>
            <if test="processInstanceId != null and processInstanceId != ''">#{processInstanceId},</if>
            <if test="operationName != null and operationName != ''">#{operationName},</if>
            <if test="operation != null">#{operation},</if>
            <if test="approvalMode != null and approvalMode != ''">#{approvalMode},</if>
            <if test="message != null and message != ''">#{message},</if>
            <if test="nodeType != null and nodeType != ''">#{nodeType},</if>
            <if test="state != null">#{state},</if>
            <if test="startTime != null ">#{startTime},</if>
            <if test="finishTime != null ">#{finishTime},</if>
            <if test="userInfo != null and userInfo != ''">#{userInfo},</if>
        </trim>
    </insert>

    <update id="update">
        update wk_hi_operation
        <set>
            <if test="finishTime != null ">finish_time = #{finishTime},</if>
            <if test="state != null">state = #{state},</if>
            <if test="userInfo != null and userInfo != ''">user_info = #{userInfo},</if>
            <if test="message != null and message != ''">message = #{message},</if>
        </set>
        where process_instance_id = #{processInstanceId} and node_id = #{nodeId}
    </update>
    <update id="batchUpdate">
        update wk_hi_operation
        <set>
            <trim prefix="finish_time = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.finishTime != null">
                        when task_id = #{item.taskId} then #{item.finishTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="state = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.state != null">
                        when task_id = #{item.taskId} then #{item.state}
                    </if>
                </foreach>
            </trim>
            <trim prefix="mark = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.mark != null and item.mark != ''">
                        when task_id = #{item.taskId} then #{item.mark}
                    </if>
                </foreach>
            </trim>
            <trim prefix="user_info = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.userInfo != null and item.userInfo != ''">
                    </if>
                    when task_id = #{item.taskId} then #{item.userInfo}
                </foreach>
            </trim>
            <trim prefix="message = case" suffix="end,">
                <foreach collection="list" separator=" " item="item">
                    <if test="item.message != null and item.message != ''">
                        when task_id = #{item.taskId} then #{item.message}
                    </if>
                </foreach>
            </trim>
        </set>
        where task_id in
        <foreach collection="list" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.taskId}
        </foreach>
    </update>


</mapper>