<?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.UserInstanceMapper">
    <sql id="userInstanceSQL">
        select instance_id,
               process_instance_id,
               user_id,
               deployment_name,
               state,
               initiator,
               submit_time,
               finish_time,
               create_time,
               create_by,
               update_time,
               update_by
        from wk_re_user_instance
    </sql>

    <select id="selectByInstanceId" resultType="cn.fateverse.workflow.entity.UserInstance">
        <include refid="userInstanceSQL"/>
        where instance_id = #{instanceId}
    </select>

    <select id="selectByProcessInstanceId" resultType="cn.fateverse.workflow.entity.UserInstance">
        <include refid="userInstanceSQL"/>
        where process_instance_id = #{processInstanceId}
    </select>

    <select id="selectByUserId" resultType="cn.fateverse.workflow.entity.UserInstance">
        <include refid="userInstanceSQL"/>
        <where>
            <if test="userId != null and userId != ''">and user_id = #{userId}</if>
            <if test="state != null and state != ''">and state = #{state}</if>
            <if test="deploymentName != null and deploymentName != ''">and deployment_name like
                concat('%',#{deploymentName},'%')
            </if>
        </where>
    </select>
    <select id="selectProcessInstanceListByUserIdAndState"
            resultType="cn.fateverse.workflow.entity.UserInstance">
        select distinct process_instance_id,
        user_id,
        deployment_name,
        submit_time,
        finish_time,
        state,
        initiator,
        instance_id
        from wk_re_user_instance wrui
        left join act_hi_taskinst aht on wrui.process_instance_id = aht.PROC_INST_ID_
        <where>
            <if test="userId != null">and wrui.user_id = #{userId} or aht.ASSIGNEE_ = #{userId}</if>
            <if test="state != null">and wrui.state = #{state}</if>
            <if test="deploymentName != null and deploymentName != ''">and deployment_name like
                concat('%',#{deploymentName},'%')
            </if>
        </where>
        order by wrui.submit_time desc
    </select>
<!--    group by process_instance_id-->

    <insert id="insert">
        insert into wk_re_user_instance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="instanceId != null and instanceId != ''">instance_id,</if>
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="processInstanceId != null and processInstanceId != ''">process_instance_id,</if>
            <if test="deploymentName != null and deploymentName != ''">deployment_name,</if>
            <if test="state != null and state != ''">state,</if>
            <if test="initiator != null and initiator != ''">initiator,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="submitTime != null">submit_time,</if>
            <if test="createTime != null ">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="instanceId != null and instanceId != ''">#{instanceId},</if>
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="processInstanceId != null and processInstanceId != ''">#{processInstanceId},</if>
            <if test="deploymentName != null and deploymentName != ''">#{deploymentName},</if>
            <if test="state != null and state != ''">#{state},</if>
            <if test="initiator != null and initiator != ''">#{initiator},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="submitTime != null ">#{submitTime},</if>
            <if test="createTime != null ">#{createTime},</if>
        </trim>
    </insert>


    <update id="update">
        update wk_re_user_instance
        <set>
            <if test="processInstanceId != null and processInstanceId != ''">process_instance_id =
                #{processInstanceId},
            </if>
            <if test="state != null and state != ''">state = #{state},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            update_time = #{updateTime}
        </set>
        where instance_id = #{instanceId};
    </update>

    <update id="updateState">
        update wk_re_user_instance
        set state = #{state}
        where process_instance_id = #{processInstanceId}
    </update>

    <update id="updateStateByProcessInstanceIdList">
        update wk_re_user_instance
        set state = #{state}
        where process_instance_id in
        <foreach collection="list" open="(" separator="," close=")" item="processInstanceId">
            #{processInstanceId}
        </foreach>
    </update>

</mapper>