<?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.hotwj.platform.securityManagement.hiddenDangerInspection.mapper.HotHiddenDangerInspectionMapper">

    <!-- status: 进度状态 无异常:1=特处理、2=审核中、3=完结 有异常:4=特处理、5=审核中、6=评估、7=治理、8=复查、9=完结-->
    <select id="selectMobilePlanPage"
            resultType="com.hotwj.platform.securityManagement.hiddenDangerInspection.domain.vo.HotHiddenDangerInspectionVo">
        SELECT
            i.id AS id,
            i.company_id AS companyId,
            i.plan_id AS planId,
            i.project_name AS projectName,
            i.project_id AS projectId,
            i.project_type AS projectType,
            p.plan_name AS planName,
            p.start_date AS planStartDate,
            p.end_date AS planEndDate,
            CASE
                WHEN i.status IS NULL OR i.status = 1 OR i.status = 2 THEN 1
                WHEN i.status = 10 THEN 1
                ELSE 2
            END AS todoSortGroup,
            CASE
                WHEN i.status IS NULL OR i.status = 1 OR i.status = 2 THEN '未做'
                WHEN i.status = 10 THEN '驳回'
                ELSE '已完成'
            END AS todoSortLabel,
            COALESCE(i.status, 0) AS status
        FROM hot_hidden_danger_inspection i
        INNER JOIN hot_hidden_danger_plan p
            ON p.id = i.plan_id
            AND p.is_deleted = 0
        WHERE i.is_deleted = 0
            AND p.company_id = #{companyId}
            AND (
                (NOW() BETWEEN p.start_date AND p.end_date)
                OR (
                    p.is_recheck = 1
                    AND NOW() BETWEEN p.recheck_start_time AND p.recheck_end_time
                )
            )
        <if test="isAdmin == false">
            AND (
                (i.project_type = 3 AND i.project_id = #{driverId})
                <if test="vehicleId != null and vehicleId != ''">
                    OR (i.project_type = 2 AND i.project_id = #{vehicleId})
                </if>
            )
        </if>
        <if test="planId != null">
            AND i.plan_id = #{planId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (
                p.plan_name LIKE CONCAT('%', #{keyword}, '%')
                OR i.project_name LIKE CONCAT('%', #{keyword}, '%')
                <if test="isAdmin == false">
                    OR i.project_id LIKE CONCAT('%', #{keyword}, '%')
                </if>
            )
        </if>
        ORDER BY
            todoSortGroup ASC,
            COALESCE(p.end_date, p.start_date, i.create_time) ASC,
            i.id DESC
    </select>
</mapper>
