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

    <resultMap type="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment" id="InspectionAppointmentResult">
        <result property="id"    column="id"    />
        <result property="partnerId"    column="partner_id"    />
        <result property="orderId"    column="order_id"    />
        <result property="goodsTitle"    column="goods_title"    />
        <result property="appointmentDay"    column="appointment_day"    />
        <result property="appointmentPeriod"    column="appointment_period"    />
        <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="selectInspectionAppointmentVo">
        select id, partner_id, order_id, goods_title, appointment_day, appointment_period, create_time, creator, update_time, updater from inspection_appointment
    </sql>

    <select id="selectInspectionAppointmentList" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment" resultMap="InspectionAppointmentResult">
        <include refid="selectInspectionAppointmentVo"/>
        <where>
            <if test="vo.partnerId != null "> and partner_id = #{vo.partnerId}</if>
            <if test="vo.orderId != null "> and order_id = #{vo.orderId}</if>
            <if test="vo.goodsTitle != null  and vo.goodsTitle != ''"> and goods_title = #{vo.goodsTitle}</if>
            <if test="vo.appointmentDay != null "> and appointment_day = #{vo.appointmentDay}</if>
            <if test="vo.appointmentPeriod != null  and vo.appointmentPeriod != ''"> and appointment_period = #{vo.appointmentPeriod}</if>
        </where>
    </select>

    <select id="selectInspectionAppointmentById" parameterType="Long" >
        select ia.*,su.nickname as buyName, su.mobile as buyPhoneNum,su2.nickname as meetName,imco.is_meet,ic.appointment_day as pickCarAppointmentDay,ic.appointment_time as pickCarAppointmentTime,ic.adress_detail,ia.is_use
        from inspection_appointment ia
        left join system_users su on su.id = ia.user_id
        LEFT JOIN inspection_meet_car_order imco ON imco.id = ia.meet_order_id
        LEFT JOIN system_users su2 on su2.id = imco.meet_man_id
        LEFT JOIN inspection_pick_car ic on ic.id = ia.pick_car_id
        where ia.id = #{id}
    </select>

    <insert id="insertInspectionAppointment" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment">
        insert into inspection_appointment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="partnerId != null">partner_id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="goodsTitle != null">goods_title,</if>
            <if test="appointmentDay != null">appointment_day,</if>
            <if test="appointmentPeriod != null">appointment_period,</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="partnerId != null">#{partnerId},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="goodsTitle != null">#{goodsTitle},</if>
            <if test="appointmentDay != null">#{appointmentDay},</if>
            <if test="appointmentPeriod != null">#{appointmentPeriod},</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="updateInspectionAppointment" parameterType="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment">
        update inspection_appointment
        <trim prefix="SET" suffixOverrides=",">
            <if test="partnerId != null">partner_id = #{partnerId},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="goodsTitle != null">goods_title = #{goodsTitle},</if>
            <if test="appointmentDay != null">appointment_day = #{appointmentDay},</if>
            <if test="appointmentPeriod != null">appointment_period = #{appointmentPeriod},</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="deleteInspectionAppointmentById" parameterType="Long">
        delete from inspection_appointment where id = #{id}
    </delete>

    <delete id="deleteInspectionAppointmentByIds" parameterType="String">
        delete from inspection_appointment where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="getAppointmentList"
            resultType="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment">
        SELECT * FROM (
        -- 部分1：预约表
        SELECT
        ip.id AS id,
        ip.appointment_day AS appointmentDay,
        CONVERT(ip.goods_title USING utf8mb4) COLLATE utf8mb4_general_ci AS goodsTitle,
        CONVERT(su.mobile USING utf8mb4) COLLATE utf8mb4_general_ci AS buyPhoneNum,
        CONVERT(su.nickname USING utf8mb4) COLLATE utf8mb4_general_ci AS buyName,
        CONVERT(suc.car_nature USING utf8mb4) COLLATE utf8mb4_general_ci AS car_nature,
        CONVERT(suc.car_brand USING utf8mb4) COLLATE utf8mb4_general_ci AS car_brand,
        CONVERT(suc.car_model USING utf8mb4) COLLATE utf8mb4_general_ci AS car_model,
        CONVERT(suc.car_no USING utf8mb4) COLLATE utf8mb4_general_ci AS car_no,
        CONVERT(igs.sku_name USING utf8mb4) COLLATE utf8mb4_general_ci AS sku_name,
        CONVERT(ip.customer_source USING utf8mb4) COLLATE utf8mb4_general_ci AS customerSource,
        ip.create_time AS createTime,
        CONVERT('appointment' USING utf8mb4) COLLATE utf8mb4_general_ci AS sourceType
        FROM inspection_appointment ip
        INNER JOIN system_users su ON ip.user_id = su.id
        LEFT JOIN order_info oi ON ip.order_id = oi.id AND oi.deleted=0
        LEFT JOIN shop_user_car suc ON suc.car_id = oi.user_car_id AND suc.deleted=0
        LEFT JOIN inspection_goods_sku igs ON igs.id = ip.sku_id
        WHERE ip.deleted=0 AND oi.validation_time IS NULL
        <if test="phoneNum!=null and phoneNum!=''">
            AND (su.mobile LIKE concat('%',#{phoneNum},'%') OR ip.car_no LIKE concat('%',#{phoneNum},'%'))
        </if>
        <if test="carNo != null and carNo != ''">
            AND ip.car_no LIKE concat('%',#{carNo},'%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND ip.appointment_day BETWEEN #{startTime} AND #{endTime}
        </if>

        UNION ALL

        -- 部分2：接车表
        SELECT
        imco.id AS id,
        imco.appointment_day AS appointmentDay,
        CONVERT(imco.goods_title USING utf8mb4) COLLATE utf8mb4_general_ci AS goodsTitle,
        CONVERT(imco.buy_phone USING utf8mb4) COLLATE utf8mb4_general_ci AS buyPhoneNum,
        CONVERT(imco.buy_name USING utf8mb4) COLLATE utf8mb4_general_ci AS buyName,
        CONVERT(imco.car_nature USING utf8mb4) COLLATE utf8mb4_general_ci AS car_nature,
        CONVERT(imco.car_model USING utf8mb4) COLLATE utf8mb4_general_ci AS car_brand,
        CONVERT(imco.car_model USING utf8mb4) COLLATE utf8mb4_general_ci AS car_model,
        CONVERT(imco.car_num USING utf8mb4) COLLATE utf8mb4_general_ci AS car_no,
        CONVERT(imco.sku_name USING utf8mb4) COLLATE utf8mb4_general_ci AS sku_name,
        CONVERT(imco.customer_source USING utf8mb4) COLLATE utf8mb4_general_ci AS customerSource,
        imco.create_time AS createTime,
        CONVERT('meetcar' USING utf8mb4) COLLATE utf8mb4_general_ci AS sourceType
        FROM inspection_meet_car_order imco
        WHERE imco.deleted=0 AND (imco.is_meet = 0 OR (imco.is_pick_car = 0 AND imco.is_meet_pick_car = 0))
        <if test="phoneNum!=null and phoneNum!=''">
            AND (imco.buy_phone LIKE concat('%',#{phoneNum},'%') OR imco.car_num LIKE concat('%',#{phoneNum},'%'))
        </if>
        <if test="partnerId!=null and partnerId!=''">
            <!-- 如果接车表也有 partner_id，这里可以写条件 -->
        </if>
        <if test="carNo != null and carNo != ''">
            AND imco.car_num LIKE concat('%',#{carNo},'%')
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND imco.appointment_day BETWEEN #{startTime} AND #{endTime}
        </if>
        ) t
        ORDER BY t.createTime DESC
    </select>



    <select id="getAppointmentOwn" resultType="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment">
        SELECT
            ip.*,su.mobile as buyPhoneNum,su.nickname as buyName,ip.goods_title as goodsTitle,
            suc.car_nature,suc.car_brand,suc.car_model,suc.car_no
        FROM
            inspection_appointment ip
                INNER JOIN system_users su ON ip.user_id = su.id
                left JOIN order_info oi ON ip.order_id = oi.id
                left join shop_user_car suc on suc.car_id = oi.user_car_id
        where  ip.deleted=0 and ip.user_id = #{userId}
        order by ip.create_time desc
    </select>
    <select id="getAppointmentListNew"
            resultType="cn.iocoder.yudao.module.inspection.entity.InspectionAppointment">
        SELECT * FROM (
        -- 部分1：预约表
        SELECT
        ip.id AS id,
        ip.appointment_day AS appointmentDay,
        CONVERT(ip.goods_title USING utf8mb4) COLLATE utf8mb4_general_ci AS goodsTitle,
        CONVERT(su.mobile USING utf8mb4) COLLATE utf8mb4_general_ci AS buyPhoneNum,
        CONVERT(su.nickname USING utf8mb4) COLLATE utf8mb4_general_ci AS buyName,
        CONVERT(suc.car_nature USING utf8mb4) COLLATE utf8mb4_general_ci AS car_nature,
        CONVERT(suc.car_brand USING utf8mb4) COLLATE utf8mb4_general_ci AS car_brand,
        CONVERT(suc.car_model USING utf8mb4) COLLATE utf8mb4_general_ci AS car_model,
        CONVERT(suc.car_no USING utf8mb4) COLLATE utf8mb4_general_ci AS car_no,
        CONVERT(igs.sku_name USING utf8mb4) COLLATE utf8mb4_general_ci AS sku_name,
        CONVERT(ip.customer_source USING utf8mb4) COLLATE utf8mb4_general_ci AS customerSource,
        ip.create_time AS createTime,
        CONVERT('appointment' USING utf8mb4) COLLATE utf8mb4_general_ci AS sourceType
        FROM inspection_appointment ip
        INNER JOIN system_users su ON ip.user_id = su.id
        LEFT JOIN order_info oi ON ip.order_id = oi.id AND oi.deleted=0
        LEFT JOIN shop_user_car suc ON suc.car_id = oi.user_car_id AND suc.deleted=0
        LEFT JOIN inspection_goods_sku igs ON igs.id = ip.sku_id
        <where>
        <if test="vo.deleted == null ">
            AND ip.deleted = 0 AND oi.validation_time IS NULL
        </if>
        <if test="vo.phoneNum!=null and vo.phoneNum!=''">
            AND (su.mobile LIKE concat('%',#{vo.phoneNum},'%') OR ip.car_no LIKE concat('%',#{vo.phoneNum},'%'))
        </if>
        <if test="vo.carNo != null and vo.carNo != ''">
            AND ip.car_no LIKE concat('%',#{vo.carNo},'%')
        </if>
        <if test="vo.startTime != null and vo.startTime != '' and vo.endTime != null and vo.endTime != ''">
            AND ip.appointment_day BETWEEN #{vo.startTime} AND #{vo.endTime}
        </if>
        </where>

        UNION ALL

        -- 部分2：接车表
        SELECT
        imco.id AS id,
        imco.appointment_day AS appointmentDay,
        CONVERT(imco.goods_title USING utf8mb4) COLLATE utf8mb4_general_ci AS goodsTitle,
        CONVERT(imco.buy_phone USING utf8mb4) COLLATE utf8mb4_general_ci AS buyPhoneNum,
        CONVERT(imco.buy_name USING utf8mb4) COLLATE utf8mb4_general_ci AS buyName,
        CONVERT(imco.car_nature USING utf8mb4) COLLATE utf8mb4_general_ci AS car_nature,
        CONVERT(imco.car_model USING utf8mb4) COLLATE utf8mb4_general_ci AS car_brand,
        CONVERT(imco.car_model USING utf8mb4) COLLATE utf8mb4_general_ci AS car_model,
        CONVERT(imco.car_num USING utf8mb4) COLLATE utf8mb4_general_ci AS car_no,
        CONVERT(imco.sku_name USING utf8mb4) COLLATE utf8mb4_general_ci AS sku_name,
        CONVERT(imco.customer_source USING utf8mb4) COLLATE utf8mb4_general_ci AS customerSource,
        imco.create_time AS createTime,
        CONVERT('meetcar' USING utf8mb4) COLLATE utf8mb4_general_ci AS sourceType
        FROM inspection_meet_car_order imco
        <where>
        <if test="vo.deleted ==  null ">
           AND  imco.deleted=0 AND (imco.is_meet = 0 OR (imco.is_pick_car = 0 AND imco.is_meet_pick_car = 0))
        </if>
        <if test="vo.phoneNum!=null and vo.phoneNum!=''">
            AND (imco.buy_phone LIKE concat('%',#{vo.phoneNum},'%') OR imco.car_num LIKE concat('%',#{vo.phoneNum},'%'))
        </if>
        <if test="vo.carNo != null and vo.carNo != ''">
            AND imco.car_num LIKE concat('%',#{vo.carNo},'%')
        </if>
        <if test="vo.startTime != null and vo.startTime != '' and vo.endTime != null and vo.endTime != ''">
            AND imco.appointment_day BETWEEN #{vo.startTime} AND #{vo.endTime}
        </if>
        </where>
        ) t
        ORDER BY t.createTime DESC
    </select>
</mapper>
