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



<!-- 不使用namespace的话sql搜索定位会比较方便 -->
<mapper namespace="ProcessInfoConfig">

	<resultMap id="RM_ProcessInfoConfig" type="com.blueland.bpm.engine.config.model.ProcessInfoConfig">
        <result property="processDefId" column="PROCESS_DEF_ID"/>
        <result property="processDefName" column="PROCESS_DEF_NAME"/>
        <result property="processTypeId" column="PROCESS_TYPE_ID"/>
        <result property="processTypeName" column="PROCESS_TYPE_NAME"/>
        <result property="processPageUrl" column="PROCESS_PAGE_URL"/>
        <result property="processPageInfo" column="PROCESS_PAGE_INFO"/>
        <result property="processStartAuth" column="PROCESS_START_AUTH"/>
        <result property="processStartIsshow" column="PROCESS_START_ISSHOW"/>
        <result property="processStartOrder" column="PROCESS_START_ORDER"/>
        <result property="deploymentId" column="DEPLOYMENT_ID"/>
        <result property="pageIshowSelectUserTree" column="PAGE_ISSHOW_SELECT_USERTREE"/>
        <result property="processHandlerClassPath" column="PROCESS_HANDLER_CLASS_PATH"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="lastUpdateTime" column="LAST_UPDATE_TIME"/>
        <result property="remark" column="REMARK"/>
        <result property="tenantId" column="TENANT_ID"/>
        <result property="processMgrState" column="PROCESS_MGR_STATE"/>
        <result property="processModelSyncState" column="PROCESS_MODEL_SYNC_STATE"/>
        <result property="processMgrIsshow" column="PROCESS_MGR_ISSHOW"/>
        <result property="otherSysDealStatus" column="OTHER_SYS_DEAL_STATUS"/>
        <result property="arisCode" column="ARIS_CODE"/>
         <result property="cprotocol" column="C_PROTOCL"/>
        <result property="murl" column="M_URL"/>
        <result property="mprotocol" column="M_PROTOCL"/>
	</resultMap>
	
	<!-- 用于select查询公用抽取的列 -->
	<sql id="ProcessInfoConfig.columns">
	    <![CDATA[
		PROCESS_MODEL_SYNC_STATE,PROCESS_MGR_STATE,DEPLOYMENT_ID,PROCESS_DEF_ID,PROCESS_HANDLER_CLASS_PATH,PAGE_ISSHOW_SELECT_USERTREE,PROCESS_DEF_NAME,PROCESS_TYPE_ID,PROCESS_PAGE_URL,PROCESS_PAGE_INFO,PROCESS_START_AUTH,PROCESS_START_ISSHOW,PROCESS_START_ORDER,CREATE_TIME,
		LAST_UPDATE_TIME,REMARK,TENANT_ID,PROCESS_MGR_ISSHOW,OTHER_SYS_DEAL_STATUS,ARIS_CODE,C_PROTOCL,M_URL,M_PROTOCL
	    ]]>
	</sql>

	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="ProcessInfoConfig.insert" useGeneratedKeys="false" keyProperty="processDefId">
    <![CDATA[
        INSERT INTO T_WF_PROCESS_INFO_CONFIG (
        	PROCESS_DEF_ID ,
        	PROCESS_DEF_NAME ,
        	PROCESS_TYPE_ID ,
        	PROCESS_PAGE_URL ,
        	PROCESS_PAGE_INFO ,
        	PROCESS_START_AUTH ,
        	PROCESS_START_ISSHOW ,
        	REMARK ,
        	PROCESS_HANDLER_CLASS_PATH,
        	PROCESS_START_ORDER,
        	DEPLOYMENT_ID,
        	CREATE_TIME,
        	TENANT_ID,
        	PROCESS_MGR_STATE,
        	PROCESS_MODEL_SYNC_STATE,
        	PROCESS_MGR_ISSHOW,
        	OTHER_SYS_DEAL_STATUS,
        	ARIS_CODE,
        	C_PROTOCL,M_URL,M_PROTOCL
        ) VALUES (
        	#{processDefId,jdbcType=VARCHAR} ,
        	#{processDefName,jdbcType=VARCHAR} ,
        	#{processTypeId,jdbcType=VARCHAR} ,
        	#{processPageUrl,jdbcType=VARCHAR} ,
        	#{processPageInfo,jdbcType=VARCHAR} ,
        	#{processStartAuth,jdbcType=VARCHAR} ,
        	'Y',
        	#{remark,jdbcType=VARCHAR} ,
        	#{processHandlerClassPath,jdbcType=VARCHAR} ,
        	#{processStartOrder,jdbcType=INTEGER},
        	#{deploymentId,jdbcType=VARCHAR},
        	#{createTime,jdbcType=TIMESTAMP},
        	#{tenantId,jdbcType=VARCHAR},
        	'UNRELEASE',
        	'Y',
        	'Y',
        	#{otherSysDealStatus,jdbcType=VARCHAR},
        	#{arisCode,jdbcType=VARCHAR} ,
        	#{cprotocol,jdbcType=VARCHAR} ,
        	#{murl,jdbcType=VARCHAR} ,
        	#{mprotocol,jdbcType=VARCHAR}
        )
    ]]>
		<!--	
			oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL 
			DB2: order="BEFORE"" values nextval for sequenceName
		<selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">
			SELECT sequenceName.nextval AS ID FROM DUAL 
        </selectKey>
		-->
	</insert>
    
	<update id="ProcessInfoConfig.update" >
    <![CDATA[
        UPDATE T_WF_PROCESS_INFO_CONFIG SET
	        PROCESS_DEF_NAME = #{processDefName,jdbcType=VARCHAR} ,
	        PROCESS_TYPE_ID = #{processTypeId,jdbcType=VARCHAR} ,
	        PROCESS_PAGE_URL = #{processPageUrl,jdbcType=VARCHAR} ,
	        PROCESS_PAGE_INFO = #{processPageInfo,jdbcType=VARCHAR} ,
	        PROCESS_START_AUTH = #{processStartAuth,jdbcType=VARCHAR} ,
	        PROCESS_START_ISSHOW = #{processStartIsshow,jdbcType=VARCHAR} ,
	        REMARK = #{remark,jdbcType=VARCHAR} ,
	        PROCESS_HANDLER_CLASS_PATH = #{processHandlerClassPath,jdbcType=VARCHAR} ,
	        PROCESS_START_ORDER=#{processStartOrder,jdbcType=INTEGER} ,
	        LAST_UPDATE_TIME=#{lastUpdateTime,jdbcType=TIMESTAMP},
	        PROCESS_MGR_STATE=#{processMgrState,jdbcType=VARCHAR} ,
	        PROCESS_MODEL_SYNC_STATE=#{processModelSyncState,jdbcType=VARCHAR},
	        PROCESS_MGR_ISSHOW=#{processMgrIsshow,jdbcType=VARCHAR},
	        OTHER_SYS_DEAL_STATUS=#{otherSysDealStatus,jdbcType=VARCHAR},
	        ARIS_CODE=#{arisCode,jdbcType=VARCHAR},
	        C_PROTOCL=#{cprotocol,jdbcType=VARCHAR} ,
	        M_URL=#{murl,jdbcType=VARCHAR} ,
	        M_PROTOCL=#{mprotocol,jdbcType=VARCHAR}
        WHERE 
	        PROCESS_DEF_ID = #{processDefId} 
    ]]>
	</update>

    <delete id="ProcessInfoConfig.delete">
    <![CDATA[
        DELETE FROM T_WF_PROCESS_INFO_CONFIG WHERE
	        PROCESS_DEF_ID = #{processDefId} 
    ]]>
    </delete>
    
    <select id="ProcessInfoConfig.getById" resultMap="RM_ProcessInfoConfig">
		SELECT <include refid="ProcessInfoConfig.columns" />,g.type_name as PROCESS_TYPE_NAME
	    <![CDATA[
		    FROM T_WF_PROCESS_INFO_CONFIG t left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
	        WHERE 
	        	PROCESS_DEF_ID = #{processDefId} 
	    ]]>
	</select>
	 <select id="ProcessInfoConfig.getLinkId" resultMap="RM_ProcessInfoConfig">
		SELECT <include refid="ProcessInfoConfig.columns" />,g.type_name as PROCESS_TYPE_NAME
	    <![CDATA[
		    FROM T_WF_PROCESS_INFO_CONFIG t left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
	        WHERE 
	        	PROCESS_DEF_ID like CONCAT(#{processDefId},'%')
	    ]]>
	</select>
	
	<sql id="ProcessInfoConfig.findConfigPage.where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>		
		  
	       <if test="@Ognl@isNotEmpty(processDefId)">
				AND PROCESS_DEF_ID like  CONCAT('%',#{processDefId},'%')
			</if>
	       <if test="@Ognl@isNotEmpty(processDefKey)">
				AND PROCESS_DEF_ID like  CONCAT(#{processDefKey},'%')
			</if>
	       <if test="@Ognl@isNotEmpty(processDefName)">
				AND PROCESS_DEF_NAME like  CONCAT('%',#{processDefName},'%')
			</if>
	       <if test="@Ognl@isNotEmpty(processTypeId)">
				AND PROCESS_TYPE_ID = #{processTypeId}
			</if>
	       <if test="@Ognl@isNotEmpty(processPageUrl)">
				AND PROCESS_PAGE_URL = #{processPageUrl}
			</if>
	       <if test="@Ognl@isNotEmpty(processPageInfo)">
				AND PROCESS_PAGE_INFO = #{processPageInfo}
			</if>
	       <if test="@Ognl@isNotEmpty(processStartAuth)">
				AND PROCESS_START_AUTH = #{processStartAuth}
			</if>
	       <if test="@Ognl@isNotEmpty(processStartIsshow)">
				AND PROCESS_START_ISSHOW = #{processStartIsshow}
			</if>
			
			<if test="@Ognl@isNotEmpty(tenantId)">
				AND TENANT_ID = #{tenantId}
			</if>
	        <if test="@Ognl@isNotEmpty(processMgrState)">
				AND PROCESS_MGR_STATE = #{processMgrState}
			</if>
			 <if test="@Ognl@isNotEmpty(processMgrIsshow)">
				AND PROCESS_MGR_ISSHOW = #{processMgrIsshow}
			</if>
		</where>
	</sql>
	
	<sql id="ProcessInfoConfig.findPage.where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>
		  T.PROCESS_DEF_ID = C.PROCESS_DEF_ID_ 				
	       <if test="@Ognl@isNotEmpty(processDefId)">
				AND PROCESS_DEF_ID = #{processDefId}
			</if>
	       <if test="@Ognl@isNotEmpty(processDefName)">
				AND PROCESS_DEF_NAME = #{processDefName}
			</if>
	       <if test="@Ognl@isNotEmpty(processTypeId)">
				AND PROCESS_TYPE_ID = #{processTypeId}
			</if>
	       <if test="@Ognl@isNotEmpty(processPageUrl)">
				AND PROCESS_PAGE_URL = #{processPageUrl}
			</if>
	       <if test="@Ognl@isNotEmpty(processPageInfo)">
				AND PROCESS_PAGE_INFO = #{processPageInfo}
			</if>
	       <if test="@Ognl@isNotEmpty(processStartAuth)">
				AND PROCESS_START_AUTH = #{processStartAuth}
			</if>
	       <if test="@Ognl@isNotEmpty(processStartIsshow)">
				AND PROCESS_START_ISSHOW = #{processStartIsshow}
			</if>
			 <if test="@Ognl@isNotEmpty(processMgrState)">
				AND PROCESS_MGR_STATE = #{processMgrState}
			</if>
			 <if test="@Ognl@isNotEmpty(processMgrIsshow)">
				AND PROCESS_MGR_ISSHOW = #{processMgrIsshow}
			</if>
			<if test="@Ognl@isNotEmpty(tenantId)">
				AND TENANT_ID = #{tenantId}
			</if>
	       
		</where>
	</sql>
	
	 <select id="ProcessInfoConfig.findConfigPage.count" resultType="long">
        SELECT count(*) FROM T_WF_PROCESS_INFO_CONFIG t left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
		<include refid="ProcessInfoConfig.findConfigPage.where"/>    
    </select>
    
    
    <select id="ProcessInfoConfig.findPage.count" resultType="long">
        SELECT count(*) FROM T_WF_PROCESS_INFO_CONFIG 
		<include refid="ProcessInfoConfig.findPage.where"/>    
    </select>
    
     <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="ProcessInfoConfig.findConfigPage" resultMap="RM_ProcessInfoConfig">
    	SELECT <include refid="ProcessInfoConfig.columns" />,g.type_name as PROCESS_TYPE_NAME
  		FROM T_WF_PROCESS_INFO_CONFIG  t  left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
		<include refid="ProcessInfoConfig.findConfigPage.where"/>
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>
    
    <select id="ProcessInfoConfig.getAllProcessIdForExport" resultType="String">
    	SELECT PROCESS_DEF_ID
  		FROM T_WF_PROCESS_INFO_CONFIG  t  
  		left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
		<include refid="ProcessInfoConfig.findConfigPage.where"/>
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>
    
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="ProcessInfoConfig.findPage" resultMap="RM_ProcessInfoConfig">
    	SELECT <include refid="ProcessInfoConfig.columns" /> 
  		FROM T_WF_PROCESS_INFO_CONFIG T,
       (SELECT T.PROCESS_DEF_ID AS PROCESS_DEF_ID_, T.PROCESS_TYPE_ID AS PROCESS_TYPE_ID_, PROCESS_START_ORDER AS PROCESS_START_ORDER_
          FROM T_WF_PROCESS_INFO_CONFIG T
         GROUP BY T.PROCESS_DEF_ID, PROCESS_START_ORDER, PROCESS_TYPE_ID
         ORDER BY T.PROCESS_TYPE_ID, T.PROCESS_START_ORDER) C
 		 
		<include refid="ProcessInfoConfig.findPage.where"/>
		
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>
    
  	<select id="ProcessInfoConfig.findConfigAndModelPage.count" resultType="long">
        SELECT count(*) FROM T_WF_PROCESS_INFO_CONFIG t left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
        left join  act_re_model m  on t.deployment_id =m.deployment_id_
		<include refid="ProcessInfoConfig.findConfigPage.where"/>    
    </select>
    
     <!--  流程配置和建模 -->
     <select id="ProcessInfoConfig.findConfigAndModelPage" resultMap="RM_ProcessInfoConfig">
    	SELECT <include refid="ProcessInfoConfig.columns" />,g.type_name as PROCESS_TYPE_NAME,m.id_ as MODEL_ID
  		FROM T_WF_PROCESS_INFO_CONFIG  t  left join T_SYS_GL_TYPE g on t.PROCESS_TYPE_ID=g.type_id
  		left join  act_re_model m  on t.deployment_id =m.deployment_id_
		<include refid="ProcessInfoConfig.findConfigPage.where"/>
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>
    
     <select id="getPreTaskInstance"  resultType="string">
 		 select assignee_ from t_wf_hi_taskinst t where t.proc_inst_id_=#{processInstanceId} and pre_task_id=#{taskId} and end_time_ is not null
  	</select>
  	
      <!--  流程引擎扩展SQL
  	<select id="getSubProcessInstance"  resultType="string">
 		 select * from ACT_HI_PROCINST p where FIND_IN_SET(proc_inst_id_, getprocchildlst(#{processInstanceId}))
  	</select>
  	<select id="getAllProcessInstance"  resultType="string">
 		select id_, proc_inst_id_, business_key_, proc_def_id_, start_time_, end_time_, duration_, start_user_id_, start_act_id_, end_act_id_, super_process_instance_id_, delete_reason_, tenant_id_, name_, proc_title, proc_state, proc_def_name, starter_org_name, starter_org_id, starter, top_process_instance_id_, 
						(select assignee_user_name from( select tk.assignee_user_name,proc_inst_id_ from ACT_HI_TASKINST tk  order by tk.start_time_) tk 
						where tk.proc_inst_id_=p.id_  limit 1) as START_USER_NAME from ACT_HI_PROCINST p where FIND_IN_SET(
						proc_inst_id_, getprocchildlst((select t.top_process_instance_id_  from ACT_HI_PROCINST t 
						where t.proc_inst_id_=#{processInstanceId} limit 1)))
  	</select>
  	
  	<select id="getTopProcessInstance"  resultType="string">
 		select * from ACT_HI_PROCINST p where p.PROC_INST_ID_ in(
						select t.top_process_instance_id_  from ACT_HI_PROCINST t 
						where t.proc_inst_id_=#{processInstanceId}) limit 1
  	</select>
  	<select id="getSubProcessInstanceIds"  resultType="string">
 		select * from ACT_HI_PROCINST t  where t.super_process_instance_id_=#{processInstanceId}
  	</select>
  	
  	<select id="getTopProcessAllTasks"  resultType="string">
 		select *from ACT_RU_TASK t where t.proc_inst_id_ in(select p.proc_inst_id_ from ACT_HI_PROCINST p where p.top_process_instance_id_=#{processInstanceId}) 
  	</select>
  
  	<select id="findBackAvtivity"  resultType="string">
 		select t.* from act_hi_taskinst t,(select t.task_def_key_,max(t.end_time_) as end_time from act_hi_taskinst t  
				WHERE t.proc_inst_id_=#{processInstanceId}
				and task_def_key_!= #{actDefId} 
				and end_time_ is not null and t.delete_reason_!='custom-completed' group by task_def_key_) a where t.task_def_key_=a.task_def_key_ and t.end_time_=a.end_time 
				and t.proc_inst_id_=#{processInstanceId} order by start_time_
  	</select> -->
</mapper>