<?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.sunxung.factoring.mapper.sys.flowable.FlowActivityDefinitionMapper">

	<resultMap type="FlowActivityDefinition" id="flowActDefResult">
		<result property="id" column="id" />
		<result property="processDefinitionId" column="proc_def_id" />
		<result property="processDefinitionKey" column="proc_def_key" />
		<result property="processDefinitionVersion" column="proc_def_version" />
		<result property="activityKey" column="activity_key" />
		<result property="activityName" column="activity_name" />
		<result property="activityType" column="activity_type" />
		<result property="timeLimit" column="time_limit"/>
		<result property="workingDay" column="is_working_day"/>
		<result property="userCreate" column="user_create" />
		<result property="userModified" column="user_modified" />
		<result property="gmtCreate" column="gmt_create" />
		<result property="gmtModified" column="gmt_modified" />
	</resultMap>

	<sql id="fields">
		f.id,
		f.proc_def_id,
		f.proc_def_key,
		f.proc_def_version,
		f.activity_key,
		f.activity_name,
		f.activity_type,
		f.time_limit,
		f.is_working_day,
		f.user_create,
		f.user_modified,
		f.gmt_create,
		f.gmt_modified
	</sql>

	<insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" parameterType="FlowActivityDefinition">
		INSERT INTO flow_act_def(proc_def_id, proc_def_key, proc_def_version, activity_key, activity_name,activity_type,user_create )  
		VALUES( #{processDefinitionId}, #{processDefinitionKey}, #{processDefinitionVersion}, #{activityKey},#{activityName},#{activityType},#{userCreate})
	</insert>
	
	<select id="findBySearch"  parameterType="SearchFlowActivityDefinition" resultMap="flowActDefResult">
	    SELECT <include refid="fields"/>
	    FROM 
	    		flow_act_def f
	    <where>
	    		<if test="procDefId != null and procDefId !=''">
	    			AND f.proc_def_id=#{procDefId}
	    		</if>
	    		<if test="procDefVersion != null">
	    			AND f.proc_def_version =#{procDefVersion}
	    		</if>
	    		<if test=" procDefKey != null and procDefKey !=''">
	    			AND f.proc_def_key=#{procDefKey}
	    		</if>
	    </where>
	    ORDER BY
			f.id ASC
	</select>
	
	<update id="updateTimeLimit" >
		UPDATE flow_act_def f
		SET f.time_limit=#{timeLimit},
			f.is_working_day=#{workingDay}
		WHERE f.id=#{id}
	</update>
	
	<!-- <insert id="addAssigneeRef" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
		INSERT INTO flow_act_assignee(flow_act_def_id, assignee_id)  
		VALUES( #{flowActDefId}, #{assigneeId})
	</insert> -->
	
	<select id="get" parameterType="Long"  resultMap="flowActDefResult">
		SELECT <include refid="fields"/>FROM flow_act_def f WHERE f.id=#{id}
	</select>
	
	<select id="getByProcActKey" resultMap="flowActDefResult">
		SELECT 
			<include refid="fields"/>
		FROM 
			flow_act_def f 
		WHERE f.proc_def_id=#{procDefId} 
		AND f.activity_key=#{activityKey}
	</select>
	
	<select id="getMaxTimeLimitByProcDefKeyActKey" resultMap="flowActDefResult">
		SELECT 
			<include refid="fields"/>
		FROM flow_act_def f 
		INNER JOIN (
			SELECT 
			max(id) as id,
			max(proc_def_version) AS proc_def_version
			FROM flow_act_def 
			WHERE proc_def_key=#{procDefKey}
			AND activity_key=#{activityKey})
		temp 
		on temp.id = f.id
	</select>
	<select id="existTask" resultType="java.lang.Integer">
		select count(*)
		from act_ru_task where ID_ = #{historicTaskId}
	</select>

</mapper>