<?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.tickets.mapper.DlRepairTicketsMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.tickets.vo.DlRepairTicketsRespVO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="ticketNo" column="ticket_no" jdbcType="VARCHAR"/>
        <result property="repairType" column="repair_type" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="VARCHAR"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userMobile" column="user_mobile" jdbcType="VARCHAR"/>
        <result property="carId" column="car_id" jdbcType="VARCHAR"/>
        <result property="carNo" column="car_no" jdbcType="VARCHAR"/>
        <result property="carVin" column="car_vin" jdbcType="VARCHAR"/>
        <result property="carBrandId" column="car_brand_id" jdbcType="VARCHAR"/>
        <result property="carBrandName" column="car_brand_name" jdbcType="VARCHAR"/>
        <result property="carBrandType" column="car_brand_type" jdbcType="VARCHAR"/>
        <result property="busiFrom" column="busi_from" jdbcType="VARCHAR"/>
        <result property="adviserId" column="adviser_id" jdbcType="VARCHAR"/>
        <result property="adviserName" column="adviser_name" jdbcType="VARCHAR"/>
        <result property="payType" column="pay_type" jdbcType="VARCHAR"/>
        <result property="repairAdvice" column="repair_advice" jdbcType="VARCHAR"/>
        <result property="qualityMileage" column="quality_mileage" jdbcType="VARCHAR"/>
        <result property="qualityDay" column="quality_day" jdbcType="VARCHAR"/>
        <result property="endCheck" column="end_check" jdbcType="VARCHAR"/>
        <result property="partDisposal" column="part_disposal" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="ticketType" column="ticket_type" jdbcType="VARCHAR"/>
        <result property="corpId" column="corp_id" jdbcType="VARCHAR"/>
        <result property="deptId" column="dept_id" jdbcType="BIGINT"/>
        <result property="count" column="count"/>
        <result property="projectPrice" column="project_price"/>
        <result property="partPrice" column="part_price"/>
        <result property="otherPrice" column="other_price"/>
        <result property="totalPrice" column="total_price"/>
        <result property="ticketsStatus" column="tickets_status"/>
        <result property="createTime" column="create_time"/>
        <result property="inTime" column="in_time" />
        <result property="outTime" column="out_time" />
        <result property="cost" column="cost" />
        <result property="profit" column="profit" />
        <result property="partStatus" column="part_status" />
        <result property="ticketsWorkStatus" column="tickets_work_status" />
        <result property="isFinish" column="is_finish" />
        <result property="nowRepairId" column="now_repair_id" />
        <result property="nowRepairName" column="now_repair_name" />
        <result property="partShow" column="part_show" />
        <result property="mileageTraveled" column="mileage_traveled" />
        <result property="threePackUnits" column="three_pack_units" />
        <result property="insuranceName" column="insurance_name" />
        <result property="managerMoney" column="manager_money" />
        <result property="rescueMoney" column="rescue_money" />
        <result property="threePackMoney" column="three_pack_money" />
        <result property="confirmFaultMoney" column="confirm_fault_money" />
        <result property="billingRemark" column="billing_remark" />
        <result property="nextInspectionDate" column="next_inspection_date" />
        <result property="insuranceExpiryDate" column="insurance_expiry_date" />
        <result property="insuranceType" column="insurance_type" />
        <result property="jiaoqiang" column="jiaoqiang" />
        <result property="shangye" column="shangye" />
        <result property="payStatus" column="pay_status" />
        <result property="settlementStr" column="settlementStr"/>
    </resultMap>

    <resultMap id="APPBaseResultMap" type="cn.iocoder.yudao.module.tickets.vo.DlRepairTicketsRespVO">
        <id property="id" column="id" jdbcType="VARCHAR"/>
        <result property="ticketNo" column="ticket_no" jdbcType="VARCHAR"/>
        <result property="repairType" column="repair_type" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="VARCHAR"/>
        <result property="userName" column="user_name" jdbcType="VARCHAR"/>
        <result property="userMobile" column="user_mobile" jdbcType="VARCHAR"/>
        <result property="carId" column="car_id" jdbcType="VARCHAR"/>
        <result property="carNo" column="car_no" jdbcType="VARCHAR"/>
        <result property="carVin" column="car_vin" jdbcType="VARCHAR"/>
        <result property="carBrandId" column="car_brand_id" jdbcType="VARCHAR"/>
        <result property="carBrandName" column="car_brand_name" jdbcType="VARCHAR"/>
        <result property="carBrandType" column="car_brand_type" jdbcType="VARCHAR"/>
        <result property="busiFrom" column="busi_from" jdbcType="VARCHAR"/>
        <result property="adviserId" column="adviser_id" jdbcType="VARCHAR"/>
        <result property="adviserName" column="adviser_name" jdbcType="VARCHAR"/>
        <result property="payType" column="pay_type" jdbcType="VARCHAR"/>
        <result property="repairAdvice" column="repair_advice" jdbcType="VARCHAR"/>
        <result property="qualityMileage" column="quality_mileage" jdbcType="VARCHAR"/>
        <result property="qualityDay" column="quality_day" jdbcType="VARCHAR"/>
        <result property="endCheck" column="end_check" jdbcType="VARCHAR"/>
        <result property="partDisposal" column="part_disposal" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="ticketType" column="ticket_type" jdbcType="VARCHAR"/>
        <result property="corpId" column="corp_id" jdbcType="VARCHAR"/>
        <result property="deptId" column="dept_id" jdbcType="BIGINT"/>
        <result property="count" column="count"/>
        <result property="projectPrice" column="project_price"/>
        <result property="partPrice" column="part_price"/>
        <result property="otherPrice" column="other_price"/>
        <result property="totalPrice" column="total_price"/>
        <result property="ticketsStatus" column="tickets_status"/>
        <result property="createTime" column="create_time"/>
        <result property="inTime" column="in_time" />
        <result property="outTime" column="out_time" />
        <result property="cost" column="cost" />
        <result property="profit" column="profit" />
        <result property="partStatus" column="part_status" />
        <result property="ticketsWorkStatus" column="tickets_work_status" />
        <result property="isFinish" column="is_finish" />
        <result property="isPickCar" column="is_pick_car" />
        <result property="nowRepairId" column="now_repair_id" />
        <result property="nowRepairName" column="now_repair_name" />
        <result property="partShow" column="part_show" />
        <result property="mileageTraveled" column="mileage_traveled" />
        <result property="threePackUnits" column="three_pack_units" />
        <result property="insuranceName" column="insurance_name" />
        <result property="managerMoney" column="manager_money" />
        <result property="rescueMoney" column="rescue_money" />
        <result property="threePackMoney" column="three_pack_money" />
        <result property="confirmFaultMoney" column="confirm_fault_money" />
        <result property="billingRemark" column="billing_remark" />
        <result property="nextInspectionDate" column="next_inspection_date" />
        <result property="insuranceExpiryDate" column="insurance_expiry_date" />
        <result property="insuranceType" column="insurance_type" />
        <result property="jiaoqiang" column="jiaoqiang" />
        <result property="shangye" column="shangye" />
        <result property="canOperate" column="can_operate" />
        <result property="handleName" column="handle_name" />
        <result property="handleMobile" column="handle_mobile" />
        <association property="booking" javaType="cn.iocoder.yudao.module.booking.entity.DlRepairBooking" select="selectBookingById" column="id"/>
        <collection property="itemList" column="id" ofType="cn.iocoder.yudao.module.tickets.entity.DlRepairTitem" select="selectItemList" />
    </resultMap>

    <sql id="Base_SQL">
        select id,
               ticket_no,
               repair_type,
               user_id,
               user_name,
               user_mobile,
               car_id,
               car_no,
               car_vin,
               car_brand_id,
               car_brand_name,
               car_brand_type,
               busi_from,
               adviser_id,
               adviser_name,
               pay_type,
               repair_advice,
               quality_mileage,
               quality_day,
               end_check,
               part_disposal,
               remark,
               ticket_type,
               corp_id,
               dept_id,
               count,
               project_price,
               part_price,
               other_price,
               total_price,
               tickets_status,
               create_time,
               in_time,
               out_time,
               cost,
               profit,
               part_status,
               tickets_work_status,
               is_finish,
               now_repair_id,
               now_repair_name,
               part_show,
               mileage_traveled,
               three_pack_units,
               insurance_name,
               manager_money,
               rescue_money,
               three_pack_money,
               confirm_fault_money,
               billing_remark,
               handle_name,
               handle_mobile,
               next_inspection_date,
               insurance_expiry_date,
               insurance_type,
               jiaoqiang,
               shangye
        from dl_repair_tickets drt
        where drt.deleted = '0'
    </sql>
    <select id="selectBookingById" resultType="cn.iocoder.yudao.module.booking.entity.DlRepairBooking">
        SELECT * FROM dl_repair_booking WHERE tickets_id = #{id}
    </select>
    <select id="getTicketsPage" resultMap="BaseResultMap">
        SELECT drt.*
        <if test="map.payStatus != null and map.payStatus != '' and map.payStatus == '01'">
            ,drr.other_data AS settlementStr
        </if>
        FROM dl_repair_tickets drt
        <if test="map.payStatus != null and map.payStatus != '' and map.payStatus == '01'">
            LEFT JOIN dl_repair_records drr ON drt.id = drr.ticket_id AND drr.type = 'jssq'
        </if>
        <where>

            <!-- 已结算的 -->
        <if test="map.ticketsStatus != null and map.ticketsStatus != '' and map.ticketsStatus == '02'">
            and drt.tickets_status in ('08', #{map.ticketsStatus})
        </if>
            <!-- 已作废的 -->
        <if test="map.ticketsStatus != null and map.ticketsStatus != '' and map.ticketsStatus == '03'">
            and drt.tickets_status = #{map.ticketsStatus}
        </if>
            <!-- 待结算的 -->
        <if test="map.ticketsStatus != null and map.ticketsStatus != '' and map.ticketsStatus == '01'">
            and (drt.tickets_status = #{map.ticketsStatus})
        </if>
        <if test="map.payStatus != null and map.payStatus != ''">
            and drt.pay_status = #{map.payStatus}
        </if>
        <if test="map.ticketNo != null and map.ticketNo != ''">
            and (
            drt.ticket_no like concat('%', #{map.ticketNo}, '%')
            or
            drt.car_no like concat('%', #{map.ticketNo}, '%')
            or
            drt.user_name like concat('%', #{map.ticketNo}, '%')
            or
            drt.user_mobile like concat('%', #{map.ticketNo}, '%')
            or
            drt.remark like concat('%', #{map.ticketNo}, '%')
            )
        </if>
        <if test="map.searchTimeArray != null and map.searchTimeArray.length > 0">
            AND (drt.create_time BETWEEN CONCAT(#{map.searchTimeArray[0]}, ' 00:00:00') AND CONCAT(#{map.searchTimeArray[1]}, '  23:59:59'))
        </if>
        <if test="map.repairType != null and map.repairType != ''">
            and drt.repair_type = #{map.repairType}
        </if>
        <if test="map.corpId != null and map.corpId != ''">
            and drt.corp_id = #{map.corpId}
        </if>
        <if test="map.userId != null and map.userId != ''">
            and drt.user_id = #{map.userId}
        </if>
        <if test="map.ticketsWorkStatus != null and map.ticketsWorkStatus != ''">
            and drt.tickets_work_status = #{map.ticketsWorkStatus}
        </if>
        </where>
        order by drt.create_time desc
    </select>

    <select id="getPageType" resultMap="APPBaseResultMap">
        -- 查待处理数据 --
        select drt.*
        <if test="map.roleCode=='repair_staff'">
            -- 维修工，需要判断出当前用户是否可以重新派工 --
            , IF(FIND_IN_SET(drt.now_repair_id, #{map.userIdsStr}) > 0,true,false) AS can_operate
        </if>
        from dl_repair_tickets drt
        <if test="map.cusFrom != null and map.cusFrom!=''">
            -- 按客户来源查，需要关联客户表 --
            left join base_customer_main bcm ON drt.user_id = bcm.id
        </if>
        <if test="map.roleCode=='repair_staff'">
            -- 维修工需要关联操作记录，查没有总检记录的工单 --
            left join dl_repair_records drr ON drt.id = drr.ticket_id AND drr.type='zj'
        </if>
        <if test=" map.statisticsType == 'yjg'">
            -- 维修工需要关联操作记录，查没有总检记录的工单 --
            inner join dl_repair_records drr ON drt.id = drr.ticket_id AND drr.type='zj'
        </if>
        left join dl_repair_titem drti
        on drt.id = drti.ticket_id AND drti.deleted = '0'
        where (drt.deleted = '0') AND drt.tickets_status IN ('04','05','01','07','06','02')
        <if test="map.ticketNo != null and map.ticketNo != ''">
            and (
            drt.ticket_no like concat('%', #{map.ticketNo}, '%')
            or
            drt.car_no like concat('%', #{map.ticketNo}, '%')
            or
            drt.user_name like concat('%', #{map.ticketNo}, '%')
            or
            drt.user_mobile like concat('%', #{map.ticketNo}, '%')
            or
            drt.remark like concat('%', #{map.ticketNo}, '%')
            )
        </if>
        <if test="map.searchTimeArray != null and map.searchTimeArray.length > 0">
            AND (drt.create_time BETWEEN CONCAT(#{map.searchTimeArray[0]}, ' 00:00:00') AND CONCAT(#{map.searchTimeArray[1]}, ' 23:59:59'))
        </if>
        <if test="map.repairType !=null and map.repairType !=''">
            AND (drt.repair_type=#{map.repairType})
        </if>
        <if test="map.roleCode=='repair_staff'">
            -- 维修工需要关联操作记录，查没有总检记录的工单 --
            AND (drr.id IS NULL)
        </if>
        <if test="map.statisticsType != null and map.statisticsType != ''">
            -- 根据统计类型查询 --
            <if test="map.statisticsType == 'wxz'">
                -- 维修中 --
                AND drt.tickets_status = '05'
            </if>
            <if test="map.statisticsType == 'wjs'">
                -- 未结算 --
                AND drt.tickets_status in ('04','05','07','01')
            </if>
            <if test="map.statisticsType == 'zc'">
                -- 在厂 --
                AND drt.is_handover = '0' AND drt.tickets_status != '03'
            </if>
            <if test="map.statisticsType == 'yjg'">
                -- 已竣工 --
            </if>
        </if>
        <if test="map.cusFrom != null and map.cusFrom!=''">
            <choose>
                <when test="map.cusFrom == '06'">
                    -- 公务车，查政企客户 --
                    AND (bcm.type_code='03')
                </when>
                <when test="map.cusFrom == '04'">
                    -- 企业用车，查代办客户 --
                    AND (bcm.type_code='02')
                </when>
                <otherwise>
                    -- 家庭自用车01、客户转介绍02、员工转介绍03、救援转维修05 --
                    AND (bcm.inviter_type=#{map.cusFrom})
                </otherwise>
            </choose>
        </if>
        <choose>
            <when test="map.selectType=='special'">
                -- 小李用的逻辑--isFinish不需要了 --
                <!-- <if test="map.isFinish != null and map.isFinish != ''">
                    AND ( drt.is_finish = #{map.isFinish})
                </if> -->
                -- 总检查待处理还是用这个 --
                <if test="map.nowRepairId != null and map.nowRepairId != ''">
                    AND ( drt.now_repair_id = #{map.nowRepairId})
                </if>
                -- 服务顾问查待处理（服务顾问之间可以相互查） --
                <if test="map.nowRepairIds != null and map.nowRepairIds.size > 0">
                    AND (
                    drt.now_repair_id in
                    <foreach collection="map.nowRepairIds" item="id" open="(" separator="," close=")">
                        #{id}
                    </foreach>
                    )
                </if>
            </when>
            <otherwise>
                -- 正常查询用的逻辑 --
                <choose>
                    <when test="map.roleCode=='repair_staff'">
                        -- 维修工的查询逻辑,涉及维修项目的维修工是自己或者本班组内的--
                        AND (
                        <foreach item="item" collection="map.userIds" index="index" open="" separator="or" close="">
                            find_in_set(#{item}, drti.repair_ids) > 0
                        </foreach>
                        )
                    </when>
                    <otherwise>
                        -- 服务顾问和仓管查待办都是查未结束的工单 --
                        AND drt.is_finish = '0'
                        <if test="map.adviserId != null and map.adviserId != ''">
                            -- 查服务顾问 当前处理人或服务顾问是自己的--
                            AND ( drt.adviser_id = #{map.adviserId} OR drt.now_repair_id = #{map.adviserId})
                        </if>
                        <if test="map.userIds != null and map.userIds.size > 0">
                            -- 查总检待处理的 --
                            AND (drt.is_finish = '0')
                            AND (drt.now_repair_id in
                            <foreach collection="map.userIds" item="item" index="index" open="(" separator=","
                                     close=")">
                                #{item}
                            </foreach>
                            )
                        </if>
                    </otherwise>
                </choose>
            </otherwise>
        </choose>
        GROUP BY drt.id
        order by drt.update_time desc
    </select>

    <select id="selectItemList" resultType="cn.iocoder.yudao.module.tickets.entity.DlRepairTitem">
        select drti.id ,
               drti.ticket_id ,
               drti.item_name ,
               drti.item_count ,
               drti.item_unit ,
               drti.item_price ,
               drti.item_discount ,
               drti.item_money ,
               drti.repair_ids ,
               drti.repair_names ,
               drti.sale_id ,
               drti.sale_name ,
               drti.item_type ,
               drti.project_id ,
               drti.part_id ,
               drti.other_id ,
               drti.item_type_id ,
               drti.item_status ,
               drti.remark
        from dl_repair_titem drti
        where drti.ticket_id = #{id} AND drti.deleted = '0'
    </select>

    <select id="getPageTypeAll" resultMap="APPBaseResultMap">
        SELECT drt.*
        FROM dl_repair_tickets drt
        <if test="map.cusFrom != null and map.cusFrom!=''">
            -- 按客户来源查，需要关联客户表 --
            LEFT JOIN base_customer_main bcm ON drt.user_id = bcm.id
        </if>
        LEFT JOIN dl_repair_titem drti
        ON drt.id = drti.ticket_id AND drti.deleted = '0' AND drti.item_type='01'
        LEFT JOIN dl_repair_worker drw
        ON FIND_IN_SET(drw.user_id, drti.repair_ids) > 0 AND drw.deleted = '0'
        WHERE drt.deleted = '0' AND tickets_status!='03'

        <!-- 模糊搜索 -->
        <if test="map.ticketNo != null and map.ticketNo != ''">
            AND (
            drt.ticket_no LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.car_no LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.user_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.user_mobile LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.remark LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.car_brand_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.handle_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drti.item_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            )
        </if>

        <!-- 时间区间 -->
        <if test="map.searchTimeArray != null and map.searchTimeArray.length > 0">
            AND (drt.create_time BETWEEN CONCAT(#{map.searchTimeArray[0]}, ' 00:00:00') AND CONCAT(#{map.searchTimeArray[1]}, ' 23:59:59'))
        </if>
        <if test="map.startDate != null and map.startDate != ''">
            AND (drt.create_time &gt;= #{map.startDate} AND drt.create_time &lt;= #{map.endDate})
        </if>

        <!-- repairType -->
        <if test="map.repairType !=null and map.repairType !=''">
            AND drt.repair_type = #{map.repairType}
        </if>

        <!-- 状态列表 -->
        <if test="map.statusList !=null and map.statusList.size > 0">
            AND drt.tickets_status IN
            <foreach collection="map.statusList" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <!-- id 列表 -->
        <if test="map.idList !=null and map.idList.size > 0">
            AND drt.id IN
            <foreach collection="map.idList" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <!-- 客户来源 -->
        <if test="map.cusFrom != null and map.cusFrom!=''">
            AND bcm.data_from = #{map.cusFrom}
        </if>

        <!-- 服务顾问 -->
        <if test="map.adviserId != null and map.adviserId != ''">
            AND drt.adviser_id = #{map.adviserId}
        </if>

        <!-- 是否交车 -->
        <if test="map.isHandover != null and map.isHandover != ''">
            AND drt.is_handover = #{map.isHandover}
        </if>

        <!-- 工人 ID -->
        <if test="map.userIds != null and map.userIds.size > 0">
            AND (
            <foreach item="item" collection="map.userIds" index="index" separator="or">
                FIND_IN_SET(#{item}, drti.repair_ids) > 0
            </foreach>
            )
        </if>

        <!-- 按工种筛选 -->
        <if test="map.workType != null and map.workType != ''">
            AND drw.work_type = #{map.workType}
        </if>

        <!-- 统计类型 -->
        <if test="map.statisticsType != null and map.statisticsType != ''">
            <if test="map.statisticsType == 'wxz'">
                AND drt.tickets_status = '05'
            </if>
            <if test="map.statisticsType == 'wjs'">
                AND drt.tickets_status IN ('04','05','07','01')
            </if>
            <if test="map.statisticsType == 'zc'">
                AND drt.is_handover = '0' AND drt.tickets_status != '03'
            </if>
        </if>

        GROUP BY drt.id
        ORDER BY drt.update_time DESC
    </select>


    <select id="selectTicketIdByParams" resultType="java.lang.String">
        SELECT
        DISTINCT drt.id
        FROM
        dl_repair_tickets drt
        LEFT JOIN dl_repair_records drr ON drt.id = drr.ticket_id
        AND drr.deleted = '0'
        WHERE
        drt.deleted = '0'
        AND drt.tickets_status !='03'
        AND drr.type = #{recordCode}
        <if test="nowDate!=null and nowDate!=''">
            AND drr.create_time LIKE CONCAT(#{nowDate},'%')
        </if>
        <if test="startTime!=null">
            AND drr.create_time &gt;= #{startTime}
            AND drr.create_time &lt;= #{endTime}
        </if>
    </select>
    <select id="getStatistics" resultType="java.util.Map"
            parameterType="cn.iocoder.yudao.module.tickets.vo.DlRepairTicketsReqVO">
        SELECT
        -- 配件毛利
        COALESCE(SUM(CASE WHEN drti.item_type = '02' THEN drti.item_profit ELSE 0 END), 0) AS totalProfit,

        -- 工单总金额 (产值)
        COALESCE(SUM(drt.total_price), 0) AS totalOutput,

        -- 配件金额
        COALESCE(SUM(CASE WHEN drti.item_type = '02' THEN drti.item_money ELSE 0 END), 0) AS totalPartsMoney,

        -- 工时+配件金额
        COALESCE(SUM(CASE WHEN drti.item_type IN ('01','02') THEN drti.item_money ELSE 0 END), 0) AS totalLaborPartsMoney,

        -- 含工时毛利率 = 配件毛利 / (工时金额+配件金额)
        CASE
        WHEN SUM(CASE WHEN drti.item_type IN ('01','02') THEN drti.item_money ELSE 0 END) = 0 THEN 0
        ELSE ROUND(SUM(CASE WHEN drti.item_type = '02' THEN drti.item_profit ELSE 0 END)
        / SUM(CASE WHEN drti.item_type IN ('01','02') THEN drti.item_money ELSE 0 END), 4)
        END AS profitRateWithLabor,

        -- 不含工时毛利率 = 配件毛利 / 配件金额
        CASE
        WHEN SUM(CASE WHEN drti.item_type = '02' THEN drti.item_money ELSE 0 END) = 0 THEN 0
        ELSE ROUND(SUM(CASE WHEN drti.item_type = '02' THEN drti.item_profit ELSE 0 END)
        / SUM(CASE WHEN drti.item_type = '02' THEN drti.item_money ELSE 0 END), 4)
        END AS profitRateWithoutLabor
        FROM dl_repair_tickets drt
        LEFT JOIN dl_repair_titem drti
        ON drt.id = drti.ticket_id
        AND drti.deleted = '0'
        <if test="map.cusFrom != null and map.cusFrom!=''">
            LEFT JOIN base_customer_main bcm ON drt.user_id = bcm.id
        </if>
        LEFT JOIN dl_repair_worker drw
        ON FIND_IN_SET(drw.user_id, drti.repair_ids) > 0 AND drw.deleted = '0'
        WHERE drt.deleted = '0'
        AND drt.tickets_status != '03'

        <!-- 以下保持和分页查询一致的条件 -->
        <if test="map.ticketNo != null and map.ticketNo != ''">
            AND (
            drt.ticket_no LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.car_no LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.user_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.user_mobile LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.remark LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.car_brand_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drt.handle_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            OR drti.item_name LIKE CONCAT('%', #{map.ticketNo}, '%')
            )
        </if>

        <if test="map.searchTimeArray != null and map.searchTimeArray.length > 0">
            AND (drt.create_time BETWEEN CONCAT(#{map.searchTimeArray[0]}, ' 00:00:00') AND CONCAT(#{map.searchTimeArray[1]}, ' 23:59:59'))
        </if>
        <if test="map.startDate != null and map.startDate != ''">
            AND (drt.create_time &gt;= #{map.startDate} AND drt.create_time &lt;= #{map.endDate})
        </if>

        <if test="map.repairType !=null and map.repairType !=''">
            AND drt.repair_type = #{map.repairType}
        </if>

        <if test="map.statusList !=null and map.statusList.size > 0">
            AND drt.tickets_status IN
            <foreach collection="map.statusList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <if test="map.idList !=null and map.idList.size > 0">
            AND drt.id IN
            <foreach collection="map.idList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

        <if test="map.cusFrom != null and map.cusFrom!=''">
            AND bcm.data_from = #{map.cusFrom}
        </if>

        <if test="map.adviserId != null and map.adviserId != ''">
            AND drt.adviser_id = #{map.adviserId}
        </if>

        <if test="map.isHandover != null and map.isHandover != ''">
            AND drt.is_handover = #{map.isHandover}
        </if>

        <if test="map.userIds != null and map.userIds.size > 0">
            AND (
            <foreach item="item" collection="map.userIds" separator="or">
                FIND_IN_SET(#{item}, drti.repair_ids) > 0
            </foreach>
            )
        </if>

        <if test="map.workType != null and map.workType != ''">
            AND drw.work_type = #{map.workType}
        </if>

        <if test="map.statisticsType != null and map.statisticsType != ''">
            <if test="map.statisticsType == 'wxz'">
                AND drt.tickets_status = '05'
            </if>
            <if test="map.statisticsType == 'wjs'">
                AND drt.tickets_status IN ('04','05','07','01')
            </if>
            <if test="map.statisticsType == 'zc'">
                AND drt.is_handover = '0' AND drt.tickets_status != '03'
            </if>
        </if>
    </select>
    <select id="selectSumAmount">
        select drr.other_data as otherData, drt.pay_status as payType
        FROM dl_repair_tickets drt
        JOIN dl_repair_records drr ON drt.id = drr.ticket_id
        <where>drt.deleted = 0 and drr.deleted = 0 and drr.type = 'jssp'
            <if test="ew != null and ew.customSqlSegment != null and ew.customSqlSegment != ''">
                AND   ${ew.sqlSegment}
            </if>
        </where>
    </select>
    <select id="selectTicketIdByParamsNew" resultType="java.util.Map">
        SELECT ${ew.sqlSelect}
        FROM dl_repair_tickets drt
        LEFT JOIN dl_repair_records drr ON drt.id = drr.ticket_id
       where 1=1
        <if test="ew != null and ew.customSqlSegment != null and ew.customSqlSegment != ''">
            AND ${ew.sqlSegment}
        </if>
    </select>
</mapper>
