<?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="com.ruoyi.bst.orderDevice.mapper.OrderDeviceMapper">

    <resultMap type="OrderDeviceVO" id="OrderDeviceResult" autoMapping="true"/>

    <sql id="selectOrderDeviceVo">
        select
            bod.id,
            bod.order_id,
            bod.device_id,
            bod.type,
            bod.start_time,
            bod.end_time,
            bod.reason,
            bod.fault_id,
            bod.status,
            bod.device_mac,
            bod.device_sn,
            bod.device_vehicle_num,
            bod.device_mch_id,
            bod.device_model_full_endurance,
            bod.finish_picture,
            bod.start_area_sub_id,
            bod.end_area_sub_id, 
            bod.start_area_sub_name,
            bod.end_area_sub_name,
            bod.return_mode,
            bod.return_lon,
            bod.return_lat,
            bo.no as order_no,
            bo.user_id as order_user_id,
            bo.start_time as order_start_time,
            bo.end_time as order_end_time,
            bo.max_time as order_max_time,
            bo.area_return_verify as order_area_return_verify,
            bo.area_id as order_area_id,
            bd.remaining_power as device_remaining_power,
            bd.lock_status as device_lock_status,
            su.nick_name as order_user_name,
            mch.nick_name as device_mch_name
        from bst_order_device bod
            left join bst_order bo on bo.id = bod.order_id
            left join bst_device bd on bd.id = bod.device_id
            left join bst_model bm on bm.id = bd.model_id
            left join sys_user su on su.user_id = bo.user_id
            left join sys_user mch on mch.user_id = bod.device_mch_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bod.id = #{query.id}</if>
        <if test="query.orderId != null "> and bod.order_id = #{query.orderId}</if>
        <if test="query.deviceId != null "> and bod.device_id = #{query.deviceId}</if>
        <if test="query.type != null  and query.type != ''"> and bod.type = #{query.type}</if>
        <if test="query.reason != null  and query.reason != ''"> and bod.reason like concat('%', #{query.reason}, '%')</if>
        <if test="query.faultId != null "> and bod.fault_id = #{query.faultId}</if>
        <if test="query.status != null  and query.status != ''"> and bod.status = #{query.status}</if>
        <if test="query.deviceMac != null  and query.deviceMac != ''"> and bod.device_mac like concat('%', #{query.deviceMac}, '%')</if>
        <if test="query.deviceSn != null  and query.deviceSn != ''"> and bod.device_sn like concat('%', #{query.deviceSn}, '%')</if>
        <if test="query.deviceVehicleNum != null  and query.deviceVehicleNum != ''"> and bod.device_vehicle_num like concat('%', #{query.deviceVehicleNum}, '%')</if>
        <if test="query.deviceMchId != null "> and bod.device_mch_id = #{query.deviceMchId}</if>
        <if test="query.orderUserId != null "> and bo.user_id = #{query.orderUserId}</if>
        <if test="query.deviceLockStatus != null  and query.deviceLockStatus != ''"> and bd.lock_status = #{query.deviceLockStatus}</if>
        <if test="query.orderUserName != null  and query.orderUserName != ''"> and su.nick_name like concat('%', #{query.orderUserName}, '%')</if>
        <if test="query.deviceMchName != null  and query.deviceMchName != ''"> and mch.nick_name like concat('%', #{query.deviceMchName}, '%')</if>
        <if test="query.startAreaSubId != null"> and bod.start_area_sub_id = #{query.startAreaSubId}</if>
        <if test="query.endAreaSubId != null"> and bod.end_area_sub_id = #{query.endAreaSubId}</if>
        <if test="query.startAreaSubName != null and query.startAreaSubName != ''"> and bod.start_area_sub_name like concat('%', #{query.startAreaSubName}, '%')</if>
        <if test="query.endAreaSubName != null and query.endAreaSubName != ''"> and bod.end_area_sub_name like concat('%', #{query.endAreaSubName}, '%')</if>
        <if test="query.returnMode != null and query.returnMode != ''"> and bod.return_mode = #{query.returnMode}</if>
        <if test="query.statusList != null and query.statusList.size() > 0">
             and bod.status in
            <foreach item="item" collection="query.statusList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.orderIds != null and query.orderIds.size() > 0">
            and bod.order_id in
            <foreach item="item" collection="query.orderIds" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "bo.area_user_id,bo.area_agent_id,bod.device_mch_id",
            null,
            null,
            query.scope
        )}
        ${query.params.dataScope}
    </sql>

    <select id="selectOrderDeviceList" parameterType="OrderDeviceQuery" resultMap="OrderDeviceResult">
        <include refid="selectOrderDeviceVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectOrderDeviceById" parameterType="Long" resultMap="OrderDeviceResult">
        <include refid="selectOrderDeviceVo"/>
        where bod.id = #{id}
    </select>

    <insert id="insertOrderDevice" parameterType="OrderDevice" useGeneratedKeys="true" keyProperty="id">
        insert into bst_order_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="reason != null and reason != ''">reason,</if>
            <if test="faultId != null">fault_id,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="deviceMac != null and deviceMac != ''">device_mac,</if>
            <if test="deviceSn != null and deviceSn != ''">device_sn,</if>
            <if test="deviceVehicleNum != null and deviceVehicleNum != ''">device_vehicle_num,</if>
            <if test="deviceMchId != null">device_mch_id,</if>
            <if test="deviceModelFullEndurance != null">device_model_full_endurance,</if>
            <if test="finishPicture != null and finishPicture != ''">finish_picture,</if>
            <if test="startAreaSubId != null">start_area_sub_id,</if>
            <if test="endAreaSubId != null">end_area_sub_id,</if>
            <if test="startAreaSubName != null and startAreaSubName != ''">start_area_sub_name,</if>
            <if test="endAreaSubName != null and endAreaSubName != ''">end_area_sub_name,</if>
            <if test="returnMode != null and returnMode != ''">return_mode,</if>
            <if test="returnLon != null ">return_lon,</if>
            <if test="returnLat != null">return_lat,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="reason != null and reason != ''">#{reason},</if>
            <if test="faultId != null">#{faultId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="deviceMac != null and deviceMac != ''">#{deviceMac},</if>
            <if test="deviceSn != null and deviceSn != ''">#{deviceSn},</if>
            <if test="deviceVehicleNum != null and deviceVehicleNum != ''">#{deviceVehicleNum},</if>
            <if test="deviceMchId != null">#{deviceMchId},</if>
            <if test="deviceModelFullEndurance != null">#{deviceModelFullEndurance},</if>
            <if test="finishPicture != null and finishPicture != ''">#{finishPicture},</if>
            <if test="startAreaSubId != null">#{startAreaSubId},</if>
            <if test="endAreaSubId != null">#{endAreaSubId},</if>
            <if test="startAreaSubName != null and startAreaSubName != ''">#{startAreaSubName},</if>
            <if test="endAreaSubName != null and endAreaSubName != ''">#{endAreaSubName},</if>
            <if test="returnMode != null and returnMode != ''">#{returnMode},</if>
            <if test="returnLon != null">#{returnLon},</if>
            <if test="returnLat != null">#{returnLat},</if>
         </trim>
    </insert>

    <update id="updateOrderDevice" parameterType="OrderDevice">
        update bst_order_device
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.orderId != null">order_id = #{data.orderId},</if>
        <if test="data.deviceId != null">device_id = #{data.deviceId},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
        <if test="data.startTime != null">start_time = #{data.startTime},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
        <if test="data.reason != null and data.reason != ''">reason = #{data.reason},</if>
        <if test="data.faultId != null">fault_id = #{data.faultId},</if>
        <if test="data.status != null and data.status != ''">status = #{data.status},</if>
        <if test="data.deviceMac != null and data.deviceMac != ''">device_mac = #{data.deviceMac},</if>
        <if test="data.deviceSn != null and data.deviceSn != ''">device_sn = #{data.deviceSn},</if>
        <if test="data.deviceVehicleNum != null and data.deviceVehicleNum != ''">device_vehicle_num = #{data.deviceVehicleNum},</if>
        <if test="data.deviceMchId != null">device_mch_id = #{data.deviceMchId},</if>
        <if test="data.deviceModelFullEndurance != null">device_model_full_endurance = #{data.deviceModelFullEndurance},</if>
        <if test="data.finishPicture != null and data.finishPicture != ''">finish_picture = #{data.finishPicture},</if>
        <if test="data.startAreaSubId != null">start_area_sub_id = #{data.startAreaSubId},</if>
        <if test="data.endAreaSubId != null">end_area_sub_id = #{data.endAreaSubId},</if>
        <if test="data.startAreaSubName != null and data.startAreaSubName != ''">start_area_sub_name = #{data.startAreaSubName},</if>
        <if test="data.endAreaSubName != null and data.endAreaSubName != ''">end_area_sub_name = #{data.endAreaSubName},</if>
        <if test="data.returnMode != null and data.returnMode != ''">return_mode = #{data.returnMode},</if>
        <if test="data.returnLon != null">return_lon = #{data.returnLon},</if>
        <if test="data.returnLat != null">return_lat = #{data.returnLat},</if>
    </sql>

    <delete id="deleteOrderDeviceById" parameterType="Long">
        delete from bst_order_device where id = #{id}
    </delete>

    <delete id="deleteOrderDeviceByIds" parameterType="String">
        delete from bst_order_device where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- updateByQuery -->

    <update id="updateByQuery">
        update bst_order_device bod
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

</mapper>
