<?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.trade.admin.workflow.mapper.FlowActivityDefinitionMapper">
    <resultMap id="flowActDefResult" type="com.trade.admin.workflow.domain.FlowActivityDefinition">
        <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="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <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.create_by,
		f.update_by,
		f.gmt_create,
		f.gmt_modified
    </sql>

    <insert id="add" useGeneratedKeys="true" keyProperty="id" keyColumn="id" >
        INSERT INTO flow_act_def(proc_def_id, proc_def_key, proc_def_version, activity_key, activity_name,
                                 activity_type, create_by)
        VALUES (#{processDefinitionId}, #{processDefinitionKey}, #{processDefinitionVersion}, #{activityKey},
                #{activityName}, #{activityType}, #{createBy})
    </insert>

    <select id="findBySearch" 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>


    <select id="get" 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>
