<?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.testor.module.wf.dao.RuTaskDao">

    <resultMap id="countToDoResult" type="java.util.HashMap">
        <result column="num" property="num" javaType="java.lang.String"/>
        <result column="processDefinitionId" property="processDefinitionId" javaType="java.lang.String"/>
        <result column="processDefinitionName" property="processDefinitionName" javaType="java.lang.String"/>
        <result column="processDefinitionKey" property="processDefinitionKey" javaType="java.lang.String"/>
    </resultMap>

    <delete id="deleteRuVariableException">
        delete from ACT_RU_VARIABLE where EXECUTION_ID_ is null
    </delete>

    <delete id="deleteHiVariableException">
        delete from act_hi_varinst where EXECUTION_ID_ is null
    </delete>

    <select id="getHisTask" resultType="com.tongtech.tfw.workflow.apis.task.model.dto.HisTask">
        SELECT
        task.ID_ AS taskId,
        task.NAME_ AS taskName,
        task.START_TIME_ AS startTime,
        task.END_TIME_ AS endTime,
        task.FORM_KEY_ as formKey,
        task.PROC_INST_ID_ AS processInstanceId,
        task.PROC_DEF_ID_ AS processDefinitionId,
        procdef.NAME_ AS processDefinitionName,
        procdef.VERSION_ AS processDefinitionVersion,
        procdef.DESCRIPTION_ as processDefinitionDescription,
        usr.FIRST_ AS startUser,
        hiproc.BUSINESS_KEY_ AS bizId
        FROM
        act_hi_taskinst task
        LEFT JOIN act_hi_procinst hiproc ON task.PROC_INST_ID_ = hiproc.PROC_INST_ID_
        LEFT JOIN act_re_procdef procdef ON task.PROC_DEF_ID_ = procdef.ID_
        LEFT JOIN act_id_user usr ON hiproc.START_USER_ID_ = usr.ID_
        WHERE
        task.END_TIME_ IS NOT NULL
        <if test="his.processDefinitionName != null"> AND procdef.NAME_ LIKE CONCAT('%',#{his.processDefinitionName},'%')</if>
        <if test="his.taskId != null"> AND task.ID_ =#{his.taskId}</if>
        <if test="his.bizId != null"> AND hiproc.BUSINESS_KEY_ = #{his.bizId}</if>
        <if test="his.processInstanceId != null"> AND task.PROC_INST_ID_ = #{his.processInstanceId}</if>
    </select>

    <select id="getTaskId" resultType="com.tongtech.tfw.workflow.apis.task.model.dto.HisTask">
        SELECT
        task.ID_ AS taskId,
        task.NAME_ AS taskName,
        task.START_TIME_ AS startTime,
        task.END_TIME_ AS endTime,
        task.FORM_KEY_ as formKey,
        task.PROC_INST_ID_ AS processInstanceId,
        task.PROC_DEF_ID_ AS processDefinitionId,
        procdef.NAME_ AS processDefinitionName,
        procdef.VERSION_ AS processDefinitionVersion,
        procdef.DESCRIPTION_ as processDefinitionDescription,
        usr.FIRST_ AS startUser,
        hiproc.BUSINESS_KEY_ AS bizId
        FROM
        act_hi_taskinst task
        LEFT JOIN act_hi_procinst hiproc ON task.PROC_INST_ID_ = hiproc.PROC_INST_ID_
        LEFT JOIN act_re_procdef procdef ON task.PROC_DEF_ID_ = procdef.ID_
        LEFT JOIN act_id_user usr ON hiproc.START_USER_ID_ = usr.ID_
        WHERE
        task.END_TIME_ IS NOT NULL
        <if test="taskId != null"> AND task.ID_ =#{taskId}</if>
    </select>

    <select id="countToDo" resultMap="countToDoResult">
        SELECT
            count(distinct task.ID_) as num,
            procdef.NAME_ AS processDefinitionName,
            procdef.KEY_ as processDefinitionKey
        FROM
            act_re_procdef procdef
                LEFT JOIN
            act_ru_task task  ON task.PROC_DEF_ID_ = procdef.ID_
                LEFT JOIN
            act_ru_identitylink idl ON task.ID_ = idl.TASK_ID_
                left join
            act_ru_variable var on var.PROC_INST_ID_= task.PROC_INST_ID_
        WHERE
            ((task.ASSIGNEE_ = #{userId})
                OR (task.ASSIGNEE_ IS NULL and idl.USER_ID_ = #{userId}
                    OR (task.ASSIGNEE_ IS NULL and idl.GROUP_ID_ IN (SELECT
                                                                         g1.GROUP_ID_
                                                                     FROM
                                                                         act_id_membership g1
                                                                     WHERE
                                                                         g1.user_id_ = #{userId}))
                 ))
        and procdef.NAME_ not like '%危险作业%'
        group by procdef.NAME_,procdef.KEY_
    </select>
    <select id="selectToDoList" resultType="com.tongtech.tfw.workflow.apis.task.model.dto.TodoTask" parameterType="com.tongtech.tfw.workflow.apis.task.model.dto.TodoTaskSqlParam">
        SELECT DISTINCT
        task.ID_ AS taskId,
        task.NAME_ AS taskName,
        task.PROC_INST_ID_ AS processInstanceId,
        task.CREATE_TIME_ AS startTime,
        task.FORM_KEY_ as formKey,
        task.ASSIGNEE_ as assignee,
        usr.FIRST_ AS startUser,
        procdef.ID_ AS processDefinitionId,
        procdef.NAME_ AS processDefinitionName,
        procdef.VERSION_ AS processDefinitionVersion,
        hiproc.BUSINESS_KEY_ AS bizId
        FROM
        act_ru_task task
        LEFT JOIN
        act_hi_procinst hiproc ON task.PROC_INST_ID_ = hiproc.PROC_INST_ID_
        LEFT JOIN
        act_re_procdef procdef ON task.PROC_DEF_ID_ = procdef.ID_
        LEFT JOIN
        act_ru_identitylink idl ON task.ID_ = idl.TASK_ID_
        LEFT JOIN
        act_id_user usr ON hiproc.START_USER_ID_ = usr.ID_
        WHERE
        ((task.ASSIGNEE_ = #{todo.userId})
        OR (
        task.ASSIGNEE_ IS NULL and idl.USER_ID_ = #{todo.userId}
        OR (task.ASSIGNEE_ IS NULL and idl.GROUP_ID_ IN (SELECT
        g1.GROUP_ID_
        FROM
        act_id_membership g1
        WHERE
        g1.user_id_ = #{todo.userId}))
        ))
        and procdef.NAME_ not like '%危险作业%'
        <if test="todo.userName != null"> AND usr.FIRST_ LIKE CONCAT('%',#{todo.userName},'%')</if>
        <if test="todo.proDefName != null"> AND procdef.NAME_ LIKE CONCAT('%',#{todo.proDefName},'%')</if>
        <if test="todo.proDefId != null"> AND procdef.ID_ = #{todo.proDefId} </if>
        <if test="todo.proDefKey != null"> AND procdef.KEY_ = #{todo.proDefKey} </if>
        ORDER BY task.CREATE_TIME_ desc
    </select>
    <select id="selectHisTaskList" resultType="com.tongtech.tfw.workflow.apis.task.model.dto.HisTask" parameterType="com.tongtech.tfw.workflow.apis.task.model.dto.HisTaskSqlParam">
        SELECT
        task.ID_ AS taskId,
        task.NAME_ AS taskName,
        task.START_TIME_ AS startTime,
        task.END_TIME_ AS endTime,
        task.FORM_KEY_ as formKey,
        task.PROC_INST_ID_ AS processInstanceId,
        task.PROC_DEF_ID_ AS processDefinitionId,
        procdef.NAME_ AS processDefinitionName,
        procdef.VERSION_ AS processDefinitionVersion,
        procdef.DESCRIPTION_ as processDefinitionDescription,
        usr.FIRST_ AS startUser,
        hiproc.BUSINESS_KEY_ AS bizId
        FROM
        act_hi_taskinst task
        LEFT JOIN act_hi_procinst hiproc ON task.PROC_INST_ID_ = hiproc.PROC_INST_ID_
        LEFT JOIN act_re_procdef procdef ON task.PROC_DEF_ID_ = procdef.ID_
        LEFT JOIN act_id_user usr ON hiproc.START_USER_ID_ = usr.ID_
        WHERE
        task.END_TIME_ IS NOT NULL
        AND task.ASSIGNEE_ = #{his.userId}
        AND task.DELETE_REASON_ IS NULL
        and procdef.NAME_ not like '%危险作业%'
        <if test="his.userName != null"> AND usr.FIRST_ LIKE CONCAT('%',#{his.userName},'%')</if>
        <if test="his.proDefName != null"> AND procdef.NAME_ LIKE CONCAT('%',#{his.proDefName},'%')</if>
        <if test="his.taskId != null"> AND task.ID_ =#{his.taskId}</if>
        <if test="his.bizId != null"> AND hiproc.BUSINESS_KEY_ = #{his.bizId}</if>
        <if test="his.processInstanceId != null"> AND task.PROC_INST_ID_ = #{his.processInstanceId}</if>
        ORDER BY task.END_TIME_ desc
    </select>
</mapper>

