<?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.hazard.dao.THazardWorkPlanDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.hazard.model.domain.THazardWorkPlan">
                <id column="id" property="id"/>
                <id column="work_type" property="workType"/>
                <id column="work_level" property="workLevel"/>
                <id column="stakeholder_work" property="stakeholderWork"/>
                <id column="involves_other_hazardous_work" property="involvesOtherHazardousWork"/>
                <id column="is_holiday" property="isHoliday"/>
                <id column="is_escalated" property="isEscalated"/>
                <id column="escalated_level" property="escalatedLevel"/>
                <id column="scheduled_start_time" property="scheduledStartTime"/>
                <id column="scheduled_end_time" property="scheduledEndTime"/>
                <id column="work_content" property="workContent"/>
                <id column="work_location" property="workLocation"/>
                <id column="work_supervisory_unit" property="workSupervisoryUnit"/>
                <id column="work_site_responsible" property="workSiteResponsible"/>
                <id column="work_site_responsible_signature" property="workSiteResponsibleSignature"/>
                <id column="project_leader" property="projectLeader"/>
                <id column="guardian" property="guardian"/>
                <id column="supervisor" property="supervisor"/>
                <id column="operator" property="operator"/>
                <id column="risk_analysis_participant" property="riskAnalysisParticipant"/>
                <id column="discloser" property="discloser"/>
                <id column="early_reminder_time" property="earlyReminderTime"/>
                <id column="work_plan_file" property="workPlanFile"/>
                <id column="work_site_responsible_end_signature" property="workSiteResponsibleEndSignature"/>
                <id column="org_id" property="orgId"/>
                <id column="cancel_opinion" property="cancelOpinion"/>
    </resultMap>
    <select id="selectToDoList" resultType="com.tongtech.tfw.workflow.apis.task.model.dto.TodoTask">
        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}))
        ))
        <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.proDefKeys != null">
            AND procdef.KEY_ IN
            <foreach item="item" index="index" collection="todo.proDefKeys" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ORDER BY task.CREATE_TIME_ desc
    </select>
</mapper>