<?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.DlTicketWaresMapper">

    <resultMap id="BaseResultMap" type="cn.iocoder.yudao.module.tickets.entity.DlTicketWares">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="no" column="no" jdbcType="VARCHAR"/>
        <result property="ticketId" column="ticket_id" jdbcType="VARCHAR"/>
        <result property="userId" column="user_id" jdbcType="INTEGER"/>
        <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="licenseNumber" column="license_number" jdbcType="VARCHAR"/>
        <result property="repairId" column="repair_id" jdbcType="INTEGER"/>
        <result property="repairName" column="repair_name" jdbcType="VARCHAR"/>
        <result property="repairWork" column="repair_work" jdbcType="VARCHAR"/>
        <result property="adviserId" column="adviser_id" jdbcType="INTEGER"/>
        <result property="adviserName" column="adviser_name" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="VARCHAR"/>
        <result property="remark" column="remark" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" />
        <result property="images" column="images" />
        <result property="toSafe" column="to_safe" />
        <result property="safeName" column="safe_name" />
        <result property="safeContact" column="safe_contact" />
        <result property="safeMobile" column="safe_mobile" />
    </resultMap>

    <sql id="Base_SQL">
        select
            dtw.id,
            dtw.no,
            dtw.ticket_id,
            dtw.user_id,
            dtw.user_name,
            dtw.user_mobile,
            dtw.car_id,
            dtw.license_number,
            dtw.repair_id,
            dtw.repair_name,
            dtw.repair_work,
            dtw.adviser_id,
            dtw.adviser_name,
            dtw.status,
            dtw.remark,
            dtw.create_time,
            dtw.images,
            dtw.to_safe,
            dtw.safe_name,
            dtw.safe_contact,
            dtw.safe_mobile
        from dl_ticket_wares dtw
        left join dl_repair_tickets drt
        on dtw.ticket_id = drt.id
        where dtw.deleted = '0'
    </sql>

    <select id="getPage" resultMap="BaseResultMap">
        <include refid="Base_SQL"/>
        <if test="map.query != null and map.query != ''">
            and (
            dtw.no like concat('%', #{map.query}, '%')
            or
            drt.ticket_no like concat('%', #{map.query}, '%')
            or
            dtw.license_number like concat('%', #{map.query}, '%')
            )
        </if>
        <if test="map.licenseNumber != null and map.licenseNumber != ''">
            and dtw.license_number like concat('%', #{map.licenseNumber}, '%')
        </if>
        <if test="map.userMobile != null and map.userMobile != ''">
            and dtw.user_mobile like concat('%', #{map.userMobile}, '%')
        </if>
        <if test="map.repairName != null and map.repairName != ''">
            and dtw.repair_name like concat('%', #{map.repairName}, '%')
        </if>
        <if test="map.status != null and map.status != ''">
            and (dtw.status = #{map.status})
        </if>
        <if test="map.userIds != null and map.userIds.size > 0">
            and (
            dtw.repair_id in
            <foreach collection="map.userIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="map.userRole != null">
            and exists (
            select 1 from dl_tw_item dti where dti.tw_id = dtw.id and dti.deleted = '0'
            <choose>
                <when test="map.userRole == 5 and map.isBack == false">
                    and (dti.wares_count > dti.wares_already_count and dti.wares_status = '1')
                </when>
                <when test="map.userRole == 5 and map.isBack == true">
                    and (dti.wares_already_count > 0 and dti.wares_status = '1')
                </when>
            </choose>
            )
        </if>
        <if test="map.adviserId != null and map.adviserId != ''">
            and (dtw.adviser_id = #{map.adviserId})
        </if>
        <if test="map.userRole == 5 and map.isBack != true">
            AND (dtw.is_end='0')
        </if>
        order by dtw.create_time desc
    </select>
</mapper>
