<?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.rescue.mapper.RescueDriverInfoMapper">

    <resultMap type="cn.iocoder.yudao.module.rescue.domain.RescueDriverInfo" id="RescueDriverInfoResult">
        <result property="id"    column="id"    />
        <result property="rescueId"    column="rescue_id"    />
        <result property="driverId"    column="driver_id"    />
        <result property="driverAccept"    column="driver_accept"    />
        <result property="driverRejectReason"    column="driver_reject_reason"    />
        <result property="creator"    column="creator"    />
        <result property="createTime"    column="create_time"    />
        <result property="updater"    column="updater"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectRescueDriverInfoVo">
        select id, rescue_id, driver_id, driver_accept, driver_reject_reason, create_time, creator, update_time, updater from rescue_driver_info
    </sql>

    <select id="selectRescueDriverInfoList" parameterType="cn.iocoder.yudao.module.rescue.domain.RescueDriverInfo" resultMap="RescueDriverInfoResult">
        <include refid="selectRescueDriverInfoVo"/>
        <where>
            <if test="rescueId != null "> and rescue_id = #{rescueId}</if>
            <if test="driverId != null "> and driver_id = #{driverId}</if>
            <if test="driverAccept != null  and driverAccept != ''"> and driver_accept = #{driverAccept}</if>
            <if test="driverRejectReason != null  and driverRejectReason != ''"> and driver_reject_reason = #{driverRejectReason}</if>
        </where>
    </select>

    <select id="selectRescueDriverInfoById" parameterType="java.lang.Long" resultMap="RescueDriverInfoResult">
        <include refid="selectRescueDriverInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertRescueDriverInfo" parameterType="cn.iocoder.yudao.module.rescue.domain.RescueDriverInfo">
        insert into rescue_driver_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="rescueId != null">rescue_id,</if>
            <if test="driverId != null">driver_id,</if>
            <if test="driverAccept != null">driver_accept,</if>
            <if test="driverRejectReason != null">driver_reject_reason,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="rescueId != null">#{rescueId},</if>
            <if test="driverId != null">#{driverId},</if>
            <if test="driverAccept != null">#{driverAccept},</if>
            <if test="driverRejectReason != null">#{driverRejectReason},</if>
         </trim>
    </insert>

    <update id="updateRescueDriverInfo" parameterType="cn.iocoder.yudao.module.rescue.domain.RescueDriverInfo">
        update rescue_driver_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="rescueId != null">rescue_id = #{rescueId},</if>
            <if test="driverId != null">driver_id = #{driverId},</if>
            <if test="driverAccept != null">driver_accept = #{driverAccept},</if>
            <if test="driverRejectReason != null">driver_reject_reason = #{driverRejectReason},</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="deleteRescueDriverInfoById" parameterType="java.lang.Long">
        delete from rescue_driver_info where id = #{id}
    </delete>

    <delete id="deleteRescueDriverInfoByIds" parameterType="java.lang.String">
        delete from rescue_driver_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="driverRescueList" resultType="cn.iocoder.yudao.module.rescue.domain.RescueInfo">
    SELECT
        ri.*,rdi.id as  rescueDriverId
    FROM
        `rescue_info` ri
        INNER JOIN rescue_driver_info rdi ON ri.id = rdi.rescue_id AND rdi.driver_accept = '2'
    WHERE
    rdi.driver_id =#{driverId} and ri.rescue_status = '2'
    and ri.driver_id is null
    ORDER BY rdi.create_time desc
    </select>

    <select id="driverRescueList2" resultType="cn.iocoder.yudao.module.rescue.domain.RescueInfo">
        SELECT
            ri.*,rdi.id as  rescueDriverId
        FROM
            `rescue_info` ri
                INNER JOIN rescue_driver_info rdi ON ri.id = rdi.rescue_id AND rdi.driver_accept = '2'
        WHERE
            rdi.driver_id =#{driverId} and ri.rescue_status = '2'
          and ri.driver_id is null
        ORDER BY rdi.create_time desc
    </select>

    <select id="driverRescuePage" resultType="cn.iocoder.yudao.module.rescue.domain.RescueInfo">
    SELECT
        ri.*,roi.set_money
    FROM
        `rescue_info` ri
        left join rescue_order_info roi on ri.id = roi.rescue_info_id
    WHERE
    ri.driver_id =#{driverId}
    <if test="rescueStatus == '3'.toString()">
      and  ri.rescue_status = #{rescueStatus}
    </if>
    <if test="rescueStatus == '5'.toString()">
        and  (ri.rescue_status = '5' or ri.rescue_status = '6')
    </if>
    ORDER BY ri.create_time desc
    </select>

    <select id="driverRescuePage2" resultType="cn.iocoder.yudao.module.rescue.domain.RescueInfo">
        SELECT
        ri.*,roi.set_money
        FROM
        `rescue_info` ri
        left join rescue_order_info roi on ri.id = roi.rescue_info_id
        WHERE
        ri.driver_id =#{map.driverId}
        <if test="map.rescueStatus == '3'.toString()">
            and  ri.rescue_status = #{map.rescueStatus}
        </if>
        <if test="map.rescueStatus == '5'.toString()">
            and  (ri.rescue_status = '5' or ri.rescue_status = '6')
        </if>
        ORDER BY ri.create_time desc
    </select>
    <select id="dqrList" resultType="java.lang.Integer">
        SELECT
            count(1)
        FROM
            `rescue_driver_info` rdi
                INNER JOIN rescue_info ri on rdi.rescue_id = ri.id
        where rdi.driver_accept ='2' and rdi.driver_id =#{driverId} and ri.rescue_status = '2' and ri.driver_id is null
    </select>
</mapper>
