<?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="cn.iocoder.yudao.module.inspection.mapper.InspectionInfoMapper">

    <resultMap type="cn.iocoder.yudao.module.inspection.entity.InspectionInfo" id="InspectionInfoResult">
        <result property="id"    column="id"    />
        <result property="inspectionOrderId"    column="inspection_order_id"    />
        <result property="workId"    column="work_id"    />
        <result property="workerName"    column="worker_name"    />
        <result property="workerPhone"    column="worker_phone"    />
        <result property="isPass"    column="is_pass"    />
        <result property="status"    column="status"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="year"    column="year"    />
        <result property="month"    column="month"    />
        <result property="day"    column="day"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="creator"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="updater"    />
    </resultMap>

    <sql id="selectInspectionInfoVo">
        select id,buy_name,buy_phone,user_address, inspection_order_id, work_id, worker_name, worker_phone, is_pass, status, start_time, end_time, year, month, day, create_time, creator, update_time, updater from inspection_info
    </sql>

    <select id="selectInspectionInfoList" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo" resultMap="InspectionInfoResult">
        <include refid="selectInspectionInfoVo"/>
        <where>
            <if test="vo.inspectionOrderId != null "> and inspection_order_id = #{vo.inspectionOrderId}</if>
            <if test="vo.workId != null "> and work_id = #{vo.workId}</if>
            <if test="vo.workerName != null  and vo.workerName != ''"> and worker_name like concat('%', #{vo.workerName}, '%')</if>
            <if test="vo.workerPhone != null  and vo.workerPhone != ''"> and worker_phone = #{vo.workerPhone}</if>
            <if test="vo.isPass != null  and vo.isPass != ''"> and is_pass = #{vo.isPass}</if>
            <if test="vo.status != null  and vo.status != ''"> and status = #{vo.status}</if>
            <if test="vo.startTime != null "> and start_time = #{vo.startTime}</if>
            <if test="vo.endTime != null "> and end_time = #{vo.endTime}</if>
            <if test="vo.year != null  and vo.year != ''"> and year = #{vo.year}</if>
            <if test="vo.month != null  and vo.month != ''"> and month = #{vo.month}</if>
            <if test="vo.day != null  and vo.day != ''"> and day = #{vo.day}</if>
        </where>
    </select>

    <select id="selectInspectionInfoById" parameterType="Long" resultMap="InspectionInfoResult">
        select info.*,o.sku_name,o.sku_id,o.goods_id
        from inspection_info info
        left join order_info o on info.inspection_order_id = o.id
        where info.id = #{id}
    </select>

    <insert id="insertInspectionInfo" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        insert into inspection_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="inspectionOrderId != null">inspection_order_id,</if>
            <if test="workId != null">work_id,</if>
            <if test="workerName != null">worker_name,</if>
            <if test="workerPhone != null">worker_phone,</if>
            <if test="isPass != null">is_pass,</if>
            <if test="status != null">status,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="year != null">year,</if>
            <if test="month != null">month,</if>
            <if test="day != null">day,</if>
            <if test="createTime != null">create_time,</if>
            <if test="creator != null">creator,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updater != null">updater,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="inspectionOrderId != null">#{inspectionOrderId},</if>
            <if test="workId != null">#{workId},</if>
            <if test="workerName != null">#{workerName},</if>
            <if test="workerPhone != null">#{workerPhone},</if>
            <if test="isPass != null">#{isPass},</if>
            <if test="status != null">#{status},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="year != null">#{year},</if>
            <if test="month != null">#{month},</if>
            <if test="day != null">#{day},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="creator != null">#{creator},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updater != null">#{updater},</if>
         </trim>
    </insert>

    <update id="updateInspectionInfo" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        update inspection_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="inspectionOrderId != null">inspection_order_id = #{inspectionOrderId},</if>
            <if test="workId != null">work_id = #{workId},</if>
            <if test="workerName != null">worker_name = #{workerName},</if>
            <if test="workerPhone != null">worker_phone = #{workerPhone},</if>
            <if test="isPass != null">is_pass = #{isPass},</if>
            <if test="status != null">status = #{status},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="year != null">year = #{year},</if>
            <if test="month != null">month = #{month},</if>
            <if test="day != null">day = #{day},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="creator != null">creator = #{creator},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updater != null">updater = #{updater},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInspectionInfoById" parameterType="Long">
        delete from inspection_info where id = #{id}
    </delete>

    <delete id="deleteInspectionInfoByIds" parameterType="String">
        delete from inspection_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="workOrder" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        select DISTINCT ins.*,oi.goods_title,su.nickname as buyName,su.mobile as buyPhone,oi.sku_name,oi.pay_money as realPayMoney
        ,oi.pay_type,oi.order_status as orderStatus,oi.goods_id,oi.sku_id,oi.pay_time,oi.goods_price,ins.create_time
         from inspection_info ins
         left join order_info oi on oi.id = ins.inspection_order_id
         left join system_users su on su.id = ins.user_id
         left join system_user_role sur on sur.user_id = su.id
         where 1=1
        <if test="carNum!=null and carNum!=''">
            and ins.car_num like concat('%',#{carNum},'%')
        </if>
        <if test="goodsTitle!=null and goodsTitle!=''">
            and oi.goods_title like concat('%',#{goodsTitle},'%')
        </if>
        <if test="roleId!=null and roleId!=''">
            and sur.role_id  = #{roleId}
        </if>

        <if test="customerSource!=null and customerSource!=''">
            and ins.customer_source =#{customerSource}
        </if>
        <if test="payType!=null and payType!=''">
            and oi.pay_type  =  #{payType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and ins.start_time between #{startTime} and #{endTime}
        </if>
        order by  ins.start_time desc
    </select>

    <select id="delworkOrder" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        select ins.*,oi.goods_title,su.nickname as buyName,su.mobile as buyPhone,oi.sku_name,oi.pay_money as realPayMoney
        ,oi.pay_type,oi.order_status as orderStatus,oi.goods_id,oi.sku_id,oi.pay_time,oi.goods_price,ins.create_time
        from del_inspection_info ins
        left join del_order_info oi on oi.id = ins.inspection_order_id
        left join system_users su on su.id = ins.user_id
        left join system_user_role sur on sur.user_id = su.id
        where 1=1
        <if test="carNum!=null and carNum!=''">
            and ins.car_num like concat('%',#{carNum},'%')
        </if>
        <if test="goodsTitle!=null and goodsTitle!=''">
            and oi.goods_title like concat('%',#{goodsTitle},'%')
        </if>
        <if test="roleId!=null and roleId!=''">
            and sur.role_id  = #{roleId}
        </if>

        <if test="customerSource!=null and customerSource!=''">
            and ins.customer_source =#{customerSource}
        </if>
        <if test="payType!=null and payType!=''">
            and oi.pay_type  =  #{payType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and ins.start_time between #{startTime} and #{endTime}
        </if>
        order by  ins.start_time desc
    </select>

    <select id="workOrderData" resultType="java.util.Map">
        select ifnull(sum(oi.pay_money),0) as payMoneySum,ifnull(sum(oi.goods_price),0) goodsPriceSum
        from inspection_info ins
        left join order_info oi on oi.id = ins.inspection_order_id
        left join system_users su on su.id = ins.user_id
        left join system_user_role sur on sur.user_id = su.id
        where 1=1
        <if test="carNum!=null and carNum!=''">
            and ins.car_num like concat('%',#{carNum},'%')
        </if>
        <if test="goodsTitle!=null and goodsTitle!=''">
            and oi.goods_title like concat('%',#{goodsTitle},'%')
        </if>
        <if test="roleId!=null and roleId!=''">
            and sur.role_id  = #{roleId}
        </if>

        <if test="customerSource!=null and customerSource!=''">
            and ins.customer_source =#{customerSource}
        </if>
        <if test="payType!=null and payType!=''">
            and oi.pay_type  =  #{payType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and ins.start_time between #{startTime} and #{endTime}
        </if>
    </select>
    <select id="pageWorkOrder" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        select distinct ins.*,oi.goods_title,su.nickname as buyName,su.mobile as buyPhone,oi.sku_name,oi.pay_money as realPayMoney
        ,oi.pay_type,oi.order_status as orderStatus,oi.goods_id,oi.sku_id,oi.pay_time,oi.goods_price
        from inspection_info ins
        left join order_info oi on oi.id = ins.inspection_order_id
        left join system_users su on su.id = ins.user_id
        left join system_user_role sur on sur.user_id = su.id
        where 1=1
        <if test="carNum!=null and carNum!=''">
            and ins.car_num like concat('%',#{carNum},'%')
        </if>
        <if test="goodsTitle!=null and goodsTitle!=''">
            and oi.goods_title like concat('%',#{goodsTitle},'%')
        </if>
        <if test="roleId!=null and roleId!=''">
            and sur.role_id  = #{roleId}
        </if>

        <if test="customerSource!=null and customerSource!=''">
            and ins.customer_source =#{customerSource}
        </if>
        <if test="payType!=null and payType!=''">
            and oi.pay_type  =  #{payType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and ins.start_time between #{startTime} and #{endTime}
        </if>
        order by  ins.start_time desc
    </select>
    <select id="pageDelWorkOrder" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        select ins.*,oi.goods_title,su.nickname as buyName,su.mobile as buyPhone,oi.sku_name,oi.pay_money as realPayMoney
        ,oi.pay_type,oi.order_status as orderStatus,oi.goods_id,oi.sku_id,oi.pay_time,oi.goods_price,ins.create_time
        from del_inspection_info ins
        left join del_order_info oi on oi.id = ins.inspection_order_id
        left join system_users su on su.id = ins.user_id
        left join system_user_role sur on sur.user_id = su.id
        where 1=1
        <if test="carNum!=null and carNum!=''">
            and ins.car_num like concat('%',#{carNum},'%')
        </if>
        <if test="goodsTitle!=null and goodsTitle!=''">
            and oi.goods_title like concat('%',#{goodsTitle},'%')
        </if>
        <if test="roleId!=null and roleId!=''">
            and sur.role_id  = #{roleId}
        </if>

        <if test="customerSource!=null and customerSource!=''">
            and ins.customer_source =#{customerSource}
        </if>
        <if test="payType!=null and payType!=''">
            and oi.pay_type  =  #{payType}
        </if>
        <if test="startTime!=null and startTime!=''">
            and ins.start_time between #{startTime} and #{endTime}
        </if>
        order by  ins.start_time desc
    </select>
    <select id="selectByUser" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionInfo">
        <if test="inspectionInfo.status != 6 and inspectionInfo.status != 8">
            SELECT *
            FROM (
            SELECT
            ii.*,
            iwn.id AS workNodeId,
            iwn.status AS workNodeStatus,
            oi.order_no AS orderNo,
            oi.phonenumber AS buyPhone,
            oi.goods_title as goodsTitle,
            oi.sku_name as skuName,
            ip.project_name AS projectName,
            su.nickname AS leadManName,
            ROW_NUMBER() OVER (PARTITION BY ii.id ORDER BY iwn.update_time DESC) as rn -- 根据需要调整排序逻辑
            FROM
            inspection_info ii
            LEFT JOIN
            inspection_work_node iwn ON ii.id = iwn.inspection_info_id
            LEFT JOIN
            order_info oi ON ii.inspection_order_id = oi.id
            LEFT JOIN
            inspection_project ip ON iwn.project_id = ip.id
            LEFT JOIN
            system_users su ON ii.lead_man_id = su.id
            <where>
                ii.deleted = 0
                <!-- 车牌号模糊查询 -->
                <if test="inspectionInfo.carNum != null">
                    AND ii.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
                </if>
                <!-- 待支付 -->
                <if test="inspectionInfo.status == '0'.toString()">
                    AND (oi.pay_time is null)
                    ORDER BY iwn.update_time DESC
                </if>
                <!-- 待接受(待检测) -->
                <if test="inspectionInfo.status == 1">
                    AND  <!-- 工单负责人或角色ID匹配 -->
                    (ii.lead_man_id = #{inspectionInfo.leadManId}
                    OR iwn.role_id IN
                    <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                        #{roleId}
                    </foreach>)
                    AND ii.status = '0'
                    AND iwn.status = '0'
                    -- AND ii.now_order_num = iwn.order_num
                    ORDER BY ii.create_time DESC
                </if>
                <!-- 进行中（检测中） -->
                <if test="inspectionInfo.status == 2">
                    AND (ii.status = '0' OR ii.status = '2')
                    AND iwn.status = '1'
                    AND iwn.deal_user_id = #{inspectionInfo.dealUserId}
                    ORDER BY iwn.update_time DESC
                </if>
                <!-- 待重检 -->
                <if test="inspectionInfo.status == 3">
                    AND  <!-- 工单负责人或角色ID匹配 -->
                    (ii.lead_man_id = #{inspectionInfo.leadManId}
                    OR iwn.role_id IN
                    <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                        #{roleId}
                    </foreach>)
                    AND ii.status = '2'
                    -- AND iwn.status = '1'
                    -- AND ii.now_order_num = iwn.order_num
                    ORDER BY ii.update_time DESC
                </if>
                <!-- 待复检 -->
                <if test="inspectionInfo.status == 4">
                    AND  <!-- 工单负责人或角色ID匹配 -->
                    (ii.lead_man_id = #{inspectionInfo.leadManId}
                    OR iwn.role_id IN
                    <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                        #{roleId}
                    </foreach>)
                    -- AND ii.status = '2'
                    AND iwn.status = '3'
                    AND ii.status != '1'
                    -- AND ii.now_order_num = iwn.order_num
                    ORDER BY ii.update_time DESC
                </if>
                <!-- 已完成 -->
                <if test="inspectionInfo.status == 5">
                    AND iwn.status = '2'
                    AND iwn.deal_user_id = #{inspectionInfo.dealUserId}
                    ORDER BY iwn.update_time DESC
                </if>
                <!-- 待接车 -->
                <if test="inspectionInfo.status == 6">
                    AND ii.is_meet_car = '0'
                    AND ii.meet_man_id = #{inspectionInfo.dealUserId}
                    ORDER BY iwn.update_time DESC
                </if>
                <!-- 待审核不合格项目 -->
                <if test="inspectionInfo.status == 7">
                    AND iwn.type = '0'
                    AND ii.status != '1'
                    ORDER BY iwn.update_time DESC
                </if>
                <!-- 新增：lead_man_id 为空 -->
                <if test="inspectionInfo.status == 9">
                    AND ii.lead_man_id IS NULL
                    AND ii.status = '0'
                    ORDER BY ii.update_time DESC
                </if>
            </where>
            ) AS subquery
            WHERE rn = 1;
        </if>
        <!-- 待接车 -->
        <if test="inspectionInfo.status == 6">
            SELECT
            ii.id AS id,
            ii.buy_name,
            ii.user_address,
            ii.car_num,
            ii.car_nature,
            ii.car_model,
            ii.car_id_no,
            ii.car_register_date,
            ii.buy_phone,
            ii.meet_man_id AS meet_id,
            ii.tenant_id,
            ii.create_time,
            null AS meetAddress,
            null AS latitude,
            null AS longitude,
            null AS appointmentDay,
            null AS appointmentTime,
            0 AS source_type
            FROM inspection_info ii
            WHERE ii.deleted = 0 AND ii.meet_man_id = #{inspectionInfo.dealUserId}  AND ii.is_meet_car = '0' AND ii.status != '1'
            <!-- 车牌号模糊查询 -->
            <if test="inspectionInfo.carNum != null">
                AND ii.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
            </if>

            UNION ALL

            SELECT
            imco.id AS id,
            imco.buy_name,
            imco.user_address,
            imco.car_num,
            imco.car_nature,
            imco.car_model,
            imco.car_id_no,
            imco.car_register_date,
            imco.buy_phone,
            imco.meet_man_id,
            imco.tenant_id,
            imco.create_time,
            imco.meet_address AS meetAddress,
            imco.latitude,
            imco.longitude,
            imco.appointment_day,
            imco.appointment_time,
            1 AS source_type
            FROM inspection_meet_car_order imco
            WHERE imco.deleted = 0 AND imco.meet_man_id = #{inspectionInfo.dealUserId} AND imco.is_meet = '0'
            <!-- 车牌号模糊查询 -->
            <if test="inspectionInfo.carNum != null">
                AND imco.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
            </if>

        </if>
        <!-- 接车订单转工单 -->
        <if test="inspectionInfo.status == 8">
            SELECT imco.*
            FROM inspection_meet_car_order imco
            WHERE imco.deleted = 0 AND imco.inspection_info_id IS NULL AND imco.is_meet = '1'
            <!-- 车牌号模糊查询 -->
            <if test="inspectionInfo.carNum != null">
                AND imco.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
            </if>
        </if>
        <!-- 之前的查询，修改为了一个工单只出现一条，不分项目 -->
        <!--    SELECT-->
        <!--        ii.*,-->
        <!--        iwn.id AS workNodeId,-->
        <!--        iwn.status AS workNodeStatus,-->
        <!--        oi.order_no AS orderNo,-->
        <!--        oi.phonenumber AS buyPhone,-->
        <!--        ip.project_name AS projectName,-->
        <!--        su.nickname AS leadManName-->
        <!--    FROM-->
        <!--        inspection_info ii-->
        <!--    LEFT JOIN-->
        <!--        inspection_work_node iwn ON ii.id = iwn.inspection_info_id-->
        <!--    LEFT JOIN-->
        <!--        order_info oi ON ii.inspection_order_id = oi.id-->
        <!--    LEFT JOIN-->
        <!--        inspection_project ip ON iwn.project_id = ip.id-->
        <!--    LEFT JOIN-->
        <!--        system_users su ON ii.lead_man_id = su.id-->
        <!--        <where>-->
        <!--        &lt;!&ndash; 车牌号模糊查询 &ndash;&gt;-->
        <!--        <if test="inspectionInfo.carNum != null">-->
        <!--            AND ii.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')-->
        <!--        </if>-->
        <!--        &lt;!&ndash; 待接受 &ndash;&gt;-->
        <!--        <if test="inspectionInfo.status == 1">-->
        <!--            AND  &lt;!&ndash; 工单负责人或角色ID匹配 &ndash;&gt;-->
        <!--            (ii.lead_man_id = #{inspectionInfo.leadManId}-->
        <!--            OR iwn.role_id IN-->
        <!--            <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">-->
        <!--                #{roleId}-->
        <!--            </foreach>)-->
        <!--            AND ii.status = '0'-->
        <!--            AND iwn.status = '0'-->
        <!--            &#45;&#45; AND ii.now_order_num = iwn.order_num-->
        <!--            ORDER BY ii.create_time DESC-->
        <!--        </if>-->
        <!--        &lt;!&ndash; 进行中 &ndash;&gt;-->
        <!--        <if test="inspectionInfo.status == 2">-->
        <!--            AND ii.status = '0'-->
        <!--            AND iwn.status = '1'-->
        <!--            AND iwn.deal_user_id = #{inspectionInfo.dealUserId}-->
        <!--            ORDER BY iwn.update_time DESC-->
        <!--        </if>-->
        <!--        &lt;!&ndash; 已完成 &ndash;&gt;-->
        <!--        <if test="inspectionInfo.status == 3">-->
        <!--            AND iwn.status = '2'-->
        <!--            AND iwn.deal_user_id = #{inspectionInfo.dealUserId}-->
        <!--            ORDER BY iwn.update_time DESC-->
        <!--        </if>-->
        <!--    </where>-->
    </select>
    <select id="countByUser" resultType="java.lang.Long">
        <if test="inspectionInfo.status != 6 and inspectionInfo.status != 8">
            SELECT COUNT(DISTINCT ii.id)
            FROM inspection_info ii
            LEFT JOIN inspection_work_node iwn ON ii.id = iwn.inspection_info_id
            LEFT JOIN order_info oi ON ii.inspection_order_id = oi.id
            LEFT JOIN inspection_project ip ON iwn.project_id = ip.id
            LEFT JOIN system_users su ON ii.lead_man_id = su.id
            <where>
                ii.deleted = 0
                <!-- 车牌号模糊查询 -->
                <if test="inspectionInfo.carNum != null">
                    AND ii.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
                </if>
                <!-- 待支付 -->
                <if test="inspectionInfo.status == '0'.toString()">
                    AND (oi.pay_time is null)
                </if>
                <!-- 待接受(待检测) -->
                <if test="inspectionInfo.status == 1">
                    AND (ii.lead_man_id = #{inspectionInfo.leadManId}
                    OR iwn.role_id IN
                    <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                        #{roleId}
                    </foreach>)
                    AND ii.status = '0'
                    AND iwn.status = '0'
                </if>
                <!-- 进行中（检测中） -->
                <if test="inspectionInfo.status == 2">
                    AND (ii.status = '0' OR ii.status = '2')
                    AND iwn.status = '1'
                    AND iwn.deal_user_id = #{inspectionInfo.dealUserId}
                </if>
                <!-- 待重检 -->
                <if test="inspectionInfo.status == 3">
                    AND (ii.lead_man_id = #{inspectionInfo.leadManId}
                    OR iwn.role_id IN
                    <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                        #{roleId}
                    </foreach>)
                    AND ii.status = '2'
                </if>
                <!-- 待复检 -->
                <if test="inspectionInfo.status == 4">
                    AND (ii.lead_man_id = #{inspectionInfo.leadManId}
                    OR iwn.role_id IN
                    <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                        #{roleId}
                    </foreach>)
                    AND iwn.status = '3'
                    AND ii.status != '1'
                </if>
                <!-- 已完成 -->
                <if test="inspectionInfo.status == 5">
                    AND iwn.status = '2'
                    AND iwn.deal_user_id = #{inspectionInfo.dealUserId}
                </if>
                <!-- 待审核不合格项目 -->
                <if test="inspectionInfo.status == 7">
                    AND iwn.type = '0'
                    AND ii.status != '1'
                </if>
                <!-- 新增：lead_man_id 为空 -->
                <if test="inspectionInfo.status == 9">
                    AND ii.lead_man_id IS NULL
                    AND ii.status = '0'
                </if>
            </where>
        </if>
        <if test="inspectionInfo.status == 6">
            SELECT COUNT(*) FROM (
            SELECT ii.id
            FROM inspection_info ii
            WHERE ii.deleted = 0
            AND ii.meet_man_id = #{inspectionInfo.dealUserId}
            AND ii.is_meet_car = '0'
            AND ii.status != '1'
            <!-- 车牌号模糊查询 -->
            <if test="inspectionInfo.carNum != null">
                AND ii.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
            </if>

            UNION ALL

            SELECT imco.id
            FROM inspection_meet_car_order imco
            WHERE imco.deleted = 0
            AND imco.meet_man_id = #{inspectionInfo.dealUserId}
            AND imco.is_meet = '0'
            <!-- 车牌号模糊查询 -->
            <if test="inspectionInfo.carNum != null">
                AND imco.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
            </if>
            ) AS combined_results
        </if>
        <!-- 接车订单转工单 -->
        <if test="inspectionInfo.status == 8">
            SELECT COUNT(*)
            FROM inspection_meet_car_order imco
            WHERE imco.deleted = 0 AND imco.inspection_info_id IS NULL AND imco.is_meet = '1'
            <!-- 车牌号模糊查询 -->
            <if test="inspectionInfo.carNum != null">
                AND imco.car_num LIKE CONCAT('%', #{inspectionInfo.carNum}, '%')
            </if>
        </if>
    </select>
</mapper>
