<?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.config.vehicleThreeInspect.mapper.HotVehicleThreeInspectMapper">

    <select id="queryProcessPageList"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectProcessVo">
        SELECT
        t.inspect_id AS inspectId,
        MAX(t.inspect_no) AS inspectNo,
        MAX(t.company_id) AS companyId,
        MAX(t.vehicle_id) AS vehicleId,
        MAX(v.plate_number) AS plateNumber,
        MAX(t.driver_name) AS driverName,
        MAX(t.driver_name) AS inspectorName,
        MAX(t.auditor_name) AS auditorName,
        MIN(t.inspect_time) AS startTime,
        MAX(t.inspect_time) AS endTime,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN
        CASE
        WHEN MIN(t.inspect_time) = MAX(t.inspect_time)
        THEN DATE_FORMAT(MIN(t.inspect_time), '%Y-%m-%d %H:%i:%s')
        ELSE CONCAT(
        DATE_FORMAT(MIN(t.inspect_time), '%Y-%m-%d %H:%i:%s'),
        ' ~ ',
        DATE_FORMAT(MAX(t.inspect_time), '%Y-%m-%d %H:%i:%s')
        )
        END
        ELSE
        CASE
        WHEN MIN(t.inspect_time) = MAX(t.inspect_time)
        THEN DATE_FORMAT(MIN(t.inspect_time), '%Y-%m-%d %H:%i:%s')
        ELSE CONCAT(
        DATE_FORMAT(MIN(t.inspect_time), '%Y-%m-%d %H:%i:%s'),
        ' ~ ',
        DATE_FORMAT(MAX(t.inspect_time), '%Y-%m-%d %H:%i:%s')
        )
        END
        END AS inspectTime,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN '已完成'
        ELSE '未完成'
        END AS completeStatus,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN 1
        ELSE 0
        END AS completed,
        CASE
        WHEN COUNT(DISTINCT CASE WHEN t.flow_status = 'APPROVED' THEN t.inspect_phase END) = 3
        THEN '已完成审核'
        ELSE '未审核'
        END AS auditCompleteStatus,
        CASE
        WHEN COUNT(DISTINCT CASE WHEN t.flow_status = 'APPROVED' THEN t.inspect_phase END) = 3
        THEN 1
        ELSE 0
        END AS auditCompleted
        FROM hot_vehicle_three_inspect t
        LEFT JOIN hot_vehicle v ON t.vehicle_id = v.id
        <where>
            t.is_deleted = 0
            AND t.inspect_id IS NOT NULL
            <if test="bo.companyId != null">
                AND t.company_id = #{bo.companyId}
            </if>
            <if test="bo.vehicleId != null">
                AND t.vehicle_id = #{bo.vehicleId}
            </if>
            <if test="bo.driverId != null and bo.driverId != ''">
                AND t.driver_id = #{bo.driverId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND t.driver_name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.inspectId != null">
                AND t.inspect_id = #{bo.inspectId}
            </if>
            <if test="bo.inspectNo != null and bo.inspectNo != ''">
                AND t.inspect_no = #{bo.inspectNo}
            </if>
            <if test="bo.params != null and bo.params.beginTime != null and bo.params.beginTime != '' and bo.params.endTime != null and bo.params.endTime != ''">
                AND EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.inspect_id = t.inspect_id
                AND t2.is_deleted = 0
                AND t2.inspect_time BETWEEN #{bo.params.beginTime} AND #{bo.params.endTime}
                )
            </if>
            <if test="bo.params != null and bo.params.beginTime != null and bo.params.beginTime != '' and (bo.params.endTime == null or bo.params.endTime == '')">
                AND EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.inspect_id = t.inspect_id
                AND t2.is_deleted = 0
                AND t2.inspect_time <![CDATA[>=]]> #{bo.params.beginTime}
                )
            </if>
            <if test="bo.params != null and (bo.params.beginTime == null or bo.params.beginTime == '') and bo.params.endTime != null and bo.params.endTime != ''">
                AND EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.inspect_id = t.inspect_id
                AND t2.is_deleted = 0
                AND t2.inspect_time <![CDATA[<=]]> #{bo.params.endTime}
                )
            </if>
        </where>
        GROUP BY t.inspect_id
        ORDER BY IFNULL(MAX(t.inspect_time), MAX(t.create_time)) DESC, MAX(t.create_time) DESC
    </select>

    <select id="querySummaryPageList"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectSummaryVo">
        SELECT
        t.inspect_id AS inspectId,
        MAX(t.inspect_no) AS inspectNo,
        MAX(t.company_id) AS companyId,
        MAX(t.vehicle_id) AS vehicleId,
        MAX(v.plate_number) AS plateNumber,
        MAX(t.driver_id) AS driverId,
        MAX(t.driver_name) AS driverName,
        MAX(t.driver_name) AS inspectorName,
        MAX(t.auditor_name) AS auditorName,
        MIN(IFNULL(t.inspect_time, t.create_time)) AS startTime,
        MAX(IFNULL(t.inspect_time, t.create_time)) AS endTime,
        CASE
        WHEN MIN(IFNULL(t.inspect_time, t.create_time)) = MAX(IFNULL(t.inspect_time, t.create_time))
        THEN DATE_FORMAT(MIN(IFNULL(t.inspect_time, t.create_time)), '%Y-%m-%d %H:%i:%s')
        ELSE CONCAT(
        DATE_FORMAT(MIN(IFNULL(t.inspect_time, t.create_time)), '%Y-%m-%d %H:%i:%s'),
        ' ~ ',
        DATE_FORMAT(MAX(IFNULL(t.inspect_time, t.create_time)), '%Y-%m-%d %H:%i:%s')
        )
        END AS inspectTime,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN '已完成'
        ELSE '未完成'
        END AS completeStatus,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN 1
        ELSE 0
        END AS completed,
        CASE
        WHEN COUNT(DISTINCT CASE WHEN t.flow_status = 'APPROVED' THEN t.inspect_phase END) = 3
        THEN '已完成审核'
        ELSE '未审核'
        END AS auditCompleteStatus,
        CASE
        WHEN COUNT(DISTINCT CASE WHEN t.flow_status = 'APPROVED' THEN t.inspect_phase END) = 3
        THEN 1
        ELSE 0
        END AS auditCompleted
        FROM hot_vehicle_three_inspect t
        LEFT JOIN hot_vehicle v ON t.vehicle_id = v.id
        <where>
            t.is_deleted = 0
            AND t.inspect_id IS NOT NULL
            <if test="bo.companyId != null">
                AND t.company_id = #{bo.companyId}
            </if>
            <if test="bo.vehicleId != null">
                AND t.vehicle_id = #{bo.vehicleId}
            </if>
            <if test="bo.driverId != null and bo.driverId != ''">
                AND t.driver_id = #{bo.driverId}
            </if>
            <if test="bo.driverName != null and bo.driverName != ''">
                AND t.driver_name LIKE CONCAT('%', #{bo.driverName}, '%')
            </if>
            <if test="bo.inspectId != null">
                AND t.inspect_id = #{bo.inspectId}
            </if>
            <if test="bo.inspectNo != null and bo.inspectNo != ''">
                AND t.inspect_no = #{bo.inspectNo}
            </if>
            <if test="bo.params != null and bo.params.beginTime != null and bo.params.beginTime != '' and bo.params.endTime != null and bo.params.endTime != ''">
                AND EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.inspect_id = t.inspect_id
                AND t2.is_deleted = 0
                AND t2.inspect_time BETWEEN #{bo.params.beginTime} AND #{bo.params.endTime}
                )
            </if>
            <if test="bo.params != null and bo.params.beginTime != null and bo.params.beginTime != '' and (bo.params.endTime == null or bo.params.endTime == '')">
                AND EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.inspect_id = t.inspect_id
                AND t2.is_deleted = 0
                AND t2.inspect_time <![CDATA[>=]]> #{bo.params.beginTime}
                )
            </if>
            <if test="bo.params != null and (bo.params.beginTime == null or bo.params.beginTime == '') and bo.params.endTime != null and bo.params.endTime != ''">
                AND EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.inspect_id = t.inspect_id
                AND t2.is_deleted = 0
                AND t2.inspect_time <![CDATA[<=]]> #{bo.params.endTime}
                )
            </if>
        </where>
        GROUP BY t.inspect_id
        ORDER BY IFNULL(MAX(t.inspect_time), MAX(t.create_time)) DESC, MAX(t.create_time) DESC
    </select>

    <select id="querySummaryByInspectId"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectSummaryVo">
        SELECT
        t.inspect_id AS inspectId,
        MAX(t.inspect_no) AS inspectNo,
        MAX(t.company_id) AS companyId,
        MAX(t.vehicle_id) AS vehicleId,
        MAX(v.plate_number) AS plateNumber,
        MAX(t.driver_id) AS driverId,
        MAX(t.driver_name) AS driverName,
        MAX(t.driver_name) AS inspectorName,
        MAX(t.auditor_name) AS auditorName,
        MIN(IFNULL(t.inspect_time, t.create_time)) AS startTime,
        MAX(IFNULL(t.inspect_time, t.create_time)) AS endTime,
        CASE
        WHEN MIN(IFNULL(t.inspect_time, t.create_time)) = MAX(IFNULL(t.inspect_time, t.create_time))
        THEN DATE_FORMAT(MIN(IFNULL(t.inspect_time, t.create_time)), '%Y-%m-%d %H:%i:%s')
        ELSE CONCAT(
        DATE_FORMAT(MIN(IFNULL(t.inspect_time, t.create_time)), '%Y-%m-%d %H:%i:%s'),
        ' ~ ',
        DATE_FORMAT(MAX(IFNULL(t.inspect_time, t.create_time)), '%Y-%m-%d %H:%i:%s')
        )
        END AS inspectTime,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN '已完成'
        ELSE '未完成'
        END AS completeStatus,
        CASE
        WHEN SUM(CASE WHEN t.inspect_phase = 3 THEN 1 ELSE 0 END) > 0 THEN 1
        ELSE 0
        END AS completed,
        CASE
        WHEN COUNT(DISTINCT CASE WHEN t.flow_status = 'APPROVED' THEN t.inspect_phase END) = 3
        THEN '已完成审核'
        ELSE '未审核'
        END AS auditCompleteStatus,
        CASE
        WHEN COUNT(DISTINCT CASE WHEN t.flow_status = 'APPROVED' THEN t.inspect_phase END) = 3
        THEN 1
        ELSE 0
        END AS auditCompleted
        FROM hot_vehicle_three_inspect t
        LEFT JOIN hot_vehicle v ON t.vehicle_id = v.id
        WHERE t.is_deleted = 0
        AND t.inspect_id = #{inspectId}
        GROUP BY t.inspect_id
    </select>

    <select id="selectPrintList"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectPrintVo">
        WITH base AS (
        SELECT
        t1.inspect_id,
        t1.inspect_no,
        t1.create_time,
        v.plate_number,
        t1.driver_name,
        t1.auditor_name,
        t1.inspect_phase,
        t1.inspect_location,
        IFNULL(t1.inspect_time, t1.create_time) AS inspect_time
        FROM hot_vehicle_three_inspect t1
        LEFT JOIN hot_vehicle v ON t1.vehicle_id = v.id
        WHERE t1.is_deleted = 0
        AND t1.inspect_id IS NOT NULL
        <if test="bo.companyId != null">
            AND t1.company_id = #{bo.companyId}
        </if>
        <if test="bo.ids != null and bo.ids != ''">
            AND FIND_IN_SET(t1.inspect_id, #{bo.ids})
        </if>
        <if test="(bo.ids == null or bo.ids == '') and bo.params.inspectIds != null and bo.params.inspectIds != ''">
            AND FIND_IN_SET(t1.inspect_id, #{bo.params.inspectIds})
        </if>
        <if test="bo.params != null and bo.params.beginTime != null and bo.params.beginTime != '' and bo.params.endTime != null and bo.params.endTime != ''">
            AND EXISTS (
            SELECT 1 FROM hot_vehicle_three_inspect t2
            WHERE t2.inspect_id = t1.inspect_id
            AND t2.inspect_time BETWEEN #{bo.params.beginTime} AND #{bo.params.endTime}
            )
        </if>
        <if test="bo.params != null and bo.params.beginTime != null and bo.params.beginTime != '' and (bo.params.endTime == null or bo.params.endTime == '')">
            AND EXISTS (
            SELECT 1 FROM hot_vehicle_three_inspect t2
            WHERE t2.inspect_id = t1.inspect_id
            AND t2.inspect_time <![CDATA[>=]]> #{bo.params.beginTime}
            )
        </if>
        <if test="bo.params != null and (bo.params.beginTime == null or bo.params.beginTime == '') and bo.params.endTime != null and bo.params.endTime != ''">
            AND EXISTS (
            SELECT 1 FROM hot_vehicle_three_inspect t2
            WHERE t2.inspect_id = t1.inspect_id
            AND t2.inspect_time <![CDATA[<=]]> #{bo.params.endTime}
            )
        </if>
        )
        SELECT
        MAX(inspect_no) as inspectNo,
        MAX(plate_number) as plateNumber,
        MAX(driver_name) as inspectorName,
        MAX(auditor_name) as auditorName,
        DATE_FORMAT(MIN(IF(inspect_phase = 1, inspect_time, NULL)), '%Y-%m-%d %H:%i') as startTime,
        DATE_FORMAT(MAX(IF(inspect_phase = 3, inspect_time, NULL)), '%Y-%m-%d %H:%i') as endTime,
        MAX(IF(inspect_phase = 1, inspect_location, NULL)) as outInspectLocation,
        MAX(IF(inspect_phase = 2, inspect_location, NULL)) as drivingInspectLocation,
        MAX(IF(inspect_phase = 3, inspect_location, NULL)) as backInspectLocation
        FROM base
        GROUP BY inspect_id
        ORDER BY MAX(create_time) DESC
    </select>

    <select id="querySummaryDetailList"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectVo">
        SELECT
        t.id,
        t.company_id,
        t.vehicle_id,
        t.driver_id,
        t.driver_name,
        t.inspect_phase,
        t.inspect_id,
        t.inspect_no,
        t.inspect_time,
        t.inspect_location,
        t.auditor_id,
        t.auditor_name,
        t.inspect_result_json,
        t.attach_img_url,
        t.inspector_sign_img_url,
        t.is_deleted,
        t.is_false_report,
        t.has_hidden_danger,
        t.audit_time,
        t.audit_result,
        t.reject_reason,
        t.auditor_sign_img_url,
        t.instance_id,
        t.flow_status,
        t.evaluator_id
        FROM hot_vehicle_three_inspect t
        WHERE t.is_deleted = 0
        AND t.inspect_id IS NOT NULL
        AND t.inspect_id IN
        <foreach collection="inspectIds" item="inspectId" open="(" separator="," close=")">
            #{inspectId}
        </foreach>
        ORDER BY t.inspect_id DESC,
        t.inspect_phase ASC,
        IFNULL(t.inspect_time, t.create_time) DESC,
        t.create_time DESC,
        t.id DESC
    </select>

    <select id="queryUnreceivedVehicles"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectUnreceivedVehicleVo">
        SELECT
        v.id AS vehicleId,
        v.plate_number AS plateNumber,
        t.auditor_name AS auditorName,
        v.vehicle_type AS vehicleType
        FROM hot_vehicle_three_inspect t
        LEFT JOIN hot_vehicle v ON t.vehicle_id = v.id
        <where>
            t.is_deleted = 0
            AND v.is_deleted = 0
            AND t.inspect_phase <![CDATA[<>]]> 3
            AND NOT EXISTS (
            SELECT 1
            FROM hot_vehicle_three_inspect t2
            WHERE t2.vehicle_id = t.vehicle_id
            AND t2.is_deleted = 0
            AND (
            t2.create_time > t.create_time
            OR (t2.create_time = t.create_time AND t2.id > t.id)
            )
            )
            <if test="bo.companyId != null">
                AND t.company_id = #{bo.companyId}
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND v.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
        </where>
        ORDER BY IFNULL(t.inspect_time, t.create_time) DESC, t.create_time DESC, t.id DESC
    </select>

    <select id="queryReceivedVehicles"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectReceivedVehicleVo">
        SELECT
        v.id AS vehicleId,
        v.plate_number AS plateNumber,
        v.vehicle_type AS vehicleType
        FROM hot_vehicle v
        LEFT JOIN hot_vehicle_three_inspect t
            ON t.vehicle_id = v.id
            AND t.is_deleted = 0
            AND NOT EXISTS (
                SELECT 1
                FROM hot_vehicle_three_inspect t2
                WHERE t2.vehicle_id = v.id
                  AND t2.is_deleted = 0
                  AND (
                    t2.create_time > t.create_time
                    OR (t2.create_time = t.create_time AND t2.id > t.id)
                  )
            )
        <where>
            v.is_deleted = 0
            AND (t.id IS NULL OR t.inspect_phase = 3)
            <if test="bo.companyId != null">
                AND v.company_id = #{bo.companyId}
            </if>
            <if test="bo.plateNumber != null and bo.plateNumber != ''">
                AND v.plate_number LIKE CONCAT('%', #{bo.plateNumber}, '%')
            </if>
        </where>
        ORDER BY IFNULL(IFNULL(t.inspect_time, t.create_time), v.create_time) DESC, t.create_time DESC, t.id DESC
    </select>

</mapper>
