<?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" />
    </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="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">
        <include refid="Base_SQL"/>
        -- 已结算的
        <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.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 #{map.searchTimeArray[0]} and #{map.searchTimeArray[1]}
        </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>
        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>
        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 #{map.searchTimeArray[0]} and #{map.searchTimeArray[1]})
        </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.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'
        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 #{map.searchTimeArray[0]} and #{map.searchTimeArray[1]})
        </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" index="index" 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" 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>
        <if test="map.userIds != null and map.userIds.size > 0">
            -- 维修工或维修厂长查所有的就是维修人是自己的或者是自己班组内的 --
            AND (
            <foreach item="item" collection="map.userIds" index="index" open="" separator="or" close="">
                find_in_set(#{item}, drti.repair_ids) > 0
            </foreach>
            )
        </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>
</mapper>
