<?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.mortals.flowable.dao.flowable.IFlowableTaskDao">

    <select id="getApplyingTasksCount" parameterType="com.mortals.xhx.flowable.task.CommonTaskQuery" resultType="int">
        select DISTINCT count(1)
        FROM
        ACT_RU_TASK t1
        INNER JOIN ACT_RU_EXECUTION t2 ON t1.PROC_INST_ID_ = t2.PROC_INST_ID_
        LEFT JOIN ACT_RU_IDENTITYLINK t3 ON t3.TASK_ID_ = t1.ID_
        LEFT JOIN ACT_RE_PROCDEF t4 ON t4.ID_ = t1.PROC_DEF_ID_
        WHERE t2.BUSINESS_KEY_ is not null
        <choose>
            <when test="params.platformSns!=null and params.platformSns.size() &gt; 0">
                and t1.TENANT_ID_ in
                <foreach item="platformSn" collection="params.platformSns" separator="," open="(" close=")" index="">
                    #{platformSn}
                </foreach>
            </when>
            <otherwise>
                <if test="params.platformSn!='' and params.platformSn!=null">
                    and t1.TENANT_ID_ = #{params.platformSn,jdbcType=VARCHAR}
                </if>
            </otherwise>
        </choose>

        <if test="params.userCode != null and params.userCode!=''">
            and ( t1.ASSIGNEE_ = #{params.userCode}

            OR (
            ( t1.ASSIGNEE_ IS NULL OR t1.ASSIGNEE_ = '' )
            AND ( t3.USER_ID_ = #{params.userCode}

        <if test="params.candidateGroups != null and params.candidateGroups.size() &gt; 0">
            OR t3.GROUP_ID_ IN
            <foreach item="groupId" collection="params.candidateGroups" separator="," open="(" close=")" index="">
                #{groupId}
            </foreach>
        </if>
            )
            )
            )
        </if>
    </select>


    <select id="getApplyingTasks" parameterType="com.mortals.xhx.flowable.task.CommonTaskQuery"
            resultType="com.mortals.xhx.common.pdu.flow.FlowTaskPdu">
        SELECT DISTINCT
        t1.ID_ AS taskId,
        t1.NAME_ AS taskName,
        t2.NAME_ AS formName,
        t2.TENANT_ID_ AS platformSn,
        t2.BUSINESS_KEY_ AS businessKey,
        t2.PROC_INST_ID_ AS procInsId,
        t4.DEPLOYMENT_ID_ AS deployId,
        t4.NAME_ AS procDefName,
        t4.CATEGORY_ AS category,
        t1.CREATE_TIME_ AS createTime
        FROM
        ACT_RU_TASK t1
        INNER JOIN ACT_RU_EXECUTION t2 ON t1.PROC_INST_ID_ = t2.PROC_INST_ID_
        LEFT JOIN ACT_RU_IDENTITYLINK t3 ON t3.TASK_ID_ = t1.ID_
        LEFT JOIN ACT_RE_PROCDEF t4 ON t4.ID_ = t1.PROC_DEF_ID_
        WHERE t2.BUSINESS_KEY_ is not null
        <choose>
            <when test="params.platformSns!=null and params.platformSns.size() &gt; 0">
                and t1.TENANT_ID_ in
                <foreach item="platformSn" collection="params.platformSns" separator="," open="(" close=")" index="">
                    #{platformSn}
                </foreach>
            </when>
            <otherwise>
                <if test="params.platformSn!='' and params.platformSn!=null">
                    and t1.TENANT_ID_ = #{params.platformSn,jdbcType=VARCHAR}
                </if>
            </otherwise>
        </choose>

        <if test="params.userCode != null and params.userCode!=''">
            and ( t1.ASSIGNEE_ = #{params.userCode}

            OR (
            ( t1.ASSIGNEE_ IS NULL OR t1.ASSIGNEE_ = '' )
            AND ( t3.USER_ID_ = #{params.userCode}
            <if test="params.candidateGroups != null and params.candidateGroups.size() &gt; 0">
                OR t3.GROUP_ID_ IN
                <foreach item="groupId" collection="params.candidateGroups" separator="," open="(" close=")" index="">
                    #{groupId}
                </foreach>
            </if>
            )
            )
            )
        </if>
        ORDER BY t1.CREATE_TIME_ DESC
        limit #{params.size},#{params.per}
    </select>


    <select id="getApplyedTasksCount" parameterType="com.mortals.xhx.flowable.task.CommonTaskQuery"
            resultType="int">
        SELECT DISTINCT
        count(1)
        FROM
        ACT_HI_TASKINST t1
        LEFT JOIN ACT_ID_USER t2 ON t1.ASSIGNEE_ = t2.ID_
        LEFT JOIN ACT_HI_PROCINST t3 ON t1.PROC_INST_ID_ = t3.PROC_INST_ID_
        LEFT JOIN ACT_HI_IDENTITYLINK t4 ON t4.TASK_ID_ = t1.ID_
        <where>
            <if test="params.platformSn!=null and params.platformSn!=''">
                and t3.TENANT_ID_ = #{params.platformSn,jdbcType=VARCHAR}
            </if>
            <if test="params.userCode!=null and params.userCode!=''">
                AND (
                t1.ASSIGNEE_ = #{params.userCode,jdbcType=VARCHAR}
                OR (
                ( t1.ASSIGNEE_ IS NULL OR t1.ASSIGNEE_ = '' )
                AND ( t4.USER_ID_ = #{params.userCode,jdbcType=VARCHAR} OR t4.GROUP_ID_ IN ( SELECT g.group_id_ FROM ACT_ID_MEMBERSHIP g WHERE g.user_id_ = #{params.userCode,jdbcType=VARCHAR} ) )
                )
                )

            </if>

            <if test="params.startTime!=null and params.startTime!=''">
                AND t1.START_TIME_ <![CDATA[ >= ]]> #{params.startTime}
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                AND t1.START_TIME_ <![CDATA[ <= ]]> #{params.endTime}
            </if>
            <if test="params.businessKey!=null and params.businessKey!=''">
                AND t3.BUSINESS_KEY_ LIKE CONCAT('%',#{params.businessKey},'%')
            </if>
        </where>

    </select>

    <select id="getApplyedTasks" parameterType="com.mortals.xhx.flowable.task.CommonTaskQuery"
            resultType="com.mortals.xhx.common.pdu.flow.FlowTaskPdu">
        SELECT DISTINCT
        t1.ID_ AS taskId,
        t1.NAME_ AS taskName,
        t2.FIRST_ AS approver,
        t2.ID_ AS approverId,
        t3.NAME_ AS formName,
        t3.BUSINESS_KEY_ AS businessKey,
        t3.PROC_INST_ID_ AS procInsId,
        t3.TENANT_ID_ as platformSn,
        t1.START_TIME_ AS createTime,
        t1.CATEGORY_ AS category,
        t1.END_TIME_ as finishTime
        FROM
        ACT_HI_TASKINST t1
        LEFT JOIN ACT_ID_USER t2 ON t1.ASSIGNEE_ = t2.ID_
        LEFT JOIN ACT_HI_PROCINST t3 ON t1.PROC_INST_ID_ = t3.PROC_INST_ID_
        LEFT JOIN ACT_HI_IDENTITYLINK t4 ON t4.TASK_ID_ = t1.ID_
        <where>
            <if test="params.platformSn!=null and params.platformSn!=''">
                and t3.TENANT_ID_ = #{params.platformSn,jdbcType=VARCHAR}
            </if>
            <if test="params.userCode!=null and params.userCode!=''">
                AND (
                t1.ASSIGNEE_ = #{params.userCode,jdbcType=VARCHAR}
                OR (
                ( t1.ASSIGNEE_ IS NULL OR t1.ASSIGNEE_ = '' )
                AND ( t4.USER_ID_ = #{params.userCode,jdbcType=VARCHAR} OR t4.GROUP_ID_ IN ( SELECT g.group_id_ FROM ACT_ID_MEMBERSHIP g WHERE g.user_id_ = #{params.userCode,jdbcType=VARCHAR} ) )
                )
                )

            </if>

            <if test="params.startTime!=null and params.startTime!=''">
                AND t1.START_TIME_ <![CDATA[ >= ]]> #{params.startTime}
            </if>
            <if test="params.endTime!=null and params.endTime!=''">
                AND t1.START_TIME_ <![CDATA[ <= ]]> #{params.endTime}
            </if>
            <if test="params.businessKey!=null and params.businessKey!=''">
                AND t3.BUSINESS_KEY_ LIKE CONCAT('%',#{params.businessKey},'%')
            </if>
        </where>

        ORDER BY t1.START_TIME_ DESC
        limit #{params.size},#{params.per}
    </select>

</mapper>
