<?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.safeCheck.dao.TSafeCheckTaskDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.safeCheck.model.domain.TSafeCheckTask">
                <id column="id" property="id"/>
                <id column="plan_id" property="planId"/>
                <id column="process_id" property="processId"/>
                <id column="process_status" property="processStatus"/>
                <id column="is_overdue" property="isOverdue"/>
                <id column="checker_id" property="checkerId"/>
                <id column="start_time" property="startTime"/>
                <id column="end_time" property="endTime"/>
                <id column="complete_time" property="completeTime"/>
    </resultMap>

    <select id="pageList" resultType="com.testor.module.safeCheck.model.domain.TSafeCheckTask">
        SELECT
            tsct.*,
            tso.org_name AS orgName,
        tsdd.dict_value as checkTypeName
        FROM
            t_safe_check_task AS tsct
                LEFT JOIN
            t_sys_org AS tso ON tso.org_id = tsct.org_id
                LEFT JOIN
            t_safe_check_person_arrange AS tscpa ON tscpa.check_person_id = tsct.check_person_id
                LEFT JOIN
            t_sys_user AS tsu ON tsu.user_id = tscpa.user_id
                left join
            t_sys_dict_data as tsdd on tsdd.dict_data_id = tsct.check_type
        <where>
            tsct.org_id IN
            <foreach item="id" collection="param.orgIdList" open="(" separator="," close=")">
                #{id}
            </foreach>
            <if test="param.checkPeople!=null and param.checkPeople != ''">
               and tsu.user_name like concat('%',#{param.checkPeople},'%')
            </if>
            <if test="param.name !=null and param.name != ''">
                and tsct.name like concat('%',#{param.name},'%')
            </if>
            <if test="param.taskStatus != null and param.taskStatus != ''">
                <choose>
                    <!-- NOT_STARTED -->
                    <when test="param.taskStatus ==  0 ">
                        AND tsct.start_time &gt;= NOW()
                    </when>
                    <!-- COMPLETED -->
                    <when test="param.taskStatus ==  2 ">
                        AND tsct.complete_time IS NOT NULL
                    </when>
                    <when test="param.taskStatus ==  1 ">
                        AND tsct.start_time &lt;= NOW()
                        AND tsct.complete_time IS  NULL
                    </when>
                </choose>
            </if>
        <if test="param.startTime !=null">
            and tsct.start_time &gt;= #{param.startTime}
        </if>
        <if test="param.endTime !=null">
            and tsct.start_time &lt;= #{param.endTime}
        </if>
        <if test="param.isOverdue!=null and param.isOverdue!= '' and  param.isOverdue == 1 ">
           and ((tsct.complete_time is null and tsct.end_time &lt;= NOW()) or tsct.is_overdue = '1' )
        </if>
        <if test="param.isOverdue!=null and param.isOverdue!= '' and param.isOverdue == 0 ">
            and  ((tsct.complete_time is null and tsct.end_time &gt;= NOW()) or (tsct.complete_time is not null and tsct.end_time >= tsct.complete_time) )
        </if>
        <if test="param.checkType != null and param.checkType != ''">
            and tsct.check_type = #{param.checkType}
        </if>
        </where>
        GROUP BY
            tsct.id, tso.org_name,tsdd.dict_value
        order by create_date desc


    </select>
</mapper>