<?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.artfess.bpm.persistence.dao.ActExecutionDao">
	<resultMap id="ActExecution" type="com.artfess.bpm.persistence.model.ActExecution">
		<id property="id" column="ID_" jdbcType="VARCHAR"/>
		<result property="rev" column="REV_" jdbcType="NUMERIC"/>
		<result property="procInstId" column="PROC_INST_ID_" jdbcType="VARCHAR"/>
		<result property="businessKey" column="BUSINESS_KEY_" jdbcType="VARCHAR"/>
		<result property="parentId" column="PARENT_ID_" jdbcType="VARCHAR"/>
		<result property="procDefId" column="PROC_DEF_ID_" jdbcType="VARCHAR"/>
		<result property="superExec" column="SUPER_EXEC_" jdbcType="VARCHAR"/>
		<result property="actId" column="ACT_ID_" jdbcType="VARCHAR"/>
		<result property="isActive" column="IS_ACTIVE_" jdbcType="BOOLEAN"/>
		<result property="isConcurrent" column="IS_CONCURRENT_" jdbcType="BOOLEAN"/>
		<result property="isScope" column="IS_SCOPE_" jdbcType="BOOLEAN"/>
		<result property="isEventScope" column="IS_EVENT_SCOPE_" jdbcType="BOOLEAN"/>
		<result property="suspensionState" column="SUSPENSION_STATE_" jdbcType="NUMERIC"/>
		<result property="cachedEntState" column="CACHED_ENT_STATE_" jdbcType="NUMERIC"/>
	</resultMap>
	
	<select id="getByParentsId"   parameterType="java.lang.String" resultType="java.lang.String">
		SELECT ID_ FROM act_ru_execution  WHERE  PARENT_ID_=#{parentId}
	</select>
	
	<select id="getBySupperId"   parameterType="java.lang.String" resultType="java.lang.String">
		SELECT ID_ FROM act_ru_execution  WHERE  SUPER_EXEC_=#{supperId}
	</select>
	
	<delete id="delTaskByByInstList">
		delete FROM act_ru_task where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach>
	</delete>
	
	
	<delete id="delCandidateByInstList">
		delete FROM act_ru_identitylink where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	<delete id="delEventSubByInstList">
		delete FROM act_ru_event_subscr where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	
	<delete id="delVarsByInstList">
		delete FROM act_ru_variable where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	<delete id="delHiVarByInstList">
		delete FROM act_hi_varinst where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	<delete id="delHiTaskByInstList">
		delete FROM act_hi_taskinst where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	<delete id="delHiProcinstByInstList">
		delete FROM act_hi_procinst where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	<delete id="delHiCandidateByInstList">
		delete FROM act_hi_identitylink where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	
	<delete id="delHiActInstByInstList">
		delete FROM act_hi_actinst where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	
	
	<delete id="delExectionByInstList">
		delete FROM act_ru_execution where PROC_INST_ID_
		in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
            #{instId} 
        	</foreach> 
	</delete>
	
	
	<!-- 撤回发起人 需要清除多实例任务相关的数据 如 会签, 同步的act_ru_variable ,act_ru_execution 数据 -->
	<delete id="delActiveByInstList"  >
	  delete FROM act_ru_execution where PROC_INST_ID_
	  in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
	            #{instId}
	         </foreach> 
	       AND ID_ != PARENT_ID_
	</delete>
	
	<delete id="delActiveVarsByInstList">
		delete FROM act_ru_variable where execution_id_ in (
			select id_ from act_ru_execution where PROC_INST_ID_
		  in <foreach collection="list" index="index" item="instId" open="(" separator="," close=")"> 
		            #{instId} 
		         </foreach> 
		         AND ID_ != PARENT_ID_
		)
	</delete>
	
	<update id="updateRejectExecution" parameterType="com.artfess.bpm.persistence.model.ActExecution">
		UPDATE act_ru_execution SET IS_SCOPE_ = 0,IS_ACTIVE_ = 0,ACT_ID_=#{actId},CACHED_ENT_STATE_=1  WHERE PARENT_ID_=#{parentId} AND ID_ != #{execId}
	</update>
	
	<update id="updateRejectExecution" databaseId="pg" parameterType="com.artfess.bpm.persistence.model.ActExecution">
		UPDATE act_ru_execution SET IS_SCOPE_ = false,IS_ACTIVE_ = false,ACT_ID_=#{actId},CACHED_ENT_STATE_=1  WHERE PARENT_ID_=#{parentId} AND ID_ != #{execId}
	</update>
	
	<delete id="removeByParentIds">
	  delete FROM act_ru_execution where PARENT_ID_
	  in <foreach collection="parentIds" index="index" item="parentId" open="(" separator="," close=")"> 
	            #{parentId}
	         </foreach> 
	       AND ID_ != PARENT_ID_
	</delete>
	
</mapper>