<?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="<%=packageName%>.flowable.repository.FlowableCommonRepository">

    <select id="selectUserByQueryCriteria" parameterType="org.flowable.idm.engine.impl.UserQueryImpl"
            resultType="org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl">
        SELECT
        t.id id,
        t.first_name firstName,
        t.last_name lastName,
        t.first_name displayName,
        t.email email
        <include refid="selectUserByQueryCriteriaSql"/>
    </select>

    <select id="selectUserCountByQueryCriteria" parameterType="org.flowable.idm.engine.impl.UserQueryImpl"
            resultType="long">
        select count(1)
        <include refid="selectUserByQueryCriteriaSql"/>
    </select>

    <sql id="selectUserByQueryCriteriaSql">
        FROM jhi_user t
        WHERE 1=1
        <if test="id != null">
            and t.id = #{id}
        </if>
        <if test="ids != null &amp;&amp; ids.size() &gt; 0">
            and t.id IN
            <foreach item="id" index="index" collection="ids"
                     open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="firstName != null">
            and t.first_name = #{firstName}
        </if>
        <if test="firstNameLike != null">
            and t.first_name like #{firstNameLike}${wildcardEscapeClause}
        </if>
    </sql>

    <select id="selectGroupByQueryCriteria" parameterType="org.flowable.idm.engine.impl.GroupQueryImpl"
            resultType="org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl">
        select r.id id,
        r.name name
        <include refid="selectGroupByQueryCriteriaSql"/>
    </select>

    <select id="selectGroupCountByQueryCriteria" parameterType="org.flowable.idm.engine.impl.GroupQueryImpl"
            resultType="org.flowable.idm.engine.impl.persistence.entity.GroupEntityImpl">
        select count(1)
        <include refid="selectGroupByQueryCriteriaSql"/>
    </select>

    <sql id="selectGroupByQueryCriteriaSql">
        from position r
        where 1=1
        <if test="id != null">
            and r.id = #{id}
        </if>
        <if test="ids != null &amp;&amp; ids.size() &gt; 0">
            and r.id IN
            <foreach item="id" index="index" collection="ids"
                     open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        <if test="name != null">
            and r.name = #{name}
        </if>
        <if test="nameLike != null">
            and r.name like #{nameLike}${wildcardEscapeClause}
        </if>
        <if test="userId != null">
            and exists(select 1 from jhi_user m where m.position_id = r.id and M.id = #{userId})
        </if>
        <if test="userIds != null &amp;&amp; userIds.size() &gt; 0">
            and exists(select 1 from jhi_user m where m.position_id = r.id and M.id in
            <foreach item="userId" index="index" collection="userIds" open="(" separator="," close=")">
                #{userId}</foreach>)
        </if>
    </sql>

    <select id="listMyInvolvedSummary" parameterType="<%=packageName%>.flowable.vo.query.ProcessInstanceQueryVo"
            resultType="<%=packageName%>.flowable.vo.ProcessDefinitionVo">
        SELECT
            DEF.CATEGORY_ "category",
            DEF.CATEGORY_ "categoryName",
            DEF.KEY_ "processDefinitionKey",
            DEF.NAME_ "processDefinitionName",
            count(1) "count"
        FROM
            ACT_HI_PROCINST RES
        LEFT OUTER JOIN ACT_RE_PROCDEF DEF ON RES.PROC_DEF_ID_ = DEF.ID_
        WHERE
        (
            EXISTS (
                SELECT
                    LINK.USER_ID_
                FROM
                    ACT_HI_IDENTITYLINK LINK
                WHERE
                    USER_ID_ = #{userId}
                AND LINK.PROC_INST_ID_ = RES.ID_
            )
        )
        <if test="ccToMe">
            AND EXISTS (
                SELECT
                    ID_
                FROM ACT_HI_IDENTITYLINK I
                WHERE I.PROC_INST_ID_ = RES.ID_
                    AND I.USER_ID_ = #{userId}
                    AND I.TYPE_ = 'CC'
            )
        </if>

        <if test="startedByMe">
            AND RES.START_USER_ID_ = #{userId}
        </if>
        <if test="unfinished != finished">
            <if test="unfinished">
                AND RES.END_TIME_ IS NULL
            </if>
            <if test="finished">
                AND RES.END_TIME_ IS NOT NULL
            </if>
        </if>
        GROUP BY
            DEF.CATEGORY_,DEF.CATEGORY_,DEF.KEY_,DEF.NAME_
        ORDER BY
            DEF.CATEGORY_,DEF.KEY_
    </select>
</mapper>
