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

    <select id="selectProcessInstanceMapByList"
            resultType="org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl">
        select ID_ as id, NAME_ as name, START_TIME_ as startTime from act_ru_execution
        where ID_ in
        <foreach collection="list" open="(" separator="," close=")" item="processInstanceId">
            #{processInstanceId}
        </foreach>
    </select>

    <select id="selectProcessInstanceListByAssigneeAndStates"
            resultType="cn.fateverse.workflow.entity.vo.ProcessInstanceVo">
        select ahp.ID_ as processInstanceId,
        ahp.NAME_ as deploymentName,
        ahp.START_TIME_ as submitTime,
        ahp.END_TIME_ as endTime,
        ahv.TEXT_ as state
        from act_hi_procinst ahp
        left join act_hi_taskinst aht on ahp.ID_ = aht.PROC_INST_ID_
        left join act_hi_varinst ahv on ahv.PROC_INST_ID_ = ahp.ID_
        <where>
            ahv.NAME_ = 'processStatus'
            <if test="userId != null">and aht.ASSIGNEE_ = #{userId}</if>
            <if test="state != null">and ahv.TEXT_ = #{state}</if>
        </where>
        group by ahp.ID_
        order by ahp.START_TIME_ desc
    </select>

    <insert id="insertUserTaskInHistorical">
        insert into act_hi_taskinst
        (ID_, PROC_DEF_ID_, TASK_DEF_KEY_, PROC_INST_ID_, EXECUTION_ID_, NAME_, ASSIGNEE_, START_TIME_, END_TIME_,
         PRIORITY_, LAST_UPDATED_TIME_)
        values (#{id}, #{processDefinitionId}, #{taskDefinitionKey}, #{processInstanceId}, #{executionId}, #{name},
                #{assignee}, sysdate(), sysdate(), 50, sysdate())
    </insert>

    <update id="batchInsertUserTaskInHistorical">
        insert into act_hi_taskinst
        (ID_, PROC_DEF_ID_, TASK_DEF_KEY_, PROC_INST_ID_, EXECUTION_ID_,NAME_, ASSIGNEE_, START_TIME_, END_TIME_,
        PRIORITY_, LAST_UPDATED_TIME_)
        values
        <foreach collection="list" separator="," item="task">
            (#{task.id},#{task.processDefinitionId},#{task.taskDefinitionKey},#{task.processInstanceId},#{task.executionId},
            #{task.name},#{task.assignee},sysdate(),sysdate(),50,sysdate())
        </foreach>
    </update>

</mapper>