<?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="selectPrintList"
            resultType="com.hotwj.platform.config.vehicleThreeInspect.domain.vo.HotVehicleThreeInspectPrintVo">
        WITH base AS (
        SELECT
        t1.inspect_id,
        t1.inspect_phase,
        CASE
        WHEN t1.flow_status IN ('APPROVED', 'REJECTED') THEN '已完结'
        WHEN t1.flow_status = 'SUBMITTED' THEN '待审核'
        ELSE t1.flow_status
        END as flow_status,
        t1.create_time,
        v.plate_number,
        DATE_FORMAT(t1.inspect_time, '%Y-%m-%d %H:%i') as inspect_time_str,
        IF(t1.has_hidden_danger = 1, '是', '否') as hidden_danger_str
        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(plate_number) as plateNumber,

        MAX(IF(inspect_phase = 1, inspect_time_str, NULL)) as startPhaseTime,
        MAX(IF(inspect_phase = 1, flow_status, NULL)) as startPhaseStatus,
        MAX(IF(inspect_phase = 1, hidden_danger_str, NULL)) as startPhaseHiddenDanger,

        MAX(IF(inspect_phase = 2, inspect_time_str, NULL)) as runningPhaseTime,
        MAX(IF(inspect_phase = 2, flow_status, NULL)) as runningPhaseStatus,
        MAX(IF(inspect_phase = 2, hidden_danger_str, NULL)) as runningPhaseHiddenDanger,

        MAX(IF(inspect_phase = 3, inspect_time_str, NULL)) as endPhaseTime,
        MAX(IF(inspect_phase = 3, flow_status, NULL)) as endPhaseStatus,
        MAX(IF(inspect_phase = 3, hidden_danger_str, NULL)) as endPhaseHiddenDanger
        FROM base
        GROUP BY inspect_id
        ORDER BY MAX(create_time) DESC
    </select>

</mapper>
