<?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="partnerId != null "> and partner_id = #{partnerId}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
            <if test="goodsTitle != null  and goodsTitle != ''"> and goods_title = #{goodsTitle}</if>
            <if test="appointmentDay != null "> and appointment_day = #{appointmentDay}</if>
            <if test="appointmentPeriod != null  and appointmentPeriod != ''"> and appointment_period = #{appointmentPeriod}</if>
        </where>
    </select>

    <select id="selectInspectionAppointmentById" parameterType="Long" resultMap="InspectionAppointmentResult">
        <include refid="selectInspectionAppointmentVo"/>
        where 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
        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 and oi.deleted=0
    left join shop_user_car suc on suc.car_id = oi.user_car_id  and suc.deleted=0
    where  ip.deleted=0 and ip.partner_id = #{partnerId}
    <if test="phoneNum!=null and phoneNum!=''">
       and  su.mobile like concat('%',#{phoneNum},'%')
    </if>
    order by ip.create_time 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>
</mapper>
