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

    <resultMap type="cn.iocoder.yudao.module.rescue.domain.RescueRefuelRecord" id="RescueRefuelRecordResult">
        <result property="id" column="id"/>
        <result property="driverId" column="driver_id"/>
        <result property="carId" column="car_id"/>
        <result property="refuelNum" column="refuel_num"/>
        <result property="refuelMoney" column="refuel_money"/>
        <result property="recordTime" column="record_time"/>
        <result property="deptId" column="dept_id"/>
        <result property="creator" column="creator"/>
        <result property="createTime" column="create_time"/>
        <result property="updater" column="updater"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="selectRescueRefuelRecordList" parameterType="cn.iocoder.yudao.module.rescue.domain.RescueRefuelRecord"
            resultType="cn.iocoder.yudao.module.rescue.domain.RescueRefuelRecord">
        SELECT
        rrc.*,su.nickname as real_name,su.mobile as phonenumber,rci.rescue_car_num
        FROM
        rescue_refuel_record rrc
        inner join driver_info di ON rrc.driver_id = di.id
        INNER JOIN system_users su ON su.id = di.user_id
        left join rescue_car_info rci on di.id = rci.possessor_id
        where rrc.deleted = '0'
        <if test="map.realName != null ">and su.nickname like concat('%',#{map.realName},'%')</if>
        <if test="map.rescueCarNum != null ">and rci.rescue_car_num like concat('%',#{map.rescueCarNum},'%')</if>
        order by rrc.record_time desc
    </select>

    <select id="selectRescueRefuelRecordById" parameterType="java.lang.Long"
            resultType="cn.iocoder.yudao.module.rescue.domain.RescueRefuelRecord">
        select *
        from rescue_refuel_record
        where id = #{id}
    </select>


    <delete id="deleteRescueRefuelRecordById" parameterType="java.lang.Long">
        delete
        from rescue_refuel_record
        where id = #{id}
    </delete>

    <delete id="deleteRescueRefuelRecordByIds" parameterType="java.lang.String">
        delete from rescue_refuel_record where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="getMonthRescueRefuelRecord" resultType="com.alibaba.fastjson.JSONObject">
        SELECT ifnull(sum(refuel_money), 0) as refuelMoney,
               ifnull(sum(refuel_num), 0)   as refuelNum
        FROM rescue_refuel_record
        where driver_id = #{driverId}
          and car_id = #{carId}
          and record_time like CONCAT(#{time}, '%')
    </select>
    <select id="listData" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
        ifnull(sum(refuel_money),0) as refuelMoney
        FROM
        rescue_refuel_record rrr
        left join rescue_car_info rci on rci.id = rrr.car_id
        left join driver_info di on di.id = rrr.driver_id
        left join system_users su on di.user_id = su.id
        <where>
            <if test="driverName != null ">
                and su.nickname like concat('%',#{driverName},'%')
            </if>
            <if test="driverCarNum != null ">
                and rci.rescue_car_num like concat('%',#{driverCarNum},'%')
            </if>
            <if test="rescueStart">and rrr.record_time like concat(#{rescueStart},'%')</if>
        </where>

    </select>
</mapper>
