<?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.ss.deviceBindRecord.mapper.DeviceBindRecordMapper">

    <resultMap type="DeviceBindRecordVO" id="SmDeviceBindRecordResult" autoMapping="true">
    </resultMap>

    <sql id="selectSmDeviceBindRecordVo">
        select
            sdbr.record_id,
            sdbr.device_id,
            sdbr.user_id,
            sdbr.create_time,
            sdbr.type,
            sdbr.user_type,
            su.user_name as user_name,
            sd.device_no as device_no
        from sm_device_bind_record sdbr
            left join sm_user su on su.user_id = sdbr.user_id
            left join sm_device sd on sd.device_id = sdbr.device_id
    </sql>

    <select id="selectSmDeviceBindRecordList" parameterType="DeviceBindRecordQuery" resultMap="SmDeviceBindRecordResult">
        <include refid="selectSmDeviceBindRecordVo"/>
        <where>
            <if test="deviceId != null "> and sdbr.device_id = #{deviceId}</if>
            <if test="userId != null "> and sdbr.user_id = #{userId}</if>
            <if test="type != null  and type != ''"> and sdbr.type = #{type}</if>
            <if test="userType != null  and userType != ''"> and sdbr.user_type = #{userType}</if>
            <if test="deviceNo != null  and deviceNo != ''"> and sd.device_no like concat('%',#{deviceNo},'%')</if>
            <if test="userName != null  and userName != ''"> and su.user_name like concat('%',#{userName},'%')</if>
        </where>
    </select>

    <select id="selectSmDeviceBindRecordByRecordId" parameterType="Long" resultMap="SmDeviceBindRecordResult">
        <include refid="selectSmDeviceBindRecordVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertSmDeviceBindRecord" parameterType="DeviceBindRecord">
        insert into sm_device_bind_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recordId != null">record_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="type != null and type != ''">type,</if>
            <if test="userType != null and userType != ''">user_type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recordId != null">#{recordId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="type != null and type != ''">#{type},</if>
            <if test="userType != null and userType != ''">#{userType},</if>
         </trim>
    </insert>

    <update id="updateSmDeviceBindRecord" parameterType="DeviceBindRecord">
        update sm_device_bind_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceId != null">device_id = #{deviceId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="userType != null and userType != ''">user_type = #{userType},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteSmDeviceBindRecordByRecordId" parameterType="Long">
        delete from sm_device_bind_record where record_id = #{recordId}
    </delete>

    <delete id="deleteSmDeviceBindRecordByRecordIds" parameterType="String">
        delete from sm_device_bind_record where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>
</mapper>
